aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/sqlvm/ast/ast.go
blob: 61a990e20a4cfea3d8f21931b830271c212712cb (plain) (blame)
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
package ast

import (
    "github.com/shopspring/decimal"
)

// ---------------------------------------------------------------------------
// Identifiers
// ---------------------------------------------------------------------------

// IdentifierNode references table, column, or function.
type IdentifierNode struct {
    Name []byte
}

// ---------------------------------------------------------------------------
// Values
// ---------------------------------------------------------------------------

// Valuer defines the interface of a constant value.
type Valuer interface {
    Value() interface{}
}

// BoolValueNode is a boolean constant.
type BoolValueNode struct {
    V bool
}

// Value returns the value of BoolValueNode.
func (n BoolValueNode) Value() interface{} { return n.V }

// IntegerValueNode is an integer constant.
type IntegerValueNode struct {
    IsAddress bool
    V         decimal.Decimal
}

// Value returns the value of IntegerValueNode.
func (n IntegerValueNode) Value() interface{} { return n.V }
func (n IntegerValueNode) String() string     { return n.V.String() }

// DecimalValueNode is a number constant.
type DecimalValueNode struct {
    V decimal.Decimal
}

// Value returns the value of DecimalValueNode.
func (n DecimalValueNode) Value() interface{} { return n.V }
func (n DecimalValueNode) String() string     { return n.V.String() }

// BytesValueNode is a dynamic or fixed bytes constant.
type BytesValueNode struct {
    V []byte
}

// Value returns the value of BytesValueNode.
func (n BytesValueNode) Value() interface{} { return n.V }
func (n BytesValueNode) String() string     { return string(n.V) }

// AnyValueNode is '*' used in SELECT and function call.
type AnyValueNode struct{}

// Value returns itself.
func (n AnyValueNode) Value() interface{} { return n }

// DefaultValueNode represents the default value used in INSERT and UPDATE.
type DefaultValueNode struct{}

// Value returns itself.
func (n DefaultValueNode) Value() interface{} { return n }

// NullValueNode is NULL.
type NullValueNode struct{}

// Value returns itself.
func (n NullValueNode) Value() interface{} { return n }

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

// IntTypeNode represents solidity int{X} and uint{X} types.
type IntTypeNode struct {
    Unsigned bool
    Size     uint32
}

// FixedTypeNode represents solidity fixed{M}x{N} and ufixed{M}x{N} types.
type FixedTypeNode struct {
    Unsigned         bool
    Size             uint32
    FractionalDigits uint32
}

// DynamicBytesTypeNode represents solidity bytes type.
type DynamicBytesTypeNode struct{}

// FixedBytesTypeNode represents solidity bytes{X} type.
type FixedBytesTypeNode struct {
    Size uint32
}

// AddressTypeNode represents solidity address type.
type AddressTypeNode struct{}

// BoolTypeNode represents solidity bool type.
type BoolTypeNode struct{}

// ---------------------------------------------------------------------------
// Operators
// ---------------------------------------------------------------------------

// UnaryOperator defines the interface of a unary operator.
type UnaryOperator interface {
    GetTarget() interface{}
    SetTarget(interface{})
}

// BinaryOperator defines the interface of a binary operator.
type BinaryOperator interface {
    GetObject() interface{}
    GetSubject() interface{}
    SetObject(interface{})
    SetSubject(interface{})
}

// UnaryOperatorNode is a base struct of unary operators.
type UnaryOperatorNode struct {
    Target interface{}
}

// GetTarget gets the target of the operation.
func (n UnaryOperatorNode) GetTarget() interface{} {
    return n.Target
}

// SetTarget sets the target of the operation.
func (n *UnaryOperatorNode) SetTarget(t interface{}) {
    n.Target = t
}

// BinaryOperatorNode is a base struct of binary operators.
type BinaryOperatorNode struct {
    Object  interface{}
    Subject interface{}
}

// GetObject gets the node on which the operation is applied.
func (n BinaryOperatorNode) GetObject() interface{} {
    return n.Object
}

// GetSubject gets the node whose value is applied on the object.
func (n BinaryOperatorNode) GetSubject() interface{} {
    return n.Subject
}

// SetObject sets the object of the operation.
func (n *BinaryOperatorNode) SetObject(o interface{}) {
    n.Object = o
}

// SetSubject sets the subject of the operation.
func (n *BinaryOperatorNode) SetSubject(s interface{}) {
    n.Subject = s
}

// PosOperatorNode is '+'.
type PosOperatorNode struct{ UnaryOperatorNode }

// NegOperatorNode is '-'.
type NegOperatorNode struct{ UnaryOperatorNode }

// NotOperatorNode is 'NOT'.
type NotOperatorNode struct{ UnaryOperatorNode }

// AndOperatorNode is 'AND'.
type AndOperatorNode struct{ BinaryOperatorNode }

// OrOperatorNode is 'OR'.
type OrOperatorNode struct{ BinaryOperatorNode }

// GreaterOrEqualOperatorNode is '>='.
type GreaterOrEqualOperatorNode struct{ BinaryOperatorNode }

// LessOrEqualOperatorNode is '<='.
type LessOrEqualOperatorNode struct{ BinaryOperatorNode }

// NotEqualOperatorNode is '<>'.
type NotEqualOperatorNode struct{ BinaryOperatorNode }

// EqualOperatorNode is '=' used in expressions.
type EqualOperatorNode struct{ BinaryOperatorNode }

// GreaterOperatorNode is '>'.
type GreaterOperatorNode struct{ BinaryOperatorNode }

// LessOperatorNode is '<'.
type LessOperatorNode struct{ BinaryOperatorNode }

// ConcatOperatorNode is '||'.
type ConcatOperatorNode struct{ BinaryOperatorNode }

// AddOperatorNode is '+'.
type AddOperatorNode struct{ BinaryOperatorNode }

// SubOperatorNode is '-'.
type SubOperatorNode struct{ BinaryOperatorNode }

// MulOperatorNode is '*'.
type MulOperatorNode struct{ BinaryOperatorNode }

// DivOperatorNode is '/'.
type DivOperatorNode struct{ BinaryOperatorNode }

// ModOperatorNode is '%'.
type ModOperatorNode struct{ BinaryOperatorNode }

// InOperatorNode is 'IN'.
type InOperatorNode struct{ BinaryOperatorNode }

// IsOperatorNode is 'IS NULL'.
type IsOperatorNode struct{ BinaryOperatorNode }

// LikeOperatorNode is 'LIKE'.
type LikeOperatorNode struct{ BinaryOperatorNode }

// CastOperatorNode is 'CAST(expr AS type)'.
type CastOperatorNode struct{ BinaryOperatorNode }

// AssignOperatorNode is '=' used in UPDATE to set values.
type AssignOperatorNode struct{ BinaryOperatorNode }

// FunctionOperatorNode is a function call.
type FunctionOperatorNode struct{ BinaryOperatorNode }

// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------

// Optional defines the interface for printing AST.
type Optional interface {
    GetOption() map[string]interface{}
}

// NilOptionNode is a base struct implementing Optional interface.
type NilOptionNode struct{}

// GetOption returns a value for printing AST.
func (n NilOptionNode) GetOption() map[string]interface{} { return nil }

// WhereOptionNode is 'WHERE' used in SELECT, UPDATE, DELETE.
type WhereOptionNode struct {
    Condition interface{}
}

// GetOption returns a value for printing AST.
func (n WhereOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Condition": n.Condition,
    }
}

// OrderOptionNode is an expression in 'ORDER BY' used in SELECT.
type OrderOptionNode struct {
    Expr       interface{}
    Desc       bool
    NullsFirst bool
}

// GetOption returns a value for printing AST.
func (n OrderOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Expr":       n.Expr,
        "Desc":       n.Desc,
        "NullsFirst": n.NullsFirst,
    }
}

// GroupOptionNode is 'GROUP BY' used in SELECT.
type GroupOptionNode struct {
    Expr interface{}
}

// GetOption returns a value for printing AST.
func (n GroupOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Expr": n.Expr,
    }
}

// OffsetOptionNode is 'OFFSET' used in SELECT.
type OffsetOptionNode struct {
    Value IntegerValueNode
}

// GetOption returns a value for printing AST.
func (n OffsetOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Value": n.Value,
    }
}

// LimitOptionNode is 'LIMIT' used in SELECT.
type LimitOptionNode struct {
    Value IntegerValueNode
}

// GetOption returns a value for printing AST.
func (n LimitOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Value": n.Value,
    }
}

// InsertWithColumnOptionNode stores columns and values used in INSERT.
type InsertWithColumnOptionNode struct {
    Column []interface{}
    Value  []interface{}
}

// GetOption returns a value for printing AST.
func (n InsertWithColumnOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Column": n.Column,
        "Value":  n.Value,
    }
}

// InsertWithDefaultOptionNode is 'DEFAULT VALUES' used in INSERT.
type InsertWithDefaultOptionNode struct{ NilOptionNode }

// PrimaryOptionNode is 'PRIMARY KEY' used in CREATE TABLE.
type PrimaryOptionNode struct{ NilOptionNode }

// NotNullOptionNode is 'NOT NULL' used in CREATE TABLE.
type NotNullOptionNode struct{ NilOptionNode }

// UniqueOptionNode is 'UNIQUE' used in CREATE TABLE and CREATE INDEX.
type UniqueOptionNode struct{ NilOptionNode }

// AutoIncrementOptionNode is 'AUTOINCREMENT' used in CREATE TABLE.
type AutoIncrementOptionNode struct{ NilOptionNode }

// DefaultOptionNode is 'DEFAULT' used in CREATE TABLE.
type DefaultOptionNode struct {
    Value interface{}
}

// GetOption returns a value for printing AST.
func (n DefaultOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Value": n.Value,
    }
}

// ForeignOptionNode is 'REFERENCES' used in CREATE TABLE.
type ForeignOptionNode struct {
    Table  IdentifierNode
    Column IdentifierNode
}

// GetOption returns a value for printing AST.
func (n ForeignOptionNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Table":  n.Table,
        "Column": n.Column,
    }
}

// ---------------------------------------------------------------------------
// Statements
// ---------------------------------------------------------------------------

// SelectStmtNode is SELECT.
type SelectStmtNode struct {
    Column []interface{}
    Table  *IdentifierNode
    Where  *WhereOptionNode
    Group  []interface{}
    Order  []interface{}
    Limit  *LimitOptionNode
    Offset *OffsetOptionNode
}

// GetOption returns a value for printing AST.
func (n SelectStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Column": n.Column,
        "Table":  n.Table,
        "Where":  n.Where,
        "Group":  n.Group,
        "Order":  n.Order,
        "Limit":  n.Limit,
        "Offset": n.Offset,
    }
}

// UpdateStmtNode is UPDATE.
type UpdateStmtNode struct {
    Table      IdentifierNode
    Assignment []interface{}
    Where      *WhereOptionNode
}

// GetOption returns a value for printing AST.
func (n UpdateStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Table":      n.Table,
        "Assignment": n.Assignment,
        "Where":      n.Where,
    }
}

// DeleteStmtNode is DELETE.
type DeleteStmtNode struct {
    Table IdentifierNode
    Where *WhereOptionNode
}

// GetOption returns a value for printing AST.
func (n DeleteStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Table": n.Table,
        "Where": n.Where,
    }
}

// InsertStmtNode is INSERT.
type InsertStmtNode struct {
    Table  IdentifierNode
    Insert interface{}
}

// GetOption returns a value for printing AST.
func (n InsertStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Table":  n.Table,
        "Insert": n.Insert,
    }
}

// CreateTableStmtNode is CREATE TABLE.
type CreateTableStmtNode struct {
    Table  IdentifierNode
    Column []interface{}
}

// GetOption returns a value for printing AST.
func (n CreateTableStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Table":  n.Table,
        "Column": n.Column,
    }
}

// ColumnSchemaNode specifies a column in CREATE TABLE.
type ColumnSchemaNode struct {
    Column     IdentifierNode
    DataType   interface{}
    Constraint []interface{}
}

// GetOption returns a value for printing AST.
func (n ColumnSchemaNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Column":     n.Column,
        "DataYype":   n.DataType,
        "Constraint": n.Constraint,
    }
}

// CreateIndexStmtNode is CREATE INDEX.
type CreateIndexStmtNode struct {
    Index  IdentifierNode
    Table  IdentifierNode
    Column []interface{}
    Unique *UniqueOptionNode
}

// GetOption returns a value for printing AST.
func (n CreateIndexStmtNode) GetOption() map[string]interface{} {
    return map[string]interface{}{
        "Index":  n.Index,
        "Table":  n.Table,
        "Column": n.Column,
        "Unique": n.Unique,
    }
}