-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
272 lines (224 loc) · 5.69 KB
/
sql.go
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
package lama
import (
"encoding/json"
"fmt"
"strings"
)
type sqlPart struct {
Sql string
Params []any
}
type Sql struct {
parts []*sqlPart
optional string
}
func NewSql(sql string, args ...any) *Sql {
return &Sql{parts: append([]*sqlPart{}, makeSqlPart(sql, args...))}
}
func NewSqlOptional(optional string) *Sql {
return &Sql{
optional: optional,
}
}
func (s *Sql) HasParts() bool {
return len(s.parts) > 0
}
func (s *Sql) Add(sep, sql string, args ...any) *Sql {
if len(s.parts) > 0 {
s.parts = append(s.parts, makeSqlPart(sep+sql, args...))
} else {
s.parts = append(s.parts, makeSqlPart(sql, args...))
}
return s
}
func (s *Sql) Or(sql string, args ...any) *Sql {
return s.Add(" OR ", sql, args...)
}
func (s *Sql) And(sql string, args ...any) *Sql {
return s.Add(" AND ", sql, args...)
}
func (s *Sql) Space(sql string, args ...any) *Sql {
return s.Add(" ", sql, args...)
}
func (s *Sql) Comma(sql string, args ...any) *Sql {
return s.Add(",", sql, args...)
}
func (s *Sql) Concat(sql string, args ...any) *Sql {
return s.Add("", sql, args...)
}
func (s *Sql) ToMysql() (sql string, params []any) {
sql, params = s.toSql()
sql = s.replace(MYSQL, sql, params)
return
}
func (s *Sql) ToPgsql() (sql string, params []any) {
sql, params = s.toSql()
sql = s.replace(PGSQL, sql, params)
return
}
func (s *Sql) ToRaw() string {
sql, params := s.toSql()
return s.replace(RAW, sql, params)
}
func (s *Sql) toSql() (string, []any) {
var sql string
var params []any
if s.optional != "" && len(s.parts) > 0 {
sql = s.optional + " "
}
for _, p := range s.parts {
sql += p.Sql
params = append(params, p.Params...)
}
return strings.TrimSpace(sql), params
}
func (s *Sql) Len() int {
return len(s.parts)
}
func (s *Sql) Print() {
sql, params := s.toSql()
Print.Printf("SQL: %v\n", sql)
Print.Printf("PARAMS: %v\n", params)
}
const (
PGSQL SqlType = "postgres"
MYSQL SqlType = "mysql"
RAW SqlType = "raw"
paramPh = "{{xX_PARAM_Xx}}"
)
type SqlType string
type JsonList []any
type JsonMap map[string]any
func (s *Sql) replace(sqlType SqlType, sql string, params []any) string {
for i, param := range params {
if sqlType == RAW {
p := paramToRaw(param)
sql = strings.Replace(sql, paramPh, p, 1)
} else if sqlType == MYSQL {
sql = strings.Replace(sql, paramPh, "?", 1)
} else if sqlType == PGSQL {
sql = strings.ReplaceAll(sql, "??", "?")
sql = strings.Replace(sql, paramPh, fmt.Sprintf("$%d", i+1), 1)
}
}
return sql
}
func makeSqlPart(text string, args ...any) *sqlPart {
tempPh := "XXX___XXX"
originalText := text
text = strings.ReplaceAll(text, "??", tempPh)
var newArgs []interface{}
for _, arg := range args {
switch v := arg.(type) {
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 []interface{}:
newPh := []string{}
for _, s := range v {
newPh = append(newPh, paramPh)
newArgs = append(newArgs, s)
}
text = strings.Replace(text, "?", strings.Join(newPh, ","), 1)
case *Sql:
if v == nil {
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, nil)
continue
}
sql, params := v.toSql()
text = strings.Replace(text, "?", sql, 1)
newArgs = append(newArgs, params...)
case JsonMap, JsonList:
bytes, err := json.Marshal(v)
if err != nil {
panic(fmt.Sprintf("cann jsonify struct: %v", err))
}
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, string(bytes))
case *JsonMap, *JsonList:
bytes, err := json.Marshal(v)
if err != nil {
panic(fmt.Sprintf("cann jsonify struct: %v", err))
}
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, string(bytes))
default:
text = strings.Replace(text, "?", paramPh, 1)
newArgs = append(newArgs, v)
}
}
extraCount := strings.Count(text, "?")
if extraCount > 0 {
panic(fmt.Sprintf("extra ? in text: %v (%d args)", originalText, len(newArgs)))
}
paramCount := strings.Count(text, paramPh)
if paramCount < len(newArgs) {
panic(fmt.Sprintf("missing ? in text: %v (%d args)", originalText, len(newArgs)))
}
text = strings.ReplaceAll(text, tempPh, "??")
return &sqlPart{
Sql: text,
Params: newArgs,
}
}
func paramToRaw(param interface{}) string {
switch p := param.(type) {
case bool:
return fmt.Sprintf("%v", p)
case float32, float64, int, int8, int16, int32, int64,
uint8, uint16, uint32, uint64:
return fmt.Sprintf("%v", p)
case *int:
if p == nil {
return "NULL"
}
return fmt.Sprintf("%v", *p)
case string:
return fmt.Sprintf("'%v'", p)
case *string:
if p == nil {
return "NULL"
}
return fmt.Sprintf("'%v'", *p)
case nil:
return "NULL"
default:
panic(fmt.Errorf("unsupported type for Raw query: %T", p))
}
}