当前位置: 首页>>代码示例>>Golang>>正文


Golang tidb.Compile函数代码示例

本文整理汇总了Golang中github.com/pingcap/tidb.Compile函数的典型用法代码示例。如果您正苦于以下问题:Golang Compile函数的具体用法?Golang Compile怎么用?Golang Compile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Compile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestShow

func (s *testStmtSuite) TestShow(c *C) {
	testSQL := `drop table if exists show_test; create table show_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1);`
	mustExec(c, s.testDB, testSQL)

	testSQL = "show columns from show_test;"
	stmtList, err := tidb.Compile(testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

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

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

	testSQL = "show create table show_test;"
	stmtList, err = tidb.Compile(testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)
	testStmt, ok = stmtList[0].(*stmts.ShowStmt)
	c.Assert(ok, IsTrue)

	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)
	c.Assert(testStmt.Pattern, NotNil)
}
开发者ID:kevinhuo88888,项目名称:tidb,代码行数:34,代码来源:show_test.go

示例2: TestTableSourceString

func (s *testTableRsetSuite) TestTableSourceString(c *C) {
	tableIdent := table.Ident{Schema: model.NewCIStr(s.dbName), Name: model.NewCIStr(s.tableName)}
	ts := &rsets.TableSource{Source: tableIdent, Name: s.tableName}

	str := ts.String()
	c.Assert(len(str), Greater, 0)

	stmtList, err := tidb.Compile(s.querySql)
	c.Assert(err, IsNil)
	c.Assert(len(stmtList), Greater, 0)

	ts = &rsets.TableSource{Source: stmtList[0], Name: s.tableName}
	str = ts.String()
	c.Assert(len(str), Greater, 0)

	ts = &rsets.TableSource{Source: stmtList[0]}
	str = ts.String()
	c.Assert(len(str), Greater, 0)

	// check panic
	defer func() {
		e := recover()
		c.Assert(e, NotNil)
	}()

	ts = &rsets.TableSource{}
	str = ts.String()
}
开发者ID:ninefive,项目名称:tidb,代码行数:28,代码来源:from_test.go

示例3: TestSelectHaving

func (s *testStmtSuite) TestSelectHaving(c *C) {
	s.fillData(s.testDB, c)

	// Test compile
	stmtList, err := tidb.Compile("select * from test where id = 2;")
	c.Assert(err, IsNil)

	str := stmtList[0].OriginText()
	c.Assert(0, Less, len(str))

	tx := mustBegin(c, s.testDB)
	rows, err := tx.Query("select id, name from test where id in (1,3) having name like 'he%';")
	c.Assert(err, IsNil)

	for rows.Next() {
		var id int
		var name string
		rows.Scan(&id, &name)
		c.Assert(id, Equals, 1)
		c.Assert(name, Equals, "hello")
	}

	rows.Close()
	mustCommit(c, tx)
}
开发者ID:ninefive,项目名称:tidb,代码行数:25,代码来源:select_test.go

示例4: TestCreateTable

func (s *testStmtSuite) TestCreateTable(c *C) {
	stmtList, err := tidb.Compile(s.createDBSql + " CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));")
	c.Assert(err, IsNil)

	for _, stmt := range stmtList {
		c.Assert(len(stmt.OriginText()), Greater, 0)

		mf := newMockFormatter()
		stmt.Explain(nil, mf)
		c.Assert(mf.Len(), Greater, 0)
	}

	// Test create an exist database
	tx := mustBegin(c, s.testDB)
	_, err = tx.Exec(fmt.Sprintf("CREATE database %s;", s.dbName))
	c.Assert(err, NotNil)
	tx.Rollback()

	// Test create an exist table
	mustExec(c, s.testDB, "CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));")

	tx = mustBegin(c, s.testDB)
	_, err = tx.Exec("CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));")
	c.Assert(err, NotNil)
	tx.Rollback()

	// Test "if not exist"
	mustExec(c, s.testDB, "CREATE TABLE if not exists test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));")
}
开发者ID:ninefive,项目名称:tidb,代码行数:29,代码来源:create_test.go

示例5: TestUse

func (s *testStmtSuite) TestUse(c *C) {
	testSQL := `create database if not exists use_test;`
	mustExec(c, s.testDB, testSQL)

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

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

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

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

	errTestSQL := `use xxx;`
	tx := mustBegin(c, s.testDB)
	_, err = tx.Exec(errTestSQL)
	c.Assert(err, NotNil)
	tx.Rollback()
}
开发者ID:yzl11,项目名称:vessel,代码行数:25,代码来源:use_test.go

示例6: TestTableSourceString

func (s *testTableRsetSuite) TestTableSourceString(c *C) {
	tableIdent := table.Ident{Schema: model.NewCIStr(s.dbName), Name: model.NewCIStr(s.tableName)}
	ts := &rsets.TableSource{Source: tableIdent, Name: s.tableName}

	str := ts.String()
	c.Assert(len(str), Greater, 0)

	store := newStore(c)
	se := newSession(c, store, s.dbName)
	ctx, ok := se.(context.Context)
	c.Assert(ok, IsTrue)
	stmtList, err := tidb.Compile(ctx, s.querySql)
	c.Assert(err, IsNil)
	c.Assert(len(stmtList), Greater, 0)

	ts = &rsets.TableSource{Source: stmtList[0], Name: s.tableName}
	str = ts.String()
	c.Assert(len(str), Greater, 0)

	ts = &rsets.TableSource{Source: stmtList[0]}
	str = ts.String()
	c.Assert(len(str), Greater, 0)

	// check panic
	defer func() {
		e := recover()
		c.Assert(e, NotNil)
	}()

	ts = &rsets.TableSource{}
	str = ts.String()
}
开发者ID:lovedboy,项目名称:tidb,代码行数:32,代码来源:from_test.go

示例7: TestSetCharsetStmt

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)
}
开发者ID:js-for-kids,项目名称:tidb,代码行数:30,代码来源:set_test.go

示例8: TestJoinRsetPlan

func (s *testJoinRsetSuite) TestJoinRsetPlan(c *C) {
	p, err := s.r.Plan(s.ctx)
	c.Assert(err, IsNil)

	_, ok := p.(*plans.JoinPlan)
	c.Assert(ok, IsTrue)

	// check join right is statement.
	querySql := fmt.Sprintf("select 1")
	stmtList, err := tidb.Compile(querySql)
	c.Assert(err, IsNil)
	c.Assert(len(stmtList), Greater, 0)

	ts := &rsets.TableSource{Source: stmtList[0]}
	s.r.Right = ts

	_, err = s.r.Plan(s.ctx)
	c.Assert(err, IsNil)

	// check join right is join rset.
	s.r.Right = &rsets.JoinRset{Left: ts}

	_, err = s.r.Plan(s.ctx)
	c.Assert(err, IsNil)

	// check error.
	s.r.Right = "xxx"
	_, err = s.r.Plan(s.ctx)
	c.Assert(err, NotNil)

	s.r.Right = nil
}
开发者ID:npk,项目名称:tidb,代码行数:32,代码来源:join_test.go

示例9: TestExplain

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)
}
开发者ID:lovedboy,项目名称:tidb,代码行数:47,代码来源:explain_test.go

示例10: TestDo

func (s *testStmtSuite) TestDo(c *C) {
	testSQL := "do 1, 2"

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

	testStmt := stmtList[0].(*stmts.DoStmt)
	c.Assert(testStmt.IsDDL(), IsFalse)
	c.Assert(len(testStmt.OriginText()), Greater, 0)

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

	mustExec(c, s.testDB, testSQL)
}
开发者ID:lovedboy,项目名称:tidb,代码行数:17,代码来源:do_test.go

示例11: TestRollback

func (s *testStmtSuite) TestRollback(c *C) {
	// Test RollbackStmt.
	testSQL := `rollback;`
	stmtList, err := tidb.Compile(s.ctx, testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

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

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

	mf := newMockFormatter()
	testStmt.Explain(nil, mf)
	c.Assert(mf.Len(), Greater, 0)
}
开发者ID:lovedboy,项目名称:tidb,代码行数:17,代码来源:transaction_test.go

示例12: TestDropDatabase

func (s *testStmtSuite) TestDropDatabase(c *C) {
	testSQL := "drop database if exists drop_test;"

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

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

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

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

	mustExec(c, s.testDB, testSQL)
}
开发者ID:caoyu0,项目名称:tidb,代码行数:19,代码来源:drop_test.go

示例13: TestCreateIndex

func (s *testStmtSuite) TestCreateIndex(c *C) {
	mustExec(c, s.testDB, s.createTableSql)
	stmtList, err := tidb.Compile("CREATE index name_idx on test (name)")
	c.Assert(err, IsNil)

	str := stmtList[0].OriginText()
	c.Assert(0, Less, len(str))

	mf := newMockFormatter()
	stmtList[0].Explain(nil, mf)
	c.Assert(mf.Len(), Greater, 0)

	tx := mustBegin(c, s.testDB)
	_, err = tx.Exec("CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));")
	c.Assert(err, NotNil)
	tx.Rollback()

	// Test not exist
	mustExec(c, s.testDB, "CREATE index name_idx on test (name)")
}
开发者ID:ninefive,项目名称:tidb,代码行数:20,代码来源:create_test.go

示例14: TestSetCharsetStmt

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)

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

	mf := newMockFormatter()
	testStmt.Explain(nil, mf)
	c.Assert(mf.Len(), Greater, 0)
}
开发者ID:nengwang,项目名称:tidb,代码行数:20,代码来源:set_test.go

示例15: TestAlterTable

func (s *testStmtSuite) TestAlterTable(c *C) {
	testSQL := "drop table if exists t; create table t (c1 int); alter table t add column c2 int;"

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

	stmtLen := len(stmtList)
	c.Assert(stmtLen, Greater, 0)

	testStmt, ok := stmtList[stmtLen-1].(*stmts.AlterTableStmt)
	c.Assert(ok, IsTrue)

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

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

	mustExec(c, s.testDB, testSQL)
}
开发者ID:lovedboy,项目名称:tidb,代码行数:21,代码来源:altertable_test.go


注:本文中的github.com/pingcap/tidb.Compile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。