forked from AirspaceTechnologies/go-psql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
535 lines (427 loc) · 11.8 KB
/
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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package psql
import (
"context"
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"errors"
)
type Query struct {
tableName string
action string // can be select, update, insert, or delete
conditions []*condition // stores conditions for where clause
values map[string]interface{} // stores values for insert or update
columns []string // stores columns for select
client QueryClient
ors []*Query
ands []*Query
orderBys []string
limit int
returning []string // holds columns to return for insert, update, and delete
}
func SubQuery() *Query {
var q Query
return &q
}
func (q *Query) Where(attrs map[string]interface{}) *Query {
for col, val := range attrs {
q.conditions = append(q.conditions, &condition{col: col, val: val, negative: false})
}
return q
}
func (q *Query) WhereNot(attrs map[string]interface{}) *Query {
for col, val := range attrs {
q.conditions = append(q.conditions, &condition{col: col, val: val, negative: true})
}
return q
}
// Instead of the where clause using field = $1 use field = %v
func (q *Query) WhereRaw(raw string, vals ...interface{}) *Query {
q.conditions = append(q.conditions, &condition{raw: raw, rawVals: vals})
return q
}
func (q *Query) Or(or *Query) *Query {
q.ors = append(q.ors, or)
return q
}
// wraps on top of Ors
func (q *Query) And(and *Query) *Query {
q.ands = append(q.ands, and)
return q
}
// pass strings like "field_name ASC"
func (q *Query) OrderBy(bys ...string) *Query {
q.orderBys = append(q.orderBys, bys...)
return q
}
// limit for select query, must be > 0
func (q *Query) Limit(i int) *Query {
q.limit = i
return q
}
// default returns
func (q *Query) Returning(cols ...string) *Query {
q.returning = append(q.returning, cols...)
return q
}
/*
Exec() executes the query either with db.Exec() (RowsAffected) or db.Query() (Rows) depending on the query
Select returns Rows
Insert returns the Rows which represents the id of the inserted record by default
Delete and Update return RowsAffected unless the Returning() function is called on the query, then it returns Rows
*/
func (q *Query) Exec(ctx context.Context) (*QueryResult, error) {
var r QueryResult
if q.client == nil || !q.client.Started() {
return &r, fmt.Errorf("client or db is nil")
}
switch q.action {
case "select":
rows, err := q.execSelect(ctx)
r.Rows = rows
return &r, err
case "insert":
rows, err := q.execInsert(ctx)
r.Rows = rows
return &r, err
case "update":
var err error
if len(q.returning) == 0 {
r.RowsAffected, err = q.execUpdate(ctx)
} else {
r.Rows, err = q.execUpdateR(ctx)
}
return &r, err
case "delete":
var err error
if len(q.returning) == 0 {
r.RowsAffected, err = q.execDelete(ctx)
} else {
r.Rows, err = q.execDeleteR(ctx)
}
return &r, err
default:
return &r, fmt.Errorf("unsupported action %v", q.action)
}
}
func (q *Query) Slice(ctx context.Context, outSlicePtr interface{}) error {
r, err := q.Exec(ctx)
if err != nil {
return err
}
return r.Slice(ctx, outSlicePtr)
}
func (q *Query) Scan(ctx context.Context, ptr interface{}) error {
r, err := q.Exec(ctx)
if err != nil {
return err
}
return r.Scan(ctx, ptr)
}
// SELECT
func SelectQuery(c QueryClient, tableName string, cols ...string) *Query {
return &Query{
client: c,
action: "select",
tableName: tableName,
columns: cols,
}
}
func (q *Query) execSelect(ctx context.Context) (*sql.Rows, error) {
where, vals := q.whereClause(1)
qs := selectQuery(q.tableName, q.columns, where, q.orderBys, q.limit)
return q.client.QueryContext(ctx, qs, vals...)
}
// INSERT
func InsertQuery(c QueryClient, tableName string, attrs map[string]interface{}) *Query {
return &Query{
client: c,
action: "insert",
tableName: tableName,
values: attrs,
}
}
func (q *Query) execInsert(ctx context.Context) (*sql.Rows, error) {
if len(q.values) == 0 {
return nil, errors.New("no values to insert")
}
cols, vals := keysValues(q.values)
qs := insertQuery(q.tableName, cols, q.returning)
return q.client.QueryContext(ctx, qs, vals...)
}
// UPDATE
func UpdateQuery(c QueryClient, tableName string, attrs map[string]interface{}) *Query {
return &Query{
client: c,
action: "update",
tableName: tableName,
values: attrs,
}
}
func (q *Query) execUpdate(ctx context.Context) (int64, error) {
cols, vals := keysValues(q.values)
where, whereVals := q.whereClause(1)
qs := updateQuery(q.tableName, cols, where, q.returning)
vals = append(vals, whereVals...)
result, err := q.client.ExecContext(ctx, qs, vals...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
func (q *Query) execUpdateR(ctx context.Context) (*sql.Rows, error) {
cols, vals := keysValues(q.values)
where, whereVals := q.whereClause(1)
qs := updateQuery(q.tableName, cols, where, q.returning)
vals = append(vals, whereVals...)
return q.client.QueryContext(ctx, qs, vals...)
}
// DELETE
func DeleteQuery(c QueryClient, tableName string) *Query {
return &Query{
client: c,
action: "delete",
tableName: tableName,
}
}
func (q *Query) execDelete(ctx context.Context) (int64, error) {
where, vals := q.whereClause(1)
qs := deleteQuery(q.tableName, where, q.returning)
result, err := q.client.ExecContext(ctx, qs, vals...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
func (q *Query) execDeleteR(ctx context.Context) (*sql.Rows, error) {
where, vals := q.whereClause(1)
qs := deleteQuery(q.tableName, where, q.returning)
return q.client.QueryContext(ctx, qs, vals...)
}
// Helpers
// i is the first $ number, startPos is the length of attributes in an update or insert query excluding where clause
func (q *Query) whereClause(i int) (string, []interface{}) {
if i < 1 {
// the numbering starts at $1 not $0
i = 1
}
vals := make([]interface{}, 0, len(q.conditions))
b := strings.Builder{}
// get length of values since the numbering of the where clause starts after values
startPos := len(q.values) + i
clauses := make([]string, 0, len(q.conditions))
for _, cond := range q.conditions {
if cond.raw != "" {
n := len(cond.rawVals)
if n == 0 {
clauses = append(clauses, cond.raw)
continue
}
replacements := make([]interface{}, n)
for j, str := range placeHolders(startPos, n) {
replacements[j] = str
}
c := fmt.Sprintf(cond.raw, replacements...)
clauses = append(clauses, c)
vals = append(vals, cond.rawVals...)
startPos += n
continue
}
// Clause returns the clause string and the new position (start position + number of values)
c, nextPos := cond.Clause(startPos)
clauses = append(clauses, c)
if startPos < nextPos {
// if the next position is greater than the start then values need to be added
if r, ok := cond.val.(Range); ok {
vals = append(vals, r.Start, r.End)
} else if err := verifyArray(reflect.TypeOf(cond.val)); err == nil {
s := reflect.ValueOf(cond.val)
n := s.Len()
for j := 0; j < n; j++ {
v := s.Index(j).Interface()
vals = append(vals, v)
}
} else {
vals = append(vals, cond.val)
}
startPos = nextPos
}
}
b.WriteString(strings.Join(clauses, " AND "))
where := b.String()
// Ors
if len(q.ors) > 0 {
newWhere, newVals := addQueries(where, "OR", startPos, q.ors)
vals = append(vals, newVals...)
startPos += len(newVals)
where = newWhere
}
// Ands
if len(q.ands) > 0 {
newWhere, newVals := addQueries(where, "AND", startPos, q.ands)
vals = append(vals, newVals...)
startPos += len(newVals) //nolint:ineffassign
where = newWhere
}
return where, vals
}
func addQueries(where, separator string, startPos int, queries []*Query) (string, []interface{}) {
var vals []interface{}
n := len(queries)
var clauses []string
if where == "" {
clauses = make([]string, 0, n)
} else {
clauses = make([]string, 1, n+1)
clauses[0] = strings.Join([]string{"(", where, ")"}, "")
}
for _, q := range queries {
addWhere, addVals := q.whereClause(startPos)
if addWhere == "" {
continue
}
addWhere = strings.Join([]string{"(", addWhere, ")"}, "")
clauses = append(clauses, addWhere)
vals = append(vals, addVals...)
startPos += len(addVals)
}
padSep := strings.Join([]string{" ", separator, " "}, "")
return strings.Join(clauses, padSep), vals
}
// Condition
type condition struct {
col string
val interface{}
negative bool
raw string
rawVals []interface{}
}
// returns the clause and the input int after incrementing by the amount of placeholders ($1) created
func (c *condition) Clause(i int) (string, int) {
var condClause string
if nullable, ok := c.val.(Nullable); c.val == nil || (ok && nullable.IsNull()) {
condClause = condClauseNull(c.negative)
} else if _, ok := c.val.(Range); ok {
condClause, i = condClauseRange(i, c.negative)
} else if err := verifyArray(reflect.TypeOf(c.val)); err == nil {
condClause, i = condClauseSlice(i, reflect.ValueOf(c.val).Len(), c.negative)
} else {
condClause, i = condClauseVal(i, c.negative)
}
var b StringsBuilder
b.WriteStrings(Quote(c.col), " ", condClause)
return b.String(), i
}
// Condition clause helpers
func condClauseNull(negative bool) string {
if negative {
return "IS NOT NULL"
}
return "IS NULL"
}
func condClauseRange(start int, negative bool) (string, int) {
var op string
if negative {
op = "NOT BETWEEN"
} else {
op = "BETWEEN"
}
var b StringsBuilder
b.WriteStrings(op, " $", strconv.Itoa(start), " AND $", strconv.Itoa(start+1))
return b.String(), start + 2
}
func condClauseSlice(start int, size int, negative bool) (string, int) {
var op string
if negative {
op = "NOT IN"
} else {
op = "IN"
}
valsStr := strings.Join(placeHolders(start, size), ", ")
var b StringsBuilder
b.WriteStrings(op, " (", valsStr, ")")
return b.String(), start + size
}
func condClauseVal(start int, negative bool) (string, int) {
var op string
if negative {
op = "!="
} else {
op = "="
}
var b StringsBuilder
b.WriteStrings(op, " $", strconv.Itoa(start))
return b.String(), start + 1
}
// QueryResult
type QueryResult struct {
*sql.Rows
RowsAffected int64
}
// Pass in a pointer to a slice to convert the rows into
func (r *QueryResult) Slice(ctx context.Context, slicePtr interface{}) error {
if r.Rows == nil {
return errors.New("result rows is nil")
}
defer r.Close()
slicePtrType := reflect.TypeOf(slicePtr)
// make sure this is a pointer
if err := verifyPtr(slicePtrType); err != nil {
return err
}
sliceType := slicePtrType.Elem()
// make sure this is a slice
if err := verifySlice(sliceType); err != nil {
return err
}
sliceElemType := sliceType.Elem()
outSliceVal := reflect.ValueOf(slicePtr).Elem()
// determine what type of elements the slice holds
var baseType reflect.Type
if sliceElemType.Kind() == reflect.Ptr {
baseType = sliceElemType.Elem()
} else {
baseType = sliceElemType
}
if scanAsStruct(sliceElemType) {
return scanStructs(r.Rows, baseType, sliceElemType, outSliceVal)
}
return scanNatives(r.Rows, baseType, sliceElemType, outSliceVal)
}
// send in the pointer to scan a single value from a single row
func (r *QueryResult) Scan(ctx context.Context, ptr interface{}) error {
if r.Rows == nil {
return errors.New("result rows is nil")
}
defer r.Close()
if !r.Rows.Next() {
if r.Rows.Err() != nil {
return r.Rows.Err()
}
return ErrNoRows
}
ptrType := reflect.TypeOf(ptr)
// make sure this is a pointer
if err := verifyPtr(ptrType); err != nil {
return err
}
if !scanAsStruct(ptrType) {
return r.Rows.Scan(ptr)
}
cols, err := r.Rows.Columns()
if err != nil {
return err
}
baseType := ptrType.Elem()
fieldIdxs := indexes(baseType)
v := reflect.ValueOf(ptr).Elem()
var vals []interface{}
if ptrType.Implements(modelInterfaceType) {
vals = modelVals(v, fieldIdxs, cols)
} else {
vals = structVals(v, cols)
}
return r.Rows.Scan(vals...)
}