-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgentity.gen.go
460 lines (368 loc) · 10.1 KB
/
gentity.gen.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
// DO NOT EDIT THIS FILE!
// It is autogenerated by gentity
package main
import (
"fmt"
"strings"
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type InsertOption struct {
ReturnAndUpdateVals bool
OnConflictStatement string
}
type DBExecutor interface {
Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
type DBExecutorKey string
func fromContext(ctx context.Context) DBExecutor {
dbe, ok := ctx.Value(DBExecutorKey("dbExecutor")).(DBExecutor)
if !ok {
panic("DBExecutor not found in context")
}
return dbe
}
/********************************
* Test autoincrement: {ID id uint64 false false [] [] 0 0 false [primary]}
* ID uint64 id <primary> <autoincrement>
* IntA int int_a
* IntB SomeInts int_b
* StrA string str_a
* TimeA time.Time time_a
* Primary index: primary
* Unique indexes:
* primary: id
* test_str_a: str_a
* Non unique indexes:
* test_int_a_int_b: int_a, int_b
********************************/
type Tests []*Test
func (e *Test) Insert(ctx context.Context, insertOptions ...InsertOption) (err error) {
dbExecutor := fromContext(ctx)
var sql, returning string
var args []any
if e.ID == 0 {
sql = `INSERT INTO "tests" (int_a, int_b, str_a, time_a)
VALUES ($1, $2, $3, $4)`
returning = ` RETURNING id`
args = []any{ e.IntA, e.IntB, e.StrA, e.TimeA }
} else {
sql = `INSERT INTO "tests" (id, int_a, int_b, str_a, time_a)
VALUES ($1, $2, $3, $4, $5)`
args = []any{ e.ID, e.IntA, e.IntB, e.StrA, e.TimeA }
}
var returnAndUpdateVals bool
for _, opt := range insertOptions {
if opt.ReturnAndUpdateVals {
returnAndUpdateVals = true
}
if opt.OnConflictStatement != "" {
sql += " ON CONFLICT "+ opt.OnConflictStatement
}
}
if returnAndUpdateVals {
returning = ` RETURNING id, int_a, int_b, str_a, time_a`
}
if returning != "" {
sql += returning
}
var rows pgx.Rows
rows, err = dbExecutor.Query(ctx, sql, args...)
defer func(){
rows.Close()
if err == nil {
err = rows.Err()
}
if err != nil {
err = fmt.Errorf("Insert query '%s' failed: %+v", sql, err)
}
}()
if returnAndUpdateVals {
if ! rows.Next() {
// TODO: on conflict do nothing case
return fmt.Errorf("Insert-query doesn't return anything, but has returning clause")
}
if err = rows.Scan(
&e.ID,
&e.IntA,
&e.IntB,
&e.StrA,
&e.TimeA,
); err != nil {
return
}
} else if e.ID == 0 {
if ! rows.Next() {
// TODO: on conflict do nothing case
return fmt.Errorf("Insert-query doesn't return anything, but has returning clause")
}
if err = rows.Scan(&e.ID); err != nil {
return
}
}
return nil
}
func (es Tests) Insert(ctx context.Context, insertOptions ...InsertOption) (err error) {
dbExecutor := fromContext(ctx)
var sql string
var sqlRows []string
var args []any
if len(es) == 0 {
return nil
}
if es[0].ID == 0 {
sql = `INSERT INTO "tests" (id, int_a, int_b, str_a, time_a) VALUES `
for i, e := range es {
sqlRows = append(sqlRows, fmt.Sprintf(`(DEFAULT, $%d, $%d, $%d, $%d)`, i * 4 + 1, i * 4 + 2, i * 4 + 3, i * 4 + 4))
args = append(args, e.IntA, e.IntB, e.StrA, e.TimeA)
}
} else {
sql = `INSERT INTO "tests" (id, int_a, int_b, str_a, time_a) VALUES `
for i, e := range es {
sqlRows = append(sqlRows, fmt.Sprintf(`($%d, $%d, $%d, $%d, $%d)`, i * 5 + 1, i * 5 + 2, i * 5 + 3, i * 5 + 4, i * 5 + 5))
args = append(args, e.ID, e.IntA, e.IntB, e.StrA, e.TimeA)
}
}
sql += strings.Join(sqlRows, ", ")
for _, opt := range insertOptions {
if opt.ReturnAndUpdateVals {
err = fmt.Errorf("ReturnAndUpdateVals option is not supported for multi-insert now")
return
}
if opt.OnConflictStatement != "" {
sql += " ON CONFLICT "+ opt.OnConflictStatement
}
}
_, err = dbExecutor.Exec(ctx, sql, args...)
if err != nil {
err = fmt.Errorf("Insert query '%s' failed: %+v", sql, err)
}
return
}
func (e *Test) Update(ctx context.Context) (err error) {
dbExecutor := fromContext(ctx)
sql := `UPDATE "tests" SET int_a = $1, int_b = $2, str_a = $3, time_a = $4 WHERE id = $5`
_, err = dbExecutor.Exec(ctx, sql, e.IntA, e.IntB, e.StrA, e.TimeA, e.ID);
if err != nil {
err = fmt.Errorf("Update query '%s' failed: %+v", sql, err)
}
return
}
func (e *Test) Delete(ctx context.Context) (err error) {
dbExecutor := fromContext(ctx)
sql := `DELETE FROM "tests" WHERE id = $1`
_, err = dbExecutor.Exec(
ctx,
sql,
e.ID,
);
if err != nil {
err = fmt.Errorf("Delete query '%s' failed: %+v", sql, err)
}
return
}
func (es Tests) Delete(ctx context.Context) (err error) {
dbExecutor := fromContext(ctx)
sql := `DELETE FROM "tests" WHERE `
rowsSql := make([]string, len(es))
var args []any
for i, e := range es {
rowsSql[i] = fmt.Sprintf(`(id = $%d)`, i * 1 + 1)
args = append(args, e.ID)
}
sql = sql + strings.Join(rowsSql, " OR ")
_, err = dbExecutor.Exec(ctx, sql, args...);
if err != nil {
err = fmt.Errorf("Delete query '%s' failed: %+v", sql, err)
}
return
}
func (Test) Find(ctx context.Context, condition string, values []interface{}) (entities Tests, err error) {
return Test{}.Query(
ctx,
`SELECT id, int_a, int_b, str_a, time_a
FROM "tests"
WHERE ` + condition,
values,
)
}
func (Test) FindCh(ctx context.Context, condition string, values []interface{}, entitiesCh chan<- *Test, errCh chan<- error) {
Test{}.QueryCh(
ctx,
`SELECT id, int_a, int_b, str_a, time_a
FROM "tests"
WHERE ` + condition,
values,
entitiesCh,
errCh,
)
}
func (Test) Query(ctx context.Context, sql string, values []interface{}) (entities Tests, err error) {
dbExecutor := fromContext(ctx)
var rows pgx.Rows
rows, err = dbExecutor.Query(
ctx,
sql,
values...
)
defer func(){
rows.Close()
if err == nil {
err = rows.Err()
}
if err != nil {
if len(sql) > 500 {
sql = sql[:500] + "..."
}
err = fmt.Errorf("Query '%s' failed: %+v", sql, err)
}
}()
for rows.Next() {
e := Test{}
if err = rows.Scan(
&e.ID,
&e.IntA,
&e.IntB,
&e.StrA,
&e.TimeA,
); err != nil {
return
}
entities = append(entities, &e)
}
return entities, nil
}
func (Test) QueryCh(ctx context.Context, sql string, values []interface{}, entitiesCh chan<- *Test, errCh chan<- error) {
var (
err error
rows pgx.Rows
)
defer func(){
if err != nil {
errCh <- err
}
close(errCh)
close(entitiesCh)
}()
dbExecutor := fromContext(ctx)
rows, err = dbExecutor.Query(ctx, sql, values...)
defer func(){
rows.Close()
if err == nil {
err = rows.Err()
}
if err != nil {
if len(sql) > 500 {
sql = sql[:500] + "..."
}
err = fmt.Errorf("Query '%s' failed: %+v", sql, err)
}
}()
if err != nil {
return
}
for rows.Next() {
e := Test{}
if err = rows.Scan(
&e.ID,
&e.IntA,
&e.IntB,
&e.StrA,
&e.TimeA,
); err != nil {
errCh <- err
return
}
entitiesCh <- &e
}
return
}
func (e Test) GetAll(ctx context.Context) (Tests, error) {
return e.Find(ctx, "1=1", []any{})
}
func (e Test) GetAllCh(ctx context.Context, entitiesCh chan<- *Test, errCh chan<- error) {
e.FindCh(ctx, "1=1", []any{}, entitiesCh, errCh)
}
func (e Test) GetByPrimary(ctx context.Context, id uint64) (*Test, error) {
es, err := e.Find(
ctx,
"id = $1",
[]any{ id },
)
if err != nil {
return nil, err
}
if len(es) == 1 {
return es[0], nil
}
return nil, nil
}
func (e Test) MultiGetByPrimary(ctx context.Context, id []uint64) (Tests, error) {
var params []any = make([]any, 0, len(id) * 1)
where := make([]string, len(id))
for i := range id {
where[i] = fmt.Sprintf("(id = $%d)", 1 + i)
params = append(params, id[i])
}
return e.Find(ctx, strings.Join(where, " OR "), params)
}
func (e Test) MultiGetByPrimaryCh(ctx context.Context, id []uint64, entitiesCh chan<- *Test, errCh chan<- error) {
var params []any = make([]any, 0, len(id) * 1)
where := make([]string, len(id))
for i := range id {
where[i] = fmt.Sprintf("(id = $%d)", 1 + i)
params = append(params, id[i])
}
e.FindCh(ctx, strings.Join(where, " OR "), params, entitiesCh, errCh)
}
func (e Test) GetByTestStrA(ctx context.Context, strA string) (*Test, error) {
es, err := e.Find(
ctx,
"str_a = $1",
[]any{ strA },
)
if err != nil {
return nil, err
}
if len(es) == 1 {
return es[0], nil
}
return nil, nil
}
func (e Test) MultiGetByTestStrA(ctx context.Context, strA []string) (Tests, error) {
var params []any = make([]any, 0, len(strA) * 1)
where := make([]string, len(strA))
for i := range strA {
where[i] = fmt.Sprintf("(str_a = $%d)", 1 + i)
params = append(params, strA[i])
}
return e.Find(ctx, strings.Join(where, " OR "), params)
}
func (e Test) MultiGetByTestStrACh(ctx context.Context, strA []string, entitiesCh chan<- *Test, errCh chan<- error) {
var params []any = make([]any, 0, len(strA) * 1)
where := make([]string, len(strA))
for i := range strA {
where[i] = fmt.Sprintf("(str_a = $%d)", 1 + i)
params = append(params, strA[i])
}
e.FindCh(ctx, strings.Join(where, " OR "), params, entitiesCh, errCh)
}
func (e Test) GetByTestIntAIntB(ctx context.Context, intA int, intB SomeInts) (Tests, error) {
return e.Find(
ctx,
"int_a = $1 AND int_b = $2",
[]any{ intA, intB },
)
}
func (e Test) GetByTestIntAIntBCh(ctx context.Context, intA int, intB SomeInts, entitiesCh chan<- *Test, errCh chan<- error) {
e.FindCh(
ctx,
"int_a = $1 AND int_b = $2",
[]any{ intA, intB },
entitiesCh,
errCh,
)
}