Skip to content

Commit e05ddd3

Browse files
authored
Support MySQL CREATE TABLE x LIKE y statements (#663)
1 parent 21a6cda commit e05ddd3

9 files changed

Lines changed: 149 additions & 7 deletions

File tree

internal/endtoend/testdata/create_table_like/mysql/go/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/create_table_like/mysql/go/models.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/create_table_like/mysql/go/query.sql.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* name: GetAll :many */
2+
SELECT * FROM super_users;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE TABLE users (
2+
id integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
first_name varchar(255) NOT NULL
4+
);
5+
6+
ALTER TABLE users
7+
ADD COLUMN last_name varchar(255);
8+
9+
CREATE TABLE super_users LIKE users;
10+
11+
ALTER TABLE users
12+
ADD COLUMN age integer NOT NULL;
13+
14+
ALTER TABLE users
15+
DROP COLUMN first_name;
16+
17+
ALTER TABLE super_users
18+
ADD COLUMN date_of_birth DATETIME(6);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"name": "querytest",
6+
"path": "go",
7+
"schema": "schema.sql",
8+
"queries": "query.sql",
9+
"engine": "mysql:beta"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
111111
Name: parseTableName(n.Table),
112112
IfNotExists: n.IfNotExists,
113113
}
114+
if n.ReferTable != nil {
115+
create.ReferTable = parseTableName(n.ReferTable)
116+
}
114117
for _, def := range n.Cols {
115118
var vals *ast.List
116119
if len(def.Tp.Elems) > 0 {

internal/sql/ast/create_table_stmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type CreateTableStmt struct {
44
IfNotExists bool
55
Name *TableName
66
Cols []*ColumnDef
7+
ReferTable *TableName
78
}
89

910
func (n *CreateTableStmt) Pos() int {

internal/sql/catalog/table.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,31 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
135135
} else if err == nil {
136136
return sqlerr.RelationExists(stmt.Name.Name)
137137
}
138+
138139
tbl := Table{Rel: stmt.Name}
139-
for _, col := range stmt.Cols {
140-
tbl.Columns = append(tbl.Columns, &Column{
141-
Name: col.Colname,
142-
Type: *col.TypeName,
143-
IsNotNull: col.IsNotNull,
144-
IsArray: col.IsArray,
145-
})
140+
141+
if stmt.ReferTable != nil && len(stmt.Cols) != 0 {
142+
return errors.New("create table node cannot have both a ReferTable and Cols")
143+
}
144+
145+
if stmt.ReferTable != nil {
146+
_, original, err := c.getTable(stmt.ReferTable)
147+
if err != nil {
148+
return err
149+
}
150+
for _, col := range original.Columns {
151+
newCol := *col // make a copy, so changes to the ReferTable don't propagate
152+
tbl.Columns = append(tbl.Columns, &newCol)
153+
}
154+
} else {
155+
for _, col := range stmt.Cols {
156+
tbl.Columns = append(tbl.Columns, &Column{
157+
Name: col.Colname,
158+
Type: *col.TypeName,
159+
IsNotNull: col.IsNotNull,
160+
IsArray: col.IsArray,
161+
})
162+
}
146163
}
147164
schema.Tables = append(schema.Tables, &tbl)
148165
return nil

0 commit comments

Comments
 (0)