-
Notifications
You must be signed in to change notification settings - Fork 22
/
builder.go
922 lines (840 loc) · 19.2 KB
/
builder.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package gosql
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strings"
)
const (
_select uint8 = iota
_insert
_replace
_update
_delete
)
var tagKey = "db"
var identKey = "`"
//SQLSegments ...
type SQLSegments struct {
table []TbName
fields []string
flags []string
join []map[string]string
where Clause
groupBy []string
having Clause
orderBy []string
limit struct {
limit int
offset int
}
union []func(*SQLSegments)
forUpdate bool
returning bool
// params []interface{}
params []map[string]interface{}
render struct {
args []interface{}
}
//sql cmd type: select|insert|repalce|update|delete
cmd uint8
}
//TbName ..
type TbName struct {
Name string
Alias string
}
//Add ..
type Add int
//Sub ..
type Sub int
//NewSQLSegment ..
func NewSQLSegment() *SQLSegments {
return &SQLSegments{}
}
//Table SQLSegments
func (s *SQLSegments) Table(name interface{}) *SQLSegments {
switch v := name.(type) {
case TbName:
s.table = append(s.table, v)
case []TbName:
s.table = append(s.table, v...)
case string:
s.table = append(s.table, TbName{v, ""})
}
return s
}
//Field SQLSegments
func (s *SQLSegments) Field(fields ...string) *SQLSegments {
if len(fields) > 0 {
s.fields = append(s.fields, fields...)
}
return s
}
//Flag SQLSegments
func (s *SQLSegments) Flag(flags ...string) *SQLSegments {
if len(flags) > 0 {
s.flags = append(s.flags, flags...)
}
return s
}
//Join SQLSegments
func (s *SQLSegments) Join(table string, conditionA, logic, conditionB string) *SQLSegments {
s.addJoin("JOIN", table, conditionA, logic, conditionB)
return s
}
//LeftJoin SQLSegments
func (s *SQLSegments) LeftJoin(table string, conditionA, logic, conditionB string) *SQLSegments {
s.addJoin("LEFT JOIN", table, conditionA, logic, conditionB)
return s
}
//RightJoin SQLSegments
func (s *SQLSegments) RightJoin(table string, conditionA, logic, conditionB string) *SQLSegments {
s.addJoin("RIGHT JOIN", table, conditionA, logic, conditionB)
return s
}
//InnerJoin SQLSegments
func (s *SQLSegments) InnerJoin(table string, conditionA, logic, conditionB string) *SQLSegments {
s.addJoin("INNER JOIN", table, conditionA, logic, conditionB)
return s
}
//CorssJoin SQLSegments
func (s *SQLSegments) CorssJoin(table string, conditionA, logic, conditionB string) *SQLSegments {
s.addJoin("CROSS JOIN", table, conditionA, logic, conditionB)
return s
}
//addJoin SQLSegments
func (s *SQLSegments) addJoin(typ string, table string, conditionA, logic, conditionB string) *SQLSegments {
var t = make(map[string]string)
t["type"] = typ
t["table"] = table
t["logic"] = logic
t["conditionA"] = conditionA
t["conditionB"] = conditionB
s.join = append(s.join, t)
return s
}
//OrderBy SQLSegments
func (s *SQLSegments) OrderBy(fields ...string) *SQLSegments {
if len(fields) > 0 {
s.orderBy = append(s.orderBy, fields...)
}
return s
}
//GroupBy SQLSegments
func (s *SQLSegments) GroupBy(fields ...string) *SQLSegments {
if len(fields) > 0 {
s.groupBy = append(s.groupBy, fields...)
}
return s
}
//Offset SQLSegments
func (s *SQLSegments) Offset(n int) *SQLSegments {
s.limit.offset = n
return s
}
//Limit SQLSegments
func (s *SQLSegments) Limit(n int) *SQLSegments {
s.limit.limit = n
return s
}
//ForUpdate SQLSegments
func (s *SQLSegments) ForUpdate() *SQLSegments {
s.forUpdate = true
return s
}
//Returning SQLSegments
func (s *SQLSegments) Returning() *SQLSegments {
s.returning = true
return s
}
//Clause ...
type Clause struct {
key interface{}
val interface{}
logic string
clause []*Clause
}
func (p *Clause) addClause(logic string, key interface{}, vals ...interface{}) *Clause {
var c = &Clause{}
c.logic = logic
switch k := key.(type) {
case func(*Clause):
k(c)
// p.clause = append(p.clause, c)
default:
c.key = key
if len(vals) > 0 {
c.val = vals[0]
}
}
// fmt.Println(p.clause)
p.clause = append(p.clause, c)
return p
}
//Where ..
func (p *Clause) Where(key interface{}, vals ...interface{}) *Clause {
p.addClause("AND", key, vals...)
return p
}
//OrWhere ..
func (p *Clause) OrWhere(key interface{}, vals ...interface{}) *Clause {
p.addClause("OR", key, vals...)
return p
}
//Build ...
func (p *Clause) Build(i int) (string, []interface{}) {
var sql = ""
var args []interface{}
if p.logic != "" && i > 0 {
sql += " " + p.logic
}
switch k := p.key.(type) {
case string:
r, _ := regexp.Compile(`\[(\>\=|\<\=|\>|\<|\<\>|\!\=|\=|\~|\!\~|like|!like|in|!in|is|!is|exists|!exists|#)\]?([a-zA-Z0-9_.\-\=\s\?\(\)]*)`)
match := r.FindStringSubmatch(k)
var context string
if len(match) > 0 {
// fmt.Println(len(match), match[1])
switch match[1] {
case "~", "like":
context = buildIdent(match[2]) + " LIKE ?"
args = append(args, p.val)
case "!~", "!like":
context = buildIdent(match[2]) + " NOT LIKE ?"
args = append(args, p.val)
case ">", ">=", "<", "<=", "<>", "!=", "=":
context = buildIdent(match[2]) + " " + match[1] + " ?"
args = append(args, p.val)
case "in", "!in":
var holder string
if reflect.TypeOf(p.val).Kind() == reflect.Slice {
v := reflect.ValueOf(p.val)
holder = buildPlaceholder(v.Len(), "?", " ,")
for n := 0; n < v.Len(); n++ {
args = append(args, v.Index(n).Interface())
}
} else {
holder = "?"
args = append(args, p.val)
}
context += buildIdent(match[2])
if match[1] == "!in" {
context += " NOT"
}
context += " IN (" + holder + ")"
case "exists", "!exists":
var sub string
switch p.val.(type) {
case string:
sub = p.val.(string)
case func(s *SQLSegments):
s := NewSQLSegment()
p.val.(func(s *SQLSegments))(s)
sub = s.BuildSelect()
args = append(args, s.render.args...)
}
if match[1] == "!exists" {
context += "NOT "
}
context += "EXISTS (" + sub + ")"
case "is":
if p.val == nil {
context = buildIdent(match[2]) + " IS NULL"
} else {
context = buildIdent(match[2]) + " IS ?"
args = append(args, p.val)
}
case "!is":
if p.val == nil {
context = buildIdent(match[2]) + " IS NOT NULL"
} else {
context = buildIdent(match[2]) + " IS NOT ?"
args = append(args, p.val)
}
case "#":
context = match[2]
if reflect.TypeOf(p.val).Kind() == reflect.Slice {
v := reflect.ValueOf(p.val)
for n := 0; n < v.Len(); n++ {
args = append(args, v.Index(n).Interface())
}
} else {
args = append(args, p.val)
}
}
sql += " " + context
} else {
if p.val != nil {
sql += " " + buildIdent(k) + " = ?"
args = append(args, p.val)
} else {
sql += " " + k
}
}
case nil:
sql += " ("
for j, c := range p.clause {
part, arg := c.Build(j)
sql += part
args = append(args, arg...)
}
sql += ")"
}
return sql, args
}
//Where ..
func (s *SQLSegments) Where(key interface{}, vals ...interface{}) *SQLSegments {
s.where.Where(key, vals...)
return s
}
//OrWhere ..
func (s *SQLSegments) OrWhere(key interface{}, vals ...interface{}) *SQLSegments {
s.where.OrWhere(key, vals...)
return s
}
//BuildWhereClause ...
func (s *SQLSegments) buildWhereClause() string {
var sql string
if len(s.where.clause) > 0 {
sql = " WHERE"
for i, c := range s.where.clause {
part, args := c.Build(i)
sql += part
s.render.args = append(s.render.args, args...)
}
}
return sql
}
//IsEmptyWhereClause ...
func (s *SQLSegments) IsEmptyWhereClause() bool {
return len(s.where.clause) < 1
}
//Having ...
func (s *SQLSegments) Having(key interface{}, vals ...interface{}) *SQLSegments {
s.having.Where(key, vals...)
return s
}
//buildHavingClause ...
func (s *SQLSegments) buildHavingClause() string {
var sql string
if len(s.having.clause) > 0 {
sql = " HAVING"
for i, c := range s.having.clause {
part, args := c.Build(i)
sql += part
s.render.args = append(s.render.args, args...)
}
}
return sql
}
func (s *SQLSegments) buildFlags() string {
var sql string
for _, v := range s.flags {
sql += " " + v
}
return sql
}
func (s *SQLSegments) buildField() string {
var sql string
if len(s.fields) == 0 {
sql += " *"
} else {
for i, v := range s.fields {
if i > 0 {
sql += ","
}
if v == "*" {
sql += " " + v
} else {
sql += " " + buildIdent(v)
}
}
}
return sql
}
func (s *SQLSegments) buildTable() string {
var sql string
for i, v := range s.table {
if i > 0 {
sql += ","
}
sql += " " + buildIdent(v.Name)
if v.Alias != "" {
sql += " AS " + buildIdent(v.Alias)
}
}
return sql
}
func (s *SQLSegments) buildJoin() string {
var sql string
for _, t := range s.join {
sql += " " + t["type"] + " " + buildIdent(t["table"]) + " ON " + buildIdent(t["conditionA"]) + " " + t["logic"] + " " + buildIdent(t["conditionB"])
}
return sql
}
func (s *SQLSegments) buildGroupBy() string {
var sql string
if len(s.groupBy) > 0 {
sql += " GROUP BY"
}
for i, v := range s.groupBy {
if i > 0 {
sql += ","
}
sql += " " + buildIdent(v)
}
return sql
}
func (s *SQLSegments) buildOrderBy() string {
var sql string
if len(s.orderBy) > 0 {
sql += " ORDER BY"
}
for i, v := range s.orderBy {
if i > 0 {
sql += ","
}
vv := strings.SplitN(v, " ", 2)
sql += " " + buildIdent(vv[0])
if len(vv) > 1 {
sql += " " + strings.ToUpper(vv[1])
}
}
return sql
}
func (s *SQLSegments) buildLimit() string {
var sql string
if s.limit.limit != 0 {
sql += fmt.Sprintf(" LIMIT %d", s.limit.limit)
}
if s.limit.offset != 0 {
sql += fmt.Sprintf(" OFFSET %d", s.limit.offset)
}
return sql
}
//Union ...
func (s *SQLSegments) Union(f func(*SQLSegments)) *SQLSegments {
s.union = append(s.union, f)
return s
}
func (s *SQLSegments) buildUnion() string {
var sql string
if len(s.union) > 0 {
sql += " UNION ("
}
for _, f := range s.union {
var ss = &SQLSegments{}
f(ss)
sql += ss.BuildSelect()
}
if len(s.union) > 0 {
sql += ")"
}
return sql
}
func (s *SQLSegments) buildForUpdate() string {
if s.forUpdate == true {
return " FOR UPDATE"
}
return ""
}
//BuildSelect ...
func (s *SQLSegments) BuildSelect() string {
var sql = fmt.Sprintf("SELECT%s%s FROM%s%s%s%s%s%s%s%s%s",
s.buildFlags(),
s.buildField(),
s.buildTable(),
s.buildJoin(),
s.buildWhereClause(),
s.buildGroupBy(),
s.buildHavingClause(),
s.buildOrderBy(),
s.buildLimit(),
s.buildUnion(),
s.buildForUpdate(),
)
s.cmd = _select
// fmt.Println(s.render.args)
return sql
}
//Params ...
func (s *SQLSegments) Params(vals ...map[string]interface{}) *SQLSegments {
s.params = append(s.params, vals...)
return s
}
//Insert ...
func (s *SQLSegments) Insert(vals ...map[string]interface{}) *SQLSegments {
s.params = append(s.params, vals...)
return s
}
//BuildInsert ...
func (s *SQLSegments) BuildInsert() string {
var sql = fmt.Sprintf("INSERT%s INTO%s%s%s",
s.buildFlags(),
s.buildTable(),
s.buildValuesForInsert(),
s.buildReturning(),
)
s.cmd = _insert
return sql
}
//BuildReplace build a replace sql
func (s *SQLSegments) BuildReplace() string {
var sql = fmt.Sprintf("REPLACE%s INTO%s%s%s",
s.buildFlags(),
s.buildTable(),
s.buildValuesForInsert(),
s.buildReturning(),
)
s.cmd = _replace
return sql
}
//BuildInsert build a insert sql
func (s *SQLSegments) buildValuesForInsert() string {
var sql string
var fields []string
for i, vals := range s.params {
if i == 0 {
for arg := range vals {
fields = append(fields, arg)
}
}
}
sql = " ("
for i, s := range fields {
if i > 0 {
sql += ","
}
sql += buildIdent(s)
}
sql += ") VALUES ("
for i, vals := range s.params {
//for keep field order in golang-map
for _, arg := range fields {
s.render.args = append(s.render.args, vals[arg])
}
//build sql
if i > 0 {
sql += "),("
}
sql += buildPlaceholder(len(fields), "?", ",")
}
sql += ")"
return sql
}
//UpdateField for set a field when update sql
func (s *SQLSegments) UpdateField(key string, val interface{}) *SQLSegments {
if len(s.params) == 0 {
s.params = append(s.params, make(map[string]interface{}, 0))
}
//update set 值只能是一行的维度(只用params[0])
s.params[0][key] = val
return s
}
//Update ..
func (s *SQLSegments) Update(vals map[string]interface{}) *SQLSegments {
//panic("Update method only one parameter is supported")
if len(vals) < 1 {
panic("Must be have values")
}
s.params = append(s.params, vals)
return s
}
//UnsafeUpdate 可以没有where条件更新 ,Update 更新必须指定where条件才能更新否则panic
func (s *SQLSegments) UnsafeUpdate(vals map[string]interface{}) *SQLSegments {
s.params = append(s.params, vals)
return s
}
//buildReturning ...
func (s *SQLSegments) buildReturning() string {
if s.returning == true {
// return " RETURNING"
return "" //mysql not suport
}
return ""
}
//BuildUpdate build a update sql
func (s *SQLSegments) BuildUpdate() string {
var sql = fmt.Sprintf("UPDATE%s%s%s%s%s%s%s",
s.buildFlags(),
s.buildTable(),
s.buildValuesForUpdate(),
s.buildWhereClause(),
s.buildOrderBy(),
s.buildLimit(),
s.buildReturning(),
)
s.cmd = _update
// fmt.Println(s.render.args)
return sql
}
//buildValuesForUpdate ...
func (s *SQLSegments) buildValuesForUpdate() string {
var buffer bytes.Buffer
buffer.WriteString(" SET ")
// var fieldSlice []string
if len(s.params) == 0 {
panic(fmt.Sprintf("Must be have values after 'UPDATE %s SET'", s.buildTable()))
}
r, _ := regexp.Compile(`\[(\+|\-)\]?([a-zA-Z0-9_.\-\=\s\?\(\)]*)`)
for i, vals := range s.params {
if len(vals) == 0 {
panic(fmt.Sprintf("Must be have values after 'UPDATE %s SET'", s.buildTable()))
}
if i == 0 {
j := 0
for arg, val := range vals {
// fieldSlice = append(fieldSlice, arg)
// s.render.args = append(s.render.args, val)
if j > 0 {
buffer.WriteString(", ")
}
match := r.FindStringSubmatch(arg)
if len(match) > 1 {
buffer.WriteString(buildIdent(match[2]))
buffer.WriteString(" = ")
buffer.WriteString(buildIdent(match[2]))
buffer.WriteString(" ")
buffer.WriteString(match[1])
buffer.WriteString(" ?")
s.render.args = append(s.render.args, val)
} else {
buffer.WriteString(buildIdent(arg))
buffer.WriteString(" = ?")
s.render.args = append(s.render.args, val)
}
j++
}
} else {
//when update just support one of vals
panic(fmt.Sprintf("when update just support one of vals: %v", vals))
}
}
return buffer.String()
}
//Delete for a part of delete sql
func (s *SQLSegments) Delete() *SQLSegments {
return s
}
//BuildDelete build a delete sql
func (s *SQLSegments) BuildDelete() string {
var sql = fmt.Sprintf("DELETE%s FROM%s%s%s%s%s",
s.buildFlags(),
s.buildTable(),
s.buildWhereClause(),
s.buildOrderBy(),
s.buildLimit(),
s.buildReturning(),
)
s.cmd = _delete
// fmt.Println(s.render.args)
return sql
}
//buildIdent
func buildIdent(name string) string {
return identKey + strings.Replace(name, ".", identKey+"."+identKey, -1) + identKey
}
//buildPlaceholder
func buildPlaceholder(l int, holder, sep string) string {
var buffer bytes.Buffer
for i := 0; i < l; i++ {
if i > 0 {
buffer.WriteString(sep)
}
buffer.WriteString(holder)
}
return buffer.String()
}
//Args for set some args
func (s *SQLSegments) Args() []interface{} {
return s.render.args
}
//Build for build a sql with SQLSegments
func (s *SQLSegments) Build() (string, []interface{}) {
if s.cmd == _select {
return s.BuildSelect(), s.Args()
}
if s.cmd == _insert {
return s.BuildInsert(), s.Args()
}
if s.cmd == _replace {
return s.BuildReplace(), s.Args()
}
if s.cmd == _update {
return s.BuildUpdate(), s.Args()
}
if s.cmd == _delete {
return s.BuildDelete(), s.Args()
}
return "", nil
}
//-------- another style --------
//Option is SQL segment part
type Option func(q SQLSegments) SQLSegments
//Table for set table
func Table(name interface{}) Option {
return func(s SQLSegments) SQLSegments {
s.Table(name)
return s
}
}
//Columns for set columns
func Columns(fields ...string) Option {
return func(s SQLSegments) SQLSegments {
s.Field(fields...)
return s
}
}
//Flag for set flag
func Flag(flags ...string) Option {
return func(s SQLSegments) SQLSegments {
s.Flag(flags...)
return s
}
}
//Join for join
func Join(table string, conditionA, logic, conditionB string) Option {
return func(s SQLSegments) SQLSegments {
s.Join(table, conditionA, logic, conditionB)
return s
}
}
//LeftJoin ..
func LeftJoin(table string, conditionA, logic, conditionB string) Option {
// return func(s Query) Query {
return func(s SQLSegments) SQLSegments {
s.LeftJoin(table, conditionA, logic, conditionB)
return s
}
}
//RightJoin for right join
func RightJoin(table string, conditionA, logic, conditionB string) Option {
return func(s SQLSegments) SQLSegments {
s.RightJoin(table, conditionA, logic, conditionB)
return s
}
}
//InnerJoin for inner join
func InnerJoin(table string, conditionA, logic, conditionB string) Option {
return func(s SQLSegments) SQLSegments {
s.InnerJoin(table, conditionA, logic, conditionB)
return s
}
}
//CorssJoin for corss join
func CorssJoin(table string, conditionA, logic, conditionB string) Option {
return func(s SQLSegments) SQLSegments {
s.CorssJoin(table, conditionA, logic, conditionB)
return s
}
}
//Union for union sql
func Union(f func(*SQLSegments)) Option {
return func(s SQLSegments) SQLSegments {
s.Union(f)
return s
}
}
//OrderBy ..
func OrderBy(fields ...string) Option {
return func(s SQLSegments) SQLSegments {
s.OrderBy(fields...)
return s
}
}
//GroupBy ..
func GroupBy(fields ...string) Option {
return func(s SQLSegments) SQLSegments {
s.GroupBy(fields...)
return s
}
}
//Offset ..
func Offset(n int) Option {
return func(s SQLSegments) SQLSegments {
s.Offset(n)
return s
}
}
//Limit ..
func Limit(n int) Option {
return func(s SQLSegments) SQLSegments {
s.Limit(n)
return s
}
}
//ForUpdate ..
func ForUpdate() Option {
return func(s SQLSegments) SQLSegments {
s.ForUpdate()
return s
}
}
//Returning ..
func Returning() Option {
return func(s SQLSegments) SQLSegments {
s.Returning()
return s
}
}
//Where ..
func Where(key interface{}, vals ...interface{}) Option {
return func(s SQLSegments) SQLSegments {
s.Where(key, vals...)
return s
}
}
//OrWhere ..
func OrWhere(key interface{}, vals ...interface{}) Option {
return func(s SQLSegments) SQLSegments {
s.OrWhere(key, vals...)
return s
}
}
//Set ..
func Set(key string, val interface{}) Option {
//only use for update()
return func(s SQLSegments) SQLSegments {
s.UpdateField(key, val)
return s
}
}
//Params ...
func Params(vals ...map[string]interface{}) Option {
return func(s SQLSegments) SQLSegments {
s.Params(vals...)
return s
}
}
//BuildSQL ..
func buildSQL(cmd uint8, opts ...Option) (string, []interface{}) {
s := SQLSegments{
cmd: cmd,
}
for _, opt := range opts {
s = opt(s)
}
return s.Build()
}
//SelectSQL ..
func SelectSQL(opts ...Option) (string, []interface{}) {
return buildSQL(_select, opts...)
}
//InsertSQL ..
func InsertSQL(opts ...Option) (string, []interface{}) {
return buildSQL(_insert, opts...)
}
//ReplaceSQL ..
func ReplaceSQL(opts ...Option) (string, []interface{}) {
return buildSQL(_replace, opts...)
}
//UpdateSQL ..
func UpdateSQL(opts ...Option) (string, []interface{}) {
return buildSQL(_update, opts...)
}
//DeleteSQL ..
func DeleteSQL(opts ...Option) (string, []interface{}) {
return buildSQL(_delete, opts...)
}