-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsplit.go
More file actions
241 lines (216 loc) · 5.42 KB
/
split.go
File metadata and controls
241 lines (216 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package sqlfile
import (
"bufio"
"context"
"io"
"strings"
)
// 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 ("identifier")
// - Dollar-quoted strings ($$string$$ or $tag$string$tag$)
func Split(ctx context.Context, r io.Reader) ([]string, error) {
scanner := bufio.NewScanner(r)
var queries []string
var currentQuery strings.Builder
var inSingleQuote bool
var inDoubleQuote bool
var inDollarQuote bool
var dollarTag string
var inMultiLineComment bool
for scanner.Scan() {
// Check context cancellation
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
line := scanner.Text()
i := 0
lineLen := len(line)
for i < lineLen {
ch := line[i]
// Handle multi-line comments
if inMultiLineComment {
if i+1 < lineLen && ch == '*' && line[i+1] == '/' {
inMultiLineComment = false
currentQuery.WriteString("*/")
i += 2
continue
}
currentQuery.WriteByte(ch)
i++
continue
}
// Handle dollar-quoted strings (PostgreSQL)
if inDollarQuote {
if ch == '$' {
// Try to match the closing tag
endTag := extractDollarTag(line[i:])
if endTag == dollarTag {
inDollarQuote = false
currentQuery.WriteString(endTag)
i += len(endTag)
continue
}
}
currentQuery.WriteByte(ch)
i++
continue
}
// Handle single-quoted strings
if inSingleQuote {
currentQuery.WriteByte(ch)
if ch == '\'' {
// Check for escaped quote ''
if i+1 < lineLen && line[i+1] == '\'' {
currentQuery.WriteByte('\'')
i += 2
continue
}
inSingleQuote = false
}
i++
continue
}
// Handle double-quoted identifiers
if inDoubleQuote {
currentQuery.WriteByte(ch)
if ch == '"' {
// Check for escaped quote ""
if i+1 < lineLen && line[i+1] == '"' {
currentQuery.WriteByte('"')
i += 2
continue
}
inDoubleQuote = false
}
i++
continue
}
// Check for single-line comment
if i+1 < lineLen && ch == '-' && line[i+1] == '-' {
// Rest of line is a comment
currentQuery.WriteString(line[i:])
break
}
// Check for multi-line comment start
if i+1 < lineLen && ch == '/' && line[i+1] == '*' {
inMultiLineComment = true
currentQuery.WriteString("/*")
i += 2
continue
}
// Check for dollar quote start
if ch == '$' {
tag := extractDollarTag(line[i:])
if tag != "" {
inDollarQuote = true
dollarTag = tag
currentQuery.WriteString(tag)
i += len(tag)
continue
}
}
// Check for single quote
if ch == '\'' {
inSingleQuote = true
currentQuery.WriteByte(ch)
i++
continue
}
// Check for double quote
if ch == '"' {
inDoubleQuote = true
currentQuery.WriteByte(ch)
i++
continue
}
// Check for semicolon (statement terminator)
if ch == ';' {
currentQuery.WriteByte(ch)
// Check if there's a comment after the semicolon on the same line
i++
if i < lineLen {
// Skip whitespace
for i < lineLen && (line[i] == ' ' || line[i] == '\t') {
currentQuery.WriteByte(line[i])
i++
}
// If there's a comment, include it
if i+1 < lineLen && line[i] == '-' && line[i+1] == '-' {
currentQuery.WriteString(line[i:])
}
}
query := strings.TrimSpace(currentQuery.String())
if query != "" && query != ";" {
queries = append(queries, query)
}
currentQuery.Reset()
break // Move to next line
}
// Regular character
currentQuery.WriteByte(ch)
i++
}
// Add newline if we're building a query
if currentQuery.Len() > 0 {
currentQuery.WriteByte('\n')
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
// Handle any remaining query
query := strings.TrimSpace(currentQuery.String())
if query != "" && query != ";" {
queries = append(queries, query)
}
return queries, nil
}
// extractDollarTag extracts a dollar-quoted string tag from the beginning of s.
// Returns empty string if no valid dollar tag is found.
// Valid tags: $$ or $identifier$ where identifier contains only alphanumeric and underscore.
func extractDollarTag(s string) string {
if len(s) == 0 || s[0] != '$' {
return ""
}
// Find the closing $
for i := 1; i < len(s); i++ {
if s[i] == '$' {
tag := s[:i+1]
// Validate tag content (only alphanumeric and underscore allowed between $)
tagContent := tag[1 : len(tag)-1]
if isValidDollarTagContent(tagContent) {
return tag
}
return ""
}
// If we hit a character that's not allowed in a tag, it's not a dollar quote
if !isValidDollarTagChar(s[i]) {
return ""
}
}
return ""
}
// isValidDollarTagContent returns true if s contains only valid characters for a dollar tag.
func isValidDollarTagContent(s string) bool {
if s == "" {
return true // $$ is valid
}
for _, ch := range s {
if !isValidDollarTagChar(byte(ch)) {
return false
}
}
return true
}
// isValidDollarTagChar returns true if ch is a valid character in a dollar tag.
// Valid characters are alphanumeric and underscore.
func isValidDollarTagChar(ch byte) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_'
}