-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.go
More file actions
387 lines (317 loc) · 9.39 KB
/
queries.go
File metadata and controls
387 lines (317 loc) · 9.39 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package queries
import (
"bufio"
"database/sql"
"embed"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
const (
positionalParamRE = `\$(\d+)`
colonParamRE = `[^:]:['"]?([A-Za-z][A-Za-z0-9_]*)['"]?`
atSignParamRE = `[^@]@['"]?([A-Za-z][A-Za-z0-9_]*)['"]?`
namedParamRE = `[:@]["']?%s["']?` // Template for replacement
)
var (
reservedNames = []string{"MI", "SS"}
)
type (
QueryStore struct {
queries map[string]*Query
}
Query struct {
Name string
Path string
Raw string
OrdinalQuery string
Mapping map[string]int
Args []string
NamedArgs []sql.NamedArg
Metadata map[string]string
parsedSections parsedSections // lazily parsed @tag sections
}
)
// NewQueryStore setups new query store
func NewQueryStore() *QueryStore {
return &QueryStore{
queries: make(map[string]*Query),
}
}
// LoadFromFile loads query/queries from specified file
func (s *QueryStore) LoadFromFile(fileName string) (err error) {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
return s.loadQueriesFromFile(fileName, file)
}
func (s *QueryStore) LoadFromDir(path string) error {
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("Directory does not exist: %s", path)
}
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(strings.ToLower(filePath), ".sql") {
err = s.LoadFromFile(filePath)
if err != nil {
return fmt.Errorf("Error loading SQL file '%s': %v", filePath, err)
}
}
return nil
})
return err
}
func (qs *QueryStore) LoadFromEmbed(sqlFS embed.FS, path string) error {
dirEntries, err := fs.ReadDir(sqlFS, path)
if err != nil {
return err
}
for _, entry := range dirEntries {
filePath := entry.Name()
if !entry.IsDir() && strings.HasSuffix(strings.ToLower(filePath), ".sql") {
file, err := sqlFS.Open(filepath.Join(path, filePath))
if err != nil {
return fmt.Errorf("Error opening SQL file '%s': %v", filePath, err)
}
defer file.Close()
err = qs.loadQueriesFromFile(filePath, file)
if err != nil {
return fmt.Errorf("Error loading SQL file '%s': %v", filePath, err)
}
}
}
return nil
}
// MustHaveQuery returns query or panics on error
func (s *QueryStore) MustHaveQuery(name string) *Query {
query, err := s.Query(name)
if err != nil {
panic(err)
}
return query
}
// Query retrieve query by given name
func (s *QueryStore) Query(name string) (*Query, error) {
query, ok := s.queries[name]
if !ok {
return nil, fmt.Errorf("Query '%s' not found", name)
}
return query, nil
}
// QueryNames returns a sorted list of all query names in the store
func (s *QueryStore) QueryNames() []string {
names := make([]string, 0, len(s.queries))
for name := range s.queries {
names = append(names, name)
}
sort.Strings(names)
return names
}
// Queries returns all queries in the store as a map
func (s *QueryStore) Queries() map[string]*Query {
// Return a copy to prevent external modification
queriesCopy := make(map[string]*Query, len(s.queries))
for name, query := range s.queries {
queriesCopy[name] = query
}
return queriesCopy
}
func (s *QueryStore) loadQueriesFromFile(fileName string, r io.Reader) error {
scanner := &Scanner{}
newQueries := scanner.Run(fileName, bufio.NewScanner(r))
for name, scannedQuery := range newQueries {
// insert query (but check whatever it already exists)
if _, ok := s.queries[name]; ok {
return fmt.Errorf("Query '%s' already exists", name)
}
q, err := NewQuery(name, fileName, scannedQuery.Query, scannedQuery.Metadata)
if err != nil {
return fmt.Errorf("Error creating query '%s': %w", name, err)
}
s.queries[name] = q
}
return nil
}
// stripSQLComments removes SQL single-line comments (--) from a query string.
// It returns a copy of the query with all comment content removed, while
// preserving the structure and line breaks of the original query.
func stripSQLComments(query string) string {
lines := strings.Split(query, "\n")
result := make([]string, 0, len(lines))
for _, line := range lines {
if idx := strings.Index(line, "--"); idx >= 0 {
line = line[:idx]
}
result = append(result, line)
}
return strings.Join(result, "\n")
}
func NewQuery(name, path, query string, metadata map[string]string) (*Query, error) {
if metadata == nil {
metadata = make(map[string]string)
}
q := Query{
Name: name,
Path: path,
Raw: query,
Metadata: metadata,
}
// Strip comments to avoid detecting parameters within comment text
cleanQuery := stripSQLComments(query)
// Detect all parameter types
positionalMatches := regexp.MustCompile(positionalParamRE).FindAllStringSubmatch(cleanQuery, -1)
colonMatches := filterReservedNames(regexp.MustCompile(colonParamRE).FindAllStringSubmatch(cleanQuery, -1))
atSignMatches := filterReservedNames(regexp.MustCompile(atSignParamRE).FindAllStringSubmatch(cleanQuery, -1))
// Validate that only one parameter style is used
if err := validateSingleParameterStyle(name, positionalMatches, colonMatches, atSignMatches); err != nil {
return nil, err
}
// Process based on detected parameter type
if len(positionalMatches) > 0 {
return handlePositionalParams(&q, name, query, cleanQuery, positionalMatches), nil
}
// Handle named parameters (colon or at-sign style)
return handleNamedParams(&q, name, query, cleanQuery), nil
}
func filterReservedNames(matches [][]string) [][]string {
filtered := make([][]string, 0, len(matches))
for _, match := range matches {
if len(match) > 1 && !isReservedName(match[1]) {
filtered = append(filtered, match)
}
}
return filtered
}
func validateSingleParameterStyle(queryName string, positional, colon, atSign [][]string) error {
styles := []string{}
if len(positional) > 0 {
styles = append(styles, "positional ($1, $2, ...)")
}
if len(colon) > 0 {
styles = append(styles, "colon (:param)")
}
if len(atSign) > 0 {
styles = append(styles, "at-sign (@param)")
}
if len(styles) > 1 {
return fmt.Errorf("mixed parameter styles detected in query '%s': found %s. Use only one style per query",
queryName, strings.Join(styles, " and "))
}
return nil
}
func handlePositionalParams(q *Query, name, query, cleanQuery string, matches [][]string) *Query {
// Find the highest parameter number
maxParam := 0
for _, match := range matches {
num := 0
fmt.Sscanf(match[1], "%d", &num)
if num > maxParam {
maxParam = num
}
}
// Create synthetic names and populate Args/NamedArgs
mapping := make(map[string]int)
namedArgs := []sql.NamedArg{}
args := []string{}
// Create args for each positional parameter in order
for i := 1; i <= maxParam; i++ {
syntheticName := fmt.Sprintf("arg%d", i)
args = append(args, syntheticName)
mapping[syntheticName] = i
namedArgs = append(namedArgs, sql.Named(syntheticName, nil))
}
q.OrdinalQuery = fmt.Sprintf("-- name: %s\n%s", name, query)
q.Mapping = mapping
q.Args = args
q.NamedArgs = namedArgs
return q
}
func handleNamedParams(q *Query, name, query, cleanQuery string) *Query {
mapping := make(map[string]int)
namedArgs := []sql.NamedArg{}
args := []string{}
position := 1
// Match both colon and at-sign parameters
colonMatches := regexp.MustCompile(colonParamRE).FindAllStringSubmatch(cleanQuery, -1)
atSignMatches := regexp.MustCompile(atSignParamRE).FindAllStringSubmatch(cleanQuery, -1)
// Combine matches (only one type will have results due to validation)
allMatches := append(colonMatches, atSignMatches...)
for _, match := range allMatches {
if len(match) < 2 {
continue
}
variable := match[1]
if isReservedName(variable) {
continue
}
// Collect all variable occurrences (including duplicates)
args = append(args, variable)
if _, ok := mapping[variable]; !ok {
mapping[variable] = position
namedArgs = append(namedArgs, sql.Named(variable, nil))
position++
}
}
// Replace named parameters with positional markers ($1, $2, etc.)
for paramName, ord := range mapping {
pattern := regexp.MustCompile(fmt.Sprintf(namedParamRE, paramName))
query = pattern.ReplaceAllLiteralString(query, fmt.Sprintf("$%d", ord))
}
q.OrdinalQuery = fmt.Sprintf("-- name: %s\n%s", name, query)
q.Mapping = mapping
q.Args = args
q.NamedArgs = namedArgs
return q
}
// Query returns ordinal query
func (q *Query) Query() string {
return q.OrdinalQuery
}
func (q *Query) RawQuery() string {
return q.Raw
}
// GetMetadata retrieves a metadata value by key
func (q *Query) GetMetadata(key string) (string, bool) {
// Normalize the key to lowercase for consistent lookup
key = strings.ToLower(strings.TrimSpace(key))
value, ok := q.Metadata[key]
return value, ok
}
// Prepare the arguments for the ordinal query. Missing arguments will
// be returned as nil
func (q *Query) Prepare(args map[string]interface{}) []interface{} {
type kv struct {
Name string
Ord int
}
// number of components is query and ordinal mapping count
components := make([]interface{}, len(q.Mapping))
var params []kv
for k, v := range q.Mapping {
params = append(params, kv{k, v})
}
sort.Slice(params, func(i, j int) bool {
return params[i].Ord < params[j].Ord
})
for i, param := range params {
components[i] = args[param.Name]
}
return components
}
func isReservedName(name string) bool {
for _, res := range reservedNames {
if name == res {
return true
}
}
return false
}