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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
|
package ast
import (
"strconv"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
"github.com/shopspring/decimal"
)
// ---------------------------------------------------------------------------
// Base
// ---------------------------------------------------------------------------
// Node is an interface which should be satisfied by all nodes in AST.
type Node interface {
GetPosition() uint32
SetPosition(uint32)
GetLength() uint32
SetLength(uint32)
GetChildren() []Node
}
// NodeBase is a base struct embedded by structs implementing Node interface.
type NodeBase struct {
Position uint32 `print:"-"`
Length uint32 `print:"-"`
}
// GetPosition returns the offset in bytes where the corresponding token starts.
func (n NodeBase) GetPosition() uint32 {
return n.Position
}
// SetPosition sets the offset in bytes where the corresponding token starts.
func (n *NodeBase) SetPosition(position uint32) {
n.Position = position
}
// GetLength returns the length in bytes of the corresponding token.
func (n NodeBase) GetLength() uint32 {
return n.Length
}
// SetLength sets the length in bytes of the corresponding token.
func (n *NodeBase) SetLength(length uint32) {
n.Length = length
}
// ---------------------------------------------------------------------------
// Identifiers
// ---------------------------------------------------------------------------
// ExprNode is an interface which should be satisfied all nodes in expressions.
type ExprNode interface {
Node
IsConstant() bool
GetType() DataType
SetType(DataType)
}
// UntaggedExprNodeBase is a base struct embedded by nodes whose types can be
// decided without any context and database schema.
type UntaggedExprNodeBase struct {
NodeBase
}
// SetType always panics because it is not reasonable to set data type on nodes
// whose types are already decided.
func (n *UntaggedExprNodeBase) SetType(t DataType) {
panic("setting type on untagged expression node")
}
// TaggedExprNodeBase is a base struct embedded by nodes whose types depend on
// the context and can only be decided after loading database schemas.
type TaggedExprNodeBase struct {
NodeBase
Type DataType `print:"-"`
}
// GetType gets the data type of the node.
func (n TaggedExprNodeBase) GetType() DataType {
return n.Type
}
// SetType sets the data type of the node.
func (n *TaggedExprNodeBase) SetType(t DataType) {
n.Type = t
}
// IdentifierNode references table, column, or function.
type IdentifierNode struct {
TaggedExprNodeBase
Name []byte
}
var _ ExprNode = (*IdentifierNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n IdentifierNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n IdentifierNode) IsConstant() bool {
return false
}
// ---------------------------------------------------------------------------
// Values
// ---------------------------------------------------------------------------
// Valuer defines the interface of a constant value.
type Valuer interface {
Value() interface{}
}
// BoolValueNode is a boolean constant.
type BoolValueNode struct {
UntaggedExprNodeBase
V bool
}
var _ ExprNode = (*BoolValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n BoolValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n BoolValueNode) IsConstant() bool {
return true
}
// GetType returns the type of 'bool'.
func (n BoolValueNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// Value returns the value of BoolValueNode.
func (n BoolValueNode) Value() interface{} {
return n.V
}
// String is used for printing AST.
func (n BoolValueNode) String() string {
return strconv.FormatBool(n.V)
}
// IntegerValueNode is an integer constant.
type IntegerValueNode struct {
TaggedExprNodeBase
IsAddress bool
V decimal.Decimal
}
var _ ExprNode = (*IntegerValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n IntegerValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n IntegerValueNode) IsConstant() bool {
return true
}
// Value returns the value of IntegerValueNode.
func (n IntegerValueNode) Value() interface{} {
return n.V
}
// String is used for printing AST.
func (n IntegerValueNode) String() string {
return n.V.String()
}
// DecimalValueNode is a number constant.
type DecimalValueNode struct {
TaggedExprNodeBase
V decimal.Decimal
}
var _ ExprNode = (*DecimalValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n DecimalValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n DecimalValueNode) IsConstant() bool {
return true
}
// Value returns the value of DecimalValueNode.
func (n DecimalValueNode) Value() interface{} {
return n.V
}
// String is used for printing AST.
func (n DecimalValueNode) String() string {
return n.V.String()
}
// BytesValueNode is a dynamic or fixed bytes constant.
type BytesValueNode struct {
TaggedExprNodeBase
V []byte
}
var _ ExprNode = (*BytesValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n BytesValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n BytesValueNode) IsConstant() bool {
return true
}
// Value returns the value of BytesValueNode.
func (n BytesValueNode) Value() interface{} {
return n.V
}
// String is used for printing AST.
func (n BytesValueNode) String() string {
return string(n.V)
}
// AnyValueNode is '*' used in SELECT and function call.
type AnyValueNode struct {
UntaggedExprNodeBase
}
var _ ExprNode = (*AnyValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n AnyValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n AnyValueNode) IsConstant() bool {
return false
}
// GetType returns the type of '*'.
func (n AnyValueNode) GetType() DataType {
return ComposeDataType(DataTypeMajorSpecial, DataTypeMinorSpecialAny)
}
// Value returns itself.
func (n AnyValueNode) Value() interface{} {
return n
}
// DefaultValueNode represents the default value used in INSERT and UPDATE.
type DefaultValueNode struct {
UntaggedExprNodeBase
}
var _ ExprNode = (*DefaultValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n DefaultValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n DefaultValueNode) IsConstant() bool {
return true
}
// GetType returns the type of 'DEFAULT'.
func (n DefaultValueNode) GetType() DataType {
return ComposeDataType(DataTypeMajorSpecial, DataTypeMinorSpecialDefault)
}
// Value returns itself.
func (n DefaultValueNode) Value() interface{} {
return n
}
// NullValueNode is NULL.
type NullValueNode struct {
TaggedExprNodeBase
}
var _ ExprNode = (*NullValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n NullValueNode) GetChildren() []Node {
return nil
}
// IsConstant returns whether a node is a constant.
func (n NullValueNode) IsConstant() bool {
return true
}
// Value returns itself.
func (n NullValueNode) Value() interface{} { return n }
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
// TypeNode is an interface which should be satisfied nodes representing types.
type TypeNode interface {
Node
GetType() (DataType, errors.ErrorCode)
}
// IntTypeNode represents solidity int{X} and uint{X} types.
type IntTypeNode struct {
NodeBase
Unsigned bool
Size uint32
}
var _ TypeNode = (*IntTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n IntTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n IntTypeNode) GetType() (DataType, errors.ErrorCode) {
if n.Size%8 != 0 || n.Size == 0 || n.Size > 256 {
if n.Unsigned {
return DataTypeUnknown, errors.ErrorCodeInvalidUintSize
}
return DataTypeUnknown, errors.ErrorCodeInvalidIntSize
}
var major DataTypeMajor
var minor DataTypeMinor
if n.Unsigned {
major = DataTypeMajorUint
} else {
major = DataTypeMajorInt
}
minor = DataTypeMinor(n.Size/8 - 1)
return ComposeDataType(major, minor), errors.ErrorCodeNil
}
// FixedTypeNode represents solidity fixed{M}x{N} and ufixed{M}x{N} types.
type FixedTypeNode struct {
NodeBase
Unsigned bool
Size uint32
FractionalDigits uint32
}
var _ TypeNode = (*FixedTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n FixedTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n FixedTypeNode) GetType() (DataType, errors.ErrorCode) {
if n.Size%8 != 0 || n.Size == 0 || n.Size > 256 {
if n.Unsigned {
return DataTypeUnknown, errors.ErrorCodeInvalidUfixedSize
}
return DataTypeUnknown, errors.ErrorCodeInvalidFixedSize
}
if n.FractionalDigits > 80 {
if n.Unsigned {
return DataTypeUnknown, errors.ErrorCodeInvalidUfixedFractionalDigits
}
return DataTypeUnknown, errors.ErrorCodeInvalidFixedFractionalDigits
}
var major DataTypeMajor
var minor DataTypeMinor
if n.Unsigned {
major = DataTypeMajorUfixed
} else {
major = DataTypeMajorFixed
}
major += DataTypeMajor(n.Size/8 - 1)
minor = DataTypeMinor(n.FractionalDigits)
return ComposeDataType(major, minor), errors.ErrorCodeNil
}
// DynamicBytesTypeNode represents solidity bytes type.
type DynamicBytesTypeNode struct {
NodeBase
}
var _ TypeNode = (*DynamicBytesTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n DynamicBytesTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n DynamicBytesTypeNode) GetType() (DataType, errors.ErrorCode) {
return ComposeDataType(DataTypeMajorDynamicBytes, DataTypeMinorDontCare),
errors.ErrorCodeNil
}
// FixedBytesTypeNode represents solidity bytes{X} type.
type FixedBytesTypeNode struct {
NodeBase
Size uint32
}
var _ TypeNode = (*FixedBytesTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n FixedBytesTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n FixedBytesTypeNode) GetType() (DataType, errors.ErrorCode) {
if n.Size == 0 || n.Size > 32 {
return DataTypeUnknown, errors.ErrorCodeInvalidBytesSize
}
major := DataTypeMajorFixedBytes
minor := DataTypeMinor(n.Size - 1)
return ComposeDataType(major, minor), errors.ErrorCodeNil
}
// AddressTypeNode represents solidity address type.
type AddressTypeNode struct {
NodeBase
}
var _ TypeNode = (*AddressTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n AddressTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n AddressTypeNode) GetType() (DataType, errors.ErrorCode) {
return ComposeDataType(DataTypeMajorAddress, DataTypeMinorDontCare),
errors.ErrorCodeNil
}
// BoolTypeNode represents solidity bool type.
type BoolTypeNode struct {
NodeBase
}
var _ TypeNode = (*BoolTypeNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n BoolTypeNode) GetChildren() []Node {
return nil
}
// GetType returns the type represented by the node.
func (n BoolTypeNode) GetType() (DataType, errors.ErrorCode) {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare),
errors.ErrorCodeNil
}
// ---------------------------------------------------------------------------
// Operators
// ---------------------------------------------------------------------------
// UnaryOperator defines the interface of a unary operator.
type UnaryOperator interface {
ExprNode
GetTarget() ExprNode
SetTarget(ExprNode)
}
// BinaryOperator defines the interface of a binary operator.
type BinaryOperator interface {
ExprNode
GetObject() ExprNode
GetSubject() ExprNode
SetObject(ExprNode)
SetSubject(ExprNode)
}
// UnaryOperatorNode is a base struct of unary operators.
type UnaryOperatorNode struct {
Target ExprNode
}
// GetChildren returns a list of child nodes used for traversing.
func (n UnaryOperatorNode) GetChildren() []Node {
return []Node{n.Target}
}
// IsConstant returns whether a node is a constant.
func (n UnaryOperatorNode) IsConstant() bool {
return n.Target.IsConstant()
}
// GetTarget gets the target of the operation.
func (n UnaryOperatorNode) GetTarget() ExprNode {
return n.Target
}
// SetTarget sets the target of the operation.
func (n *UnaryOperatorNode) SetTarget(t ExprNode) {
n.Target = t
}
// BinaryOperatorNode is a base struct of binary operators.
type BinaryOperatorNode struct {
Object ExprNode
Subject ExprNode
}
// GetChildren returns a list of child nodes used for traversing.
func (n BinaryOperatorNode) GetChildren() []Node {
return []Node{n.Object, n.Subject}
}
// IsConstant returns whether a node is a constant.
func (n BinaryOperatorNode) IsConstant() bool {
return n.Object.IsConstant() && n.Subject.IsConstant()
}
// GetObject gets the node on which the operation is applied.
func (n BinaryOperatorNode) GetObject() ExprNode {
return n.Object
}
// GetSubject gets the node whose value is applied on the object.
func (n BinaryOperatorNode) GetSubject() ExprNode {
return n.Subject
}
// SetObject sets the object of the operation.
func (n *BinaryOperatorNode) SetObject(o ExprNode) {
n.Object = o
}
// SetSubject sets the subject of the operation.
func (n *BinaryOperatorNode) SetSubject(s ExprNode) {
n.Subject = s
}
// PosOperatorNode is '+'.
type PosOperatorNode struct {
TaggedExprNodeBase
UnaryOperatorNode
}
var _ UnaryOperator = (*PosOperatorNode)(nil)
// NegOperatorNode is '-'.
type NegOperatorNode struct {
TaggedExprNodeBase
UnaryOperatorNode
}
var _ UnaryOperator = (*NegOperatorNode)(nil)
// NotOperatorNode is 'NOT'.
type NotOperatorNode struct {
UntaggedExprNodeBase
UnaryOperatorNode
}
var _ UnaryOperator = (*NotOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n NotOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// AndOperatorNode is 'AND'.
type AndOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*AndOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n AndOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// OrOperatorNode is 'OR'.
type OrOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*OrOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n OrOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// GreaterOrEqualOperatorNode is '>='.
type GreaterOrEqualOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*GreaterOrEqualOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n GreaterOrEqualOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// LessOrEqualOperatorNode is '<='.
type LessOrEqualOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*LessOrEqualOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n LessOrEqualOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// NotEqualOperatorNode is '<>'.
type NotEqualOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*NotEqualOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n NotEqualOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// EqualOperatorNode is '=' used in expressions.
type EqualOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*EqualOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n EqualOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// GreaterOperatorNode is '>'.
type GreaterOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*GreaterOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n GreaterOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// LessOperatorNode is '<'.
type LessOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*LessOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n LessOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// ConcatOperatorNode is '||'.
type ConcatOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*ConcatOperatorNode)(nil)
// GetType returns the type of 'bytes'.
func (n ConcatOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorDynamicBytes, DataTypeMinorDontCare)
}
// AddOperatorNode is '+'.
type AddOperatorNode struct {
TaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*AddOperatorNode)(nil)
// SubOperatorNode is '-'.
type SubOperatorNode struct {
TaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*SubOperatorNode)(nil)
// MulOperatorNode is '*'.
type MulOperatorNode struct {
TaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*MulOperatorNode)(nil)
// DivOperatorNode is '/'.
type DivOperatorNode struct {
TaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*DivOperatorNode)(nil)
// ModOperatorNode is '%'.
type ModOperatorNode struct {
TaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*ModOperatorNode)(nil)
// IsOperatorNode is 'IS NULL'.
type IsOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*IsOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n IsOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// LikeOperatorNode is 'LIKE'.
type LikeOperatorNode struct {
UntaggedExprNodeBase
BinaryOperatorNode
}
var _ BinaryOperator = (*LikeOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n LikeOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// ---------------------------------------------------------------------------
// Cast
// ---------------------------------------------------------------------------
// CastOperatorNode is 'CAST(expr AS type)'.
type CastOperatorNode struct {
UntaggedExprNodeBase
SourceExpr ExprNode
TargetType TypeNode
}
var _ ExprNode = (*CastOperatorNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n CastOperatorNode) GetChildren() []Node {
return []Node{n.SourceExpr, n.TargetType}
}
// IsConstant returns whether a node is a constant.
func (n CastOperatorNode) IsConstant() bool {
return n.SourceExpr.IsConstant()
}
// GetType returns the type of CAST expression, which is always the target type.
func (n CastOperatorNode) GetType() DataType {
if dt, code := n.TargetType.GetType(); code == errors.ErrorCodeNil {
return dt
}
return DataTypeUnknown
}
// ---------------------------------------------------------------------------
// Assignment
// ---------------------------------------------------------------------------
// AssignOperatorNode is '=' used in UPDATE to set values.
type AssignOperatorNode struct {
NodeBase
Column *IdentifierNode
Expr ExprNode
}
var _ Node = (*AssignOperatorNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n AssignOperatorNode) GetChildren() []Node {
return []Node{n.Column, n.Expr}
}
// ---------------------------------------------------------------------------
// In
// ---------------------------------------------------------------------------
// InOperatorNode is 'IN'.
type InOperatorNode struct {
UntaggedExprNodeBase
Left ExprNode
Right []ExprNode
}
var _ ExprNode = (*InOperatorNode)(nil)
// GetType returns the type of 'bool'.
func (n InOperatorNode) GetType() DataType {
return ComposeDataType(DataTypeMajorBool, DataTypeMinorDontCare)
}
// GetChildren returns a list of child nodes used for traversing.
func (n InOperatorNode) GetChildren() []Node {
nodes := make([]Node, 1+len(n.Right))
nodes[0] = n.Left
for i := 0; i < len(n.Right); i++ {
nodes[i+1] = n.Right[i]
}
return nodes
}
// IsConstant returns whether a node is a constant.
func (n InOperatorNode) IsConstant() bool {
if !n.Left.IsConstant() {
return false
}
for i := 0; i < len(n.Right); i++ {
if !n.Right[i].IsConstant() {
return false
}
}
return true
}
// ---------------------------------------------------------------------------
// Function
// ---------------------------------------------------------------------------
// FunctionOperatorNode is a function call.
type FunctionOperatorNode struct {
TaggedExprNodeBase
Name *IdentifierNode
Args []ExprNode
}
var _ ExprNode = (*FunctionOperatorNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n FunctionOperatorNode) GetChildren() []Node {
nodes := make([]Node, 1+len(n.Args))
nodes[0] = n.Name
for i := 0; i < len(n.Args); i++ {
nodes[i+1] = n.Args[i]
}
return nodes
}
// IsConstant returns whether a node is a constant.
func (n FunctionOperatorNode) IsConstant() bool {
return false
}
// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------
// WhereOptionNode is 'WHERE' used in SELECT, UPDATE, DELETE.
type WhereOptionNode struct {
NodeBase
Condition ExprNode
}
var _ Node = (*WhereOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n WhereOptionNode) GetChildren() []Node {
return []Node{n.Condition}
}
// OrderOptionNode is an expression in 'ORDER BY' used in SELECT.
type OrderOptionNode struct {
NodeBase
Expr ExprNode
Desc bool
NullsFirst bool
}
var _ Node = (*OrderOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n OrderOptionNode) GetChildren() []Node {
return []Node{n.Expr}
}
// GroupOptionNode is 'GROUP BY' used in SELECT.
type GroupOptionNode struct {
NodeBase
Expr ExprNode
}
var _ Node = (*GroupOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n GroupOptionNode) GetChildren() []Node {
return []Node{n.Expr}
}
// OffsetOptionNode is 'OFFSET' used in SELECT.
type OffsetOptionNode struct {
NodeBase
Value *IntegerValueNode
}
var _ Node = (*OffsetOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n OffsetOptionNode) GetChildren() []Node {
return []Node{n.Value}
}
// LimitOptionNode is 'LIMIT' used in SELECT.
type LimitOptionNode struct {
NodeBase
Value *IntegerValueNode
}
var _ Node = (*LimitOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n LimitOptionNode) GetChildren() []Node {
return []Node{n.Value}
}
// InsertWithColumnOptionNode stores columns and values used in INSERT.
type InsertWithColumnOptionNode struct {
NodeBase
Column []*IdentifierNode
Value [][]ExprNode
}
var _ Node = (*InsertWithColumnOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n InsertWithColumnOptionNode) GetChildren() []Node {
size := len(n.Column)
for i := 0; i < len(n.Value); i++ {
size += len(n.Value[i])
}
nodes := make([]Node, size)
idx := 0
for i := 0; i < len(n.Column); i, idx = i+1, idx+1 {
nodes[idx] = n.Column[i]
}
for i := 0; i < len(n.Value); i++ {
for j := 0; j < len(n.Value[i]); j, idx = j+1, idx+1 {
nodes[idx] = n.Value[i][j]
}
}
return nodes
}
// InsertWithDefaultOptionNode is 'DEFAULT VALUES' used in INSERT.
type InsertWithDefaultOptionNode struct {
NodeBase
}
var _ Node = (*InsertWithDefaultOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n InsertWithDefaultOptionNode) GetChildren() []Node {
return nil
}
// PrimaryOptionNode is 'PRIMARY KEY' used in CREATE TABLE.
type PrimaryOptionNode struct {
NodeBase
}
var _ Node = (*PrimaryOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n PrimaryOptionNode) GetChildren() []Node {
return nil
}
// NotNullOptionNode is 'NOT NULL' used in CREATE TABLE.
type NotNullOptionNode struct {
NodeBase
}
var _ Node = (*NotNullOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n NotNullOptionNode) GetChildren() []Node {
return nil
}
// UniqueOptionNode is 'UNIQUE' used in CREATE TABLE and CREATE INDEX.
type UniqueOptionNode struct {
NodeBase
}
var _ Node = (*UniqueOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n UniqueOptionNode) GetChildren() []Node {
return nil
}
// AutoIncrementOptionNode is 'AUTOINCREMENT' used in CREATE TABLE.
type AutoIncrementOptionNode struct {
NodeBase
}
var _ Node = (*AutoIncrementOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n AutoIncrementOptionNode) GetChildren() []Node {
return nil
}
// DefaultOptionNode is 'DEFAULT' used in CREATE TABLE.
type DefaultOptionNode struct {
NodeBase
Value ExprNode
}
var _ Node = (*DefaultValueNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n DefaultOptionNode) GetChildren() []Node {
return []Node{n.Value}
}
// ForeignOptionNode is 'REFERENCES' used in CREATE TABLE.
type ForeignOptionNode struct {
NodeBase
Table *IdentifierNode
Column *IdentifierNode
}
var _ Node = (*ForeignOptionNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n ForeignOptionNode) GetChildren() []Node {
return []Node{n.Table, n.Column}
}
// ---------------------------------------------------------------------------
// Statements
// ---------------------------------------------------------------------------
// SelectStmtNode is SELECT.
type SelectStmtNode struct {
NodeBase
Column []ExprNode
Table *IdentifierNode
Where *WhereOptionNode
Group []*GroupOptionNode
Order []*OrderOptionNode
Limit *LimitOptionNode
Offset *OffsetOptionNode
}
var _ Node = (*SelectStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n SelectStmtNode) GetChildren() []Node {
nodes := make([]Node, len(n.Column)+2+len(n.Group)+len(n.Order)+2)
idx := 0
for i := 0; i < len(n.Column); i, idx = i+1, idx+1 {
nodes[idx] = n.Column[i]
}
nodes[idx] = n.Table
idx++
nodes[idx] = n.Where
idx++
for i := 0; i < len(n.Group); i, idx = i+1, idx+1 {
nodes[idx] = n.Group[i]
}
for i := 0; i < len(n.Order); i, idx = i+1, idx+1 {
nodes[idx] = n.Order[i]
}
nodes[idx] = n.Limit
idx++
nodes[idx] = n.Offset
idx++
return nodes
}
// UpdateStmtNode is UPDATE.
type UpdateStmtNode struct {
NodeBase
Table *IdentifierNode
Assignment []*AssignOperatorNode
Where *WhereOptionNode
}
var _ Node = (*UpdateStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n UpdateStmtNode) GetChildren() []Node {
nodes := make([]Node, 1+len(n.Assignment)+1)
idx := 0
nodes[idx] = n.Table
for i := 0; i < len(n.Assignment); i, idx = i+1, idx+1 {
nodes[idx] = n.Assignment[i]
}
nodes[idx] = n.Where
idx++
return nodes
}
// DeleteStmtNode is DELETE.
type DeleteStmtNode struct {
NodeBase
Table *IdentifierNode
Where *WhereOptionNode
}
var _ Node = (*DeleteStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n DeleteStmtNode) GetChildren() []Node {
return []Node{n.Table, n.Where}
}
// InsertStmtNode is INSERT.
type InsertStmtNode struct {
NodeBase
Table *IdentifierNode
Insert Node
}
var _ Node = (*InsertStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n InsertStmtNode) GetChildren() []Node {
return []Node{n.Table, n.Insert}
}
// CreateTableStmtNode is CREATE TABLE.
type CreateTableStmtNode struct {
NodeBase
Table *IdentifierNode
Column []*ColumnSchemaNode
}
var _ Node = (*CreateTableStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n CreateTableStmtNode) GetChildren() []Node {
nodes := make([]Node, 1+len(n.Column))
nodes[0] = n.Table
for i := 0; i < len(n.Column); i++ {
nodes[i+1] = n.Column[i]
}
return nodes
}
// ColumnSchemaNode specifies a column in CREATE TABLE.
type ColumnSchemaNode struct {
NodeBase
Column *IdentifierNode
DataType TypeNode
Constraint []Node
}
var _ Node = (*ColumnSchemaNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n ColumnSchemaNode) GetChildren() []Node {
nodes := make([]Node, 2+len(n.Constraint))
nodes[0] = n.Column
nodes[1] = n.DataType
for i := 0; i < len(n.Constraint); i++ {
nodes[i+2] = n.Constraint[i]
}
return nodes
}
// CreateIndexStmtNode is CREATE INDEX.
type CreateIndexStmtNode struct {
NodeBase
Index *IdentifierNode
Table *IdentifierNode
Column []*IdentifierNode
Unique *UniqueOptionNode
}
var _ Node = (*CreateIndexStmtNode)(nil)
// GetChildren returns a list of child nodes used for traversing.
func (n CreateIndexStmtNode) GetChildren() []Node {
nodes := make([]Node, 2+len(n.Column)+1)
idx := 0
nodes[idx] = n.Index
idx++
nodes[idx] = n.Table
idx++
for i := 0; i < len(n.Column); i, idx = i+1, idx+1 {
nodes[idx] = n.Column[i]
}
nodes[idx] = n.Unique
idx++
return nodes
}
|