Skip to content

Commit

Permalink
Merge pull request #6724 from planetscale/create-db-parsing
Browse files Browse the repository at this point in the history
Add if not exists support to database ddl in parsing
  • Loading branch information
harshit-gangal authored Sep 18, 2020
2 parents 9073c3e + 7950eca commit d738f85
Show file tree
Hide file tree
Showing 5 changed files with 607 additions and 634 deletions.
27 changes: 17 additions & 10 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ type (

// DBDDL represents a CREATE, DROP, or ALTER database statement.
DBDDL struct {
Action string
DBName string
IfExists bool
Collate string
Charset string
Action string
DBName string
IfExists bool
IfNotExists bool
Collate string
Charset string
}

// DDL represents a CREATE, ALTER, DROP, RENAME, TRUNCATE or ANALYZE statement.
Expand Down Expand Up @@ -359,16 +360,16 @@ type ColumnType struct {
Type string

// Generic field options.
NotNull BoolVal
Autoincrement BoolVal
NotNull bool
Autoincrement bool
Default Expr
OnUpdate Expr
Comment *Literal

// Numeric field options
Length *Literal
Unsigned BoolVal
Zerofill BoolVal
Unsigned bool
Zerofill bool
Scale *Literal

// Text field options
Expand Down Expand Up @@ -991,7 +992,13 @@ func (node *SetTransaction) Format(buf *TrackedBuffer) {
// Format formats the node.
func (node *DBDDL) Format(buf *TrackedBuffer) {
switch node.Action {
case CreateStr, AlterStr:
case CreateStr:
notExists := ""
if node.IfNotExists {
notExists = " if not exists"
}
buf.WriteString(fmt.Sprintf("%s database%s %v", node.Action, notExists, node.DBName))
case AlterStr:
buf.WriteString(fmt.Sprintf("%s database %s", node.Action, node.DBName))
case DropStr:
exists := ""
Expand Down
9 changes: 5 additions & 4 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1610,16 +1610,17 @@ var (
input: "create schema test_db",
output: "create database test_db",
}, {
input: "create database if not exists test_db",
output: "create database test_db",
input: "create database if not exists test_db",
}, {
input: "create schema if not exists test_db",
output: "create database if not exists test_db",
}, {
input: "drop database test_db",
}, {
input: "drop schema test_db",
output: "drop database test_db",
}, {
input: "drop database if exists test_db",
output: "drop database test_db",
input: "drop database if exists test_db",
}, {
input: "delete a.*, b.* from tbl_a a, tbl_b b where a.id = b.id and b.name = 'test'",
output: "delete a, b from tbl_a as a, tbl_b as b where a.id = b.id and b.name = 'test'",
Expand Down
20 changes: 0 additions & 20 deletions go/vt/sqlparser/rewriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d738f85

Please sign in to comment.