/
daoparse.go
286 lines (228 loc) · 5.1 KB
/
daoparse.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package sqlx
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/bingoohuang/sqlparser/sqlparser"
)
// nolint:goerr113
func (p *SQLParsed) checkFuncInOut(numIn int, f StructField) error {
if numIn == 0 && !p.isBindBy(ByNone) {
return fmt.Errorf("sql %s required bind varialbes, but the func %v has none", p.RawStmt, f.Type)
}
if numIn != 1 && p.isBindBy(ByName) {
return fmt.Errorf("sql %s required named varialbes, but the func %v has non-one arguments",
p.RawStmt, f.Type)
}
if p.isBindBy(BySeq, ByAuto) {
if numIn < p.MaxSeq {
// nolint:goerr113
return fmt.Errorf("sql %s required max %d vars, but the func %v has only %d arguments",
p.RawStmt, p.MaxSeq, f.Type, numIn)
}
}
return nil
}
type bindBy int
const (
// ByNone means no bind params.
ByNone bindBy = iota
// ByAuto means auto seq for bind params.
ByAuto
// BySeq means specific seq for bind params.
BySeq
// ByName means named bind params.
ByName
)
func (b bindBy) String() string {
switch b {
case ByNone:
return "byNone"
case ByAuto:
return "byAuto"
case BySeq:
return "bySeq"
case ByName:
return "byName"
default:
return "Unknown"
}
}
// SQLParsed is the structure of the parsed SQL.
type SQLParsed struct {
ID string
SQL SQLPart
BindBy bindBy
Vars []string
MaxSeq int
IsQuery bool
opt *CreateDaoOpt
RawStmt string
fp FieldParts
runSQL string
}
func (p SQLParsed) replaceQuery(query string) (string, error) {
if SQLReplacer == nil {
return query, nil
}
return SQLReplacer.ReplacerQuery(query)
}
func (p SQLParsed) isBindBy(by ...bindBy) bool {
for _, b := range by {
if p.BindBy == b {
return true
}
}
return false
}
var sqlre = regexp.MustCompile(`'?:\w*'?`)
type FieldParts struct {
fieldParts []FieldPart
fieldVars []interface{}
}
func (p *FieldParts) AddFieldSqlPart(part string, varVal []interface{}, joinedSep bool) {
p.fieldParts = append(p.fieldParts, FieldPart{
PartSQL: part,
BindVal: varVal,
PartSQLPlTimes: strings.Count(part, "?"),
JoinedSep: joinedSep,
})
}
// ParseSQL parses the sql.
func ParseSQL(name, stmt string) (*SQLParsed, error) {
p := &SQLParsed{ID: name}
if err := p.fastParseSQL(stmt); err != nil {
return nil, err
}
return p, nil
}
func (p *SQLParsed) fastParseSQL(stmt string) error {
p.Vars = make([]string, 0)
p.RawStmt = sqlre.ReplaceAllStringFunc(stmt, func(v string) string {
if v[0:1] == "'" {
v = v[2:]
} else {
v = v[1:]
}
if v != "" && v[len(v)-1:] == "'" {
v = v[:len(v)-1]
}
p.Vars = append(p.Vars, v)
return "?"
})
var err error
p.BindBy, p.MaxSeq, err = parseBindBy(p.ID, p.Vars)
if err != nil {
return err
}
_, p.IsQuery = IsQuerySQL(p.RawStmt)
return nil
}
func (p *SQLParsed) parseSQL(runSQl string) error {
p.Vars = make([]string, 0)
p.runSQL = sqlre.ReplaceAllStringFunc(runSQl, func(v string) string {
if v[0:1] == "'" {
v = v[2:]
} else {
v = v[1:]
}
if v != "" && v[len(v)-1:] == "'" {
v = v[:len(v)-1]
}
p.Vars = append(p.Vars, v)
return "?"
})
if len(p.fp.fieldParts) > 0 {
parsed, err := sqlparser.Parse(p.runSQL)
if err != nil {
panic(err)
}
w, hasWhere := parsed.(sqlparser.IWhere)
if hasWhere {
hasWhere = w.GetWhere() != nil
}
for i, f := range p.fp.fieldParts {
if f.JoinedSep {
if i == 0 && !hasWhere {
p.runSQL += " where " + f.PartSQL
} else {
p.runSQL += " and " + f.PartSQL
}
} else {
p.runSQL += " " + f.PartSQL
}
p.Vars = append(p.Vars, f.VarMarks()...)
p.fp.fieldVars = append(p.fp.fieldVars, f.Vars()...)
}
}
if p.opt != nil && p.opt.DBGetter != nil {
p.runSQL = convertSQLBindMarks(p.opt.DBGetter.GetDB(), p.runSQL)
}
return nil
}
type FieldPart struct {
PartSQL string
BindVal []interface{}
PartSQLPlTimes int
JoinedSep bool
}
func (p FieldPart) VarMarks() []string {
vars := make([]string, p.PartSQLPlTimes)
for i := 0; i < p.PartSQLPlTimes; i++ {
vars[i] = "?"
}
return vars
}
func (p FieldPart) Vars() []interface{} {
vars := make([]interface{}, p.PartSQLPlTimes)
for i := 0; i < p.PartSQLPlTimes; i++ {
vars[i] = p.BindVal[i]
}
return vars
}
func parseBindBy(sqlName string, vars []string) (bindBy bindBy, maxSeq int, err error) {
bindBy = ByNone
for _, v := range vars {
if v == "" {
if bindBy == ByAuto {
maxSeq++
continue
}
if bindBy != ByNone {
// nolint:goerr113
return 0, 0, fmt.Errorf("[%s] illegal mixed bind mod (%v-%v)", sqlName, bindBy, ByAuto)
}
bindBy = ByAuto
maxSeq++
continue
}
n, err := strconv.Atoi(v)
if err == nil {
if bindBy == BySeq {
if maxSeq < n {
maxSeq = n
}
continue
}
if bindBy != ByNone {
// nolint:goerr113
return 0, 0, fmt.Errorf("[%s] illegal mixed bind mod (%v-%v)", sqlName, bindBy, BySeq)
}
bindBy = BySeq
maxSeq = n
continue
}
if bindBy == ByName {
maxSeq++
continue
}
if bindBy != ByNone {
// nolint:goerr113
return 0, 0, fmt.Errorf("[%s] illegal mixed bind mod (%v-%v)", sqlName, bindBy, ByName)
}
bindBy = ByName
maxSeq++
}
return bindBy, maxSeq, nil
}