-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddl.go
More file actions
395 lines (348 loc) · 13 KB
/
Copy pathddl.go
File metadata and controls
395 lines (348 loc) · 13 KB
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
package ast
// CreateTableStmt is CREATE TABLE, in both the column-list and the AS SELECT
// forms.
//
// cmd ::= create_table create_table_args.
// create_table ::= createkw temp TABLE ifnotexists nm dbnm.
// create_table_args ::= LP columnlist conslist_opt RP table_option_set.
// create_table_args ::= AS select.
type CreateTableStmt struct {
Span
Temp bool `json:"temp,omitempty"`
IfNotExists bool `json:"ifNotExists,omitempty"`
Name *QualifiedName `json:"name"`
Columns []*ColumnDef `json:"columns,omitempty"`
Constraints []*TableConstraint `json:"constraints,omitempty"`
Options []*TableOption `json:"options,omitempty"` // WITHOUT ROWID, STRICT
Select *SelectStmt `json:"select,omitempty"`
}
// TableOption is one entry of a table_option_set. SQLite rejects an unknown
// one from a grammar action rather than the grammar, so any name parses.
//
// table_option ::= WITHOUT nm. / table_option ::= nm.
type TableOption struct {
Span
Without bool `json:"without,omitempty"`
Name *Ident `json:"name"`
}
func (n *TableOption) Children() []Node { return nodes(n.Name) }
func (*CreateTableStmt) stmtNode() {}
func (n *CreateTableStmt) Children() []Node {
out := nodes(n.Name)
out = appendNodes(out, n.Columns)
out = appendNodes(out, n.Constraints)
out = appendNodes(out, n.Options)
return append(out, nodes(n.Select)...)
}
// ColumnDef is one column of a CREATE TABLE (or of ALTER TABLE ADD COLUMN).
//
// columnname ::= nm typetoken. / carglist ::= carglist ccons.
type ColumnDef struct {
Span
Name *ColumnName `json:"name"`
Type *TypeName `json:"type,omitempty"`
Constraints []*ColumnConstraint `json:"constraints,omitempty"`
}
func (n *ColumnDef) Children() []Node {
out := nodes(n.Name, n.Type)
out = appendNodes(out, n.Constraints)
return out
}
// ColumnName wraps the identifier naming a column, so that a ColumnDef's
// name keeps its own span distinct from the whole definition's.
type ColumnName = Ident
// ColumnConstraintKind enumerates the "ccons" alternatives.
type ColumnConstraintKind int
const (
ColumnNull ColumnConstraintKind = iota
ColumnNotNull
ColumnPrimaryKey
ColumnUnique
ColumnCheck
ColumnDefault
ColumnCollate
ColumnReferences
ColumnGenerated
ColumnDeferrable // a bare [NOT] DEFERRABLE clause
)
// ColumnConstraint is one column constraint. Name is the preceding
// CONSTRAINT clause, if any; SQLite attaches it to the constraint that
// follows.
//
// ccons ::= CONSTRAINT nm. / NULL onconf. / NOT NULL onconf.
// ccons ::= PRIMARY KEY sortorder onconf autoinc. / UNIQUE onconf.
// ccons ::= CHECK LP expr RP. / DEFAULT .... / COLLATE ids.
// ccons ::= REFERENCES nm eidlist_opt refargs. / defer_subclause.
// ccons ::= [GENERATED ALWAYS] AS generated.
type ColumnConstraint struct {
Span
Name *Ident `json:"name,omitempty"`
Kind ColumnConstraintKind `json:"kind"`
OnConflict ConflictAction `json:"onConflict,omitempty"`
Order SortOrder `json:"order,omitempty"` // PRIMARY KEY ASC/DESC
AutoIncrement bool `json:"autoIncrement,omitempty"`
Expr Expr `json:"expr,omitempty"` // CHECK, DEFAULT, GENERATED
Collation *Ident `json:"collation,omitempty"`
References *ForeignKeyClause `json:"references,omitempty"`
GeneratedKind *Ident `json:"generatedKind,omitempty"` // STORED / VIRTUAL
AlwaysWord bool `json:"alwaysWord,omitempty"` // GENERATED ALWAYS spelling
Deferrable *DeferClause `json:"deferrable,omitempty"`
}
func (n *ColumnConstraint) Children() []Node {
return nodes(n.Name, n.Expr, n.Collation, n.References, n.GeneratedKind, n.Deferrable)
}
// TableConstraintKind enumerates the "tcons" alternatives.
type TableConstraintKind int
const (
TablePrimaryKey TableConstraintKind = iota
TableUnique
TableCheck
TableForeignKey
)
// TableConstraint is one table-level constraint.
//
// tcons ::= CONSTRAINT nm.
// tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf. / UNIQUE LP sortlist RP onconf.
// tcons ::= CHECK LP expr RP onconf.
// tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs
// defer_subclause_opt.
type TableConstraint struct {
Span
Name *Ident `json:"name,omitempty"`
Kind TableConstraintKind `json:"kind"`
Columns []*OrderingTerm `json:"columns,omitempty"` // PRIMARY KEY / UNIQUE
AutoIncrement bool `json:"autoIncrement,omitempty"`
OnConflict ConflictAction `json:"onConflict,omitempty"`
Expr Expr `json:"expr,omitempty"` // CHECK
FKColumns []*IndexedColumn `json:"fkColumns,omitempty"`
References *ForeignKeyClause `json:"references,omitempty"`
Deferrable *DeferClause `json:"deferrable,omitempty"`
}
func (n *TableConstraint) Children() []Node {
out := nodes(n.Name)
out = appendNodes(out, n.Columns)
out = append(out, nodes(n.Expr)...)
out = appendNodes(out, n.FKColumns)
return append(out, nodes(n.References, n.Deferrable)...)
}
// IndexedColumn is one entry of an "eidlist": a column name with the COLLATE
// and ASC/DESC decorations SQLite still parses for historical schemas.
//
// eidlist ::= eidlist COMMA nm collate sortorder.
type IndexedColumn struct {
Span
Name *Ident `json:"name"`
Collation *Ident `json:"collation,omitempty"`
Order SortOrder `json:"order,omitempty"`
}
func (n *IndexedColumn) Children() []Node { return nodes(n.Name, n.Collation) }
// ForeignKeyAction is the action of an ON DELETE / ON UPDATE clause.
type ForeignKeyAction int
const (
FKNoAction ForeignKeyAction = iota
FKSetNull
FKSetDefault
FKCascade
FKRestrict
)
// ForeignKeyClause is a REFERENCES clause and its arguments.
//
// ccons ::= REFERENCES nm eidlist_opt refargs.
// refarg ::= MATCH nm. / ON INSERT refact. / ON DELETE refact. / ON UPDATE refact.
type ForeignKeyClause struct {
Span
Table *Ident `json:"table"`
Columns []*IndexedColumn `json:"columns,omitempty"`
Args []*ForeignKeyArg `json:"args,omitempty"`
}
func (n *ForeignKeyClause) Children() []Node {
out := nodes(n.Table)
out = appendNodes(out, n.Columns)
out = appendNodes(out, n.Args)
return out
}
// ForeignKeyArg is one refarg: a MATCH clause or an ON <event> action.
type ForeignKeyArg struct {
Span
Match *Ident `json:"match,omitempty"`
Event string `json:"event,omitempty"` // INSERT, DELETE, UPDATE
Action ForeignKeyAction `json:"action,omitempty"`
}
func (n *ForeignKeyArg) Children() []Node { return nodes(n.Match) }
// DeferClause is "[NOT] DEFERRABLE [INITIALLY DEFERRED|IMMEDIATE]".
//
// defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt.
// defer_subclause ::= DEFERRABLE init_deferred_pred_opt.
type DeferClause struct {
Span
Not bool `json:"not,omitempty"`
InitiallyDeferred bool `json:"initiallyDeferred,omitempty"`
HasInitially bool `json:"hasInitially,omitempty"`
}
func (n *DeferClause) Children() []Node { return nil }
// CreateIndexStmt is CREATE [UNIQUE] INDEX.
//
// cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP
// where_opt.
type CreateIndexStmt struct {
Span
Unique bool `json:"unique,omitempty"`
IfNotExists bool `json:"ifNotExists,omitempty"`
Name *QualifiedName `json:"name"`
Table *Ident `json:"table"`
Columns []*OrderingTerm `json:"columns"`
Where Expr `json:"where,omitempty"`
}
func (*CreateIndexStmt) stmtNode() {}
func (n *CreateIndexStmt) Children() []Node {
out := nodes(n.Name, n.Table)
out = appendNodes(out, n.Columns)
return append(out, nodes(n.Where)...)
}
// CreateViewStmt is CREATE [TEMP] VIEW.
//
// cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select.
type CreateViewStmt struct {
Span
Temp bool `json:"temp,omitempty"`
IfNotExists bool `json:"ifNotExists,omitempty"`
Name *QualifiedName `json:"name"`
Columns []*IndexedColumn `json:"columns,omitempty"`
Select *SelectStmt `json:"select"`
}
func (*CreateViewStmt) stmtNode() {}
func (n *CreateViewStmt) Children() []Node {
out := nodes(n.Name)
out = appendNodes(out, n.Columns)
return append(out, nodes(n.Select)...)
}
// TriggerTime is BEFORE, AFTER or INSTEAD OF.
type TriggerTime int
const (
TriggerBefore TriggerTime = iota
TriggerAfter
TriggerInsteadOf
)
// CreateTriggerStmt is CREATE [TEMP] TRIGGER.
//
// cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END.
// trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time
// trigger_event ON fullname foreach_clause when_clause.
type CreateTriggerStmt struct {
Span
Temp bool `json:"temp,omitempty"`
IfNotExists bool `json:"ifNotExists,omitempty"`
Name *QualifiedName `json:"name"`
Time TriggerTime `json:"time,omitempty"`
HasTime bool `json:"hasTime,omitempty"`
Event string `json:"event"` // DELETE, INSERT, UPDATE
UpdateOf []*Ident `json:"updateOf,omitempty"`
Table *QualifiedName `json:"table"`
ForEachRow bool `json:"forEachRow,omitempty"`
When Expr `json:"when,omitempty"`
Body []Stmt `json:"body"`
}
func (*CreateTriggerStmt) stmtNode() {}
func (n *CreateTriggerStmt) Children() []Node {
out := nodes(n.Name)
out = appendNodes(out, n.UpdateOf)
out = append(out, nodes(n.Table, n.When)...)
out = appendNodes(out, n.Body)
return out
}
// CreateVirtualTableStmt is CREATE VIRTUAL TABLE ... USING module(args).
// Module arguments are kept as their raw source text, exactly as SQLite does
// (the "anylist" production accepts any balanced token sequence).
//
// create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm.
// cmd ::= create_vtab LP vtabarglist RP.
type CreateVirtualTableStmt struct {
Span
IfNotExists bool `json:"ifNotExists,omitempty"`
Name *QualifiedName `json:"name"`
Module *Ident `json:"module"`
HasArgs bool `json:"hasArgs,omitempty"`
Args []string `json:"args,omitempty"`
}
func (*CreateVirtualTableStmt) stmtNode() {}
func (n *CreateVirtualTableStmt) Children() []Node {
return nodes(n.Name, n.Module)
}
// AlterAction enumerates the ALTER TABLE forms of the pinned build.
type AlterAction int
const (
AlterRenameTable AlterAction = iota
AlterRenameColumn
AlterAddColumn
AlterDropColumn
AlterDropConstraint
AlterDropNotNull
AlterSetNotNull
AlterAddConstraintCheck
)
// AlterTableStmt is ALTER TABLE in all the forms the pinned build accepts.
//
// cmd ::= ALTER TABLE fullname RENAME TO nm.
// cmd ::= alter_add carglist. (ALTER TABLE ... ADD [COLUMN] nm typetoken)
// cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm.
// cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm.
// cmd ::= ALTER TABLE fullname DROP CONSTRAINT nm.
// cmd ::= ALTER TABLE fullname ALTER kwcolumn_opt nm DROP NOT NULL.
// cmd ::= ALTER TABLE fullname ALTER kwcolumn_opt nm SET NOT NULL onconf.
// cmd ::= ALTER TABLE fullname ADD [CONSTRAINT nm] CHECK LP expr RP onconf.
type AlterTableStmt struct {
Span
Table *QualifiedName `json:"table"`
Action AlterAction `json:"action"`
ColumnKeyword bool `json:"columnKeyword,omitempty"`
Column *Ident `json:"column,omitempty"`
NewName *Ident `json:"newName,omitempty"`
ConstraintName *Ident `json:"constraintName,omitempty"`
ColumnDef *ColumnDef `json:"columnDef,omitempty"`
Expr Expr `json:"expr,omitempty"`
OnConflict ConflictAction `json:"onConflict,omitempty"`
}
func (*AlterTableStmt) stmtNode() {}
func (n *AlterTableStmt) Children() []Node {
return nodes(n.Table, n.Column, n.NewName, n.ConstraintName, n.ColumnDef, n.Expr)
}
// DropTableStmt is DROP TABLE.
//
// cmd ::= DROP TABLE ifexists fullname.
type DropTableStmt struct {
Span
IfExists bool `json:"ifExists,omitempty"`
Name *QualifiedName `json:"name"`
}
func (*DropTableStmt) stmtNode() {}
func (n *DropTableStmt) Children() []Node { return nodes(n.Name) }
// DropViewStmt is DROP VIEW.
//
// cmd ::= DROP VIEW ifexists fullname.
type DropViewStmt struct {
Span
IfExists bool `json:"ifExists,omitempty"`
Name *QualifiedName `json:"name"`
}
func (*DropViewStmt) stmtNode() {}
func (n *DropViewStmt) Children() []Node { return nodes(n.Name) }
// DropIndexStmt is DROP INDEX.
//
// cmd ::= DROP INDEX ifexists fullname.
type DropIndexStmt struct {
Span
IfExists bool `json:"ifExists,omitempty"`
Name *QualifiedName `json:"name"`
}
func (*DropIndexStmt) stmtNode() {}
func (n *DropIndexStmt) Children() []Node { return nodes(n.Name) }
// DropTriggerStmt is DROP TRIGGER.
//
// cmd ::= DROP TRIGGER ifexists fullname.
type DropTriggerStmt struct {
Span
IfExists bool `json:"ifExists,omitempty"`
Name *QualifiedName `json:"name"`
}
func (*DropTriggerStmt) stmtNode() {}
func (n *DropTriggerStmt) Children() []Node { return nodes(n.Name) }