MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / extractQueries

Function extractQueries

pkg/sql/parser/integration_test.go:144–200  ·  view source on GitHub ↗

extractQueries extracts individual SQL queries from a file Queries are separated by semicolons, and comments are handled

(content string)

Source from the content-addressed store, hash-verified

142// extractQueries extracts individual SQL queries from a file
143// Queries are separated by semicolons, and comments are handled
144func extractQueries(content string) []string {
145 queries := []string{}
146 currentQuery := strings.Builder{}
147 inBlockComment := false
148
149 lines := strings.Split(content, "\n")
150
151 for _, line := range lines {
152 trimmed := strings.TrimSpace(line)
153
154 // Handle line comments
155 if strings.HasPrefix(trimmed, "--") {
156 continue
157 }
158
159 // Handle block comments
160 if strings.Contains(line, "/*") {
161 inBlockComment = true
162 }
163 if strings.Contains(line, "*/") {
164 inBlockComment = false
165 continue
166 }
167 if inBlockComment {
168 continue
169 }
170
171 // Skip empty lines
172 if trimmed == "" {
173 continue
174 }
175
176 // Add line to current query
177 currentQuery.WriteString(line)
178 currentQuery.WriteString("\n")
179
180 // Check if query ends with semicolon
181 if strings.HasSuffix(trimmed, ";") {
182 query := strings.TrimSpace(currentQuery.String())
183 // Remove trailing semicolon
184 query = strings.TrimSuffix(query, ";")
185 query = strings.TrimSpace(query)
186 if query != "" {
187 queries = append(queries, query)
188 }
189 currentQuery.Reset()
190 }
191 }
192
193 // Add last query if it doesn't end with semicolon
194 lastQuery := strings.TrimSpace(currentQuery.String())
195 if lastQuery != "" && lastQuery != ";" {
196 queries = append(queries, lastQuery)
197 }
198
199 return queries
200}
201

Callers 2

testDialectQueriesFunction · 0.70

Calls 2

StringMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected