MCPcopy
hub / github.com/sqlc-dev/sqlc / Split

Function Split

internal/sql/sqlfile/split.go:18–194  ·  view source on GitHub ↗

Split reads SQL queries from an io.Reader and returns them as a slice of strings. Each SQL query is delimited by a semicolon (;). The function handles: - Single-line comments (-- comment) - Multi-line comments (/* comment */) - Single-quoted strings ('string') - Double-quoted identifiers ("identifie

(ctx context.Context, r io.Reader)

Source from the content-addressed store, hash-verified

16// - Double-quoted identifiers ("identifier")
17// - Dollar-quoted strings ($$string$$ or $tag$string$tag$)
18func Split(ctx context.Context, r io.Reader) ([]string, error) {
19 scanner := bufio.NewScanner(r)
20 var queries []string
21 var currentQuery strings.Builder
22 var inSingleQuote bool
23 var inDoubleQuote bool
24 var inDollarQuote bool
25 var dollarTag string
26 var inMultiLineComment bool
27
28 for scanner.Scan() {
29 // Check context cancellation
30 select {
31 case <-ctx.Done():
32 return nil, ctx.Err()
33 default:
34 }
35
36 line := scanner.Text()
37 i := 0
38 lineLen := len(line)
39
40 for i < lineLen {
41 ch := line[i]
42
43 // Handle multi-line comments
44 if inMultiLineComment {
45 if i+1 < lineLen && ch == '*' && line[i+1] == '/' {
46 inMultiLineComment = false
47 currentQuery.WriteString("*/")
48 i += 2
49 continue
50 }
51 currentQuery.WriteByte(ch)
52 i++
53 continue
54 }
55
56 // Handle dollar-quoted strings (PostgreSQL)
57 if inDollarQuote {
58 if ch == '$' {
59 // Try to match the closing tag
60 endTag := extractDollarTag(line[i:])
61 if endTag == dollarTag {
62 inDollarQuote = false
63 currentQuery.WriteString(endTag)
64 i += len(endTag)
65 continue
66 }
67 }
68 currentQuery.WriteByte(ch)
69 i++
70 continue
71 }
72
73 // Handle single-quoted strings
74 if inSingleQuote {
75 currentQuery.WriteByte(ch)

Callers 2

TestSplitFunction · 0.85

Calls 5

extractDollarTagFunction · 0.85
ScanMethod · 0.45
ErrMethod · 0.45
StringMethod · 0.45
ResetMethod · 0.45

Tested by 2

TestSplitFunction · 0.68