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
|
package schema
import (
"errors"
"io"
"github.com/shopspring/decimal"
"github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/core/vm/sqlvm/ast"
se "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors"
"github.com/dexon-foundation/dexon/rlp"
)
// Error defines for encode and decode.
var (
ErrEncodeUnexpectedType = errors.New("encode unexpected type")
ErrDecodeUnexpectedType = errors.New("decode unexpected type")
)
// ColumnAttr defines bit flags for describing column attribute.
type ColumnAttr uint16
const (
// ColumnAttrPrimaryKey is a no-op. Primary key constraints are converted
// to a unique index during contract creation.
ColumnAttrPrimaryKey ColumnAttr = 1 << iota
// ColumnAttrNotNull is a no-op. We have not supported NULL values so all
// columns are implicitly non-null.
ColumnAttrNotNull
// ColumnAttrUnique is a no-op. Unique constraints are converted to unique
// indices during contract creation.
ColumnAttrUnique
// ColumnAttrHasDefault indicates whether a column has a default value. The
// default value does not affect the starting value of AUTOINCREMENT.
ColumnAttrHasDefault
// ColumnAttrHasForeignKey indicates whether a column references a column
// on a different table.
ColumnAttrHasForeignKey
// ColumnAttrHasSequence indicates whether a column is declared with
// AUTOINCREMENT. It is only valid on integer fields.
ColumnAttrHasSequence
)
// GetDeclaredFlags returns flags which can be mapped to the source code tokens.
func (a ColumnAttr) GetDeclaredFlags() ColumnAttr {
mask := ColumnAttrPrimaryKey |
ColumnAttrNotNull |
ColumnAttrUnique |
ColumnAttrHasDefault |
ColumnAttrHasForeignKey |
ColumnAttrHasSequence
return a & mask
}
// GetDerivedFlags returns flags which are not declared in the source code but
// can be derived from it.
func (a ColumnAttr) GetDerivedFlags() ColumnAttr {
mask := ColumnAttr(0)
return a & mask
}
// TableRef defines the type for table index in Schema.
type TableRef uint8
// ColumnRef defines the type for column index in Table.Columns.
type ColumnRef uint8
// IndexRef defines the type for array index of Column.Indices.
type IndexRef uint8
// SequenceRef defines the type for sequence index in Table.
type SequenceRef uint8
// SelectColumnRef defines the type for column index in SelectStmtNode.Column.
type SelectColumnRef uint16
// IndexAttr defines bit flags for describing index attribute.
type IndexAttr uint16
const (
// IndexAttrUnique indicates whether an index is unique.
IndexAttrUnique IndexAttr = 1 << iota
// IndexAttrReferenced indicates whether an index is referenced by columns
// with foreign key constraints. This attribute cannot be specified by
// users. It is computed automatically during contract creation.
IndexAttrReferenced
)
// GetDeclaredFlags returns flags which can be mapped to the source code tokens.
func (a IndexAttr) GetDeclaredFlags() IndexAttr {
mask := IndexAttrUnique
return a & mask
}
// GetDerivedFlags returns flags which are not declared in the source code but
// can be derived from it.
func (a IndexAttr) GetDerivedFlags() IndexAttr {
mask := IndexAttrReferenced
return a & mask
}
// Schema defines sqlvm schema struct.
type Schema []Table
// SetupColumnOffset set all tables' column offset.
func (s Schema) SetupColumnOffset() {
for i := range s {
s[i].SetupColumnOffset()
}
}
// Table defiens sqlvm table struct.
type Table struct {
Name []byte
Columns []Column
Indices []Index
}
// GetFieldType return fields' data type.
func (t *Table) GetFieldType(fields []uint8) ([]ast.DataType, error) {
types := make([]ast.DataType, len(fields))
columns := t.Columns
for i, f := range fields {
if int(f) < 0 || int(f) >= len(columns) {
return nil, se.ErrorCodeIndexOutOfRange
}
types[i] = columns[int(fields[i])].Type
}
return types, nil
}
// SetupColumnOffset set columns' slot and byte offset.
func (t *Table) SetupColumnOffset() {
slotOffset := uint8(0)
byteOffset := uint8(0)
for i, col := range t.Columns {
size := col.Type.Size()
if size+byteOffset > common.HashLength {
slotOffset++
byteOffset = 0
}
t.Columns[i].SlotOffset = slotOffset
t.Columns[i].ByteOffset = byteOffset
byteOffset += size
}
}
// Index defines sqlvm index struct.
type Index struct {
Name []byte
Attr IndexAttr
Columns []ColumnRef // Columns must be sorted in ascending order.
}
type column struct {
Name []byte
Type ast.DataType
Attr ColumnAttr
ForeignTable TableRef
ForeignColumn ColumnRef
Sequence SequenceRef
Rest interface{}
SlotOffset uint8
ByteOffset uint8
}
// Column defines sqlvm index struct.
type Column struct {
column
Default interface{} // decimal.Decimal, bool, []byte
}
// NewColumn return a Column instance.
func NewColumn(Name []byte, Type ast.DataType, Attr ColumnAttr, Sequence SequenceRef,
FT TableRef, FC ColumnRef) Column {
c := column{
Name: Name,
Type: Type,
Attr: Attr,
Sequence: Sequence,
ForeignTable: FT,
ForeignColumn: FC,
}
return Column{
column: c,
}
}
var _ rlp.Decoder = (*Column)(nil)
var _ rlp.Encoder = Column{}
// EncodeRLP encodes column with rlp encode.
func (c Column) EncodeRLP(w io.Writer) error {
if c.Default != nil {
switch d := c.Default.(type) {
case bool:
v := byte(0)
if d {
v = byte(1)
}
c.Rest = []byte{v}
case []byte:
c.Rest = d
case decimal.Decimal:
var err error
c.Rest, err = ast.DecimalEncode(c.Type, d)
if err != nil {
return err
}
default:
return ErrEncodeUnexpectedType
}
} else {
c.Rest = nil
}
return rlp.Encode(w, c.column)
}
// DecodeRLP decodes column with rlp decode.
func (c *Column) DecodeRLP(s *rlp.Stream) error {
defer func() { c.Rest = nil }()
err := s.Decode(&c.column)
if err != nil {
return err
}
switch rest := c.Rest.(type) {
case []interface{}:
// nil is converted to empty list by encoder, while empty list is
// converted to []interface{} by decoder.
// So we view this case as nil and skip it.
case []byte:
major, _ := ast.DecomposeDataType(c.Type)
switch major {
case ast.DataTypeMajorBool:
if rest[0] == 1 {
c.Default = true
} else {
c.Default = false
}
case ast.DataTypeMajorFixedBytes, ast.DataTypeMajorDynamicBytes:
c.Default = rest
default:
d, err := ast.DecimalDecode(c.Type, rest)
if err != nil {
return err
}
c.Default = d
}
default:
return ErrDecodeUnexpectedType
}
return nil
}
// TableDescriptor identifies a table in a schema by an array index.
type TableDescriptor struct {
Table TableRef
}
// ColumnDescriptor identifies a column in a schema by array indices.
type ColumnDescriptor struct {
Table TableRef
Column ColumnRef
}
// IndexDescriptor identifies a index in a schema by array indices.
type IndexDescriptor struct {
Table TableRef
Index IndexRef
}
// SelectColumnDescriptor identifies a column specified in a select command by
// an array index.
type SelectColumnDescriptor struct {
SelectColumn SelectColumnRef
}
|