-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparams.go
More file actions
189 lines (164 loc) · 4.07 KB
/
params.go
File metadata and controls
189 lines (164 loc) · 4.07 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
package godbc
// ParameterError represents an error with parameter binding
type ParameterError struct {
Name string
Message string
}
func (e *ParameterError) Error() string {
if e.Name != "" {
return "parameter '" + e.Name + "': " + e.Message
}
return "parameter: " + e.Message
}
// NamedParams holds parsed named parameter information
type NamedParams struct {
// Query is the converted query with positional ? placeholders
Query string
// Names contains the parameter names in order of their first appearance
Names []string
// Positions maps parameter names to their positions (1-based, matching ODBC binding)
// A single named parameter may appear multiple times in the query
Positions map[string][]int
}
// ParseNamedParams parses a query with named parameters and converts to positional placeholders.
// Supports the following named parameter styles:
// - :name (Oracle/PostgreSQL style)
// - @name (SQL Server style)
// - $name (PostgreSQL style - not $1 which is positional)
//
// Returns nil if no named parameters are found (query uses positional ? only).
// The original query is preserved if it contains only ? placeholders.
func ParseNamedParams(query string) *NamedParams {
if len(query) == 0 {
return nil
}
// Quick scan to see if we have any named parameters
hasNamed := false
for i := 0; i < len(query); i++ {
c := query[i]
if c == ':' || c == '@' || c == '$' {
// Check if followed by a valid identifier start
if i+1 < len(query) && isIdentStart(query[i+1]) {
hasNamed = true
break
}
}
}
if !hasNamed {
return nil
}
result := &NamedParams{
Positions: make(map[string][]int),
}
var output []byte
position := 0
i := 0
for i < len(query) {
c := query[i]
// Skip string literals (single quotes)
if c == '\'' {
start := i
i++
for i < len(query) {
if query[i] == '\'' {
if i+1 < len(query) && query[i+1] == '\'' {
// Escaped quote
i += 2
continue
}
i++
break
}
i++
}
output = append(output, query[start:i]...)
continue
}
// Skip string literals (double quotes - identifiers)
if c == '"' {
start := i
i++
for i < len(query) {
if query[i] == '"' {
if i+1 < len(query) && query[i+1] == '"' {
// Escaped quote
i += 2
continue
}
i++
break
}
i++
}
output = append(output, query[start:i]...)
continue
}
// Skip comments (-- style)
if c == '-' && i+1 < len(query) && query[i+1] == '-' {
start := i
for i < len(query) && query[i] != '\n' {
i++
}
output = append(output, query[start:i]...)
continue
}
// Skip comments (/* */ style)
if c == '/' && i+1 < len(query) && query[i+1] == '*' {
start := i
i += 2
for i+1 < len(query) {
if query[i] == '*' && query[i+1] == '/' {
i += 2
break
}
i++
}
output = append(output, query[start:i]...)
continue
}
// Check for named parameter
if (c == ':' || c == '@' || c == '$') && i+1 < len(query) && isIdentStart(query[i+1]) {
// Extract the parameter name
start := i + 1
end := start
for end < len(query) && isIdentChar(query[end]) {
end++
}
name := query[start:end]
position++
// Record the position for this name
result.Positions[name] = append(result.Positions[name], position)
// Add to names list if first occurrence
found := false
for _, n := range result.Names {
if n == name {
found = true
break
}
}
if !found {
result.Names = append(result.Names, name)
}
// Replace with ?
output = append(output, '?')
i = end
continue
}
// Regular character - copy as-is
output = append(output, c)
i++
}
if len(result.Names) == 0 {
return nil
}
result.Query = string(output)
return result
}
// isIdentStart returns true if c is a valid identifier start character
func isIdentStart(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
}
// isIdentChar returns true if c is a valid identifier character
func isIdentChar(c byte) bool {
return isIdentStart(c) || (c >= '0' && c <= '9')
}