diff --git a/executor/builder.go b/executor/builder.go index 27ba1b9360c16..e85d27bc1874a 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -502,6 +502,7 @@ func (b *executorBuilder) buildShow(v *plannercore.Show) Executor { Table: v.Table, Column: v.Column, User: v.User, + IfNotExists: v.IfNotExists, Flag: v.Flag, Full: v.Full, GlobalScope: v.GlobalScope, diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index 1e0e2b3b9a002..693947c7dabb9 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -333,7 +333,11 @@ func (s *seqTestSuite) TestShow(c *C) { tk.MustExec(testSQL) testSQL = "show create database show_test_DB;" tk.MustQuery(testSQL).Check(testutil.RowsWithSep("|", - "show_test_DB|CREATE DATABASE `show_test_DB` /* !40100 DEFAULT CHARACTER SET utf8mb4 */", + "show_test_DB|CREATE DATABASE `show_test_DB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + )) + testSQL = "show create database if not exists show_test_DB;" + tk.MustQuery(testSQL).Check(testutil.RowsWithSep("|", + "show_test_DB|CREATE DATABASE /*!32312 IF NOT EXISTS*/ `show_test_DB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", )) tk.MustExec("use show_test_DB") diff --git a/executor/show.go b/executor/show.go index a94172e16a3c5..043971b9fd326 100644 --- a/executor/show.go +++ b/executor/show.go @@ -46,13 +46,14 @@ import ( type ShowExec struct { baseExecutor - Tp ast.ShowStmtType // Databases/Tables/Columns/.... - DBName model.CIStr - Table *ast.TableName // Used for showing columns. - Column *ast.ColumnName // Used for `desc table column`. - Flag int // Some flag parsed from sql, such as FULL. - Full bool - User *auth.UserIdentity // Used for show grants. + Tp ast.ShowStmtType // Databases/Tables/Columns/.... + DBName model.CIStr + Table *ast.TableName // Used for showing columns. + Column *ast.ColumnName // Used for `desc table column`. + Flag int // Some flag parsed from sql, such as FULL. + Full bool + User *auth.UserIdentity // Used for show grants. + IfNotExists bool // Used for `show create database if not exists` // GlobalScope is used by show variables GlobalScope bool @@ -764,9 +765,13 @@ func (e *ShowExec) fetchShowCreateDatabase() error { sqlMode := e.ctx.GetSessionVars().SQLMode var buf bytes.Buffer - fmt.Fprintf(&buf, "CREATE DATABASE %s", escape(db.Name, sqlMode)) + var ifNotExists string + if e.IfNotExists { + ifNotExists = "/*!32312 IF NOT EXISTS*/ " + } + fmt.Fprintf(&buf, "CREATE DATABASE %s%s", ifNotExists, escape(db.Name, sqlMode)) if s := db.Charset; len(s) > 0 { - fmt.Fprintf(&buf, " /* !40100 DEFAULT CHARACTER SET %s */", s) + fmt.Fprintf(&buf, " /*!40100 DEFAULT CHARACTER SET %s */", s) } e.appendRow([]interface{}{db.Name.O, buf.String()}) diff --git a/go.mod b/go.mod index aac0314e95b31..8c87769f48057 100644 --- a/go.mod +++ b/go.mod @@ -48,7 +48,7 @@ require ( github.com/pingcap/gofail v0.0.0-20181217135706-6a951c1e42c3 github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20181203065228-c14302da291c - github.com/pingcap/parser v0.0.0-20190103075927-c065c7404641 + github.com/pingcap/parser v0.0.0-20190103131433-5d5a6dd34655 github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.1-0.20181218072513-b2235d442b06+incompatible github.com/pingcap/tipb v0.0.0-20181012112600-11e33c750323 diff --git a/go.sum b/go.sum index 9fc08d73e161b..49785cec81608 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,8 @@ github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e h1:P73/4dPCL96rG github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw= github.com/pingcap/kvproto v0.0.0-20181203065228-c14302da291c h1:Qf5St5XGwKgKQLar9lEXoeO0hJMVaFBj3JqvFguWtVg= github.com/pingcap/kvproto v0.0.0-20181203065228-c14302da291c/go.mod h1:Ja9XPjot9q4/3JyCZodnWDGNXt4pKemhIYCvVJM7P24= -github.com/pingcap/parser v0.0.0-20190103075927-c065c7404641 h1:KTGU8kr2wY+FRiHHs8I5lp385b+OzYnbOr3/tPVw7mU= -github.com/pingcap/parser v0.0.0-20190103075927-c065c7404641/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v0.0.0-20190103131433-5d5a6dd34655 h1:nAGxJRuzO6T7mL/uAaOOZSVl9x+Vmy6cTYuzQjFDRw4= +github.com/pingcap/parser v0.0.0-20190103131433-5d5a6dd34655/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE= github.com/pingcap/pd v2.1.0-rc.4+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E= github.com/pingcap/tidb-tools v2.1.1-0.20181218072513-b2235d442b06+incompatible h1:Bsd+NHosPVowEGB3BCx+2d8wUQGDTXSSC5ljeNS6cXo= diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index 217c4449dafc0..efca01ffd618a 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -305,13 +305,14 @@ type Deallocate struct { type Show struct { baseSchemaProducer - Tp ast.ShowStmtType // Databases/Tables/Columns/.... - DBName string - Table *ast.TableName // Used for showing columns. - Column *ast.ColumnName // Used for `desc table column`. - Flag int // Some flag parsed from sql, such as FULL. - Full bool - User *auth.UserIdentity // Used for show grants. + Tp ast.ShowStmtType // Databases/Tables/Columns/.... + DBName string + Table *ast.TableName // Used for showing columns. + Column *ast.ColumnName // Used for `desc table column`. + Flag int // Some flag parsed from sql, such as FULL. + Full bool + User *auth.UserIdentity // Used for show grants. + IfNotExists bool // Used for `show create database if not exists` Conditions []expression.Expression diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 8d0d44ec576a3..427f557874b8f 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -914,6 +914,7 @@ func (b *PlanBuilder) buildShow(show *ast.ShowStmt) (Plan, error) { Flag: show.Flag, Full: show.Full, User: show.User, + IfNotExists: show.IfNotExists, GlobalScope: show.GlobalScope, }.Init(b.ctx) switch showTp := show.Tp; showTp {