forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
493 lines (452 loc) · 11.3 KB
/
table.go
File metadata and controls
493 lines (452 loc) · 11.3 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
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
491
492
493
package catalog
import (
"errors"
"fmt"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)
// Table describes a relational database table
//
// A database table is a collection of related data held in a table format within a database.
// It consists of columns and rows.
type Table struct {
Rel *ast.TableName
Columns []*Column
Comment string
}
func checkMissing(err error, missingOK bool) error {
var serr *sqlerr.Error
if errors.As(err, &serr) {
if serr.Err == sqlerr.NotFound && missingOK {
return nil
}
}
return err
}
func (table *Table) isExistColumn(cmd *ast.AlterTableCmd) (int, error) {
for i, c := range table.Columns {
if c.Name == *cmd.Name {
return i, nil
}
}
if !cmd.MissingOk {
return -1, sqlerr.ColumnNotFound(table.Rel.Name, *cmd.Name)
}
// Missing column is allowed
return -1, nil
}
func (c *Catalog) addColumn(table *Table, cmd *ast.AlterTableCmd) error {
for _, c := range table.Columns {
if c.Name == cmd.Def.Colname {
if !cmd.MissingOk {
return sqlerr.ColumnExists(table.Rel.Name, cmd.Def.Colname)
}
return nil
}
}
tc, err := c.defineColumn(table.Rel, cmd.Def)
if err != nil {
return err
}
table.Columns = append(table.Columns, tc)
return nil
}
func (table *Table) alterColumnType(cmd *ast.AlterTableCmd) error {
index, err := table.isExistColumn(cmd)
if err != nil {
return err
}
if index >= 0 {
table.Columns[index].Type = *cmd.Def.TypeName
table.Columns[index].IsArray = cmd.Def.IsArray
table.Columns[index].ArrayDims = cmd.Def.ArrayDims
}
return nil
}
func (c *Catalog) dropColumn(table *Table, cmd *ast.AlterTableCmd) error {
index, err := table.isExistColumn(cmd)
if err != nil {
return err
}
if index < 0 {
return nil
}
col := table.Columns[index]
if col.linkedType {
drop := &ast.DropTypeStmt{
Types: []*ast.TypeName{
&col.Type,
},
}
if err := c.dropType(drop); err != nil {
return err
}
}
table.Columns = append(table.Columns[:index], table.Columns[index+1:]...)
return nil
}
func (table *Table) dropNotNull(cmd *ast.AlterTableCmd) error {
index, err := table.isExistColumn(cmd)
if err != nil {
return err
}
if index >= 0 {
table.Columns[index].IsNotNull = false
}
return nil
}
func (table *Table) setNotNull(cmd *ast.AlterTableCmd) error {
index, err := table.isExistColumn(cmd)
if err != nil {
return err
}
if index >= 0 {
table.Columns[index].IsNotNull = true
}
return nil
}
// Column describes a set of data values of a particular type in a relational database table
//
// TODO: Should this just be ast Nodes?
type Column struct {
Name string
Type ast.TypeName
IsNotNull bool
IsUnsigned bool
IsArray bool
ArrayDims int
Comment string
Length *int
linkedType bool
}
// An interface is used to resolve a circular import between the catalog and compiler packages.
// The createView function requires access to functions in the compiler package to parse the SELECT
// statement that defines the view.
type columnGenerator interface {
OutputColumns(node ast.Node) ([]*Column, error)
}
func (c *Catalog) getTable(tableName *ast.TableName) (*Schema, *Table, error) {
schemaName := tableName.Schema
if schemaName == "" {
schemaName = c.DefaultSchema
}
var schema *Schema
for i := range c.Schemas {
if c.Schemas[i].Name == schemaName {
schema = c.Schemas[i]
break
}
}
if schema == nil {
return nil, nil, sqlerr.SchemaNotFound(schemaName)
}
table, _, err := schema.getTable(tableName)
if err != nil {
return nil, nil, err
}
return schema, table, nil
}
func isStmtImplemented(stmt *ast.AlterTableStmt) bool {
var implemented bool
for _, item := range stmt.Cmds.Items {
switch cmd := item.(type) {
case *ast.AlterTableCmd:
switch cmd.Subtype {
case ast.AT_AddColumn:
implemented = true
case ast.AT_AlterColumnType:
implemented = true
case ast.AT_DropColumn:
implemented = true
case ast.AT_DropNotNull:
implemented = true
case ast.AT_SetNotNull:
implemented = true
}
}
}
return implemented
}
func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
if !isStmtImplemented(stmt) {
return nil
}
_, table, err := c.getTable(stmt.Table)
if err != nil {
return checkMissing(err, stmt.MissingOk)
}
for _, item := range stmt.Cmds.Items {
switch cmd := item.(type) {
case *ast.AlterTableCmd:
switch cmd.Subtype {
case ast.AT_AddColumn:
if err := c.addColumn(table, cmd); err != nil {
return err
}
case ast.AT_AlterColumnType:
if err := table.alterColumnType(cmd); err != nil {
return err
}
case ast.AT_DropColumn:
if err := c.dropColumn(table, cmd); err != nil {
return err
}
case ast.AT_DropNotNull:
if err := table.dropNotNull(cmd); err != nil {
return err
}
case ast.AT_SetNotNull:
if err := table.setNotNull(cmd); err != nil {
return err
}
}
}
}
return nil
}
func (c *Catalog) alterTableSetSchema(stmt *ast.AlterTableSetSchemaStmt) error {
ns := stmt.Table.Schema
if ns == "" {
ns = c.DefaultSchema
}
oldSchema, err := c.getSchema(ns)
if err != nil {
return checkMissing(err, stmt.MissingOk)
}
tbl, idx, err := oldSchema.getTable(stmt.Table)
if err != nil {
return checkMissing(err, stmt.MissingOk)
}
tbl.Rel.Schema = *stmt.NewSchema
newSchema, err := c.getSchema(*stmt.NewSchema)
if err != nil {
return err
}
if _, _, err := newSchema.getTable(stmt.Table); err == nil {
return sqlerr.RelationExists(stmt.Table.Name)
}
oldSchema.Tables = append(oldSchema.Tables[:idx], oldSchema.Tables[idx+1:]...)
newSchema.Tables = append(newSchema.Tables, tbl)
return nil
}
func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
ns := stmt.Name.Schema
if ns == "" {
ns = c.DefaultSchema
}
schema, err := c.getSchema(ns)
if err != nil {
return err
}
_, _, err = schema.getTable(stmt.Name)
if err == nil && stmt.IfNotExists {
return nil
} else if err == nil {
return sqlerr.RelationExists(stmt.Name.Name)
}
tbl := Table{Rel: stmt.Name, Comment: stmt.Comment}
coltype := make(map[string]ast.TypeName) // used to check for duplicate column names
seen := make(map[string]bool) // used to check for duplicate column names
for _, inheritTable := range stmt.Inherits {
t, _, err := schema.getTable(inheritTable)
if err != nil {
return err
}
// check and ignore duplicate columns
for _, col := range t.Columns {
if notNull, ok := seen[col.Name]; ok {
seen[col.Name] = notNull || col.IsNotNull
if a, ok := coltype[col.Name]; ok {
if !sameType(&a, &col.Type) {
return fmt.Errorf("column %q has a type conflict", col.Name)
}
}
continue
}
seen[col.Name] = col.IsNotNull
coltype[col.Name] = col.Type
tbl.Columns = append(tbl.Columns, col)
}
}
if stmt.ReferTable != nil {
_, original, err := c.getTable(stmt.ReferTable)
if err != nil {
return err
}
for _, col := range original.Columns {
newCol := *col // make a copy, so changes to the ReferTable don't propagate
tbl.Columns = append(tbl.Columns, &newCol)
}
}
for _, col := range stmt.Cols {
if notNull, ok := seen[col.Colname]; ok {
seen[col.Colname] = notNull || col.IsNotNull
if a, ok := coltype[col.Colname]; ok {
if !sameType(&a, col.TypeName) {
return fmt.Errorf("column %q has a type conflict", col.Colname)
}
}
continue
}
tc, err := c.defineColumn(stmt.Name, col)
if err != nil {
return err
}
tbl.Columns = append(tbl.Columns, tc)
}
// If one of the merged columns was not null, mark the column as not null
for i := range tbl.Columns {
if notNull, ok := seen[tbl.Columns[i].Name]; ok {
tbl.Columns[i].IsNotNull = notNull
}
}
schema.Tables = append(schema.Tables, &tbl)
return nil
}
func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Column, error) {
tc := &Column{
Name: col.Colname,
Type: *col.TypeName,
IsNotNull: col.IsNotNull,
IsUnsigned: col.IsUnsigned,
IsArray: col.IsArray,
ArrayDims: col.ArrayDims,
Comment: col.Comment,
Length: col.Length,
}
if col.Vals != nil {
typeName := ast.TypeName{
Name: fmt.Sprintf("%s_%s", table.Name, col.Colname),
}
s := &ast.CreateEnumStmt{TypeName: &typeName, Vals: col.Vals}
if err := c.createEnum(s); err != nil {
return nil, err
}
tc.Type = typeName
tc.linkedType = true
}
return tc, nil
}
func (c *Catalog) dropTable(stmt *ast.DropTableStmt) error {
for _, name := range stmt.Tables {
ns := name.Schema
if ns == "" {
ns = c.DefaultSchema
}
schema, err := c.getSchema(ns)
if errors.Is(err, sqlerr.NotFound) && stmt.IfExists {
continue
} else if err != nil {
return err
}
tbl, idx, err := schema.getTable(name)
if errors.Is(err, sqlerr.NotFound) && stmt.IfExists {
continue
} else if err != nil {
return err
}
drop := &ast.DropTypeStmt{}
for _, col := range tbl.Columns {
if !col.linkedType {
continue
}
drop.Types = append(drop.Types, &col.Type)
}
if err := c.dropType(drop); err != nil {
return err
}
schema.Tables = append(schema.Tables[:idx], schema.Tables[idx+1:]...)
}
return nil
}
func (c *Catalog) renameColumn(stmt *ast.RenameColumnStmt) error {
_, tbl, err := c.getTable(stmt.Table)
if err != nil {
return checkMissing(err, stmt.MissingOk)
}
idx := -1
for i := range tbl.Columns {
if tbl.Columns[i].Name == stmt.Col.Name {
idx = i
}
if tbl.Columns[i].Name == *stmt.NewName {
return sqlerr.ColumnExists(tbl.Rel.Name, *stmt.NewName)
}
}
if idx == -1 {
return sqlerr.ColumnNotFound(tbl.Rel.Name, stmt.Col.Name)
}
tbl.Columns[idx].Name = *stmt.NewName
if tbl.Columns[idx].linkedType {
name := fmt.Sprintf("%s_%s", tbl.Rel.Name, *stmt.NewName)
rename := &ast.RenameTypeStmt{
Type: &tbl.Columns[idx].Type,
NewName: &name,
}
if err := c.renameType(rename); err != nil {
return err
}
}
return nil
}
func (c *Catalog) renameTable(stmt *ast.RenameTableStmt) error {
sch, tbl, err := c.getTable(stmt.Table)
if err != nil {
return checkMissing(err, stmt.MissingOk)
}
if _, _, err := sch.getTable(&ast.TableName{Name: *stmt.NewName}); err == nil {
return sqlerr.RelationExists(*stmt.NewName)
}
if stmt.NewName != nil {
tbl.Rel.Name = *stmt.NewName
}
for idx := range tbl.Columns {
if tbl.Columns[idx].linkedType {
name := fmt.Sprintf("%s_%s", *stmt.NewName, tbl.Columns[idx].Name)
rename := &ast.RenameTypeStmt{
Type: &tbl.Columns[idx].Type,
NewName: &name,
}
if err := c.renameType(rename); err != nil {
return err
}
}
}
return nil
}
func (c *Catalog) createTableAs(stmt *ast.CreateTableAsStmt, colGen columnGenerator) error {
cols, err := colGen.OutputColumns(stmt.Query)
if err != nil {
return err
}
catName := ""
if stmt.Into.Rel.Catalogname != nil {
catName = *stmt.Into.Rel.Catalogname
}
schemaName := ""
if stmt.Into.Rel.Schemaname != nil {
schemaName = *stmt.Into.Rel.Schemaname
}
tbl := Table{
Rel: &ast.TableName{
Catalog: catName,
Schema: schemaName,
Name: *stmt.Into.Rel.Relname,
},
Columns: cols,
}
ns := tbl.Rel.Schema
if ns == "" {
ns = c.DefaultSchema
}
schema, err := c.getSchema(ns)
if err != nil {
return err
}
_, _, err = schema.getTable(tbl.Rel)
if err == nil {
return sqlerr.RelationExists(tbl.Rel.Name)
}
schema.Tables = append(schema.Tables, &tbl)
return nil
}