-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_advance_query_test.go
341 lines (314 loc) · 11.8 KB
/
example_advance_query_test.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
package rapier_test
import (
"testing"
rapier "github.com/thinkgos/gorm-rapier"
"github.com/thinkgos/gorm-rapier/testdata"
"gorm.io/gorm/clause"
)
func Test_Example_AdvanceQuery_Locking(t *testing.T) {
// Basic FOR UPDATE lock
_, _ = rapier.NewExecutor[testdata.Dict](db).
LockingUpdate().
TakeOne()
// Basic FOR UPDATE lock with Clauses api
_, _ = rapier.NewExecutor[testdata.Dict](db).
Clauses(clause.Locking{Strength: "UPDATE"}).
TakeOne()
// Basic FOR SHARE lock
_, _ = rapier.NewExecutor[testdata.Dict](db).
LockingShare().
TakeOne()
// Basic FOR SHARE lock with Clauses api
_, _ = rapier.NewExecutor[testdata.Dict](db).
Clauses(clause.Locking{Strength: "SHARE"}).
TakeOne()
// Basic FOR UPDATE NOWAIT lock with Clauses api
_, _ = rapier.NewExecutor[testdata.Dict](db).
Clauses(clause.Locking{Strength: "UPDATE", Options: "NOWAIT"}).
TakeOne()
}
func Test_Example_AdvanceQuery_SubQuery(t *testing.T) {
refDict := testdata.Ref_Dict()
_ = rapier.NewExecutor[testdata.Dict](db).
SelectExpr(
refDict.Id,
refDict.Key,
rapier.NewExecutor[testdata.Dict](db).
SelectExpr(rapier.Star.Count()).
Where(
refDict.Name.Eq("kkk"),
).
IntoSubQueryExpr().As("total"),
).
Where(refDict.Key.LeftLike("key")).
Find(&struct{}{})
_, _ = rapier.NewExecutor[testdata.Dict](db).
Where(refDict.Key.EqSubQuery(
rapier.NewExecutor[testdata.Dict](db).
SelectExpr(refDict.Key).
Where(refDict.Id.Eq(1001)).
IntoDB(),
)).
FindAll()
}
func Test_Example_AdvanceQuery_FromSubQuery(t *testing.T) {
refDict := testdata.Ref_Dict()
_, _ = rapier.NewExecutor[testdata.Dict](db).
TableExpr(
rapier.From{
Alias: "u",
SubQuery: rapier.NewExecutor[testdata.Dict](db).
SelectExpr(refDict.Key).
IntoDB(),
},
rapier.From{
Alias: "p",
SubQuery: rapier.NewExecutor[testdata.Dict](db).
SelectExpr(refDict.Key).
IntoDB(),
},
).
FindAll()
}
func Test_Example_AdvanceQuery_IN_WithMultipleColumns(t *testing.T) {
refDict := testdata.Ref_Dict()
record1, _ := rapier.NewExecutor[testdata.Dict](db).
Where(
rapier.NewColumns(refDict.Name, refDict.IsPin).
In([][]any{{"name1", true}, {"name2", false}}),
).
FindAll()
_ = record1
record2, _ := rapier.NewExecutor[testdata.Dict](db).
Where(
rapier.NewColumns(refDict.Name, refDict.IsPin).
In(
rapier.NewExecutor[testdata.Dict](db).
SelectExpr(refDict.Name, refDict.IsPin).
Where(refDict.Id.In(10, 11)).
IntoDB(),
),
).
FindAll()
_ = record2
}
func Test_Example_AdvanceQuery_FirstOrInit(t *testing.T) {
refDict := testdata.Ref_Dict()
// NOTE!!!: if with expr condition will not initialize the field when initializing, so we should use
// `Attrs`, `AttrsExpr`, `Assign`, `AssignExpr` attributes to indicate these fields.
// `Attrs`, `AttrsExpr`
// with expr
result, _ := rapier.NewExecutor[testdata.Dict](db).
Where(refDict.Name.Eq("myname")).
AttrsExpr(refDict.Remark.Value("remark11")).
FirstOrInit()
newdict := result.Data
rowsAffected := result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Condition use expr. here will not initialize the field of the condition when initializing.
// if not found
// newdict -> Dict{ Remark: "remark11" }
//
// if found, `Attrs`, `AttrsExpr` are ignored
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark" }
// with original gorm api
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(&testdata.Dict{
Name: "non_existing",
}).
FirstOrInit()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Condition not use expr, here will initialize the field of the condition when initializing.
// newdict -> Dict{ Name: "non_existing" } if not found
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(&testdata.Dict{
Name: "myname",
}).
Attrs(&testdata.Dict{Remark: "remark11"}).
FirstOrInit()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Condition not use expr, here will initialize the field of the condition when initializing.
// if not found
// newdict -> Dict{ Name: "myname", Remark: "remark11" }
//
// if found, `Attrs`, `AttrsExpr` are ignored
// newdict -> Dict{ Id: 1, Name: "myname", Remark: "remark" }
// `Assign`, `AssignExpr`
// with expr
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(refDict.Name.Eq("myname")).
AssignExpr(refDict.Remark.Value("remark11")).
FirstOrInit()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Where condition use expr, here will not initialize the field of the condition when initializing.
// if not found
// newdict -> Dict{ Remark: "remark11" }
//
// if not found
// newdict -> Dict{ Name: "non_existing" }
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(&testdata.Dict{
Name: "myname",
}).
Assign(&testdata.Dict{Remark: "remark11"}).
FirstOrInit()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: condition not use expr, here will initialize the field of the condition when initializing.
// if not found
// newdict -> Dict{ Name: "myname", Remark: "remark11" }
//
// if found, `Assign`, `AssignExpr` are set on the struct
// newdict -> Dict{ Id: 1, Name: "myname", Remark: "remark11" }
}
func Test_Example_AdvanceQuery_FirstOrCreate(t *testing.T) {
refDict := testdata.Ref_Dict()
// NOTE!!!: if with expr condition will not initialize the field when creating, so we should use
// `Attrs`, `AttrsExpr`, `Assign`, `AssignExpr` attributes to indicate these fields.
// `Attrs`, `AttrsExpr`
// with expr
result, _ := rapier.NewExecutor[testdata.Dict](db).
Where(refDict.Name.Eq("myname")).
AttrsExpr(refDict.Remark.Value("remark11")).
FirstOrCreate()
newdict := result.Data
rowsAffected := result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Condition use expr. here will not initialize the field of the condition when creating.
// if not found. initialize with additional attributes
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1;
// INSERT INTO `dict` (`key`,`name`,`is_pin`,`remark`,`created_at`,`updated_at`) VALUES ("","",false,"remark11","2024-03-08 02:20:10.853","2024-03-08 02:20:10.853");
// newdict -> Dict{ Id: 11, Name: "", Remark: "remark11" } if not found
//
// if found, `Attrs`, `AttrsExpr` are ignored.
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark" }
// with original gorm api
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(&testdata.Dict{
Name: "myname",
}).
Attrs(&testdata.Dict{Remark: "remark11"}).
FirstOrCreate()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Condition not use expr, here will initialize the field of the condition when creating.
// if not found, initialize with given conditions and additional attributes
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1;
// INSERT INTO `dict` (`key`,`name`,`is_pin`,`remark`,`created_at`,`updated_at`) VALUES ("","myname",false,"remark11","2024-03-08 02:20:10.853","2024-03-08 02:20:10.853");
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark11" }
//
// if found, `Attrs`, `AttrsExpr` are ignored
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark" }
// `Assign`, `AssignExpr`
// with expr
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(refDict.Name.Eq("myname")).
AssignExpr(refDict.Remark.Value("remark11")).
FirstOrCreate()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: Where condition use expr, here will not initialize the field of the condition when creating.
// whether it is found or not, and `Assign`, `AssignExpr` attributes are saved back to the database.
// if no found
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1
// INSERT INTO `dict` (`key`,`name`,`is_pin`,`remark`,`created_at`,`updated_at`) VALUES ("","",false,"remark11","2024-03-08 02:26:12.619","2024-03-08 02:26:12.619");
// newdict -> Dict{ Id: 11, Name: "", Remark: "remark11", ... }
//
// if found
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1
// UPDATE `dict` SET `remark` = "remark11" WHERE id = "11"
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark11", ... }
result, _ = rapier.NewExecutor[testdata.Dict](db).
Where(&testdata.Dict{
Name: "myname",
}).
Assign(&testdata.Dict{Remark: "remark11"}).
FirstOrCreate()
newdict = result.Data
rowsAffected = result.RowsAffected
_ = newdict
_ = rowsAffected
// NOTE: condition not use expr, here will initialize the field of the condition when creating.
// whether it is found or not, and `Assign`, `AssignExpr` attributes are saved back to the database.
// if no found
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1
// INSERT INTO `dict` (`key`,`name`,`is_pin`,`remark`,`created_at`,`updated_at`) VALUES ("","myname",false,"remark11","2024-03-08 02:26:12.619","2024-03-08 02:26:12.619");
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark11", ... }
//
// if found
// SELECT * FROM `dict` WHERE `dict`.`name` = "myname" ORDER BY `dict`.`id` LIMIT 1
// UPDATE `dict` SET `remark` = "remark11" WHERE id = "11"
// newdict -> Dict{ Id: 11, Name: "myname", Remark: "remark11", ... }
}
func Test_Example_AdvanceQuery_Pluck(t *testing.T) {
var ids []int64
refDict := testdata.Ref_Dict()
// with expr api
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprString(refDict.Name)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprBool(refDict.IsPin)
_ = rapier.NewExecutor[testdata.Dict](db).Pluck("id", &ids)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprInt(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprInt8(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprInt16(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprInt32(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprInt64(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprUint(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprUint8(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprUint16(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprUint32(refDict.Id)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckExprUint64(refDict.Id)
// with original gorm api
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckString("name")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckBool("is_pin")
_ = rapier.NewExecutor[testdata.Dict](db).Pluck("id", &ids)
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckInt("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckInt8("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckInt16("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckInt32("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckInt64("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckUint("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckUint8("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckUint16("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckUint32("id")
_, _ = rapier.NewExecutor[testdata.Dict](db).PluckUint64("id")
}
func Test_Example_AdvanceQuery_Count(t *testing.T) {
total, err := rapier.NewExecutor[testdata.Dict](db).Count()
_ = err
_ = total
}
func Test_Example_AdvanceQuery_Exist(t *testing.T) {
refDict := testdata.Ref_Dict()
b, err := rapier.NewExecutor[testdata.Dict](db).Where(refDict.Id.Eq(100)).Exist()
_ = err
_ = b
}
func Test_Example_AdvanceQuery_FindByPage(t *testing.T) {
rows, total, err := rapier.NewExecutor[testdata.Dict](db).
FindAllByPage(10, 10)
_ = err
_ = rows
_ = total
rows, total, err = rapier.NewExecutor[testdata.Dict](db).
FindAllPaginate(2, 10)
_ = err
_ = rows
_ = total
}