-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.go
More file actions
226 lines (199 loc) · 5.21 KB
/
utils.go
File metadata and controls
226 lines (199 loc) · 5.21 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
package bqb
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"
)
func dialectReplace(dialect Dialect, sql string, params []any) (string, error) {
const (
questionMark = "?"
doubleQuestionMarkDelimiter = "??"
parameterPlaceholder = paramPh
)
switch dialect {
case RAW:
for _, param := range params {
p, err := paramToRaw(param)
if err != nil {
return "", err
}
sql = strings.Replace(sql, paramPh, p, 1)
}
return sql, nil
case MYSQL, SQL:
return strings.ReplaceAll(sql, paramPh, questionMark), nil
case PGSQL:
sql = strings.ReplaceAll(sql, doubleQuestionMarkDelimiter, questionMark)
parts := strings.Split(sql, paramPh)
var builder strings.Builder
for i := range params {
_, _ = builder.WriteString(parts[i] + "$" + strconv.Itoa(i+1))
}
builder.WriteString(parts[len(parts)-1])
return builder.String(), nil
default:
// No replacement defined for dialect
return sql, nil
}
}
func convertArg(text string, arg any) (string, []any, []error) {
var newArgs []any
var errs []error
switch v := arg.(type) {
case Embedder:
text = strings.Replace(text, "?", v.RawValue(), 1)
case driver.Valuer:
text = strings.Replace(text, "?", paramPh, 1)
val, err := v.Value()
if err != nil {
errs = append(errs, err)
} else {
newArgs = append(newArgs, val)
}
case []int:
newPh := []string{}
for _, i := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, i)
}
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
case []*int:
newPh := []string{}
for _, i := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, i)
}
if len(newPh) > 0 {
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
} else {
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, nil)
}
case []string:
newPh := []string{}
for _, s := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, s)
}
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
case []*string:
newPh := []string{}
for _, s := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, s)
}
if len(newPh) > 0 {
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
} else {
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, nil)
}
case []any:
newPh := []string{}
for _, s := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, s)
}
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
case *Query:
if v == nil {
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, nil)
return text, newArgs, errs
}
sql, params, err := v.toSql()
text = strings.Replace(text, "?", sql, 1)
if err != nil {
errs = append(errs, err)
} else {
newArgs = append(newArgs, params...)
}
case JsonMap, JsonList:
text = strings.Replace(text, "?", paramPh, 1)
bytes, err := json.Marshal(v)
if err != nil {
errs = append(errs, fmt.Errorf("cann jsonify struct: %v", err))
} else {
newArgs = append(newArgs, string(bytes))
}
case *JsonMap, *JsonList:
text = strings.Replace(text, "?", paramPh, 1)
bytes, err := json.Marshal(v)
if err != nil {
errs = append(errs, fmt.Errorf("cann jsonify struct: %v", err))
} else {
newArgs = append(newArgs, string(bytes))
}
case Embedded:
text = strings.Replace(text, "?", string(v), 1)
case Folded, *Folded:
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, v)
default:
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, v)
}
return text, newArgs, errs
}
func checkParamCounts(text, original string, args []any) error {
extraCount := strings.Count(text, "?")
if extraCount > 0 {
return fmt.Errorf("extra ? in text: %v (%d args)", original, len(args))
}
paramCount := strings.Count(text, paramPh)
if paramCount < len(args) {
return fmt.Errorf("missing ? in text: %v (%d args)", original, len(args))
}
return nil
}
func makePart(text string, args ...any) QueryPart {
tempPh := "XXX___XXX"
originalText := text
text = strings.ReplaceAll(text, "??", tempPh)
var newArgs []any
errs := make([]error, 0)
for _, arg := range args {
argText, fArgs, argErrs := convertArg(text, arg)
if len(argErrs) > 0 {
errs = append(errs, argErrs...)
}
newArgs = append(newArgs, fArgs...)
text = strings.ReplaceAll(argText, "??", tempPh)
}
if err := checkParamCounts(text, originalText, newArgs); err != nil {
errs = append(errs, err)
}
text = strings.ReplaceAll(text, tempPh, "??")
return QueryPart{
Text: text,
Params: newArgs,
Errs: errs,
}
}
func paramToRaw(param any) (string, error) {
switch p := param.(type) {
case bool:
return fmt.Sprintf("%v", p), nil
case float32, float64, int, int8, int16, int32, int64,
uint8, uint16, uint32, uint64:
return fmt.Sprintf("%v", p), nil
case *int:
if p == nil {
return "NULL", nil
}
return fmt.Sprintf("%v", *p), nil
case string:
return fmt.Sprintf("'%v'", p), nil
case *string:
if p == nil {
return "NULL", nil
}
return fmt.Sprintf("'%v'", *p), nil
case nil:
return "NULL", nil
default:
return "", fmt.Errorf("unsupported type for Raw query: %T", p)
}
}