Skip to content
Permalink
Browse files

Merge pull request #206 from pingcap/qiuyesuifeng/fix-show-stmt-bug

stmt/stmts: fix show stmt use current db bug.
  • Loading branch information
qiuyesuifeng committed Sep 20, 2015
2 parents a3d9409 + 28b0c73 commit a4df8a99d1ed676a9516f88aac9f3a4c7a1b2e67
Showing with 23 additions and 1 deletion.
  1. +11 −1 stmt/stmts/show.go
  2. +12 −0 tidb_test.go
@@ -20,6 +20,7 @@ import (
"github.com/pingcap/tidb/plan/plans"
"github.com/pingcap/tidb/rset"
"github.com/pingcap/tidb/rset/rsets"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/stmt"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util/format"
@@ -70,7 +71,7 @@ func (s *ShowStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
log.Debug("Exec Show Stmt")
r := &plans.ShowPlan{
Target: s.Target,
DBName: s.DBName,
DBName: s.getDBName(ctx),
TableName: s.TableIdent.Name.O,
ColumnName: s.ColumnName,
Flag: s.Flag,
@@ -81,3 +82,12 @@ func (s *ShowStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {

return rsets.Recordset{Ctx: ctx, Plan: r}, nil
}

func (s *ShowStmt) getDBName(ctx context.Context) string {
if len(s.DBName) > 0 {
return s.DBName
}

// if s.DBName is empty, we should use current db name if possible.
return db.GetCurrentSchema(ctx)
}
@@ -771,6 +771,18 @@ func (s *testSessionSuite) TestSubQuery(c *C) {
match(c, rows[1], 2)
}

func (s *testSessionSuite) TestShow(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)

mustExecSQL(c, se, "create table if not exists t (c int)")
r := mustExecSQL(c, se, `show columns from t`)
rows, err := r.Rows(-1, 0)
c.Assert(err, IsNil)
c.Assert(rows, HasLen, 1)
match(c, rows[0], "c", "INT", "YES", "", nil, "")
}

func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)

0 comments on commit a4df8a9

Please sign in to comment.
You can’t perform that action at this time.