forked from cockroachdb/cockroach
/
create.go
444 lines (392 loc) · 11.1 KB
/
create.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
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// This code was derived from https://github.com/youtube/vitess.
//
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package parser
import (
"bytes"
"fmt"
)
// CreateDatabase represents a CREATE DATABASE statement.
type CreateDatabase struct {
IfNotExists bool
Name Name
Encoding *StrVal
}
// Format implements the NodeFormatter interface.
func (node *CreateDatabase) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("CREATE DATABASE ")
if node.IfNotExists {
buf.WriteString("IF NOT EXISTS ")
}
FormatNode(buf, f, node.Name)
if node.Encoding != nil {
buf.WriteString(" ENCODING=")
node.Encoding.Format(buf, f)
}
}
// IndexElem represents a column with a direction in a CREATE INDEX statement.
type IndexElem struct {
Column Name
Direction Direction
}
// Format implements the NodeFormatter interface.
func (node IndexElem) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Column)
if node.Direction != DefaultDirection {
buf.WriteByte(' ')
buf.WriteString(node.Direction.String())
}
}
// IndexElemList is list of IndexElem.
type IndexElemList []IndexElem
// Format pretty-prints the contained names separated by commas.
// Format implements the NodeFormatter interface.
func (l IndexElemList) Format(buf *bytes.Buffer, f FmtFlags) {
for i, indexElem := range l {
if i > 0 {
buf.WriteString(", ")
}
FormatNode(buf, f, indexElem)
}
}
// CreateIndex represents a CREATE INDEX statement.
type CreateIndex struct {
Name Name
Table *QualifiedName
Unique bool
IfNotExists bool
Columns IndexElemList
// Extra columns to be stored together with the indexed ones as an optimization
// for improved reading performance.
Storing NameList
}
// Format implements the NodeFormatter interface.
func (node *CreateIndex) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("CREATE ")
if node.Unique {
buf.WriteString("UNIQUE ")
}
buf.WriteString("INDEX ")
if node.IfNotExists {
buf.WriteString("IF NOT EXISTS ")
}
if node.Name != "" {
fmt.Fprintf(buf, "%s ", node.Name)
}
fmt.Fprintf(buf, "ON %s (", node.Table)
FormatNode(buf, f, node.Columns)
buf.WriteByte(')')
if node.Storing != nil {
fmt.Fprintf(buf, " STORING (%s)", node.Storing)
}
}
// TableDef represents a column, index or constraint definition within a CREATE
// TABLE statement.
type TableDef interface {
NodeFormatter
// Placeholder function to ensure that only desired types (*TableDef) conform
// to the TableDef interface.
tableDef()
setName(name Name)
}
func (*ColumnTableDef) tableDef() {}
func (*IndexTableDef) tableDef() {}
func (*FamilyTableDef) tableDef() {}
// TableDefs represents a list of table definitions.
type TableDefs []TableDef
// Format implements the NodeFormatter interface.
func (node TableDefs) Format(buf *bytes.Buffer, f FmtFlags) {
for i, n := range node {
if i > 0 {
buf.WriteString(", ")
}
FormatNode(buf, f, n)
}
}
// Nullability represents either NULL, NOT NULL or an unspecified value (silent
// NULL).
type Nullability int
// The values for NullType.
const (
NotNull Nullability = iota
Null
SilentNull
)
// ColumnTableDef represents a column definition within a CREATE TABLE
// statement.
type ColumnTableDef struct {
Name Name
Type ColumnType
Nullable Nullability
PrimaryKey bool
Unique bool
DefaultExpr Expr
CheckExpr Expr
References struct {
Table *QualifiedName
Col Name
}
}
func newColumnTableDef(
name Name, typ ColumnType, qualifications []ColumnQualification,
) *ColumnTableDef {
d := &ColumnTableDef{
Name: name,
Type: typ,
Nullable: SilentNull,
}
for _, c := range qualifications {
switch t := c.(type) {
case *ColumnDefault:
d.DefaultExpr = t.Expr
case NotNullConstraint:
d.Nullable = NotNull
case NullConstraint:
d.Nullable = Null
case PrimaryKeyConstraint:
d.PrimaryKey = true
case UniqueConstraint:
d.Unique = true
case *ColumnCheckConstraint:
d.CheckExpr = t.Expr
case *ColumnFKConstraint:
d.References.Table = t.Table
d.References.Col = t.Col
default:
panic(fmt.Sprintf("unexpected column qualification: %T", c))
}
}
return d
}
func (node *ColumnTableDef) setName(name Name) {
node.Name = name
}
// Format implements the NodeFormatter interface.
func (node *ColumnTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
fmt.Fprintf(buf, "%s ", node.Name)
FormatNode(buf, f, node.Type)
switch node.Nullable {
case Null:
buf.WriteString(" NULL")
case NotNull:
buf.WriteString(" NOT NULL")
}
if node.PrimaryKey {
buf.WriteString(" PRIMARY KEY")
} else if node.Unique {
buf.WriteString(" UNIQUE")
}
if node.DefaultExpr != nil {
buf.WriteString(" DEFAULT ")
FormatNode(buf, f, node.DefaultExpr)
}
if node.CheckExpr != nil {
buf.WriteString(" CHECK (")
FormatNode(buf, f, node.CheckExpr)
buf.WriteByte(')')
}
if node.References.Table != nil {
buf.WriteString(" REFERENCES ")
FormatNode(buf, f, node.References.Table)
if node.References.Col != "" {
buf.WriteString(" (")
FormatNode(buf, f, node.References.Col)
buf.WriteByte(')')
}
}
}
// ColumnQualification represents a constraint on a column.
type ColumnQualification interface {
columnQualification()
}
func (*ColumnDefault) columnQualification() {}
func (NotNullConstraint) columnQualification() {}
func (NullConstraint) columnQualification() {}
func (PrimaryKeyConstraint) columnQualification() {}
func (UniqueConstraint) columnQualification() {}
func (*ColumnCheckConstraint) columnQualification() {}
func (*ColumnFKConstraint) columnQualification() {}
// ColumnDefault represents a DEFAULT clause for a column.
type ColumnDefault struct {
Expr Expr
}
// NotNullConstraint represents NOT NULL on a column.
type NotNullConstraint struct{}
// NullConstraint represents NULL on a column.
type NullConstraint struct{}
// PrimaryKeyConstraint represents NULL on a column.
type PrimaryKeyConstraint struct{}
// UniqueConstraint represents UNIQUE on a column.
type UniqueConstraint struct{}
// ColumnCheckConstraint represents either a check on a column.
type ColumnCheckConstraint struct {
Expr Expr
}
// ColumnFKConstraint represents a FK-constaint on a column.
type ColumnFKConstraint struct {
Table *QualifiedName
Col Name // empty-string means use PK
}
// NameListToIndexElems converts a NameList to an IndexElemList with all
// members using the `DefaultDirection`.
func NameListToIndexElems(lst NameList) IndexElemList {
elems := make(IndexElemList, 0, len(lst))
for _, n := range lst {
elems = append(elems, IndexElem{Column: Name(n), Direction: DefaultDirection})
}
return elems
}
// IndexTableDef represents an index definition within a CREATE TABLE
// statement.
type IndexTableDef struct {
Name Name
Columns IndexElemList
Storing NameList
}
func (node *IndexTableDef) setName(name Name) {
node.Name = name
}
// Format implements the NodeFormatter interface.
func (node *IndexTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("INDEX ")
if node.Name != "" {
FormatNode(buf, f, node.Name)
buf.WriteByte(' ')
}
buf.WriteByte('(')
FormatNode(buf, f, node.Columns)
buf.WriteByte(')')
if node.Storing != nil {
buf.WriteString(" STORING (")
FormatNode(buf, f, node.Storing)
buf.WriteByte(')')
}
}
// ConstraintTableDef represents a constraint definition within a CREATE TABLE
// statement.
type ConstraintTableDef interface {
TableDef
// Placeholder function to ensure that only desired types
// (*ConstraintTableDef) conform to the ConstraintTableDef interface.
constraintTableDef()
}
func (*UniqueConstraintTableDef) constraintTableDef() {}
// UniqueConstraintTableDef represents a unique constraint within a CREATE
// TABLE statement.
type UniqueConstraintTableDef struct {
IndexTableDef
PrimaryKey bool
}
// Format implements the NodeFormatter interface.
func (node *UniqueConstraintTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
if node.Name != "" {
fmt.Fprintf(buf, "CONSTRAINT %s ", node.Name)
}
if node.PrimaryKey {
buf.WriteString("PRIMARY KEY ")
} else {
buf.WriteString("UNIQUE ")
}
buf.WriteByte('(')
FormatNode(buf, f, node.Columns)
buf.WriteByte(')')
if node.Storing != nil {
buf.WriteString(" STORING (")
FormatNode(buf, f, node.Storing)
buf.WriteByte(')')
}
}
func (*CheckConstraintTableDef) tableDef() {}
func (*CheckConstraintTableDef) constraintTableDef() {}
// CheckConstraintTableDef represents a check constraint within a CREATE
// TABLE statement.
type CheckConstraintTableDef struct {
Name Name
Expr Expr
}
func (node *CheckConstraintTableDef) setName(name Name) {
node.Name = name
}
// Format implements the NodeFormatter interface.
func (node *CheckConstraintTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
if node.Name != "" {
fmt.Fprintf(buf, "CONSTRAINT %s ", node.Name)
}
fmt.Fprintf(buf, "CHECK (")
FormatNode(buf, f, node.Expr)
buf.WriteByte(')')
}
// FamilyElem represents a column in a FAMILY constraint.
type FamilyElem struct {
Column Name
}
// Format implements the NodeFormatter interface.
func (node FamilyElem) Format(buf *bytes.Buffer, f FmtFlags) {
FormatNode(buf, f, node.Column)
}
// FamilyElemList is list of FamilyElem.
type FamilyElemList []FamilyElem
// Format pretty-prints the contained names separated by commas.
// Format implements the NodeFormatter interface.
func (l FamilyElemList) Format(buf *bytes.Buffer, f FmtFlags) {
for i, FamilyElem := range l {
if i > 0 {
buf.WriteString(", ")
}
FormatNode(buf, f, FamilyElem)
}
}
// FamilyTableDef represents a family definition within a CREATE TABLE
// statement.
type FamilyTableDef struct {
Name Name
Columns FamilyElemList
}
func (node *FamilyTableDef) setName(name Name) {
node.Name = name
}
// Format implements the NodeFormatter interface.
func (node *FamilyTableDef) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("FAMILY ")
if node.Name != "" {
FormatNode(buf, f, node.Name)
buf.WriteByte(' ')
}
buf.WriteByte('(')
FormatNode(buf, f, node.Columns)
buf.WriteByte(')')
}
// CreateTable represents a CREATE TABLE statement.
type CreateTable struct {
IfNotExists bool
Table *QualifiedName
Defs TableDefs
}
// Format implements the NodeFormatter interface.
func (node *CreateTable) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("CREATE TABLE ")
if node.IfNotExists {
buf.WriteString("IF NOT EXISTS ")
}
FormatNode(buf, f, node.Table)
buf.WriteString(" (")
FormatNode(buf, f, node.Defs)
buf.WriteByte(')')
}