-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp.go
1133 lines (953 loc) · 23.3 KB
/
exp.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package kdb
import (
"bytes"
"fmt"
"github.com/sdming/kdb/ansi"
"strings"
)
const (
_defaultCapicity int = 7
_nilStr string = "<nil>"
)
// RawSqler is wrap of ToSql() string
type RawSqler interface {
// ToSql return native sql
ToSql() string
}
// Expression is interface of sql expression
type Expression interface {
Node() NodeType
}
func asExpression(v interface{}) Expression {
if v == nil {
return DbNull
}
e, ok := v.(Expression)
if ok {
return e
}
return &Value{Value: v}
}
// SortDir is direction of orderby
type SortDir string
// String
func (sd SortDir) String() string {
return string(sd)
}
// ToSql return native sql of SortDir(asc/desc)
func (sd SortDir) ToSql() string {
return string(sd)
}
const (
Asc SortDir = ansi.Asc
Desc SortDir = ansi.Desc
)
// JoinType is type of sql table join
type JoinType string
// String
func (j JoinType) String() string {
return string(j)
}
// ToSql return vative sql of JoinType(left join, right join...)
func (j JoinType) ToSql() string {
return string(j)
}
const (
CrossJoin JoinType = ansi.CrossJoin
InnerJoin JoinType = ansi.InnerJoin
LeftJoin JoinType = ansi.LeftJoin
RightJoin JoinType = ansi.RightJoin
//FullJoin JoinType = ansi.FullJoin
)
// Func is sql function
type Func string
// String
func (f Func) String() string {
return string(f)
}
// Node return NodeFunc
func (f Func) Node() NodeType {
return NodeFunc
}
const (
Count Func = ansi.Count
Sum Func = ansi.Sum
Avg Func = ansi.Avg
Min Func = ansi.Min
Max Func = ansi.Max
CurrentTime Func = "currenttime"
)
// Operator is operator in sql
type Operator string
// String
func (op Operator) String() string {
return string(op)
}
// ToSql return native sql of operator(>,<,=,<>,...)
func (op Operator) ToSql() string {
return string(op)
}
// Node return NodeOperator
func (op Operator) Node() NodeType {
return NodeOperator
}
const (
IsNull Operator = ansi.IsNull
IsNotNull Operator = ansi.IsNotNull
LessThan Operator = ansi.LessThan
LessOrEquals Operator = ansi.LessOrEquals
GreaterThan Operator = ansi.GreaterThan
GreaterOrEquals Operator = ansi.GreaterOrEquals
Equals Operator = ansi.Equals
NotEquals Operator = ansi.NotEquals
Like Operator = ansi.Like
NotLike Operator = ansi.NotLike
In Operator = ansi.In
NotIn Operator = ansi.NotIn
Exists Operator = ansi.Exists
NotExists Operator = ansi.NotExists
All Operator = ansi.All
Some Operator = ansi.Some
Any Operator = ansi.Any
And Operator = ansi.And
Or Operator = ansi.Or
OpenParentheses Operator = ansi.OpenParentheses
CloseParentheses Operator = ansi.CloseParentheses
)
// NodeType
type NodeType int
const (
NodeZero NodeType = 0
NodeText NodeType = 1
NodeProcedure NodeType = 2
NodeInsert NodeType = 3
NodeQuery NodeType = 4
NodeUpdate NodeType = 5
NodeDelete NodeType = 6
NodeNull NodeType = 11
NodeValue NodeType = 12
NodeSql NodeType = 13
NodeTable NodeType = 31
NodeColumn NodeType = 32
NodeAlias NodeType = 33
NodeCondition NodeType = 34
NodeSet NodeType = 35
NodeAggregate NodeType = 36
NodeSelect NodeType = 41
NodeFrom NodeType = 42
NodeJoin NodeType = 43
NodeWhere NodeType = 44
NodeGroupBy NodeType = 45
NodeHaving NodeType = 46
NodeOrderBy NodeType = 47
NodeOutput NodeType = 48
NodeOperator = 61
NodeFunc = 62
NodeParameter = 63
)
// String
func (n NodeType) String() string {
switch n {
case NodeZero:
return "Zero"
case NodeText:
return "Text"
case NodeProcedure:
return "Procedure"
case NodeInsert:
return "Insert"
case NodeQuery:
return "Query"
case NodeUpdate:
return "Update"
case NodeDelete:
return "Delete"
case NodeNull:
return "Null"
case NodeValue:
return "Value"
case NodeSql:
return "Sql"
case NodeTable:
return "Table"
case NodeColumn:
return "Column"
case NodeAlias:
return "Alias"
case NodeCondition:
return "Condition"
case NodeSet:
return "Set"
case NodeAggregate:
return "Aggregate"
case NodeSelect:
return "Select"
case NodeFrom:
return "From"
case NodeJoin:
return "Join"
case NodeWhere:
return "Where"
case NodeGroupBy:
return "GroupBy"
case NodeHaving:
return "Having"
case NodeOrderBy:
return "OrderBy"
case NodeOutput:
return "Output "
case NodeOperator:
return "Operator"
case NodeFunc:
return "Func"
}
return "Unknow"
}
// Null is null in database
type Null string
// String
func (n Null) String() string {
return string(n)
}
// ToSql return null
func (n Null) ToSql() string {
return string(n)
}
// Node return NodeNull
func (n Null) Node() NodeType {
return NodeNull
}
// DbNull mean null in database
const DbNull Null = ansi.Null
// Sql is sql statement
type Sql string
// String
func (s Sql) String() string {
return string(s)
}
// ToSql return Sql
func (s Sql) ToSql() string {
return string(s)
}
// Node return NodeSql
func (s Sql) Node() NodeType {
return NodeSql
}
// Column is an column, like, table.coumn, column, table.*, *
type Column string
// String
func (c Column) String() string {
return string(c)
}
// Split split column and return table and column
func (c Column) Split() (table string, column string) {
s := string(c)
if s == "" {
return "", ""
}
i := strings.Index(s, ".")
if i < 0 {
return "", s
}
return s[0:i], s[i+1:]
}
// Node return NodeColumn
func (c Column) Node() NodeType {
return NodeColumn
}
// Value is raw value
type Value struct {
// Value is embed value
Value interface{}
}
// String
func (v *Value) String() string {
if v == nil {
return _nilStr
}
return fmt.Sprint(v.Value)
}
// Node return NodeValue
func (v *Value) Node() NodeType {
return NodeValue
}
// Set is set clause in update or insert
type Set struct {
Column Column
Value Expression
}
// String
func (a *Set) String() string {
if a == nil {
return _nilStr
}
return fmt.Sprintf("%v = %v", a.Column, a.Value)
}
// Node return NodeSet
func (a *Set) Node() NodeType {
return NodeSet
}
// newSet return *Set
func newSet(column string, value Expression) *Set {
return &Set{
Column: Column(column),
Value: value,
}
}
// Condition is condition in where or having
type Condition struct {
Right Expression
Left Expression
Op Operator
}
// String
func (c *Condition) String() string {
if c == nil {
return _nilStr
}
if c.Right == nil && c.Left == nil {
return fmt.Sprint(c.Op)
} else if c.Left == nil {
return fmt.Sprint(c.Op, "(", c.Right, ")")
} else if c.Right == nil {
return fmt.Sprint(c.Left, " ", c.Op)
}
return fmt.Sprintf("%v %v %v", c.Left, c.Op, c.Right)
}
// Node return NodeCondition
func (c *Condition) Node() NodeType {
return NodeCondition
}
// Conditions is collection of condition
type Conditions struct {
Conditions []Expression
needLogicOperator bool
}
// isEmpty
func (c *Conditions) isEmpty() bool {
if c == nil || c.Conditions == nil || len(c.Conditions) == 0 {
return true
}
return false
}
// String
func (c *Conditions) String() string {
if c == nil {
return _nilStr
}
buf := bytes.Buffer{}
deep := 0
for i := 0; i < len(c.Conditions); i++ {
item := c.Conditions[i]
if i > 0 {
buf.WriteString("\n")
}
if item == CloseParentheses {
deep--
}
if deep > 0 {
buf.WriteString(strings.Repeat("\t", deep))
}
buf.WriteString(fmt.Sprint(item))
if item == OpenParentheses {
deep++
}
}
return buf.String()
}
func (c *Conditions) checkLogicOperator() {
if c.needLogicOperator {
c.Conditions = append(c.Conditions, And)
}
}
func (c *Conditions) set(exp Expression) {
c.checkLogicOperator()
if c.Conditions == nil {
c.Conditions = make([]Expression, 0, _defaultCapicity)
}
c.Conditions = append(c.Conditions, exp)
c.needLogicOperator = true
}
// Condition append a condition
func (c *Conditions) Condition(op Operator, left, right Expression) *Conditions {
c.set(&Condition{
Op: op,
Right: right,
Left: left,
})
return c
}
// And append logic operation And
func (c *Conditions) And() *Conditions {
if c.needLogicOperator {
c.Conditions = append(c.Conditions, And)
c.needLogicOperator = false
}
return c
}
// Or append logic operation Or
func (c *Conditions) Or() *Conditions {
if c.needLogicOperator {
c.Conditions = append(c.Conditions, Or)
c.needLogicOperator = false
}
return c
}
// OpenParentheses append a '('
func (c *Conditions) OpenParentheses() *Conditions {
c.checkLogicOperator()
c.Conditions = append(c.Conditions, OpenParentheses)
c.needLogicOperator = false
return c
}
// CloseParentheses append a ')'
func (c *Conditions) CloseParentheses() *Conditions {
c.Conditions = append(c.Conditions, CloseParentheses)
c.needLogicOperator = true
return c
}
// Sql append raw sql
func (c *Conditions) Sql(sqlStr string) *Conditions {
c.set(Sql(sqlStr))
return c
}
// Exists append operation Exists
func (c *Conditions) Exists(exp Expression) *Conditions {
return c.Condition(Exists, nil, exp)
}
// NotExists append operation NotExists
func (c *Conditions) NotExists(exp Expression) *Conditions {
return c.Condition(NotExists, nil, exp)
}
// Compare append compare operation
func (c *Conditions) Compare(op Operator, column string, value interface{}) *Conditions {
return c.Condition(op, Column(column), asExpression(value))
}
// Like append Like operation
func (c *Conditions) Like(column string, value string) *Conditions {
//return c.Condition(Like, Column(column), Sql(value))
return c.Condition(Like, Column(column), &Value{Value: value})
}
// NotLike append NotLike operation
func (c *Conditions) NotLike(column string, value string) *Conditions {
//return c.Condition(NotLike, Column(column), Sql(value))
return c.Condition(NotLike, Column(column), &Value{Value: value})
}
// LessOrEquals append <= operation
func (c *Conditions) LessOrEquals(column string, value interface{}) *Conditions {
return c.Condition(LessOrEquals, Column(column), asExpression(value))
}
// LessThan append < operation
func (c *Conditions) LessThan(column string, value interface{}) *Conditions {
return c.Condition(LessThan, Column(column), asExpression(value))
}
// GreaterOrEquals append >= operation
func (c *Conditions) GreaterOrEquals(column string, value interface{}) *Conditions {
return c.Condition(GreaterOrEquals, Column(column), asExpression(value))
}
// GreaterThan append > operation
func (c *Conditions) GreaterThan(column string, value interface{}) *Conditions {
return c.Condition(GreaterThan, Column(column), asExpression(value))
}
// GreaterThan append = operation
func (c *Conditions) Equals(column string, value interface{}) *Conditions {
return c.Condition(Equals, Column(column), asExpression(value))
}
// NotEquals append <> operation
func (c *Conditions) NotEquals(column string, value interface{}) *Conditions {
return c.Condition(NotEquals, Column(column), asExpression(value))
}
// IsNull append is null operation
func (c *Conditions) IsNull(column string) *Conditions {
return c.Condition(IsNull, Column(column), nil)
}
// IsNotNull append is not null operation
func (c *Conditions) IsNotNull(column string) *Conditions {
return c.Condition(IsNotNull, Column(column), nil)
}
// In append in(...) operation
func (c *Conditions) In(column string, value interface{}) *Conditions {
return c.Condition(In, Column(column), asExpression(value))
}
// NotIn append not in(...) operation
func (c *Conditions) NotIn(column string, value interface{}) *Conditions {
return c.Condition(NotIn, Column(column), asExpression(value))
}
func newConditions() *Conditions {
return &Conditions{
Conditions: make([]Expression, 0, _defaultCapicity),
}
}
//Aggregate is sql aggregate Func
type Aggregate struct {
Name Func
Exp Expression
}
// String
func (a *Aggregate) String() string {
if a == nil {
return _nilStr
}
return fmt.Sprintf("%v (%v)", a.Name, a.Exp)
}
// Node return Node
func (a *Aggregate) Node() NodeType {
return NodeAggregate
}
// NewAggregate return *Aggregate
func NewAggregate(name Func, exp Expression) *Aggregate {
return &Aggregate{
Name: name,
Exp: exp,
}
}
// Where is sql where clause
type Where struct {
*Conditions
}
// String
func (w *Where) String() string {
if w == nil {
return _nilStr
}
return fmt.Sprint(ansi.Where, "\n", w.Conditions)
}
// Node return NodeWhere
func (w *Where) Node() NodeType {
return NodeWhere
}
// NewWhere return *Where
func NewWhere() *Where {
return &Where{newConditions()}
}
// Having is sql having clause
type Having struct {
*Conditions
}
// String
func (h *Having) String() string {
if h == nil {
return _nilStr
}
return fmt.Sprint(ansi.Having, "\n", h.Conditions)
}
// Node return NodeHaving
func (h *Having) Node() NodeType {
return NodeHaving
}
func (h *Having) addAggregate(op Operator, name Func, column string, value Expression) {
h.Condition(op, NewAggregate(name, Column(column)), value)
}
// Avg append avg(...)
func (h *Having) Avg(op Operator, column string, value interface{}) *Having {
h.addAggregate(op, Avg, column, asExpression(value))
return h
}
// Count append count(...)
func (h *Having) Count(op Operator, column string, value interface{}) *Having {
h.addAggregate(op, Count, column, asExpression(value))
return h
}
// Sum append sum(...)
func (h *Having) Sum(op Operator, column string, value interface{}) *Having {
h.addAggregate(op, Sum, column, asExpression(value))
return h
}
// Min append min(...)
func (h *Having) Min(op Operator, column string, value interface{}) *Having {
h.addAggregate(op, Min, column, asExpression(value))
return h
}
// Max append max(...)
func (h *Having) Max(op Operator, column string, value interface{}) *Having {
h.addAggregate(op, Max, column, asExpression(value))
return h
}
// NewHaving return *Having
func NewHaving() *Having {
return &Having{newConditions()}
}
// GroupBy is sql group by clause
type GroupBy struct {
Fields []Expression
}
// String
func (g *GroupBy) String() string {
if g == nil {
return _nilStr
}
return fmt.Sprint(ansi.GroupBy, " ", g.Fields)
}
// Node return Node()
func (g *GroupBy) Node() NodeType {
return NodeGroupBy
}
func (g *GroupBy) add(exp Expression) {
if g.Fields == nil {
g.Fields = make([]Expression, 0, _defaultCapicity)
}
g.Fields = append(g.Fields, exp)
}
// By append a expression
func (g *GroupBy) By(exp Expression) *GroupBy {
g.add(exp)
return g
}
// Column append columns
func (g *GroupBy) Column(columns ...string) *GroupBy {
for i := 0; i < len(columns); i++ {
g.add(Column(columns[i]))
}
return g
}
// NewGroupBy return *GroupBy
func NewGroupBy() *GroupBy {
return &GroupBy{Fields: make([]Expression, 0, _defaultCapicity)}
}
// Table is tables in sql from clasuse
type Table struct {
Name string
Alias string
}
// String
func (t *Table) String() string {
if t == nil {
return _nilStr
}
if t.Alias == "" {
return t.Name
}
return fmt.Sprint(t.Name, " AS ", t.Alias)
}
// Node return NodeTable
func (t *Table) Node() NodeType {
return NodeTable
}
func newTable(name string, alias string) *Table {
return &Table{
Name: name,
Alias: alias,
}
}
// Field is each field in sql select clause
type Field struct {
Exp Expression
Alias string
}
// String
func (f *Field) String() string {
if f == nil {
return _nilStr
}
if f.Alias == "" {
return fmt.Sprint(f.Exp)
}
return fmt.Sprint(f.Exp, " AS ", f.Alias)
}
// Select is sql select clause
type Select struct {
Fields []*Field
}
// String
func (s *Select) String() string {
if s == nil {
return _nilStr
}
buf := bytes.Buffer{}
for i := 0; i < len(s.Fields); i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprint(s.Fields[i]))
}
return buf.String()
}
// Node return NodeSelect
func (s *Select) Node() NodeType {
return NodeSelect
}
// NewSelect return *Select
func NewSelect() *Select {
return &Select{Fields: make([]*Field, 0, _defaultCapicity)}
}
func (s *Select) addField(exp Expression, alias string) *Select {
if s.Fields == nil {
s.Fields = make([]*Field, 0, 5)
}
s.Fields = append(s.Fields, &Field{
Exp: exp,
Alias: alias,
})
return s
}
// Column append columns to select list
func (s *Select) Column(columns ...string) *Select {
if len(columns) == 0 {
return s
}
for i := 0; i < len(columns); i++ {
s.addField(Column(columns[i]), "")
}
return s
}
// ColumnAs append [column] as [alias] to select list
func (s *Select) ColumnAs(column, alias string) *Select {
return s.addField(Column(column), alias)
}
// All append *
func (s *Select) All() *Select {
return s.addField(Sql(ansi.WildcardAll), "")
}
// Exp append a expression
func (s *Select) Exp(exp Expression, alias string) *Select {
return s.addField(exp, alias)
}
// Aggregate append a aggregate function
func (s *Select) Aggregate(name Func, exp Expression, alias string) *Select {
return s.addField(NewAggregate(name, exp), alias)
}
// Avg append avg(...)
func (s *Select) Avg(column string, alias string) *Select {
return s.Aggregate(Avg, Column(column), alias)
}
// Count append count(...)
func (s *Select) Count(column string, alias string) *Select {
return s.Aggregate(Count, Column(column), alias)
}
// Sum append sum(...)
func (s *Select) Sum(column string, alias string) *Select {
return s.Aggregate(Sum, Column(column), alias)
}
// Min append min(...)
func (s *Select) Min(column string, alias string) *Select {
return s.Aggregate(Min, Column(column), alias)
}
// Max append max(...)
func (s *Select) Max(column string, alias string) *Select {
return s.Aggregate(Max, Column(column), alias)
}
// OrderByField is each field in sql order by clause
type OrderByField struct {
Exp Expression
Direction SortDir
}
// String
func (oi *OrderByField) String() string {
if oi == nil {
return _nilStr
}
return fmt.Sprint(oi.Exp, " ", oi.Direction)
}
// OrderBy is sql order by clause
type OrderBy struct {
Fields []*OrderByField
}
// String
func (od *OrderBy) String() string {
if od == nil {
return _nilStr
}
if len(od.Fields) == 0 {
return ""
}
buf := bytes.Buffer{}
buf.WriteString(ansi.OrderBy)
for i := 0; i < len(od.Fields); i++ {
if i > 0 {
buf.WriteString(", ")
} else {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprint(od.Fields[i]))
}
return buf.String()
}
// Node return NodeOrderBy
func (od *OrderBy) Node() NodeType {
return NodeOrderBy
}
func (od *OrderBy) isEmpty() bool {
return len(od.Fields) == 0
}
// By append a orderby field with direction
func (od *OrderBy) By(direction SortDir, exp Expression) *OrderBy {
if od.Fields == nil {
od.Fields = make([]*OrderByField, 0, _defaultCapicity)
}
od.Fields = append(od.Fields, &OrderByField{Exp: exp, Direction: direction})
return od
}
// Asc append a column to order by as asc
func (od *OrderBy) Asc(columns ...string) *OrderBy {
for i := 0; i < len(columns); i++ {
od.By(Asc, Column(columns[i]))
}
return od
}
// Desc append a column to order by as desc
func (od *OrderBy) Desc(columns ...string) *OrderBy {
for i := 0; i < len(columns); i++ {
od.By(Desc, Column(columns[i]))
}
return od
}
// NewOrderBy return *OrderBy
func NewOrderBy() *OrderBy {
return &OrderBy{Fields: make([]*OrderByField, 0, _defaultCapicity)}
}
// From is sql from clause
type From struct {
Table *Table
Tables []*Table
Joins []*Join
}
// String
func (f *From) String() string {
if f == nil {
return _nilStr
}
var buf bytes.Buffer
buf.WriteString(ansi.From)
buf.WriteString(" ")
if f.Table != nil {
buf.WriteString(fmt.Sprint(f.Table))
}
for i := 0; i < len(f.Tables); i++ {
buf.WriteString(", ")
buf.WriteString(fmt.Sprint(f.Tables[i]))
}
for i := 0; i < len(f.Joins); i++ {
buf.WriteString("\n")
buf.WriteString(fmt.Sprint(f.Joins[i]))
}
return buf.String()
}
// Node return NodeFrom
func (f *From) Node() NodeType {
return NodeFrom
}
// NewFrom return *From
func NewFrom(table, alias string) *From {
return &From{
Table: newTable(table, alias),
}
}
// ThenFrom append a table to from
func (f *From) ThenFrom(table, alias string) *From {
if f.Tables == nil {
f.Tables = make([]*Table, 0, _defaultCapicity)
}
f.Tables = append(f.Tables, newTable(table, alias))
return f