Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor,planner: support show create view #9309

Merged
merged 6 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ var (
ErrCoalesceOnlyOnHashPartition = terror.ClassDDL.New(codeCoalesceOnlyOnHashPartition, mysql.MySQLErrName[mysql.ErrCoalesceOnlyOnHashPartition])
// ErrViewWrongList returns create view must include all columns in the select clause
ErrViewWrongList = terror.ClassDDL.New(codeViewWrongList, mysql.MySQLErrName[mysql.ErrViewWrongList])
// ErrTableIsNotView returns for table is not view.
ErrTableIsNotView = terror.ClassDDL.New(codeErrWrongObject, "'%s.%s' is not VIEW")
// ErrTableIsNotBaseTable returns for table is not base table.
ErrTableIsNotBaseTable = terror.ClassDDL.New(codeErrWrongObject, "'%s.%s' is not BASE TABLE")
// ErrWrongObject returns for wrong object.
ErrWrongObject = terror.ClassDDL.New(codeErrWrongObject, mysql.MySQLErrName[mysql.ErrWrongObject])
)

// DDL is responsible for updating schema in data store and maintaining in-memory InfoSchema cache.
Expand Down
6 changes: 3 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ func (d *ddl) CreateView(ctx sessionctx.Context, s *ast.CreateViewStmt) (err err
var oldViewTblID int64
if oldView != nil {
if !oldView.Meta().IsView() {
return ErrTableIsNotView.GenWithStackByArgs(ident.Schema, ident.Name)
return ErrWrongObject.GenWithStackByArgs(ident.Schema, ident.Name, "VIEW")
}
oldViewTblID = oldView.Meta().ID
}
Expand Down Expand Up @@ -1442,7 +1442,7 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A

is := d.infoHandle.Get()
if is.TableIsView(ident.Schema, ident.Name) {
return ErrTableIsNotBaseTable.GenWithStackByArgs(ident.Schema, ident.Name)
return ErrWrongObject.GenWithStackByArgs(ident.Schema, ident.Name, "BASE TABLE")
}

for _, spec := range validSpecs {
Expand Down Expand Up @@ -2406,7 +2406,7 @@ func (d *ddl) DropView(ctx sessionctx.Context, ti ast.Ident) (err error) {
}

if !tb.Meta().IsView() {
return ErrTableIsNotView.GenWithStackByArgs(ti.Schema, ti.Name)
return ErrWrongObject.GenWithStackByArgs(ti.Schema, ti.Name, "VIEW")
}

job := &model.Job{
Expand Down
6 changes: 3 additions & 3 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *testSuite3) TestCreateView(c *C) {
tk.MustExec("create or replace view v1 (c,d) as select a,b from t1 ")
tk.MustExec("create table if not exists t1 (a int ,b int)")
_, err = tk.Exec("create or replace view t1 as select * from t1")
c.Assert(err.Error(), Equals, ddl.ErrTableIsNotView.GenWithStackByArgs("test", "t1").Error())
c.Assert(err.Error(), Equals, ddl.ErrWrongObject.GenWithStackByArgs("test", "t1", "VIEW").Error())
}

func (s *testSuite3) TestCreateDropDatabase(c *C) {
Expand Down Expand Up @@ -264,7 +264,7 @@ func (s *testSuite3) TestAlterTableAddColumn(c *C) {
tk.MustQuery("select c3 from alter_test").Check(testkit.Rows("CURRENT_TIMESTAMP"))
tk.MustExec("create or replace view alter_view as select c1,c2 from alter_test")
_, err = tk.Exec("alter table alter_view add column c4 varchar(50)")
c.Assert(err.Error(), Equals, ddl.ErrTableIsNotBaseTable.GenWithStackByArgs("test", "alter_view").Error())
c.Assert(err.Error(), Equals, ddl.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error())
tk.MustExec("drop view alter_view")
}

Expand Down Expand Up @@ -312,7 +312,7 @@ func (s *testSuite3) TestAlterTableModifyColumn(c *C) {
c.Assert(createSQL, Equals, expected)
tk.MustExec("create or replace view alter_view as select c1,c2 from mc")
_, err = tk.Exec("alter table alter_view modify column c2 text")
c.Assert(err.Error(), Equals, ddl.ErrTableIsNotBaseTable.GenWithStackByArgs("test", "alter_view").Error())
c.Assert(err.Error(), Equals, ddl.ErrWrongObject.GenWithStackByArgs("test", "alter_view", "BASE TABLE").Error())
tk.MustExec("drop view alter_view")
}

Expand Down
2 changes: 2 additions & 0 deletions executor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
ErrDBaccessDenied = terror.ClassExecutor.New(mysql.ErrDBaccessDenied, mysql.MySQLErrName[mysql.ErrDBaccessDenied])
ErrTableaccessDenied = terror.ClassExecutor.New(mysql.ErrTableaccessDenied, mysql.MySQLErrName[mysql.ErrTableaccessDenied])
ErrBadDB = terror.ClassExecutor.New(mysql.ErrBadDB, mysql.MySQLErrName[mysql.ErrBadDB])
ErrWrongObject = terror.ClassExecutor.New(mysql.ErrWrongObject, mysql.MySQLErrName[mysql.ErrWrongObject])
)

func init() {
Expand All @@ -63,6 +64,7 @@ func init() {
mysql.ErrDBaccessDenied: mysql.ErrDBaccessDenied,
mysql.ErrTableaccessDenied: mysql.ErrTableaccessDenied,
mysql.ErrBadDB: mysql.ErrBadDB,
mysql.ErrWrongObject: mysql.ErrWrongObject,
}
terror.ErrClassToMySQLCodes[terror.ClassExecutor] = tableMySQLErrCodes
}
23 changes: 23 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func (e *ShowExec) fetchAll() error {
return e.fetchShowColumns()
case ast.ShowCreateTable:
return e.fetchShowCreateTable()
case ast.ShowCreateView:
return e.fetchShowCreateView()
case ast.ShowCreateDatabase:
return e.fetchShowCreateDatabase()
case ast.ShowDatabases:
Expand Down Expand Up @@ -764,6 +766,27 @@ func (e *ShowExec) fetchShowCreateTable() error {
return nil
}

func (e *ShowExec) fetchShowCreateView() error {
db, ok := e.is.SchemaByName(e.DBName)
if !ok {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(e.DBName.O)
}

tb, err := e.getTable()
if err != nil {
return errors.Trace(err)
}

if !tb.Meta().IsView() {
return ErrWrongObject.GenWithStackByArgs(db.Name.O, tb.Meta().Name.O, "VIEW")
}

var buf bytes.Buffer
e.fetchShowCreateTable4View(tb.Meta(), &buf)
e.appendRow([]interface{}{tb.Meta().Name.O, buf.String(), tb.Meta().Charset, tb.Meta().Collate})
return nil
}

func (e *ShowExec) fetchShowCreateTable4View(tb *model.TableInfo, buf *bytes.Buffer) {
sqlMode := e.ctx.GetSessionVars().SQLMode

Expand Down
1 change: 1 addition & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func (s *testSuite2) TestShowCreateTable(c *C) {
tk.MustExec("drop view if exists v1")
tk.MustExec("create or replace definer=`root`@`127.0.0.1` view v1 as select * from t1")
tk.MustQuery("show create table v1").Check(testutil.RowsWithSep("|", "v1|CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v1` (`a`, `b`) AS select * from t1 "))
tk.MustQuery("show create view v1").Check(testutil.RowsWithSep("|", "v1|CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v1` (`a`, `b`) AS select * from t1 "))

tk.MustExec("drop view v1")
tk.MustExec("drop table t1")
Expand Down
5 changes: 5 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,9 @@ func (b *PlanBuilder) buildShow(show *ast.ShowStmt) (Plan, error) {
if table, err := b.is.TableByName(show.Table.Schema, show.Table.Name); err == nil {
isView = table.Meta().IsView()
}
case ast.ShowCreateView:
err := ErrSpecificAccessDenied.GenWithStackByArgs("SHOW VIEW")
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ShowViewPriv, "", "", "", err)
}
p.SetSchema(buildShowSchema(show, isView))
}
Expand Down Expand Up @@ -1744,6 +1747,8 @@ func buildShowSchema(s *ast.ShowStmt, isView bool) (schema *expression.Schema) {
} else {
names = []string{"View", "Create View", "character_set_client", "collation_connection"}
}
case ast.ShowCreateView:
names = []string{"View", "Create View", "character_set_client", "collation_connection"}
case ast.ShowCreateDatabase:
names = []string{"Database", "Create Database"}
case ast.ShowGrants:
Expand Down