forked from dolthub/go-mysql-server
/
show_create_table.go
388 lines (329 loc) · 10.7 KB
/
show_create_table.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
// Copyright 2020-2021 Dolthub, Inc.
//
// 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.
package plan
import (
"fmt"
"io"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/transform"
"github.com/gabereiser/go-mysql-server/sql/types"
)
var ErrNotView = errors.NewKind("'%' is not VIEW")
// ShowCreateTable is a node that shows the CREATE TABLE statement for a table.
type ShowCreateTable struct {
*UnaryNode
IsView bool
Indexes []sql.Index
Checks sql.CheckConstraints
targetSchema sql.Schema
primaryKeySchema sql.PrimaryKeySchema
asOf sql.Expression
}
var _ sql.Node = (*ShowCreateTable)(nil)
var _ sql.Expressioner = (*ShowCreateTable)(nil)
var _ sql.SchemaTarget = (*ShowCreateTable)(nil)
var _ Versionable = (*ShowCreateTable)(nil)
// NewShowCreateTable creates a new ShowCreateTable node.
func NewShowCreateTable(table sql.Node, isView bool) *ShowCreateTable {
return NewShowCreateTableWithAsOf(table, isView, nil)
}
// NewShowCreateTableWithAsOf creates a new ShowCreateTable node for a specific version of a table.
func NewShowCreateTableWithAsOf(table sql.Node, isView bool, asOf sql.Expression) *ShowCreateTable {
return &ShowCreateTable{
UnaryNode: &UnaryNode{table},
IsView: isView,
asOf: asOf,
}
}
// Resolved implements the Resolvable interface.
func (sc *ShowCreateTable) Resolved() bool {
if !sc.Child.Resolved() {
return false
}
for _, col := range sc.targetSchema {
if !col.Default.Resolved() {
return false
}
}
return true
}
func (sc ShowCreateTable) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(1, len(children))
}
child := children[0]
switch child.(type) {
case *SubqueryAlias, *ResolvedTable, sql.UnresolvedTable:
default:
return nil, sql.ErrInvalidChildType.New(sc, child, (*SubqueryAlias)(nil))
}
sc.Child = child
return &sc, nil
}
// CheckPrivileges implements the interface sql.Node.
func (sc *ShowCreateTable) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
// The table won't be visible during the resolution step if the user doesn't have the correct privileges
return true
}
func (sc ShowCreateTable) WithTargetSchema(schema sql.Schema) (sql.Node, error) {
sc.targetSchema = schema
return &sc, nil
}
func (sc *ShowCreateTable) TargetSchema() sql.Schema {
return sc.targetSchema
}
func (sc ShowCreateTable) WithPrimaryKeySchema(schema sql.PrimaryKeySchema) (sql.Node, error) {
sc.primaryKeySchema = schema
return &sc, nil
}
func (sc *ShowCreateTable) Expressions() []sql.Expression {
return transform.WrappedColumnDefaults(sc.targetSchema)
}
func (sc ShowCreateTable) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
if len(exprs) != len(sc.targetSchema) {
return nil, sql.ErrInvalidChildrenNumber.New(sc, len(exprs), len(sc.targetSchema))
}
sc.targetSchema = transform.SchemaWithDefaults(sc.targetSchema, exprs)
return &sc, nil
}
// Schema implements the Node interface.
func (sc *ShowCreateTable) Schema() sql.Schema {
switch sc.Child.(type) {
case *SubqueryAlias:
return sql.Schema{
&sql.Column{Name: "View", Type: types.LongText, Nullable: false},
&sql.Column{Name: "Create View", Type: types.LongText, Nullable: false},
&sql.Column{Name: "character_set_client", Type: types.LongText, Nullable: false},
&sql.Column{Name: "collation_connection", Type: types.LongText, Nullable: false},
}
case *ResolvedTable, sql.UnresolvedTable:
return sql.Schema{
&sql.Column{Name: "Table", Type: types.LongText, Nullable: false},
&sql.Column{Name: "Create Table", Type: types.LongText, Nullable: false},
}
default:
panic(fmt.Sprintf("unexpected type %T", sc.Child))
}
}
// GetTargetSchema returns the final resolved target schema of show create table.
func (sc *ShowCreateTable) GetTargetSchema() sql.Schema {
return sc.targetSchema
}
// WithAsOf implements the Versionable interface.
func (sc *ShowCreateTable) WithAsOf(asOf sql.Expression) (sql.Node, error) {
nsc := *sc
nsc.asOf = asOf
return &nsc, nil
}
// AsOf implements the Versionable interface.
func (sc *ShowCreateTable) AsOf() sql.Expression {
return sc.asOf
}
// RowIter implements the Node interface
func (sc *ShowCreateTable) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return &showCreateTablesIter{
table: sc.Child,
isView: sc.IsView,
indexes: sc.Indexes,
checks: sc.Checks,
schema: sc.targetSchema,
pkSchema: sc.primaryKeySchema,
}, nil
}
// String implements the fmt.Stringer interface.
func (sc *ShowCreateTable) String() string {
t := "TABLE"
if sc.IsView {
t = "VIEW"
}
name := ""
if nameable, ok := sc.Child.(sql.Nameable); ok {
name = nameable.Name()
}
asOfClause := ""
if sc.asOf != nil {
asOfClause = fmt.Sprintf("as of %v", sc.asOf)
}
return fmt.Sprintf("SHOW CREATE %s %s %s", t, name, asOfClause)
}
type showCreateTablesIter struct {
table sql.Node
schema sql.Schema
didIteration bool
isView bool
indexes []sql.Index
checks sql.CheckConstraints
pkSchema sql.PrimaryKeySchema
}
func (i *showCreateTablesIter) Next(ctx *sql.Context) (sql.Row, error) {
if i.didIteration {
return nil, io.EOF
}
i.didIteration = true
var row sql.Row
switch table := i.table.(type) {
case *ResolvedTable:
// MySQL behavior is to allow show create table for views, but not show create view for tables.
if i.isView {
return nil, ErrNotView.New(table.Name())
}
composedCreateTableStatement, err := i.produceCreateTableStatement(ctx, table.Table, i.schema, i.pkSchema)
if err != nil {
return nil, err
}
row = sql.NewRow(
table.Name(), // "Table" string
composedCreateTableStatement, // "Create Table" string
)
case *SubqueryAlias:
characterSetClient, err := ctx.GetSessionVariable(ctx, "character_set_client")
if err != nil {
return nil, err
}
collationConnection, err := ctx.GetSessionVariable(ctx, "collation_connection")
if err != nil {
return nil, err
}
row = sql.NewRow(
table.Name(), // "View" string
produceCreateViewStatement(table), // "Create View" string
characterSetClient,
collationConnection,
)
default:
panic(fmt.Sprintf("unexpected type %T", i.table))
}
return row, nil
}
type NameAndSchema interface {
sql.Nameable
Schema() sql.Schema
}
func (i *showCreateTablesIter) produceCreateTableStatement(ctx *sql.Context, table sql.Table, schema sql.Schema, pkSchema sql.PrimaryKeySchema) (string, error) {
colStmts := make([]string, len(schema))
var primaryKeyCols []string
var pkOrdinals []int
if len(pkSchema.Schema) > 0 {
pkOrdinals = pkSchema.PkOrdinals
}
// Statement creation parts for each column
for i, col := range schema {
var colDefault string
// TODO: The columns that are rendered in defaults should be backticked
if col.Default != nil {
// TODO : string literals should have character set introducer
colDefault = col.Default.String()
if colDefault != "NULL" && col.Default.IsLiteral() && !types.IsTime(col.Default.Type()) && !types.IsText(col.Default.Type()) {
v, err := col.Default.Eval(ctx, nil)
if err != nil {
return "", err
}
colDefault = fmt.Sprintf("'%v'", v)
}
}
if col.PrimaryKey && len(pkSchema.Schema) == 0 {
pkOrdinals = append(pkOrdinals, i)
}
colStmts[i] = sql.GenerateCreateTableColumnDefinition(col.Name, col.Type, col.Nullable, col.AutoIncrement, col.Default != nil, colDefault, col.Comment)
}
for _, i := range pkOrdinals {
primaryKeyCols = append(primaryKeyCols, schema[i].Name)
}
if len(primaryKeyCols) > 0 {
colStmts = append(colStmts, sql.GenerateCreateTablePrimaryKeyDefinition(primaryKeyCols))
}
for _, index := range i.indexes {
// The primary key may or may not be declared as an index by the table. Don't print it twice if it's here.
if isPrimaryKeyIndex(index, table) {
continue
}
prefixLengths := index.PrefixLengths()
var indexCols []string
for i, expr := range index.Expressions() {
col := GetColumnFromIndexExpr(expr, table)
if col != nil {
indexDef := sql.QuoteIdentifier(col.Name)
if len(prefixLengths) > i && prefixLengths[i] != 0 {
indexDef += fmt.Sprintf("(%v)", prefixLengths[i])
}
indexCols = append(indexCols, indexDef)
}
}
colStmts = append(colStmts, sql.GenerateCreateTableIndexDefinition(index.IsUnique(), index.IsSpatial(), index.ID(), indexCols, index.Comment()))
}
fkt, err := getForeignKeyTable(table)
if err == nil && fkt != nil {
fks, err := fkt.GetDeclaredForeignKeys(ctx)
if err != nil {
return "", err
}
for _, fk := range fks {
onDelete := ""
if len(fk.OnDelete) > 0 && fk.OnDelete != sql.ForeignKeyReferentialAction_DefaultAction {
onDelete = string(fk.OnDelete)
}
onUpdate := ""
if len(fk.OnUpdate) > 0 && fk.OnUpdate != sql.ForeignKeyReferentialAction_DefaultAction {
onUpdate = string(fk.OnUpdate)
}
colStmts = append(colStmts, sql.GenerateCreateTableForiegnKeyDefinition(fk.Name, fk.Columns, fk.ParentTable, fk.ParentColumns, onDelete, onUpdate))
}
}
if i.checks != nil {
for _, check := range i.checks {
colStmts = append(colStmts, sql.GenerateCreateTableCheckConstraintClause(check.Name, check.Expr.String(), check.Enforced))
}
}
return sql.GenerateCreateTableStatement(table.Name(), colStmts, table.Collation().CharacterSet().Name(), table.Collation().Name()), nil
}
// isPrimaryKeyIndex returns whether the index given matches the table's primary key columns. Order is not considered.
func isPrimaryKeyIndex(index sql.Index, table sql.Table) bool {
var pks []*sql.Column
for _, col := range table.Schema() {
if col.PrimaryKey {
pks = append(pks, col)
}
}
if len(index.Expressions()) != len(pks) {
return false
}
for _, expr := range index.Expressions() {
if col := GetColumnFromIndexExpr(expr, table); col != nil {
found := false
for _, pk := range pks {
if col == pk {
found = true
break
}
}
if !found {
return false
}
} else {
return false
}
}
return true
}
func produceCreateViewStatement(view *SubqueryAlias) string {
return fmt.Sprintf(
"CREATE VIEW `%s` AS %s",
view.Name(),
view.TextDefinition,
)
}
func (i *showCreateTablesIter) Close(*sql.Context) error {
return nil
}