本文整理汇总了Golang中github.com/pingcap/tidb.CreateSession函数的典型用法代码示例。如果您正苦于以下问题:Golang CreateSession函数的具体用法?Golang CreateSession怎么用?Golang CreateSession使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CreateSession函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: SetUpSuite
func (ts *testMemoryTableSuite) SetUpSuite(c *C) {
driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Check(err, IsNil)
ts.store = store
ts.se, err = tidb.CreateSession(ts.store)
c.Assert(err, IsNil)
// create table
tp1 := types.NewFieldType(mysql.TypeLong)
col1 := &model.ColumnInfo{
ID: 1,
Name: model.NewCIStr("a"),
Offset: 0,
FieldType: *tp1,
}
tp2 := types.NewFieldType(mysql.TypeVarchar)
tp2.Flen = 255
col2 := &model.ColumnInfo{
ID: 2,
Name: model.NewCIStr("b"),
Offset: 1,
FieldType: *tp2,
}
tblInfo := &model.TableInfo{
ID: 100,
Name: model.NewCIStr("t"),
Columns: []*model.ColumnInfo{col1, col2},
}
alloc := autoid.NewMemoryAllocator(int64(10))
ts.tbl, _ = tables.MemoryTableFromMeta(alloc, tblInfo)
}
开发者ID:XuHuaiyu,项目名称:tidb,代码行数:33,代码来源:memory_tables_test.go
示例2: NewGCWorker
// NewGCWorker creates a GCWorker instance.
func NewGCWorker(store kv.Storage) (*GCWorker, error) {
session, err := tidb.CreateSession(store)
if err != nil {
return nil, errors.Trace(err)
}
ver, err := store.CurrentVersion()
if err != nil {
return nil, errors.Trace(err)
}
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
worker := &GCWorker{
uuid: strconv.FormatUint(ver.Ver, 16),
desc: fmt.Sprintf("host:%s, pid:%d, start at %s", hostName, os.Getpid(), time.Now()),
store: store.(*tikvStore),
session: session,
gcIsRunning: false,
lastFinish: time.Now(),
quit: make(chan struct{}),
done: make(chan error),
}
go worker.start()
return worker, nil
}
开发者ID:pingcap,项目名称:tidb,代码行数:27,代码来源:gc_worker.go
示例3: TestConstraintNames
func (ts *testSuite) TestConstraintNames(c *C) {
se, _ := tidb.CreateSession(ts.store)
ctx := se.(context.Context)
schemaName := model.NewCIStr("test_constraint")
tblName := model.NewCIStr("t")
tbIdent := table.Ident{
Schema: schemaName,
Name: tblName,
}
err := sessionctx.GetDomain(ctx).DDL().CreateSchema(ctx, tbIdent.Schema, ts.charsetInfo)
c.Assert(err, IsNil)
tbStmt := statement(ctx, "create table t (a int, b int, index a (a, b), index a (a))").(*stmts.CreateTableStmt)
err = sessionctx.GetDomain(ctx).DDL().CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints)
c.Assert(err, NotNil)
tbStmt = statement(ctx, "create table t (a int, b int, index A (a, b), index (a))").(*stmts.CreateTableStmt)
err = sessionctx.GetDomain(ctx).DDL().CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints)
c.Assert(err, IsNil)
tbl, err := sessionctx.GetDomain(ctx).InfoSchema().TableByName(schemaName, tblName)
indices := tbl.Indices()
c.Assert(len(indices), Equals, 2)
c.Assert(indices[0].Name.O, Equals, "A")
c.Assert(indices[1].Name.O, Equals, "a_2")
err = sessionctx.GetDomain(ctx).DDL().DropSchema(ctx, tbIdent.Schema)
c.Assert(err, IsNil)
}
开发者ID:lovedboy,项目名称:tidb,代码行数:29,代码来源:ddl_test.go
示例4: newSession
func newSession(c *C, store kv.Storage, dbName string) tidb.Session {
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
mustExecSQL(c, se, "create database if not exists "+dbName)
mustExecSQL(c, se, "use "+dbName)
return se
}
开发者ID:kellerli,项目名称:tidb,代码行数:7,代码来源:show_test.go
示例5: TestBusyServerCop
func (s *testStoreSuite) TestBusyServerCop(c *C) {
client := newBusyClient(s.store.client)
s.store.client = client
session, err := tidb.CreateSession(s.store)
c.Assert(err, IsNil)
var wg sync.WaitGroup
wg.Add(2)
client.setBusy(true)
go func() {
defer wg.Done()
time.Sleep(time.Millisecond * 100)
client.setBusy(false)
}()
go func() {
defer wg.Done()
rs, err := session.Execute(`SELECT variable_value FROM mysql.tidb WHERE variable_name="bootstrapped"`)
c.Assert(err, IsNil)
row, err := rs[0].Next()
c.Assert(err, IsNil)
c.Assert(row, NotNil)
c.Assert(row.Data[0].GetString(), Equals, "True")
}()
wg.Wait()
}
开发者ID:pingcap,项目名称:tidb,代码行数:29,代码来源:store_test.go
示例6: TestConstraintNames
func (ts *testSuite) TestConstraintNames(c *C) {
handle := infoschema.NewHandle(ts.store)
handle.Set(nil)
dd := ddl.NewDDL(ts.store, handle)
se, _ := tidb.CreateSession(ts.store)
ctx := se.(context.Context)
schemaName := model.NewCIStr("test")
tblName := model.NewCIStr("t")
tbIdent := table.Ident{
Schema: schemaName,
Name: tblName,
}
err := dd.CreateSchema(ctx, tbIdent.Schema)
c.Assert(err, IsNil)
tbStmt := statement("create table t (a int, b int, index a (a, b), index a (a))").(*stmts.CreateTableStmt)
err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints)
c.Assert(err, NotNil)
tbStmt = statement("create table t (a int, b int, index A (a, b), index (a))").(*stmts.CreateTableStmt)
err = dd.CreateTable(ctx, tbIdent, tbStmt.Cols, tbStmt.Constraints)
c.Assert(err, IsNil)
tbl, err := handle.Get().TableByName(schemaName, tblName)
indices := tbl.Indices()
c.Assert(len(indices), Equals, 2)
c.Assert(indices[0].Name.O, Equals, "A")
c.Assert(indices[1].Name.O, Equals, "a_2")
err = dd.DropSchema(ctx, tbIdent.Schema)
c.Assert(err, IsNil)
}
开发者ID:nengwang,项目名称:tidb,代码行数:30,代码来源:ddl_test.go
示例7: TestSetPwd
func (s *testSuite) TestSetPwd(c *C) {
defer testleak.AfterTest(c)()
tk := testkit.NewTestKit(c, s.store)
createUserSQL := `CREATE USER 'testpwd'@'localhost' IDENTIFIED BY '';`
tk.MustExec(createUserSQL)
result := tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
rowStr := fmt.Sprintf("%v", []byte(""))
result.Check(testkit.Rows(rowStr))
// set password for
tk.MustExec(`SET PASSWORD FOR 'testpwd'@'localhost' = 'password';`)
result = tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
rowStr = fmt.Sprintf("%v", []byte(util.EncodePassword("password")))
result.Check(testkit.Rows(rowStr))
// set password
setPwdSQL := `SET PASSWORD = 'pwd'`
// Session user is empty.
_, err := tk.Exec(setPwdSQL)
c.Check(err, NotNil)
tk.Se, err = tidb.CreateSession(s.store)
c.Check(err, IsNil)
ctx := tk.Se.(context.Context)
ctx.GetSessionVars().User = "[email protected]"
// Session user doesn't exist.
_, err = tk.Exec(setPwdSQL)
c.Check(terror.ErrorEqual(err, executor.ErrPasswordNoMatch), IsTrue)
// normal
ctx.GetSessionVars().User = "[email protected]"
tk.MustExec(setPwdSQL)
result = tk.MustQuery(`SELECT Password FROM mysql.User WHERE User="testpwd" and Host="localhost"`)
rowStr = fmt.Sprintf("%v", []byte(util.EncodePassword("pwd")))
result.Check(testkit.Rows(rowStr))
}
开发者ID:pingcap,项目名称:tidb,代码行数:35,代码来源:executor_simple_test.go
示例8: Exec
// Exec executes a sql statement.
func (tk *TestKit) Exec(sql string, args ...interface{}) (ast.RecordSet, error) {
var err error
if tk.Se == nil {
tk.Se, err = tidb.CreateSession(tk.store)
tk.c.Assert(err, check.IsNil)
}
if len(args) == 0 {
var rss []ast.RecordSet
rss, err = tk.Se.Execute(sql)
if err == nil && len(rss) > 0 {
return rss[0], nil
}
return nil, err
}
stmtID, _, _, err := tk.Se.PrepareStmt(sql)
if err != nil {
return nil, err
}
rs, err := tk.Se.ExecutePreparedStmt(stmtID, args...)
if err != nil {
return nil, err
}
err = tk.Se.DropPreparedStmt(stmtID)
if err != nil {
return nil, err
}
return rs, nil
}
开发者ID:astaxie,项目名称:tidb,代码行数:29,代码来源:testkit.go
示例9: TestValidator
func (s *testValidatorSuite) TestValidator(c *C) {
cases := []struct {
sql string
inPrepare bool
err error
}{
{"select ?", false, parser.ErrSyntax},
{"select ?", true, nil},
{"create table t(id int not null auto_increment default 2, key (id))", true,
errors.New("Invalid default value for 'id'")},
{"create table t(id int not null default 2 auto_increment, key (id))", true,
errors.New("Invalid default value for 'id'")},
{"create table t(id int not null auto_increment)", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id int not null auto_increment, c int auto_increment, key (id, c))", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id int not null auto_increment, c int, key (c, id))", true,
errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")},
{"create table t(id decimal auto_increment, key (id))", true,
errors.New("Incorrect column specifier for column 'id'")},
{"create table t(id float auto_increment, key (id))", true, nil},
}
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
for _, ca := range cases {
stmts, err1 := tidb.Parse(se.(context.Context), ca.sql)
c.Assert(err1, IsNil)
c.Assert(stmts, HasLen, 1)
stmt := stmts[0]
err = optimizer.Validate(stmt, ca.inPrepare)
c.Assert(terror.ErrorEqual(err, ca.err), IsTrue)
}
}
开发者ID:astaxie,项目名称:tidb,代码行数:35,代码来源:validator_test.go
示例10: SetUpSuite
func (s *testDBSuite) SetUpSuite(c *C) {
trySkipDBTest(c)
var err error
s.schemaName = "test_db"
uri := "memory://test"
s.store, err = tidb.NewStore(uri)
c.Assert(err, IsNil)
s.db, err = sql.Open("tidb", fmt.Sprintf("%s/%s", uri, s.schemaName))
c.Assert(err, IsNil)
s.s, err = tidb.CreateSession(s.store)
c.Assert(err, IsNil)
s.mustExec(c, "create table t1 (c1 int, c2 int, c3 int, primary key(c1))")
s.mustExec(c, "create table t2 (c1 int, c2 int, c3 int)")
// set proper schema lease
s.lease = 500 * time.Millisecond
ctx := s.s.(context.Context)
sessionctx.GetDomain(ctx).SetLease(s.lease)
}
开发者ID:duzhanyuan,项目名称:tidb,代码行数:25,代码来源:ddl_db_test.go
示例11: main
func main() {
tidb.RegisterLocalStore("boltdb", boltdb.Driver{})
tidb.RegisterStore("tikv", tikv.Driver{})
metric.RunMetric(3 * time.Second)
printer.PrintTiDBInfo()
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
if *lease < 0 {
log.Fatalf("invalid lease seconds %d", *lease)
}
tidb.SetSchemaLease(time.Duration(*lease) * time.Second)
cfg := &server.Config{
Addr: fmt.Sprintf(":%s", *port),
LogLevel: *logLevel,
StatusAddr: fmt.Sprintf(":%s", *statusPort),
Socket: *socket,
}
log.SetLevelByString(cfg.LogLevel)
store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *store, *storePath))
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
// Create a session to load information schema.
se, err := tidb.CreateSession(store)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
se.Close()
var driver server.IDriver
driver = server.NewTiDBDriver(store)
var svr *server.Server
svr, err = server.NewServer(cfg, driver)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sc
log.Infof("Got signal [%d] to exit.", sig)
svr.Close()
os.Exit(0)
}()
log.Error(svr.Run())
}
开发者ID:anywhy,项目名称:tidb,代码行数:59,代码来源:main.go
示例12: SetUpSuite
func (ts *testSuite) SetUpSuite(c *C) {
driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Check(err, IsNil)
ts.store = store
ts.se, err = tidb.CreateSession(ts.store)
c.Assert(err, IsNil)
}
开发者ID:xxwwbb3,项目名称:tidb,代码行数:8,代码来源:tables_test.go
示例13: sessionExec
func sessionExec(c *C, s kv.Storage, sql string) {
se, err := tidb.CreateSession(s)
c.Assert(err, IsNil)
_, err = se.Execute("use test_db")
rs, err := se.Execute(sql)
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
c.Assert(rs, IsNil)
se.Close()
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:9,代码来源:ddl_db_test.go
示例14: SetUpSuite
func (ts *testSuite) SetUpSuite(c *C) {
table.TableFromMeta = tables.TableFromMeta
driver := localstore.Driver{goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Check(err, IsNil)
ts.store = store
ts.se, err = tidb.CreateSession(ts.store)
c.Assert(err, IsNil)
_, err = ts.se.Execute("CREATE DATABASE test")
c.Assert(err, IsNil)
}
开发者ID:npk,项目名称:tidb,代码行数:11,代码来源:tables_test.go
示例15: SetUpSuite
func (p *testIndexSuit) SetUpSuite(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
p.store = store
se, _ := tidb.CreateSession(store)
p.ctx = se.(context.Context)
p.cols = []*column.Col{
{
ColumnInfo: model.ColumnInfo{
ID: 0,
Name: model.NewCIStr("id"),
Offset: 0,
DefaultValue: 0,
FieldType: *types.NewFieldType(mysql.TypeLonglong),
State: model.StatePublic,
},
},
{
ColumnInfo: model.ColumnInfo{
ID: 1,
Name: model.NewCIStr("name"),
Offset: 1,
DefaultValue: nil,
FieldType: *types.NewFieldType(mysql.TypeVarchar),
State: model.StatePublic,
},
},
}
p.tbl = tables.NewTable(2, "t2", 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,
State: model.StatePublic,
},
X: kv.NewKVIndex("i", "id", 0, false),
}
p.tbl.AddIndex(idxCol)
var i int64
for i = 0; i < 10; i++ {
p.tbl.AddRecord(p.ctx, []interface{}{i * 10, "hello"}, 0)
}
}
开发者ID:henrylee2cn,项目名称:tidb,代码行数:53,代码来源:index_test.go
示例16: SetUpSuite
func (p *testIndexSuit) SetUpSuite(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
p.store = store
se, _ := tidb.CreateSession(store)
p.ctx = se.(context.Context)
tbInfo := &model.TableInfo{
ID: 2,
Name: model.NewCIStr("t2"),
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,
},
},
Indices: []*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,
State: model.StatePublic,
},
},
}
p.tbl, err = tables.TableFromMeta(&simpleAllocator{}, tbInfo)
c.Assert(err, IsNil)
var i int64
for i = 0; i < 10; i++ {
p.tbl.AddRecord(p.ctx, []interface{}{i * 10, "hello"})
}
}
开发者ID:lovedboy,项目名称:tidb,代码行数:53,代码来源:index_test.go
示例17: newSession
func newSession(c *C, store kv.Storage, dbName string) tidb.Session {
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
id := atomic.AddUint64(&testConnID, 1)
se.SetConnectionID(id)
c.Assert(err, IsNil)
se.Auth(`[email protected]%`, nil, []byte("012345678901234567890"))
if len(dbName) != 0 {
mustExecSQL(c, se, "create database if not exists "+dbName)
mustExecSQL(c, se, "use "+dbName)
}
return se
}
开发者ID:pingcap,项目名称:tidb,代码行数:13,代码来源:perfschema_test.go
示例18: backgroundExec
func backgroundExec(s kv.Storage, sql string, done chan error) {
se, err := tidb.CreateSession(s)
if err != nil {
done <- errors.Trace(err)
return
}
defer se.Close()
_, err = se.Execute("use test_db")
if err != nil {
done <- errors.Trace(err)
return
}
_, err = se.Execute(sql)
done <- errors.Trace(err)
}
开发者ID:jmptrader,项目名称:tidb,代码行数:15,代码来源:ddl_db_test.go
示例19: OpenCtx
// OpenCtx implements IDriver.
func (qd *TiDBDriver) OpenCtx(capability uint32, collation uint8, dbname string) (IContext, error) {
session, _ := tidb.CreateSession(qd.store)
session.SetClientCapability(capability)
if dbname != "" {
_, err := session.Execute("use " + dbname)
if err != nil {
return nil, err
}
}
tc := &TiDBContext{
session: session,
currentDB: dbname,
stmts: make(map[int]*TiDBStatement),
}
return tc, nil
}
开发者ID:awesomeleo,项目名称:tidb,代码行数:17,代码来源:driver_tidb.go
示例20: TestT
func (*testSuite) TestT(c *C) {
var ident = ast.Ident{
Name: model.NewCIStr("t"),
}
c.Assert(ident.String(), Not(Equals), "")
driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Assert(err, IsNil)
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
ctx := se.(context.Context)
db.BindCurrentSchema(ctx, "test")
fullIdent := ident.Full(ctx)
c.Assert(fullIdent.Schema.L, Equals, "test")
c.Assert(fullIdent.Name.L, Equals, "t")
c.Assert(fullIdent.String(), Not(Equals), "")
fullIdent2 := fullIdent.Full(ctx)
c.Assert(fullIdent2.Schema.L, Equals, fullIdent.Schema.L)
}
开发者ID:astaxie,项目名称:tidb,代码行数:19,代码来源:table_test.go
注:本文中的github.com/pingcap/tidb.CreateSession函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论