Ejemplo n.º 1
0
func (s *testStmtSuite) TestSetCharsetStmt(c *C) {
	testSQL := `SET NAMES utf8;`

	stmtList, err := tidb.Compile(testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

	testStmt, ok := stmtList[0].(*stmts.SetCharsetStmt)
	c.Assert(ok, IsTrue)

	c.Assert(testStmt.IsDDL(), IsFalse)
	c.Assert(len(testStmt.OriginText()), Greater, 0)

	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	for _, v := range variable.SetNamesVariables {
		c.Assert(sessionVars.Systems[v] != "utf8", IsTrue)
	}
	_, err = testStmt.Exec(ctx)
	c.Assert(err, IsNil)
	for _, v := range variable.SetNamesVariables {
		c.Assert(sessionVars.Systems[v], Equals, "utf8")
	}
	c.Assert(sessionVars.Systems[variable.CollationConnection], Equals, "utf8_general_ci")

	mf := newMockFormatter()
	testStmt.Explain(nil, mf)
	c.Assert(mf.Len(), Greater, 0)
}
Ejemplo n.º 2
0
// CreateSession creates a new session environment.
func CreateSession(store kv.Storage) (Session, error) {
	s := &session{
		values: make(map[fmt.Stringer]interface{}),
		store:  store,
		sid:    atomic.AddInt64(&sessionID, 1),
	}
	domain, err := domap.Get(store)
	if err != nil {
		return nil, err
	}
	sessionctx.BindDomain(s, domain)

	variable.BindSessionVars(s)
	variable.GetSessionVars(s).SetStatusFlag(mysql.ServerStatusAutocommit, true)
	sessionMu.Lock()
	defer sessionMu.Unlock()

	_, ok := storeBootstrapped[store.UUID()]
	if !ok {
		bootstrap(s)
		storeBootstrapped[store.UUID()] = true
	}
	// Add auth here
	return s, nil
}
Ejemplo n.º 3
0
// CreateSession creates a new session environment.
func CreateSession(store kv.Storage) (Session, error) {
	s := &session{
		values: make(map[fmt.Stringer]interface{}),
		store:  store,
		sid:    atomic.AddInt64(&sessionID, 1),
	}
	domain, err := domap.Get(store)
	if err != nil {
		return nil, err
	}
	sessionctx.BindDomain(s, domain)

	variable.BindSessionVars(s)
	variable.GetSessionVars(s).SetStatusFlag(mysql.ServerStatusAutocommit, true)

	// session implements variable.GlobalVarAccessor. Bind it to ctx.
	variable.BindGlobalVarAccessor(s, s)

	// session implements autocommit.Checker. Bind it to ctx
	autocommit.BindAutocommitChecker(s, s)
	sessionMu.Lock()
	defer sessionMu.Unlock()

	_, ok := storeBootstrapped[store.UUID()]
	if !ok {
		s.initing = true
		bootstrap(s)
		s.initing = false
		storeBootstrapped[store.UUID()] = true
	}
	// TODO: Add auth here
	privChecker := &privileges.UserPrivileges{}
	privilege.BindPrivilegeChecker(s, privChecker)
	return s, nil
}
Ejemplo n.º 4
0
func (p *testFromSuit) SetUpSuite(c *C) {
	store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
	c.Assert(err, IsNil)
	p.vars = map[string]interface{}{}
	p.txn, _ = store.Begin()
	p.cols = []*column.Col{
		{
			ColumnInfo: model.ColumnInfo{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
			},
		},
		{
			ColumnInfo: model.ColumnInfo{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
			},
		},
	}
	p.tbl = tables.NewTable(1, "t", "test", p.cols, &simpleAllocator{})
	variable.BindSessionVars(p)

	var i int64
	for i = 0; i < 10; i++ {
		p.tbl.AddRecord(p, []interface{}{i * 10, "hello"})
	}
}
Ejemplo n.º 5
0
func (p *testShowSuit) SetUpSuite(c *C) {
	nc := mock.NewContext()
	p.ctx = nc
	variable.BindSessionVars(p.ctx)
	variable.BindGlobalVarAccessor(p.ctx, nc)
	variable.RegisterStatistics(p.ms)

	p.dbName = "testshowplan"
	p.store = newStore(c, p.dbName)
	p.txn, _ = p.store.Begin()
	se := newSession(c, p.store, p.dbName)
	p.createDBSQL = fmt.Sprintf("create database if not exists %s;", p.dbName)
	p.dropDBSQL = fmt.Sprintf("drop database if exists %s;", p.dbName)
	p.useDBSQL = fmt.Sprintf("use %s;", p.dbName)
	p.createTableSQL = `CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));`

	mustExecSQL(c, se, p.createDBSQL)
	mustExecSQL(c, se, p.useDBSQL)
	mustExecSQL(c, se, p.createTableSQL)

	p.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
	p.createUserTableSQL = tidb.CreateUserTable
	p.createDBPrivTableSQL = tidb.CreateDBPrivTable
	p.createTablePrivTableSQL = tidb.CreateTablePrivTable
	p.createColumnPrivTableSQL = tidb.CreateColumnPrivTable

	mustExecSQL(c, se, p.createSystemDBSQL)
	mustExecSQL(c, se, p.createUserTableSQL)
	mustExecSQL(c, se, p.createDBPrivTableSQL)
	mustExecSQL(c, se, p.createTablePrivTableSQL)
	mustExecSQL(c, se, p.createColumnPrivTableSQL)

}
Ejemplo n.º 6
0
func (s *testStmtSuite) SetUpTest(c *C) {
	log.SetLevelByString("error")
	s.dbName = "teststmts"
	var err error
	s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/"+s.dbName+"/"+s.dbName)
	c.Assert(err, IsNil)
	// create db
	s.createDBSql = fmt.Sprintf("create database if not exists %s;", s.dbName)
	s.dropDBSql = fmt.Sprintf("drop database if exists %s;", s.dbName)
	s.useDBSql = fmt.Sprintf("use %s;", s.dbName)
	s.createTableSql = `
    CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));
    CREATE TABLE test1(id INT NOT NULL DEFAULT 2, name varchar(255), PRIMARY KEY(id), INDEX name(name));
    CREATE TABLE test2(id INT NOT NULL DEFAULT 3, name varchar(255), PRIMARY KEY(id));`

	s.selectSql = `SELECT * from test limit 2;`
	mustExec(c, s.testDB, s.createDBSql)
	mustExec(c, s.testDB, s.useDBSql)

	s.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
	s.createUserTableSQL = tidb.CreateUserTable
	s.createDBPrivTableSQL = tidb.CreateDBPrivTable
	s.createTablePrivTableSQL = tidb.CreateTablePrivTable
	s.createColumnPrivTableSQL = tidb.CreateColumnPrivTable

	mustExec(c, s.testDB, s.createSystemDBSQL)
	mustExec(c, s.testDB, s.createUserTableSQL)
	mustExec(c, s.testDB, s.createDBPrivTableSQL)
	mustExec(c, s.testDB, s.createTablePrivTableSQL)
	mustExec(c, s.testDB, s.createColumnPrivTableSQL)

	s.ctx = mock.NewContext()
	variable.BindSessionVars(s.ctx)
}
Ejemplo n.º 7
0
func (p *testShowSuit) SetUpSuite(c *C) {
	var err error
	store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
	c.Assert(err, IsNil)
	p.ctx = mock.NewContext()
	p.txn, _ = store.Begin()
	variable.BindSessionVars(p.ctx)
}
Ejemplo n.º 8
0
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)
}
Ejemplo n.º 9
0
func (s *testEvaluatorSuite) TestCurrentUser(c *C) {
	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	sessionVars.User = "******"

	d, err := builtinCurrentUser(types.MakeDatums(), ctx)
	c.Assert(err, IsNil)
	c.Assert(d.GetString(), Equals, "root@localhost")
}
Ejemplo n.º 10
0
func (s *testEvaluatorSuite) TestConnectionID(c *C) {
	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	sessionVars.ConnectionID = uint64(1)

	d, err := builtinConnectionID(types.MakeDatums(), ctx)
	c.Assert(err, IsNil)
	c.Assert(d.GetUint64(), Equals, uint64(1))
}
Ejemplo n.º 11
0
// CreateSession creates a new session environment.
func CreateSession(store kv.Storage) (Session, error) {
	s := &session{
		values:      make(map[fmt.Stringer]interface{}),
		store:       store,
		sid:         atomic.AddInt64(&sessionID, 1),
		debugInfos:  make(map[string]interface{}),
		maxRetryCnt: 10,
		parser:      parser.New(),
	}
	domain, err := domap.Get(store)
	if err != nil {
		return nil, errors.Trace(err)
	}
	sessionctx.BindDomain(s, domain)

	variable.BindSessionVars(s)
	variable.GetSessionVars(s).SetStatusFlag(mysql.ServerStatusAutocommit, true)

	// session implements variable.GlobalVarAccessor. Bind it to ctx.
	variable.BindGlobalVarAccessor(s, s)

	// session implements autocommit.Checker. Bind it to ctx
	autocommit.BindAutocommitChecker(s, s)
	sessionMu.Lock()
	defer sessionMu.Unlock()

	ver := getStoreBootstrapVersion(store)
	if ver == notBootstrapped {
		// if no bootstrap and storage is remote, we must use a little lease time to
		// bootstrap quickly, after bootstrapped, we will reset the lease time.
		// TODO: Using a bootstap tool for doing this may be better later.
		if !localstore.IsLocalStore(store) {
			sessionctx.GetDomain(s).SetLease(chooseMinLease(100*time.Millisecond, schemaLease))
		}

		s.SetValue(context.Initing, true)
		bootstrap(s)
		s.ClearValue(context.Initing)

		if !localstore.IsLocalStore(store) {
			sessionctx.GetDomain(s).SetLease(schemaLease)
		}

		finishBootstrap(store)
	} else if ver < currentBootstrapVersion {
		s.SetValue(context.Initing, true)
		upgrade(s)
		s.ClearValue(context.Initing)
	}

	// TODO: Add auth here
	privChecker := &privileges.UserPrivileges{}
	privilege.BindPrivilegeChecker(s, privChecker)
	return s, nil
}
Ejemplo n.º 12
0
func (s *testEvaluatorSuite) TestFoundRows(c *C) {
	ctx := mock.NewContext()
	d, err := builtinFoundRows(types.MakeDatums(), ctx)
	c.Assert(err, NotNil)

	variable.BindSessionVars(ctx)

	d, err = builtinFoundRows(types.MakeDatums(), ctx)
	c.Assert(err, IsNil)
	c.Assert(d.GetUint64(), Equals, uint64(0))
}
Ejemplo n.º 13
0
func newMockResolve(node ast.Node) error {
	indices := []*model.IndexInfo{
		{
			Name: model.NewCIStr("b"),
			Columns: []*model.IndexColumn{
				{
					Name: model.NewCIStr("b"),
				},
			},
		},
		{
			Name: model.NewCIStr("c_d_e"),
			Columns: []*model.IndexColumn{
				{
					Name: model.NewCIStr("c"),
				},
				{
					Name: model.NewCIStr("d"),
				},
				{
					Name: model.NewCIStr("e"),
				},
			},
		},
	}
	pkColumn := &model.ColumnInfo{
		State: model.StatePublic,
		Name:  model.NewCIStr("a"),
	}
	col0 := &model.ColumnInfo{
		State: model.StatePublic,
		Name:  model.NewCIStr("b"),
	}
	col1 := &model.ColumnInfo{
		State: model.StatePublic,
		Name:  model.NewCIStr("c"),
	}
	col2 := &model.ColumnInfo{
		State: model.StatePublic,
		Name:  model.NewCIStr("d"),
	}
	pkColumn.Flag = mysql.PriKeyFlag
	table := &model.TableInfo{
		Columns:    []*model.ColumnInfo{pkColumn, col0, col1, col2},
		Indices:    indices,
		Name:       model.NewCIStr("t"),
		PKIsHandle: true,
	}
	is := infoschema.MockInfoSchema([]*model.TableInfo{table})
	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	return MockResolveName(node, is, "test", ctx)
}
Ejemplo n.º 14
0
func (p *testIndexSuit) SetUpSuite(c *C) {
	store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
	c.Assert(err, IsNil)
	p.store = store
	p.vars = map[string]interface{}{}
	p.txn, _ = p.store.Begin()
	p.cols = []*column.Col{
		{
			ColumnInfo: model.ColumnInfo{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
			},
		},
		{
			ColumnInfo: model.ColumnInfo{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
			},
		},
	}
	p.tbl = tables.NewTable(2, "t2", "test", p.cols, &simpleAllocator{})

	idxCol := &column.IndexedCol{
		IndexInfo: model.IndexInfo{
			Name:  model.NewCIStr("id"),
			Table: model.NewCIStr("t2"),
			Columns: []*model.IndexColumn{
				{
					Name:   model.NewCIStr("id"),
					Offset: 0,
					Length: 0,
				},
			},
			Unique:  false,
			Primary: false,
		},
		X: kv.NewKVIndex("i", "id", false),
	}
	p.tbl.AddIndex(idxCol)

	variable.BindSessionVars(p)

	var i int64
	for i = 0; i < 10; i++ {
		p.tbl.AddRecord(p, []interface{}{i * 10, "hello"})
	}
}
Ejemplo n.º 15
0
func (s *testBuiltinSuite) TestConnectionID(c *C) {
	ctx := mock.NewContext()
	m := map[interface{}]interface{}{}
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	sessionVars.ConnectionID = uint64(1)

	m[ExprEvalArgCtx] = ctx
	v, err := builtinConnectionID(nil, m)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, uint64(1))
}
Ejemplo n.º 16
0
func (s *testBuiltinSuite) TestCurrentUser(c *C) {
	ctx := mock.NewContext()
	m := map[interface{}]interface{}{}
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	sessionVars.User = "******"

	m[ExprEvalArgCtx] = ctx
	v, err := builtinCurrentUser(nil, m)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, "root@localhost")
}
Ejemplo n.º 17
0
func (s *testBuiltinSuite) TestFoundRows(c *C) {
	ctx := newMockCtx()
	m := map[interface{}]interface{}{}
	v, err := builtinFoundRows(nil, m)
	c.Assert(err, NotNil)

	variable.BindSessionVars(ctx)

	m[ExprEvalArgCtx] = ctx
	v, err = builtinFoundRows(nil, m)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, uint64(0))
}
Ejemplo n.º 18
0
// CreateSession creates a new session environment.
func CreateSession(store kv.Storage) (Session, error) {
	s := &session{
		values: make(map[fmt.Stringer]interface{}),
		store:  store,
		sid:    atomic.AddInt64(&sessionID, 1),
	}
	domain, err := domap.Get(store)
	if err != nil {
		return nil, err
	}
	sessionctx.BindDomain(s, domain)

	variable.BindSessionVars(s)
	variable.GetSessionVars(s).SetStatus(mysql.ServerStatusAutocommit)
	return s, nil
}
Ejemplo n.º 19
0
func (s *testStmtSuite) TestExplain(c *C) {
	testSQL := "explain select 1"

	stmtList, err := tidb.Compile(s.ctx, testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

	testStmt, ok := stmtList[0].(*stmts.ExplainStmt)
	c.Assert(ok, IsTrue)

	c.Assert(testStmt.IsDDL(), IsFalse)
	c.Assert(len(testStmt.OriginText()), Greater, 0)

	newTestSql := "explain " + testSQL
	newTestStmt := &stmts.ExplainStmt{S: testStmt, Text: newTestSql}

	mf := newMockFormatter()
	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	newTestStmt.Explain(ctx, mf)
	c.Assert(mf.Len(), Greater, 0)

	_, err = testStmt.Exec(ctx)
	c.Assert(err, IsNil)

	showColumnSQL := "desc t;"

	stmtList, err = tidb.Compile(s.ctx, showColumnSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

	testStmt, ok = stmtList[0].(*stmts.ExplainStmt)
	c.Assert(ok, IsTrue)

	showStmt, ok := testStmt.S.(*stmts.ShowStmt)
	c.Assert(ok, IsTrue)

	// Mock DBName for ShowStmt
	showStmt.DBName = "test"

	mf = newMockFormatter()
	testStmt.Explain(ctx, mf)
	c.Assert(mf.Len(), Greater, 0)

	_, err = testStmt.Exec(ctx)
	c.Assert(err, IsNil)
}
Ejemplo n.º 20
0
func (*testSessionSuite) TestSession(c *C) {
	ctx := mock.NewContext()

	variable.BindSessionVars(ctx)

	v := variable.GetSessionVars(ctx)
	c.Assert(v, NotNil)

	// For AffectedRows
	v.AddAffectedRows(1)
	c.Assert(v.AffectedRows, Equals, uint64(1))
	v.AddAffectedRows(1)
	c.Assert(v.AffectedRows, Equals, uint64(2))

	// For FoundRows
	v.AddFoundRows(1)
	c.Assert(v.FoundRows, Equals, uint64(1))
	v.AddFoundRows(1)
	c.Assert(v.FoundRows, Equals, uint64(2))

	// For last insert id
	v.SetLastInsertID(uint64(1))
	c.Assert(v.LastInsertID, Equals, uint64(1))

	v.SetSystemVar("autocommit", types.NewStringDatum("1"))
	val := v.GetSystemVar("autocommit")
	c.Assert(val.GetString(), Equals, "1")
	c.Assert(v.SetSystemVar("autocommit", types.Datum{}), NotNil)

	v.SetSystemVar("sql_mode", types.NewStringDatum("strict_trans_tables"))
	val = v.GetSystemVar("sql_mode")
	c.Assert(val.GetString(), Equals, "STRICT_TRANS_TABLES")
	c.Assert(v.StrictSQLMode, IsTrue)
	v.SetSystemVar("sql_mode", types.NewStringDatum(""))
	c.Assert(v.StrictSQLMode, IsFalse)

	v.SetSystemVar("character_set_connection", types.NewStringDatum("utf8"))
	v.SetSystemVar("collation_connection", types.NewStringDatum("utf8_general_ci"))
	charset, collation := variable.GetCharsetInfo(ctx)
	c.Assert(charset, Equals, "utf8")
	c.Assert(collation, Equals, "utf8_general_ci")

	c.Assert(v.SetSystemVar("character_set_results", types.Datum{}), IsNil)
}
Ejemplo n.º 21
0
// Create a new session on store but only do ddl works.
func (s *testSessionSuite) bootstrapWithError(store kv.Storage, c *C) {
	ss := &session{
		values: make(map[fmt.Stringer]interface{}),
		store:  store,
		sid:    atomic.AddInt64(&sessionID, 1),
	}
	domain, err := domap.Get(store)
	c.Assert(err, IsNil)
	sessionctx.BindDomain(ss, domain)
	variable.BindSessionVars(ss)
	variable.GetSessionVars(ss).SetStatusFlag(mysql.ServerStatusAutocommit, true)
	// session implements autocommit.Checker. Bind it to ctx
	autocommit.BindAutocommitChecker(ss, ss)
	sessionMu.Lock()
	defer sessionMu.Unlock()
	b, err := checkBootstrapped(ss)
	c.Assert(b, IsFalse)
	c.Assert(err, IsNil)
	doDDLWorks(ss)
	// Leave dml unfinished.
}
Ejemplo n.º 22
0
func (p *testFromSuit) SetUpSuite(c *C) {
	store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
	c.Assert(err, IsNil)
	p.vars = map[string]interface{}{}
	p.txn, _ = store.Begin()
	tbInfo := &model.TableInfo{
		ID:    1,
		Name:  model.NewCIStr("t"),
		State: model.StatePublic,
		Columns: []*model.ColumnInfo{
			{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
				State:        model.StatePublic,
			},
			{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
				State:        model.StatePublic,
			},
		},
	}
	p.tbl, err = tables.TableFromMeta(&simpleAllocator{}, tbInfo)
	c.Assert(err, IsNil)
	variable.BindSessionVars(p)

	var i int64
	for i = 0; i < 10; i++ {
		_, err = p.tbl.AddRecord(p, []interface{}{i * 10, "hello"})
		c.Assert(err, IsNil)
	}
}
Ejemplo n.º 23
0
func (s *testEvaluatorSuite) TestSleep(c *C) {
	defer testleak.AfterTest(c)()
	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	sessVars := variable.GetSessionVars(ctx)

	// non-strict model
	sessVars.StrictSQLMode = false
	d := make([]types.Datum, 1)
	_, err := builtinSleep(d, nil)
	c.Assert(err, NotNil)
	ret, err := builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))
	d[0].SetInt64(-1)
	ret, err = builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))

	// for error case under the strict model
	sessVars.StrictSQLMode = true
	d[0].SetNull()
	_, err = builtinSleep(d, ctx)
	c.Assert(err, NotNil)
	d[0].SetFloat64(-2.5)
	_, err = builtinSleep(d, ctx)
	c.Assert(err, NotNil)

	// strict model
	d[0].SetFloat64(0.5)
	start := time.Now()
	ret, err = builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))
	sub := time.Since(start)
	c.Assert(sub.Nanoseconds(), GreaterEqual, int64(0.5*1e9))
}
Ejemplo n.º 24
0
func (*testSessionSuite) TestSession(c *C) {
	ctx := mock.NewContext()

	variable.BindSessionVars(ctx)

	v := variable.GetSessionVars(ctx)
	c.Assert(v, NotNil)

	// For AffectedRows
	v.AddAffectedRows(1)
	c.Assert(v.AffectedRows, Equals, uint64(1))
	v.AddAffectedRows(1)
	c.Assert(v.AffectedRows, Equals, uint64(2))

	// For FoundRows
	v.AddFoundRows(1)
	c.Assert(v.FoundRows, Equals, uint64(1))
	v.AddFoundRows(1)
	c.Assert(v.FoundRows, Equals, uint64(2))

	// For last insert id
	v.SetLastInsertID(uint64(1))
	c.Assert(v.LastInsertID, Equals, uint64(1))
}
Ejemplo n.º 25
0
func (s *testEvaluatorSuite) TestLastInsertID(c *C) {
	defer testleak.AfterTest(c)()
	cases := []struct {
		exprStr   []ast.ExprNode
		resultStr string
	}{
		{nil, "0"},
		{[]ast.ExprNode{ast.NewValueExpr(1)}, "1"},
	}

	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	c.Log(ctx)
	for _, ca := range cases {
		expr := &ast.FuncCallExpr{
			FnName: model.NewCIStr("last_insert_id"),
			Args:   ca.exprStr,
		}
		val, err := Eval(ctx, expr)
		c.Assert(err, IsNil)
		valStr := fmt.Sprintf("%v", val.GetValue())
		c.Assert(valStr, Equals, ca.resultStr, Commentf("for %s", ca.exprStr))
	}
}
Ejemplo n.º 26
0
func (s *testVariableSuite) SetUpSuite(c *C) {
	s.ctx = newMockCtx()
	variable.BindSessionVars(s.ctx)
}
Ejemplo n.º 27
0
func testNewContext(c *C, d *ddl) context.Context {
	ctx := d.newReorgContext()
	variable.BindSessionVars(ctx)
	return ctx
}
Ejemplo n.º 28
0
func (s *testSuite) SetUpSuite(c *C) {
	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
	var err error
	s.store, err = driver.Open("memory:test_inspect")
	c.Assert(err, IsNil)

	s.ctx = mock.NewContext()
	s.ctx.Store = s.store
	variable.BindSessionVars(s.ctx)

	txn, err := s.store.Begin()
	c.Assert(err, IsNil)
	t := meta.NewMeta(txn)

	s.dbInfo = &model.DBInfo{
		ID:   1,
		Name: model.NewCIStr("a"),
	}
	err = t.CreateDatabase(s.dbInfo)
	c.Assert(err, IsNil)

	col := &model.ColumnInfo{
		Name:         model.NewCIStr("c"),
		ID:           0,
		Offset:       0,
		DefaultValue: 1,
		State:        model.StatePublic,
		FieldType:    *types.NewFieldType(mysql.TypeLong),
	}
	col1 := &model.ColumnInfo{
		Name:         model.NewCIStr("c1"),
		ID:           1,
		Offset:       1,
		DefaultValue: 1,
		State:        model.StatePublic,
		FieldType:    *types.NewFieldType(mysql.TypeLong),
	}
	idx := &model.IndexInfo{
		Name:   model.NewCIStr("c"),
		ID:     1,
		Unique: true,
		Columns: []*model.IndexColumn{{
			Name:   model.NewCIStr("c"),
			Offset: 0,
			Length: 255,
		}},
		State: model.StatePublic,
	}
	s.tbInfo = &model.TableInfo{
		ID:      1,
		Name:    model.NewCIStr("t"),
		State:   model.StatePublic,
		Columns: []*model.ColumnInfo{col, col1},
		Indices: []*model.IndexInfo{idx},
	}
	err = t.CreateTable(s.dbInfo.ID, s.tbInfo)
	c.Assert(err, IsNil)

	err = txn.Commit()
	c.Assert(err, IsNil)
}
Ejemplo n.º 29
0
func (s *testVariableSuite) SetUpSuite(c *C) {
	nc := mock.NewContext()
	s.ctx = nc
	variable.BindSessionVars(s.ctx)
	variable.BindGlobalVarAccessor(s.ctx, nc)
}
Ejemplo n.º 30
0
func (s *testHelperSuite) TestGetTimeValue(c *C) {
	v, err := GetTimeValue(nil, "2012-12-12 00:00:00", mysql.TypeTimestamp, mysql.MinFsp)
	c.Assert(err, IsNil)

	timeValue, ok := v.(mysql.Time)
	c.Assert(ok, IsTrue)
	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

	ctx := newMockCtx()
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)

	sessionVars.Systems["timestamp"] = ""
	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, mysql.MinFsp)
	c.Assert(err, IsNil)

	timeValue, ok = v.(mysql.Time)
	c.Assert(ok, IsTrue)
	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

	sessionVars.Systems["timestamp"] = "0"
	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, mysql.MinFsp)
	c.Assert(err, IsNil)

	timeValue, ok = v.(mysql.Time)
	c.Assert(ok, IsTrue)
	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

	delete(sessionVars.Systems, "timestamp")
	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, mysql.MinFsp)
	c.Assert(err, IsNil)

	timeValue, ok = v.(mysql.Time)
	c.Assert(ok, IsTrue)
	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

	sessionVars.Systems["timestamp"] = "1234"

	tbl := []struct {
		Expr interface{}
		Ret  interface{}
	}{
		{"2012-12-12 00:00:00", "2012-12-12 00:00:00"},
		{CurrentTimestamp, time.Unix(1234, 0).Format(mysql.TimeFormat)},
		{ZeroTimestamp, "0000-00-00 00:00:00"},
		{Value{"2012-12-12 00:00:00"}, "2012-12-12 00:00:00"},
		{Value{int64(0)}, "0000-00-00 00:00:00"},
		{Value{}, nil},
		{CurrentTimeExpr, CurrentTimestamp},
		{NewUnaryOperation(opcode.Minus, Value{int64(0)}), "0000-00-00 00:00:00"},
		{mockExpr{}, nil},
	}

	for _, t := range tbl {
		v, err := GetTimeValue(ctx, t.Expr, mysql.TypeTimestamp, mysql.MinFsp)
		c.Assert(err, IsNil)

		switch x := v.(type) {
		case mysql.Time:
			c.Assert(x.String(), DeepEquals, t.Ret)
		default:
			c.Assert(x, DeepEquals, t.Ret)
		}
	}

	errTbl := []struct {
		Expr interface{}
	}{
		{"2012-13-12 00:00:00"},
		{Value{"2012-13-12 00:00:00"}},
		{Value{0}},
		{Value{int64(1)}},
		{&Ident{CIStr: model.NewCIStr("xxx")}},
		{NewUnaryOperation(opcode.Minus, Value{int64(1)})},
	}

	for _, t := range errTbl {
		_, err := GetTimeValue(ctx, t.Expr, mysql.TypeTimestamp, mysql.MinFsp)
		c.Assert(err, NotNil)
	}
}