-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_query.go
339 lines (323 loc) · 9.75 KB
/
crud_query.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
package korm
import (
"bytes"
"strconv"
)
func (this *GoSourceWriter) AddFunc_ORM_Query(info StructType) {
this.buf.WriteString(`// Select ` + info.Name + "\n")
this.AddImportPath("strings")
this.AddImportPath("bytes")
tName := info.GetOrmSubType_SelectName()
this.buf.WriteString(`type ` + tName + ` struct {
supper *` + ormAllName + `
bufWhere bytes.Buffer
argsWhere []interface{}
orderBy []string
limit int
offset int
linkOpList []string
isLinkBegin bool
joinNode *` + this.getLeftJoinNodeName() + `
rootInfo ` + this.getLeftJoinRootInfoName() + `
}
func (this *` + info.GetOrmTypeName() + `) Select() *` + tName + `{
one := &` + info.GetOrmSubType_SelectName() + `{supper: this.supper, linkOpList: []string{"AND"}, isLinkBegin: true}
one.joinNode = &` + this.getLeftJoinNodeName() + `{TableName: "_0"}
one.joinNode.Root = &one.rootInfo
one.rootInfo.TableNameIdx = 1
return one
}
`)
this.AddFuncOrderBy(tName, info, false)
this.AddImportPath("strconv")
this.setLimitOffset(tName)
this.buildFn_Count_Exist(tName, info.Name)
this.buildFn_ResultOne(tName, info.Name)
this.buildFn_ResultOne2(tName, info)
this.buildFn_ResultList(tName, info)
this.buildFn_ResultMap(tName, info)
this.buildFn_ResultTotalMatch(tName, info)
this.AddImportPath("context")
for _, one := range info.FieldList {
if one.IsStarExpr == false {
continue
}
otherStruct := this.structMap[one.TypeName]
ljName := otherStruct.GetLeftJoinName()
this.buf.WriteString(`func (this *` + tName + `) LeftJoin_` + one.FiledName + `() *` + ljName + `{
node := this.joinNode.AddLeftJoin("` + one.TypeName + `", ` + strconv.Quote(one.FiledName) + `, "` + one.LjField_This + `", "` + one.LjFiled_Other + `")
return &` + ljName + `{
joinNode: node,
bufWhere: &this.bufWhere,
argsWhereP: &this.argsWhere,
isLinkBeginP: &this.isLinkBegin,
linkOpListP: &this.linkOpList,
orderByP: &this.orderBy,
}
}
`)
this.defineLeftJoinType(ljName, otherStruct)
}
}
func (info *StructType) BuildQueryString(this *GoSourceWriter, ret string) string {
var buf bytes.Buffer
info.BuildQueryStringL1(&buf, "")
buf.WriteString(`
var result *sql.Rows
` + this.writeErrDecl_IfMust() + `
` + createCodeQuery(createCodeQuery_Req{
arg0: "buf2.String(), this.argsWhere...",
isExec: false,
ret0: "result, err",
}) + this.writeCheckErr(ret) + `
defer result.Close()
`)
return buf.String()
}
func (info *StructType) BuildQueryStringOnConn() string {
var buf bytes.Buffer
info.BuildQueryStringL1(&buf, "SQL_CALC_FOUND_ROWS ")
buf.WriteString(`var conn *sql.Conn
var result *sql.Rows
if this.supper.db != nil {
var err error
conn, err = this.supper.db.Conn(context.Background())
if err != nil {
panic(err)
}
defer conn.Close()
result, err = conn.QueryContext(context.Background(), buf2.String(), this.argsWhere...)
if err != nil {
panic(err)
}
} else {
var err error
result, err = this.supper.tx.Query(buf2.String(), this.argsWhere...)
if err != nil {
panic(err)
}
}
`)
return buf.String()
}
func (info *StructType) BuildQueryStringL1(buf *bytes.Buffer, selectPrefixMysql string) {
buf.WriteString(info.isD_CallFillNameList() + `
var buf2 bytes.Buffer
buf2.WriteString("SELECT ")
`)
if selectPrefixMysql != "" {
buf.WriteString(`if this.supper.mode == korm.InitModeMysql {
buf2.WriteString("` + selectPrefixMysql + ` ")
}
`)
}
if info.IsView {
bufIn := bytes.NewBuffer(nil)
for idx, one := range info.FieldList {
if idx > 0 {
bufIn.WriteString(` + "," + `)
}
node, fn := info.View_getQueryNode_ByFieldName(one.FiledName)
bufIn.WriteString(node + `.joinNode.TableName + "." + ` + "\"`" + fn + "`\" ")
}
buf.WriteString("buf2.WriteString(" + bufIn.String() + ")\n")
} else {
buf.WriteString(`this.joinNode.FillSelect(&buf2, true)` + "\n")
}
infoDotName := info.Name
if info.IsView {
infoDotName = info.ViewBeginD
}
buf.WriteString(`
buf2.WriteString(" FROM ` + quoteForSql(infoDotName) + ` "+this.joinNode.TableName+" ")
buf2.WriteString(this.` + func() string {
if info.IsView {
return "query."
}
return ""
}() + `rootInfo.BufLeftJoin.String())
` + gAddWhereToBuf2 + `
if len(this.orderBy) > 0 {
buf2.WriteString("ORDER BY " + strings.Join(this.orderBy, ",") + " ")
}
if this.limit != 0 {
buf2.WriteString("LIMIT " + strconv.Itoa(this.limit) + " ")
}
if this.offset != 0 {
buf2.WriteString("OFFSET " + strconv.Itoa(this.offset) + " ")
}
`)
}
type AddWhereAllField_Req struct {
isLinkBegin string
linkOpList string
argsWhere string
}
func (this *GoSourceWriter) AddWhereAllField(info StructType, name string, isLeftJoinPart bool, needTableName bool) {
this.AddWhereAllFieldL1(info, name, isLeftJoinPart, func(fieldName string, fnName string) string {
buf := bytes.NewBuffer(nil)
buf.WriteString("this.supper.bufWhere.WriteString(")
if fnName != "" {
buf.WriteString(`"` + fnName + `(" + `)
}
if needTableName {
buf.WriteString("this.supper.joinNode.TableName+`.`+")
}
buf.WriteString(`"` + quoteForSql(fieldName))
if fnName != "" {
buf.WriteString(`)`)
}
buf.WriteString(` ")` + "\n")
return buf.String()
})
}
func (this *GoSourceWriter) AddWhereAllFieldL1(info StructType, name string, isLeftJoinPart bool, fnWField func(fieldName string, fnName string) string) {
req := AddWhereAllField_Req{
isLinkBegin: "this.isLinkBegin",
linkOpList: "this.linkOpList",
argsWhere: "this.supper.argsWhere",
}
if isLeftJoinPart {
req.isLinkBegin = `(*this.isLinkBeginP)`
req.linkOpList = `(*this.linkOpListP)`
req.argsWhere = `(*this.supper.argsWhereP)`
}
for _, f := range info.FieldList {
this.FiledOp_AddWhere(name, f, req, fnWField)
}
this.ChangeLinkOpBegin(name, "AND", req)
this.ChangeLinkOpBegin(name, "OR", req)
this.ChangeLinkOpEnd(name, req)
}
func (this *GoSourceWriter) FiledOp_AddWhere(name string, f StructFieldType, req AddWhereAllField_Req, fnWField func(fieldName string, fnName string) string) {
ftNameLink := prefix + `Where_` + name
ftName := ftNameLink + `_` + f.FiledName
writeTypeAndCond := func() {
this.buf.WriteString(`type ` + ftName + ` struct{
supper *` + name + `
isLinkBegin bool
linkOpList []string
}
func (this *` + name + `) Where` + `_` + f.FiledName + `() *` + ftName + `{
isLinkBeginValue := ` + req.isLinkBegin + `
` + req.isLinkBegin + ` = false
return &` + ftName + `{supper: this, isLinkBegin: isLinkBeginValue, linkOpList: ` + req.linkOpList + `}
}
`)
}
var fnName string
var fnNameInSql string
var fnTypeName string
writeTypeFunc := func() {
fnTypeName = ftName + "_" + fnName
this.buf.WriteString(`type ` + fnTypeName + ` struct{
supper *` + name + `
isLinkBegin bool
linkOpList []string
}
func (this *` + ftName + `) ` + fnName + `() *` + fnTypeName + `{
isLinkBeginValue := this.isLinkBegin
this.isLinkBegin = false
return &` + fnTypeName + `{supper: this.supper, isLinkBegin: isLinkBeginValue, linkOpList: this.linkOpList}
}
`)
}
declStr := ""
argsName := f.FiledName
addFn := func(op string, sqlStr string) {
this.buf.WriteString(`func (this *` + ftName + `)` + op + `(` + f.FiledName + ` ` + f.TypeName + `) *` + name + ` {
if this.supper.bufWhere.Len() > 0 {
if this.isLinkBegin == false {
this.supper.bufWhere.WriteString(this.linkOpList[0] + " ")
}
} else {
this.supper.bufWhere.WriteString("WHERE ")
}
` + fnWField(f.FiledName, "") + `
` + declStr + `this.supper.bufWhere.WriteString("` + sqlStr + ` ")
` + req.argsWhere + ` = append(` + req.argsWhere + `, ` + argsName + `)
return this.supper
}
`)
}
addFnFunc := func(op string, sqlStr string) {
this.buf.WriteString(`func (this *` + fnTypeName + `)` + op + `(` + fnNameInSql + ` int) *` + name + ` {
if this.supper.bufWhere.Len() > 0 {
if this.isLinkBegin == false {
this.supper.bufWhere.WriteString(this.linkOpList[0] + " ")
}
} else {
this.supper.bufWhere.WriteString("WHERE ")
}
` + fnWField(f.FiledName, fnNameInSql) + `
` + declStr + `this.supper.bufWhere.WriteString("` + sqlStr + ` ")
` + req.argsWhere + ` = append(` + req.argsWhere + `, ` + fnNameInSql + `)
return this.supper
}
`)
}
switch f.TypeName {
default:
if f.TypeName == "string" || f.IsIntOrUintType() {
writeTypeAndCond()
addFn("Equal", "=?")
addFn("NotEqual", "!=?")
addFn("Greater", ">?")
addFn("GreaterOrEqual", ">=?")
addFn("Less", "<?")
addFn("LessOrEqual", "<=?")
this.buf.WriteString(`func (this *` + ftName + `) In(vList []` + f.TypeName + `) *` + name + ` {
if len(vList) == 0 {
this.supper.bufWhere.WriteString("= '' AND 0 ") // 什么都不存在, 直接返回
return this.supper
}
this.supper.bufWhere.WriteString("IN (")
for idx, v := range vList {
if idx > 0 {
this.supper.bufWhere.WriteString(", ")
}
this.supper.bufWhere.WriteString("?")
` + req.argsWhere + ` = append(` + req.argsWhere + `, v)
}
this.supper.bufWhere.WriteString(") ")
return this.supper
}
`)
}
case "bool":
writeTypeAndCond()
declStr = `v` + f.FiledName + ` := strconv.FormatBool(` + f.FiledName + `)` + "\n"
argsName = "v" + f.FiledName
addFn("Equal", "=?")
case "time.Time":
writeTypeAndCond()
declStr = `v` + f.FiledName + ` := ` + f.FiledName + `.UTC().Format(time.RFC3339Nano)` + "\n"
argsName = "v" + f.FiledName
addFn("Equal", "=?")
addFn("NotEqual", "!=?")
addFn("GreaterOrEqual", ">=?")
addFn("Less", "<?")
addFn("LessOrEqual", "<=?")
case "[]byte":
writeTypeAndCond()
}
if f.TypeName == "string" || f.TypeName == "[]byte" {
fnName = "Length"
fnNameInSql = "length"
writeTypeFunc()
addFnFunc("Equal", "=?")
addFnFunc("NotEqual", "!=?")
addFnFunc("GreaterOrEqual", ">=?")
addFnFunc("Less", "<?")
addFnFunc("LessOrEqual", "<=?")
}
}
func (t StructType) GetOrmSubType_SelectName() string {
return prefix + t.Name + "_SelectObj"
}
func (this *GoSourceWriter) writeStr_IfMustValue_Equal(v bool, s string) string {
if v == this.req.GenMustFn {
return s
}
return ""
}