forked from cockroachdb/cockroach
/
create.go
314 lines (276 loc) · 7.71 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
// 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"
"strings"
)
// CreateDatabase represents a CREATE DATABASE statement.
type CreateDatabase struct {
IfNotExists bool
Name Name
}
func (node *CreateDatabase) String() string {
var buf bytes.Buffer
buf.WriteString("CREATE DATABASE ")
if node.IfNotExists {
buf.WriteString("IF NOT EXISTS ")
}
buf.WriteString(node.Name.String())
return buf.String()
}
// IndexElem represents a column with a direction in a CREATE INDEX statement.
type IndexElem struct {
Column Name
Direction Direction
}
func (node IndexElem) String() string {
if node.Direction == DefaultDirection {
return node.Column.String()
}
return fmt.Sprintf("%s %s", node.Column, node.Direction)
}
// IndexElemList is list of IndexElem.
type IndexElemList []IndexElem
// String formats the contained names as a comma-separated, escaped string.
func (l IndexElemList) String() string {
colStrs := make([]string, 0, len(l))
for _, indexElem := range l {
colStrs = append(colStrs, indexElem.String())
}
return strings.Join(colStrs, ", ")
}
// 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
}
func (node *CreateIndex) String() string {
var buf bytes.Buffer
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 (%s)", node.Table, node.Columns)
if node.Storing != nil {
fmt.Fprintf(&buf, " STORING (%s)", node.Storing)
}
return buf.String()
}
// TableDef represents a column or index definition within a CREATE TABLE
// statement.
type TableDef interface {
// Placeholder function to ensure that only desired types (*TableDef) conform
// to the TableDef interface.
tableDef()
setName(name Name)
}
func (*ColumnTableDef) tableDef() {}
func (*IndexTableDef) tableDef() {}
// TableDefs represents a list of table definitions.
type TableDefs []TableDef
func (node TableDefs) String() string {
var prefix string
var buf bytes.Buffer
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// 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
}
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
default:
panic(fmt.Sprintf("unexpected column qualification: %T", c))
}
}
return d
}
func (node *ColumnTableDef) setName(name Name) {
node.Name = name
}
func (node *ColumnTableDef) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s %s", node.Name, 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 {
fmt.Fprintf(&buf, " DEFAULT %s", node.DefaultExpr)
}
return buf.String()
}
// 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() {}
// 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{}
// 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
}
func (node *IndexTableDef) String() string {
var buf bytes.Buffer
buf.WriteString("INDEX ")
if node.Name != "" {
fmt.Fprintf(&buf, "%s ", node.Name)
}
fmt.Fprintf(&buf, "(%s)", node.Columns)
if node.Storing != nil {
fmt.Fprintf(&buf, " STORING (%s)", node.Storing)
}
return buf.String()
}
// 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
}
func (node *UniqueConstraintTableDef) String() string {
var buf bytes.Buffer
if node.Name != "" {
fmt.Fprintf(&buf, "CONSTRAINT %s ", node.Name)
}
if node.PrimaryKey {
buf.WriteString("PRIMARY KEY ")
} else {
buf.WriteString("UNIQUE ")
}
fmt.Fprintf(&buf, "(%s)", node.Columns)
if node.Storing != nil {
fmt.Fprintf(&buf, " STORING (%s)", node.Storing)
}
return buf.String()
}
// CreateTable represents a CREATE TABLE statement.
type CreateTable struct {
IfNotExists bool
Table *QualifiedName
Defs TableDefs
}
func (node *CreateTable) String() string {
var buf bytes.Buffer
buf.WriteString("CREATE TABLE")
if node.IfNotExists {
buf.WriteString(" IF NOT EXISTS")
}
fmt.Fprintf(&buf, " %s (%s)", node.Table, node.Defs)
return buf.String()
}