Skip to content

Commit

Permalink
plans: Add test case for show variables
Browse files Browse the repository at this point in the history
Add test case for show variables
  • Loading branch information
shenli committed Sep 11, 2015
1 parent 20cf24f commit 92f9d3b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 10 deletions.
21 changes: 14 additions & 7 deletions plan/plans/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ func (s *ShowPlan) isColOK(c *column.Col) bool {

// Do implements plan.Plan Do interface.
func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
is := sessionctx.GetDomain(ctx).InfoSchema()
dbName := model.NewCIStr(s.DBName)
switch s.Target {
case stmt.ShowEngines:
f(0, []interface{}{"InnoDB", "DEFAULT", "Supports transactions, row-level locking, and foreign keys", "YES", "YES", "YES"})
Expand All @@ -81,7 +79,8 @@ func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
f(0, []interface{}{d})
}
case stmt.ShowTables:

is := sessionctx.GetDomain(ctx).InfoSchema()
dbName := model.NewCIStr(s.DBName)
if !is.SchemaExists(dbName) {
return errors.Errorf("Can not find DB: %s", dbName)
}
Expand All @@ -98,6 +97,8 @@ func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
f(0, []interface{}{v})
}
case stmt.ShowColumns:
is := sessionctx.GetDomain(ctx).InfoSchema()
dbName := model.NewCIStr(s.DBName)
if !is.SchemaExists(dbName) {
return errors.Errorf("Can not find DB: %s", dbName)
}
Expand Down Expand Up @@ -150,10 +151,8 @@ func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
f(0, row)
}
case stmt.ShowVariables:
sessionVars := variable.GetSessionVars(ctx)
for _, v := range variable.SysVars {
if s.GlobalScope != (v.Scope == variable.ScopeGlobal) {
continue
}
if s.Pattern != nil {
p, ok := s.Pattern.(*expressions.PatternLike)
if !ok {
Expand All @@ -172,7 +171,15 @@ func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
continue
}
}
row := []interface{}{v.Name, v.Value}
value := v.Value
if !s.GlobalScope {
// Try to get Session Scope variable value
sv, ok := sessionVars.Systems[v.Name]
if ok {
value = sv
}
}
row := []interface{}{v.Name, value}
f(0, row)
}
}
Expand Down
101 changes: 100 additions & 1 deletion plan/plans/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,103 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package plans
package plans_test

import (
"fmt"

. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/expression/expressions"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/plan/plans"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/stmt"
)

type testShowSuit struct {
txn kv.Transaction
vars map[string]interface{}
}

// implement Context interface
func (p *testShowSuit) GetTxn(forceNew bool) (kv.Transaction, error) { return p.txn, nil }

func (p *testShowSuit) FinishTxn(rollback bool) error { return nil }

// SetValue saves a value associated with this context for key
func (p *testShowSuit) SetValue(key fmt.Stringer, value interface{}) {
p.vars[key.String()] = value
}

// Value returns the value associated with this context for key
func (p *testShowSuit) Value(key fmt.Stringer) interface{} {
return p.vars[key.String()]
}

// ClearValue clears the value associated with this context for key
func (p *testShowSuit) ClearValue(key fmt.Stringer) {}

var _ = Suite(&testShowSuit{})

func (p *testShowSuit) SetUpSuite(c *C) {
var err error
store, err = tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
p.vars = map[string]interface{}{}
p.txn, _ = store.Begin()
variable.BindSessionVars(p)
}

func (p *testShowSuit) TestShowVariables(c *C) {
pln := &plans.ShowPlan{
Target: stmt.ShowVariables,
GlobalScope: true,
Pattern: &expressions.PatternLike{
Pattern: &expressions.Value{
Val: "character_set_results",
},
},
}
fls := pln.GetFields()
c.Assert(fls, HasLen, 2)
c.Assert(fls[0].Name, Equals, "Variable_name")
c.Assert(fls[1].Name, Equals, "Value")

sessionVars := variable.GetSessionVars(p)
ret := map[string]string{}
pln.Do(p, func(id interface{}, data []interface{}) (bool, error) {
ret[data[0].(string)] = data[1].(string)
return true, nil
})
c.Assert(ret, HasLen, 1)
v, ok := ret["character_set_results"]
c.Assert(ok, IsTrue)
c.Assert(v, Equals, "latin1")
// Set session variable to utf8
sessionVars.Systems["character_set_results"] = "utf8"
pln.Do(p, func(id interface{}, data []interface{}) (bool, error) {
ret[data[0].(string)] = data[1].(string)
return true, nil
})
c.Assert(ret, HasLen, 1)
v, ok = ret["character_set_results"]
c.Assert(ok, IsTrue)
// Show global varibale get latin1
c.Assert(v, Equals, "latin1")

pln.GlobalScope = false
pln.Do(p, func(id interface{}, data []interface{}) (bool, error) {
ret[data[0].(string)] = data[1].(string)
return true, nil
})
c.Assert(ret, HasLen, 1)
v, ok = ret["character_set_results"]
c.Assert(ok, IsTrue)
// Show session varibale get utf8
c.Assert(v, Equals, "utf8")
}

func (p *testShowSuit) TearDownSuite(c *C) {
p.txn.Commit()
}
5 changes: 3 additions & 2 deletions stmt/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ const (
)

const (
NoneScope = iota
SessionScope
// SessionScope shows varibales in session scope.
SessionScope = iota
// GlobalScope shows varibales in global scope.
GlobalScope
)

Expand Down
7 changes: 7 additions & 0 deletions stmt/stmts/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ func (s *testStmtSuite) TestShow(c *C) {
mf := newMockFormatter()
testStmt.Explain(nil, mf)
c.Assert(mf.Len(), Greater, 0)

testSQL = "SHOW VARIABLES LIKE 'character_set_results';"
stmtList, err = tidb.Compile(testSQL)
c.Assert(err, IsNil)
c.Assert(stmtList, HasLen, 1)
testStmt, ok = stmtList[0].(*stmts.ShowStmt)
c.Assert(ok, IsTrue)
}

0 comments on commit 92f9d3b

Please sign in to comment.