-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
interface.go
494 lines (458 loc) · 13.8 KB
/
interface.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package generate
import (
"fmt"
"strconv"
"strings"
"gorm.io/gen/internal/model"
"gorm.io/gen/internal/parser"
)
// InterfaceMethod interface's method
type InterfaceMethod struct { // feature will replace InterfaceMethod to parser.Method
Doc string // comment
S string // First letter of
OriginStruct parser.Param // origin struct name
TargetStruct string // generated query struct bane
MethodName string // generated function name
Params []parser.Param // function input params
Result []parser.Param // function output params
ResultData parser.Param // output data
Section *Section // Parse split SQL into sections
SQLParams []parser.Param // variable in sql need function input
SQLString string // SQL
GormOption string // gorm execute method Find or Exec or Take
Table string // specified by user. if empty, generate it with gorm
InterfaceName string // origin interface name
Package string // interface package name
HasForParams bool //
}
// FuncSign function signature
func (m *InterfaceMethod) FuncSign() string {
return fmt.Sprintf("%s(%s) (%s)", m.MethodName, m.GetParamInTmpl(), m.GetResultParamInTmpl())
}
// HasSQLData has variable or for params will creat params map
func (m *InterfaceMethod) HasSQLData() bool {
return len(m.SQLParams) > 0 || m.HasForParams
}
// HasGotPoint parameter has pointer or not
func (m *InterfaceMethod) HasGotPoint() bool {
return !m.HasNeedNewResult()
}
// HasNeedNewResult need pointer or not
func (m *InterfaceMethod) HasNeedNewResult() bool {
return !m.ResultData.IsArray && ((m.ResultData.IsNull() && m.ResultData.IsTime()) || m.ResultData.IsMap())
}
// GormRunMethodName return single data use Take() return array use Find
func (m *InterfaceMethod) GormRunMethodName() string {
if m.ResultData.IsArray {
return "Find"
}
return "Take"
}
// ReturnSQLResult return sql result
func (m *InterfaceMethod) ReturnSQLResult() bool {
for _, res := range m.Result {
if res.IsSQLResult() {
return true
}
}
return false
}
// ReturnSQLRow return sql result
func (m *InterfaceMethod) ReturnSQLRow() bool {
for _, res := range m.Result {
if res.IsSQLRow() {
return true
}
}
return false
}
// ReturnSQLRows return sql result
func (m *InterfaceMethod) ReturnSQLRows() bool {
for _, res := range m.Result {
if res.IsSQLRows() {
return true
}
}
return false
}
// ReturnNothing not return error and rowAffected
func (m *InterfaceMethod) ReturnNothing() bool {
for _, res := range m.Result {
if res.IsError() || res.Name == "rowsAffected" {
return false
}
}
return true
}
// ReturnRowsAffected return rows affected
func (m *InterfaceMethod) ReturnRowsAffected() bool {
for _, res := range m.Result {
if res.Name == "rowsAffected" {
return true
}
}
return false
}
// ReturnError return error
func (m *InterfaceMethod) ReturnError() bool {
for _, res := range m.Result {
if res.IsError() {
return true
}
}
return false
}
// IsRepeatFromDifferentInterface check different interface has same mame method
func (m *InterfaceMethod) IsRepeatFromDifferentInterface(newMethod *InterfaceMethod) bool {
return m.MethodName == newMethod.MethodName && m.InterfaceName != newMethod.InterfaceName && m.TargetStruct == newMethod.TargetStruct
}
// IsRepeatFromSameInterface check different interface has same mame method
func (m *InterfaceMethod) IsRepeatFromSameInterface(newMethod *InterfaceMethod) bool {
return m.MethodName == newMethod.MethodName && m.InterfaceName == newMethod.InterfaceName && m.TargetStruct == newMethod.TargetStruct
}
//GetParamInTmpl return param list
func (m *InterfaceMethod) GetParamInTmpl() string {
return paramToString(m.Params)
}
// GetResultParamInTmpl return result list
func (m *InterfaceMethod) GetResultParamInTmpl() string {
return paramToString(m.Result)
}
// SQLParamName sql param map key,
func (m *InterfaceMethod) SQLParamName(param string) string {
return strings.Replace(param, ".", "", -1)
}
// paramToString param list to string used in tmpl
func paramToString(params []parser.Param) string {
var res []string
for _, param := range params {
res = append(res, param.TmplString())
}
return strings.Join(res, ",")
}
// DocComment return comment sql add "//" every line
func (m *InterfaceMethod) DocComment() string {
return strings.Replace(strings.Replace(strings.TrimSpace(m.Doc), "\n", "\n// ", -1), "// ", "// ", -1)
}
// checkMethod check interface methods
func (m *InterfaceMethod) checkMethod(methods []*InterfaceMethod, s *QueryStructMeta) (err error) {
if model.GormKeywords.FullMatch(m.MethodName) {
return fmt.Errorf("can not use keyword as method name:%s", m.MethodName)
}
// TODO check methods Always empty?
for _, method := range methods {
if m.IsRepeatFromDifferentInterface(method) {
return fmt.Errorf("can not generate method with the same name from different interface:[%s.%s] and [%s.%s]",
m.InterfaceName, m.MethodName, method.InterfaceName, method.MethodName)
}
}
for _, f := range s.Fields {
if f.Name == m.MethodName {
return fmt.Errorf("can not generate method same name with struct field:[%s.%s] and [%s.%s]",
m.InterfaceName, m.MethodName, s.ModelStructName, f.Name)
}
}
return nil
}
// checkParams check all parameters
func (m *InterfaceMethod) checkParams(params []parser.Param) (err error) {
paramList := make([]parser.Param, len(params))
for i, param := range params {
switch {
case param.Package == "UNDEFINED":
param.Package = m.Package
case param.IsError() || param.IsNull():
return fmt.Errorf("type error on interface [%s] param: [%s]", m.InterfaceName, param.Name)
case param.IsGenM():
param.Type = "map[string]interface{}"
param.Package = ""
case param.IsGenT():
param.Type = m.OriginStruct.Type
param.Package = m.OriginStruct.Package
}
paramList[i] = param
}
m.Params = paramList
return
}
// checkResult check all parameters and replace gen.T by target structure. Parameters must be one of int/string/struct/map
func (m *InterfaceMethod) checkResult(result []parser.Param) (err error) {
resList := make([]parser.Param, len(result))
var hasError bool
for i, param := range result {
if param.Package == "UNDEFINED" {
param.Package = m.Package
}
if param.IsGenM() {
param.Type = "map[string]interface{}"
param.Package = ""
}
switch {
case param.InMainPkg():
return fmt.Errorf("query method cannot return struct of main package in [%s.%s]", m.InterfaceName, m.MethodName)
case param.IsError():
if hasError {
return fmt.Errorf("query method cannot return more than 1 error value in [%s.%s]", m.InterfaceName, m.MethodName)
}
param.SetName("err")
hasError = true
case param.Eq(m.OriginStruct) || param.IsGenT():
if !m.ResultData.IsNull() {
return fmt.Errorf("query method cannot return more than 1 data value in [%s.%s]", m.InterfaceName, m.MethodName)
}
param.SetName("result")
param.Type = m.OriginStruct.Type
param.Package = m.OriginStruct.Package
m.ResultData = param
case param.IsInterface():
return fmt.Errorf("query method can not return interface in [%s.%s]", m.InterfaceName, m.MethodName)
case param.IsGenRowsAffected():
param.Type = "int64"
param.Package = ""
param.SetName("rowsAffected")
m.GormOption = "Exec"
case param.IsSQLResult():
param.Type = "Result"
param.Package = "sql"
param.SetName("result")
m.GormOption = "Statement.ConnPool.ExecContext"
case param.IsSQLRow():
param.Type = "Row"
param.Package = "sql"
param.SetName("row")
m.GormOption = "Raw"
param.IsPointer = true
case param.IsSQLRows():
param.Type = "Rows"
param.Package = "sql"
param.SetName("rows")
m.GormOption = "Raw"
param.IsPointer = true
default:
if !m.ResultData.IsNull() {
return fmt.Errorf("query method cannot return more than 1 data value in [%s.%s]", m.InterfaceName, m.MethodName)
}
if param.Package == "" && !(param.IsBaseType() || param.IsMap() || param.IsTime()) {
param.Package = m.Package
}
param.SetName("result")
m.ResultData = param
}
resList[i] = param
}
m.Result = resList
return
}
// checkSQL get sql from comment and check it
func (m *InterfaceMethod) checkSQL() (err error) {
m.SQLString = m.parseDocString()
if err = m.sqlStateCheckAndSplit(); err != nil {
err = fmt.Errorf("interface %s member method %s check sql err:%w", m.InterfaceName, m.MethodName, err)
}
return
}
func (m *InterfaceMethod) parseDocString() string {
docString := strings.TrimSpace(m.getSQLDocString())
switch {
case strings.HasPrefix(strings.ToLower(docString), "sql("):
docString = docString[4 : len(docString)-1]
m.GormOption = "Raw"
if m.ResultData.IsNull() {
m.GormOption = "Exec"
}
case strings.HasPrefix(strings.ToLower(docString), "where("):
docString = docString[6 : len(docString)-1]
m.GormOption = "Where"
default:
m.GormOption = "Raw"
if m.ResultData.IsNull() {
m.GormOption = "Exec"
}
}
// if wrapped by ", trim it
if strings.HasPrefix(docString, `"`) && strings.HasSuffix(docString, `"`) {
docString = docString[1 : len(docString)-1]
}
return docString
}
func (m *InterfaceMethod) getSQLDocString() string {
docString := strings.TrimSpace(m.Doc)
/*
// methodName descriptive message
// (this blank line is needed)
// sql
*/
if index := strings.Index(docString, "\n\n"); index != -1 {
if strings.Contains(docString[index+2:], m.MethodName) {
docString = docString[:index]
} else {
docString = docString[index+2:]
}
}
/* //methodName sql */
docString = strings.TrimPrefix(docString, m.MethodName)
// TODO: using sql key word to split comment
return docString
}
// sqlStateCheckAndSplit check sql with an adeterministic finite automaton
func (m *InterfaceMethod) sqlStateCheckAndSplit() error {
sqlString := m.SQLString
m.Section = NewSection()
var buf model.SQLBuffer
for i := 0; !strOutRange(i, sqlString); i++ {
b := sqlString[i]
switch b {
case '"':
_ = buf.WriteByte(sqlString[i])
for i++; ; i++ {
if strOutRange(i, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
_ = buf.WriteByte(sqlString[i])
if sqlString[i] == '"' && sqlString[i-1] != '\\' {
break
}
}
case '\'':
_ = buf.WriteByte(sqlString[i])
for i++; ; i++ {
if strOutRange(i, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
_ = buf.WriteByte(sqlString[i])
if sqlString[i] == '\'' && sqlString[i-1] != '\\' {
break
}
}
case '\\':
if sqlString[i+1] == '@' {
i++
buf.WriteSQL(sqlString[i])
continue
}
buf.WriteSQL(b)
case '{', '@':
if sqlClause := buf.Dump(); strings.TrimSpace(sqlClause) != "" {
m.Section.members = append(m.Section.members, section{
Type: model.SQL,
Value: strconv.Quote(sqlClause),
})
}
if strOutRange(i+1, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
if b == '{' && sqlString[i+1] == '{' {
for i += 2; ; i++ {
if strOutRange(i, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
if sqlString[i] == '"' {
_ = buf.WriteByte(sqlString[i])
for i++; ; i++ {
if strOutRange(i, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
_ = buf.WriteByte(sqlString[i])
if sqlString[i] == '"' && sqlString[i-1] != '\\' {
break
}
}
i++
}
if strOutRange(i+1, sqlString) {
return fmt.Errorf("incomplete SQL:%s", sqlString)
}
if sqlString[i] == '}' && sqlString[i+1] == '}' {
i++
sqlClause := buf.Dump()
part, err := m.Section.checkTemplate(sqlClause)
if err != nil {
return fmt.Errorf("sql [%s] dynamic template %s err:%w", sqlString, sqlClause, err)
}
m.Section.members = append(m.Section.members, part)
break
}
buf.WriteSQL(sqlString[i])
}
}
if b == '@' {
i++
status := model.DATA
if sqlString[i] == '@' {
i++
status = model.VARIABLE
}
for ; ; i++ {
if strOutRange(i, sqlString) || isEnd(sqlString[i]) {
varString := buf.Dump()
params, err := m.Section.checkSQLVar(varString, status, m)
if err != nil {
return fmt.Errorf("sql [%s] varable %s err:%s", sqlString, varString, err)
}
m.Section.members = append(m.Section.members, params)
i--
break
}
buf.WriteSQL(sqlString[i])
}
}
default:
buf.WriteSQL(b)
}
}
if sqlClause := buf.Dump(); strings.TrimSpace(sqlClause) != "" {
m.Section.members = append(m.Section.members, section{
Type: model.SQL,
Value: strconv.Quote(sqlClause),
})
}
return nil
}
// checkSQLVarByParams return external parameters, table name
func (m *InterfaceMethod) checkSQLVarByParams(param string, status model.Status) (result section, err error) {
for _, p := range m.Params {
structName := strings.Split(param, ".")[0]
if p.Name == structName {
if p.Name != param {
p = parser.Param{
Name: param,
Type: "string",
}
}
switch status {
case model.DATA:
if !m.isParamExist(param) {
m.SQLParams = append(m.SQLParams, p)
}
case model.VARIABLE:
if p.Type != "string" || p.IsArray {
err = fmt.Errorf("variable name must be string :%s type is %s", param, p.TypeName())
return
}
param = fmt.Sprintf("%s.Quote(%s)", m.S, param)
}
result = section{
Type: status,
Value: param,
}
return
}
}
if param == "table" {
result = section{
Type: model.SQL,
Value: strconv.Quote(m.Table),
}
return
}
return result, fmt.Errorf("unknow variable param:%s", param)
}
// isParamExist check param duplicate
func (m *InterfaceMethod) isParamExist(paramName string) bool {
for _, param := range m.SQLParams {
if param.Name == paramName {
return true
}
}
return false
}