forked from fragmenta/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
executable file
·804 lines (655 loc) · 19.7 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Package query lets you build and execute SQL chainable queries against a database of your choice, and defer execution of SQL until you wish to extract a count or array of models.
// NB in order to allow cross-compilation, we exlude sqlite drivers by default
// uncomment them to allow use of sqlite
package query
import (
"database/sql"
"fmt"
"sort"
"strconv"
"strings"
)
// FIXME - this package global should in theory be protected by a mutex, even if it is only for debugging
// Debug sets whether we output debug statements for SQL
var Debug bool
func init() {
Debug = false // default to false
}
// Result holds the results of a query as map[string]interface{}
type Result map[string]interface{}
// Func is a function which applies effects to queries
type Func func(q *Query) *Query
// Query provides all the chainable relational query builder methods
type Query struct {
// Database - database name and primary key, set with New()
tablename string
primarykey string
// SQL - Private fields used to store sql before building sql query
sql string
sel string
join string
where string
group string
having string
order string
offset string
limit string
// Extra args to be substituted in the *where* clause
args []interface{}
}
// New builds a new Query, given the table and primary key
func New(t string, pk string) *Query {
// If we have no db, return nil
if database == nil {
return nil
}
q := &Query{
tablename: t,
primarykey: pk,
}
return q
}
// Exec the given sql and args against the database directly
// Returning sql.Result (NB not rows)
func Exec(sql string, args ...interface{}) (sql.Result, error) {
results, err := database.Exec(sql, args...)
return results, err
}
// Rows executes the given sql and args against the database directly
// Returning sql.Rows
func Rows(sql string, args ...interface{}) (*sql.Rows, error) {
results, err := database.Query(sql, args...)
return results, err
}
// Copy returns a new copy of this query which can be mutated without affecting the original
func (q *Query) Copy() *Query {
return &Query{
tablename: q.tablename,
primarykey: q.primarykey,
sql: q.sql,
sel: q.sel,
join: q.join,
where: q.where,
group: q.group,
having: q.having,
order: q.order,
offset: q.offset,
limit: q.limit,
args: q.args,
}
}
// TODO: These should instead be something like query.New("table_name").Join(a,b).Insert() and just have one multiple function?
// InsertJoin inserts a join clause on the query
func (q *Query) InsertJoin(a int64, b int64) error {
return q.InsertJoins([]int64{a}, []int64{b})
}
// InsertJoins using an array of ids (more general version of above)
// This inserts joins for every possible relation between the ids
func (q *Query) InsertJoins(a []int64, b []int64) error {
// Make sure we have some data
if len(a) == 0 || len(b) == 0 {
return fmt.Errorf("Null data for joins insert %s", q.table())
}
values := ""
for _, av := range a {
for _, bv := range b {
// NB no zero values allowed, we simply ignore zero values
if av != 0 && bv != 0 {
values += fmt.Sprintf("(%d,%d),", av, bv)
}
}
}
values = strings.TrimRight(values, ",")
sql := fmt.Sprintf("INSERT into %s VALUES %s;", q.table(), values)
if Debug {
fmt.Printf("JOINS SQL:%s\n", sql)
}
_, err := database.Exec(sql)
return err
}
// UpdateJoins updates the given joins, using the given id to clear joins first
func (q *Query) UpdateJoins(id int64, a []int64, b []int64) error {
if Debug {
fmt.Printf("SetJoins %s %s=%d: %v %v \n", q.table(), q.pk(), id, a, b)
}
// First delete any existing joins
err := q.Where(fmt.Sprintf("%s=?", q.pk()), id).Delete()
if err != nil {
return err
}
// Now join all a's with all b's by generating joins for each possible combination
// Make sure we have data in both cases, otherwise do not attempt insert any joins
if len(a) > 0 && len(b) > 0 {
// Now insert all new ids - NB the order of arguments here MUST match the order in the table
err = q.InsertJoins(a, b)
if err != nil {
return err
}
}
return nil
}
// Insert inserts a record in the database
func (q *Query) Insert(params map[string]string) (int64, error) {
// Insert and retrieve ID in one step from db
sql := q.insertSQL(params)
if Debug {
fmt.Printf("INSERT SQL:%s %v\n", sql, valuesFromParams(params))
}
id, err := database.Insert(sql, valuesFromParams(params)...)
if err != nil {
return 0, err
}
return id, nil
}
// insertSQL sets the insert sql for update statements, turn params into sql i.e. "col"=?
// NB we always use parameterized queries, never string values.
func (q *Query) insertSQL(params map[string]string) string {
var cols, vals []string
for i, k := range sortedParamKeys(params) {
cols = append(cols, database.QuoteField(k))
vals = append(vals, database.Placeholder(i+1))
}
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES(%s) %s;", q.table(), strings.Join(cols, ","), strings.Join(vals, ","), database.InsertSQL(q.pk()))
return query
}
// Update one model specified in this query - the column names MUST be verified in the model
func (q *Query) Update(params map[string]string) error {
// We should check the query has a where limitation to avoid updating all?
// pq unfortunately does not accept limit(1) here
return q.UpdateAll(params)
}
// Delete one model specified in this relation
func (q *Query) Delete() error {
// We should check the query has a where limitation?
return q.DeleteAll()
}
// UpdateAll updates all models specified in this relation
func (q *Query) UpdateAll(params map[string]string) error {
// Build query SQL, allowing for null fields
var output []string
for _, key := range sortedParamKeys(params) {
v := params[key]
// Special case the value 'null'
if v == "null" {
// Set the value to null in the db, rather than a value
output = append(output, fmt.Sprintf("%s=null", database.QuoteField(key)))
// Remove from params as we have added to the update statement and don't require an argument
delete(params, key)
} else {
// Set the value using a placeholder
output = append(output, fmt.Sprintf("%s=?", database.QuoteField(key)))
}
}
querySQL := strings.Join(output, ",")
// Create sql for update from all params except null params using placeholders for args
q.Select(fmt.Sprintf("UPDATE %s SET %s", q.table(), querySQL))
// Execute, after PREpending params to args
// In an update statement, the where comes at the end so update args come first
values := valuesFromParams(params)
q.args = append(values, q.args...)
// If debug mode, output a string representation
if Debug {
fmt.Printf("UPDATE SQL:%s\n%v\n", q.QueryString(), values)
}
// Return the result of execution
_, err := q.Result()
return err
}
// DeleteAll delets *all* models specified in this relation
func (q *Query) DeleteAll() error {
q.Select(fmt.Sprintf("DELETE FROM %s", q.table()))
if Debug {
fmt.Printf("DELETE SQL:%s <= %v\n", q.QueryString(), q.args)
}
// Execute
_, err := q.Result()
return err
}
// Count fetches a count of model objects (executes SQL).
func (q *Query) Count() (int64, error) {
// In order to get consistent results, we use the same query builder
// but reset select to simple count select
// Store the previous select and set
s := q.sel
countSelect := fmt.Sprintf("SELECT COUNT(%s) FROM %s", q.pk(), q.table())
q.Select(countSelect)
// Store the previous order (minus order by) and set to empty
// Order must be blank on count because of limited select
o := strings.Replace(q.order, "ORDER BY ", "", 1)
q.order = ""
// Fetch count from db for our sql with count select and no order set
var count int64
rows, err := q.Rows()
if err != nil {
return 0, fmt.Errorf("Error querying database for count: %s\nQuery:%s", err, q.QueryString())
}
// We expect just one row, with one column (count)
defer rows.Close()
for rows.Next() {
err = rows.Scan(&count)
if err != nil {
return 0, err
}
}
// Reset select after getting count query
q.Select(s)
q.Order(o)
q.reset()
return count, err
}
// Result executes the query against the database, returning sql.Result, and error (no rows)
// (Executes SQL)
func (q *Query) Result() (sql.Result, error) {
results, err := database.Exec(q.QueryString(), q.args...)
return results, err
}
// Rows executes the query against the database, and return the sql rows result for this query
// (Executes SQL)
func (q *Query) Rows() (*sql.Rows, error) {
results, err := database.Query(q.QueryString(), q.args...)
return results, err
}
// FirstResult executes the SQL and returrns the first result
func (q *Query) FirstResult() (Result, error) {
// Set a limit on the query
q.Limit(1)
// Fetch all results (1)
results, err := q.Results()
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, fmt.Errorf("No results found for Query:%s", q.QueryString())
}
// Return the first result
return results[0], nil
}
// ResultInt64 returns the first result from a query stored in the column named col as an int64.
func (q *Query) ResultInt64(c string) (int64, error) {
result, err := q.FirstResult()
if err != nil || result[c] == nil {
return 0, err
}
var i int64
switch result[c].(type) {
case int64:
i = result[c].(int64)
case int:
i = int64(result[c].(int))
case float64:
i = int64(result[c].(float64))
case string:
f, err := strconv.ParseFloat(result[c].(string), 64)
if err != nil {
return i, err
}
i = int64(f)
}
return i, nil
}
// ResultFloat64 returns the first result from a query stored in the column named col as a float64.
func (q *Query) ResultFloat64(c string) (float64, error) {
result, err := q.FirstResult()
if err != nil || result[c] == nil {
return 0, err
}
var f float64
switch result[c].(type) {
case float64:
f = result[c].(float64)
case int:
f = float64(result[c].(int))
case int64:
f = float64(result[c].(int))
case string:
f, err = strconv.ParseFloat(result[c].(string), 64)
if err != nil {
return f, err
}
}
return f, nil
}
// Results returns an array of results
func (q *Query) Results() ([]Result, error) {
// Make an empty result set map
var results []Result
// Fetch rows from db for our sql
rows, err := q.Rows()
if err != nil {
return results, fmt.Errorf("Error querying database for rows: %s\nQUERY:%s", err, q)
}
// Close rows before returning
defer rows.Close()
// Fetch the columns from the database
cols, err := rows.Columns()
if err != nil {
return results, fmt.Errorf("Error fetching columns: %s\nQUERY:%s\nCOLS:%s", err, q, cols)
}
// For each row, construct an entry in results with a map of column string keys to values
for rows.Next() {
result, err := scanRow(cols, rows)
if err != nil {
return results, fmt.Errorf("Error fetching row: %s\nQUERY:%s\nCOLS:%s", err, q, cols)
}
results = append(results, result)
}
return results, nil
}
// ResultIDs returns an array of ids as the result of a query
// FIXME - this should really use the query primary key, not "id" hardcoded
func (q *Query) ResultIDs() []int64 {
var ids []int64
if Debug {
fmt.Printf("#info ResultIDs:%s\n", q.DebugString())
}
results, err := q.Results()
if err != nil {
return ids
}
for _, r := range results {
if r["id"] != nil {
ids = append(ids, r["id"].(int64))
}
}
return ids
}
// ResultIDSets returns a map from a values to arrays of b values, the order of a,b is respected not the table key order
func (q *Query) ResultIDSets(a, b string) map[int64][]int64 {
idSets := make(map[int64][]int64, 0)
results, err := q.Results()
if err != nil {
return idSets
}
for _, r := range results {
if r[a] != nil && r[b] != nil {
av := r[a].(int64)
bv := r[b].(int64)
idSets[av] = append(idSets[av], bv)
}
}
if Debug {
fmt.Printf("#info ResultIDSets:%s\n", q.DebugString())
}
return idSets
}
// QueryString builds a query string to use for results
func (q *Query) QueryString() string {
if q.sql == "" {
// if we have arguments override the selector
if q.sel == "" {
// Note q.table() etc perform quoting on field names
q.sel = fmt.Sprintf("SELECT %s.* FROM %s", q.table(), q.table())
}
q.sql = fmt.Sprintf("%s %s %s %s %s %s %s %s", q.sel, q.join, q.where, q.group, q.having, q.order, q.limit, q.offset)
q.sql = strings.TrimRight(q.sql, " ")
q.sql = strings.Replace(q.sql, " ", " ", -1)
q.sql = strings.Replace(q.sql, " ", " ", -1)
// Replace ? with whatever placeholder db prefers
q.replaceArgPlaceholders()
q.sql = q.sql + ";"
}
return q.sql
}
// CHAINABLE FINDERS
// Apply the Func to this query, and return the modified Query
// This allows chainable finders from other packages
// e.g. q.Apply(status.Published) where status.Published is a Func
func (q *Query) Apply(f Func) *Query {
return f(q)
}
// Conditions applies a series of query funcs to a query
func (q *Query) Conditions(funcs ...Func) *Query {
for _, f := range funcs {
q = f(q)
}
return q
}
// SQL defines sql manually and overrides all other setters
// Completely replaces all stored sql
func (q *Query) SQL(sql string) *Query {
q.sql = sql
q.reset()
return q
}
// Limit sets the sql LIMIT with an int
func (q *Query) Limit(limit int) *Query {
q.limit = fmt.Sprintf("LIMIT %d", limit)
q.reset()
return q
}
// Offset sets the sql OFFSET with an int
func (q *Query) Offset(offset int) *Query {
q.offset = fmt.Sprintf("OFFSET %d", offset)
q.reset()
return q
}
// Where defines a WHERE clause on SQL - Additional calls add WHERE () AND () clauses
func (q *Query) Where(sql string, args ...interface{}) *Query {
if len(q.where) > 0 {
q.where = fmt.Sprintf("%s AND (%s)", q.where, sql)
} else {
q.where = fmt.Sprintf("WHERE (%s)", sql)
}
// NB this assumes that args are only supplied for where clauses
// this may be an incorrect assumption!
if args != nil {
if q.args == nil {
q.args = args
} else {
q.args = append(q.args, args...)
}
}
q.reset()
return q
}
// OrWhere defines a where clause on SQL - Additional calls add WHERE () OR () clauses
func (q *Query) OrWhere(sql string, args ...interface{}) *Query {
if len(q.where) > 0 {
q.where = fmt.Sprintf("%s OR (%s)", q.where, sql)
} else {
q.where = fmt.Sprintf("WHERE (%s)", sql)
}
if args != nil {
if q.args == nil {
q.args = args
} else {
q.args = append(q.args, args...)
}
}
q.reset()
return q
}
// WhereIn adds a Where clause which selects records IN() the given array
// If IDs is an empty array, the query limit is set to 0
func (q *Query) WhereIn(col string, IDs []int64) *Query {
// Return no results, so that when chaining callers
// don't have to check for empty arrays
if len(IDs) == 0 {
q.Limit(0)
q.reset()
return q
}
in := ""
for _, ID := range IDs {
in = fmt.Sprintf("%s%d,", in, ID)
}
in = strings.TrimRight(in, ",")
sql := fmt.Sprintf("%s IN (%s)", col, in)
if len(q.where) > 0 {
q.where = fmt.Sprintf("%s AND (%s)", q.where, sql)
} else {
q.where = fmt.Sprintf("WHERE (%s)", sql)
}
q.reset()
return q
}
// Define a join clause on SQL - we create an inner join like this:
// INNER JOIN extras_seasons ON extras.id = extra_id
// q.Select("SELECT units.* FROM units INNER JOIN sites ON units.site_id = sites.id")
// rails join example
// INNER JOIN "posts_tags" ON "posts_tags"."tag_id" = "tags"."id" WHERE "posts_tags"."post_id" = 111
// Join adds an inner join to the query
func (q *Query) Join(otherModel string) *Query {
modelTable := q.tablename
tables := []string{
modelTable,
ToPlural(otherModel),
}
sort.Strings(tables)
joinTable := fmt.Sprintf("%s_%s", tables[0], tables[1])
sql := fmt.Sprintf("INNER JOIN %s ON %s.id = %s.%s_id", database.QuoteField(joinTable), database.QuoteField(modelTable), database.QuoteField(joinTable), ToSingular(modelTable))
if len(q.join) > 0 {
q.join = fmt.Sprintf("%s %s", q.join, sql)
} else {
q.join = fmt.Sprintf("%s", sql)
}
q.reset()
return q
}
// Order defines ORDER BY sql
func (q *Query) Order(sql string) *Query {
if sql == "" {
q.order = ""
} else {
q.order = fmt.Sprintf("ORDER BY %s", sql)
}
q.reset()
return q
}
// Group defines GROUP BY sql
func (q *Query) Group(sql string) *Query {
if sql == "" {
q.group = ""
} else {
q.group = fmt.Sprintf("GROUP BY %s", sql)
}
q.reset()
return q
}
// Having defines HAVING sql
func (q *Query) Having(sql string) *Query {
if sql == "" {
q.having = ""
} else {
q.having = fmt.Sprintf("HAVING %s", sql)
}
q.reset()
return q
}
// Select defines SELECT sql
func (q *Query) Select(sql string) *Query {
q.sel = sql
q.reset()
return q
}
// DebugString returns a query representation string useful for debugging
func (q *Query) DebugString() string {
return fmt.Sprintf("--\nQuery-SQL:%s\nARGS:%s\n--", q.QueryString(), q.argString())
}
// Clear sql/query caches
func (q *Query) reset() {
// Perhaps later clear cached compiled representation of query too
// clear stored sql
q.sql = ""
}
// Return an arg string (for debugging)
func (q *Query) argString() string {
output := "-"
for _, a := range q.args {
output = output + fmt.Sprintf("'%s',", q.argToString(a))
}
output = strings.TrimRight(output, ",")
output = output + ""
return output
}
// Convert arguments to string - used only for debug argument strings
// Not to be exported or used to try to escape strings...
func (q *Query) argToString(arg interface{}) string {
switch arg.(type) {
case string:
return arg.(string)
case []byte:
return string(arg.([]byte))
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return fmt.Sprintf("%d", arg)
case int64, uint64:
return fmt.Sprintf("%d", arg)
case float32, float64:
return fmt.Sprintf("%f", arg)
case bool:
return fmt.Sprintf("%d", arg)
default:
return fmt.Sprintf("%v", arg)
}
}
// Ask model for primary key name to use
func (q *Query) pk() string {
return database.QuoteField(q.primarykey)
}
// Ask model for table name to use
func (q *Query) table() string {
return database.QuoteField(q.tablename)
}
// Replace ? with whatever database prefers (psql uses numbered args)
func (q *Query) replaceArgPlaceholders() {
// Match ? and replace with argument placeholder from database
for i := range q.args {
q.sql = strings.Replace(q.sql, "?", database.Placeholder(i+1), 1)
}
}
// Sorts the param names given - map iteration order is explicitly random in Go
// but we need params in a defined order to avoid unexpected results.
func sortedParamKeys(params map[string]string) []string {
sortedKeys := make([]string, len(params))
i := 0
for k := range params {
sortedKeys[i] = k
i++
}
sort.Strings(sortedKeys)
return sortedKeys
}
// Generate a set of values for the params in order
func valuesFromParams(params map[string]string) []interface{} {
// NB DO NOT DEPEND ON PARAMS ORDER - see note on SortedParamKeys
var values []interface{}
for _, key := range sortedParamKeys(params) {
values = append(values, params[key])
}
return values
}
func scanRow(cols []string, rows *sql.Rows) (Result, error) {
// We return a map[string]interface{} for each row scanned
result := Result{}
values := make([]interface{}, len(cols))
for i := 0; i < len(cols); i++ {
var col interface{}
values[i] = &col
}
// Scan results into these interfaces
err := rows.Scan(values...)
if err != nil {
return nil, fmt.Errorf("Error scanning row: %s", err)
}
// Make a string => interface map and hand off to caller
// We fix up a few types which the pq driver returns as less handy equivalents
// We enforce usage of int64 at all times as all our records use int64
for i := 0; i < len(cols); i++ {
v := *values[i].(*interface{})
if values[i] != nil {
switch v.(type) {
default:
result[cols[i]] = v
case bool:
result[cols[i]] = v.(bool)
case int:
result[cols[i]] = int64(v.(int))
case []byte: // text cols are given as bytes
result[cols[i]] = string(v.([]byte))
case int64:
result[cols[i]] = v.(int64)
}
}
}
return result, nil
}