本文整理汇总了Golang中github.com/pingcap/tidb/model.NewCIStr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCIStr函数的具体用法?Golang NewCIStr怎么用?Golang NewCIStr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCIStr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AlterTable
func (d *ddl) AlterTable(ctx context.Context, ident ast.Ident, specs []*ast.AlterTableSpec) (err error) {
// now we only allow one schema changes at the same time.
if len(specs) != 1 {
return errors.New("can't run multi schema changes in one DDL")
}
for _, spec := range specs {
switch spec.Tp {
case ast.AlterTableAddColumn:
err = d.AddColumn(ctx, ident, spec)
case ast.AlterTableDropColumn:
err = d.DropColumn(ctx, ident, spec.DropColumn.Name)
case ast.AlterTableDropIndex:
err = d.DropIndex(ctx, ident, model.NewCIStr(spec.Name))
case ast.AlterTableAddConstraint:
constr := spec.Constraint
switch spec.Constraint.Tp {
case ast.ConstraintKey, ast.ConstraintIndex:
err = d.CreateIndex(ctx, ident, false, model.NewCIStr(constr.Name), spec.Constraint.Keys)
case ast.ConstraintUniq, ast.ConstraintUniqIndex, ast.ConstraintUniqKey:
err = d.CreateIndex(ctx, ident, true, model.NewCIStr(constr.Name), spec.Constraint.Keys)
default:
// nothing to do now.
}
default:
// nothing to do now.
}
if err != nil {
return errors.Trace(err)
}
}
return nil
}
示例2: TestCoveringIndex
func (s *testPlanSuite) TestCoveringIndex(c *C) {
cases := []struct {
columnNames []string
indexNames []string
indexLens []int
isCovering bool
}{
{[]string{"a"}, []string{"a"}, []int{-1}, true},
{[]string{"a"}, []string{"a", "b"}, []int{-1, -1}, true},
{[]string{"a", "b"}, []string{"b", "a"}, []int{-1, -1}, true},
{[]string{"a", "b"}, []string{"b", "c"}, []int{-1, -1}, false},
{[]string{"a", "b"}, []string{"a", "b"}, []int{50, -1}, false},
{[]string{"a", "b"}, []string{"a", "c"}, []int{-1, -1}, false},
}
for _, ca := range cases {
var columns []*model.ColumnInfo
for _, cn := range ca.columnNames {
columns = append(columns, &model.ColumnInfo{Name: model.NewCIStr(cn)})
}
var indexCols []*model.IndexColumn
for i := range ca.indexNames {
icn := ca.indexNames[i]
icl := ca.indexLens[i]
indexCols = append(indexCols, &model.IndexColumn{Name: model.NewCIStr(icn), Length: icl})
}
covering := isCoveringIndex(columns, indexCols)
c.Assert(covering, Equals, ca.isCovering)
}
}
示例3: buildResultField
func buildResultField(tableName, name string, tp byte, size int) *ast.ResultField {
cs := charset.CharsetBin
cl := charset.CharsetBin
flag := mysql.UnsignedFlag
if tp == mysql.TypeVarchar || tp == mysql.TypeBlob {
cs = mysql.DefaultCharset
cl = mysql.DefaultCollationName
flag = 0
}
fieldType := types.FieldType{
Charset: cs,
Collate: cl,
Tp: tp,
Flen: size,
Flag: uint(flag),
}
colInfo := &model.ColumnInfo{
Name: model.NewCIStr(name),
FieldType: fieldType,
}
expr := &ast.ValueExpr{}
expr.SetType(&fieldType)
return &ast.ResultField{
Column: colInfo,
ColumnAsName: colInfo.Name,
TableAsName: model.NewCIStr(tableName),
DBName: model.NewCIStr(infoschema.Name),
Expr: expr,
}
}
示例4: 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)
}
示例5: TestIndexError
func (s *testDDLSuite) TestIndexError(c *C) {
defer testleak.AfterTest(c)()
store := testCreateStore(c, "test_index_error")
defer store.Close()
d := newDDL(store, nil, nil, testLease)
defer d.close()
ctx := testNewContext(c, d)
doDDLJobErr(c, -1, 1, model.ActionAddIndex, nil, ctx, d)
doDDLJobErr(c, -1, 1, model.ActionDropIndex, nil, ctx, d)
dbInfo := testSchemaInfo(c, d, "test")
tblInfo := testTableInfo(c, d, "t", 3)
testCreateSchema(c, ctx, d, dbInfo)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionAddIndex, []interface{}{1}, ctx, d)
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionAddIndex,
[]interface{}{false, model.NewCIStr("t"), []*ast.IndexColName{{Column: &ast.ColumnName{Name: model.NewCIStr("c")}, Length: 256}}}, ctx, d)
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionAddIndex,
[]interface{}{false, model.NewCIStr("c1_index"), []*ast.IndexColName{{Column: &ast.ColumnName{Name: model.NewCIStr("c")}, Length: 256}}}, ctx, d)
testCreateIndex(c, ctx, d, dbInfo, tblInfo, false, "c1_index", "c1")
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionAddIndex,
[]interface{}{false, model.NewCIStr("c1_index"), []*ast.IndexColName{{Column: &ast.ColumnName{Name: model.NewCIStr("c1")}, Length: 256}}}, ctx, d)
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionDropIndex, []interface{}{1}, ctx, d)
testDropIndex(c, ctx, d, dbInfo, tblInfo, "c1_index")
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionDropIndex, []interface{}{model.NewCIStr("c1_index")}, ctx, d)
}
示例6: buildExplain
func (b *planBuilder) buildExplain(explain *ast.ExplainStmt) Plan {
if show, ok := explain.Stmt.(*ast.ShowStmt); ok {
return b.buildShow(show)
}
targetPlan, err := Optimize(b.ctx, explain.Stmt, b.is)
if err != nil {
b.err = errors.Trace(err)
return nil
}
p := &Explain{StmtPlan: targetPlan}
addChild(p, targetPlan)
schema := make(expression.Schema, 0, 3)
schema = append(schema, &expression.Column{
ColName: model.NewCIStr("ID"),
RetType: types.NewFieldType(mysql.TypeString),
})
schema = append(schema, &expression.Column{
ColName: model.NewCIStr("Json"),
RetType: types.NewFieldType(mysql.TypeString),
})
schema = append(schema, &expression.Column{
ColName: model.NewCIStr("ParentID"),
RetType: types.NewFieldType(mysql.TypeString),
})
p.SetSchema(schema)
return p
}
示例7: testTableInfo
// create a test table with num int columns and with no index.
func testTableInfo(c *C, d *ddl, name string, num int) *model.TableInfo {
var err error
tblInfo := &model.TableInfo{
Name: model.NewCIStr(name),
}
tblInfo.ID, err = d.genGlobalID()
c.Assert(err, IsNil)
cols := make([]*model.ColumnInfo, num)
for i := range cols {
col := &model.ColumnInfo{
Name: model.NewCIStr(fmt.Sprintf("c%d", i+1)),
Offset: i,
DefaultValue: i + 1,
State: model.StatePublic,
}
col.FieldType = *types.NewFieldType(mysql.TypeLong)
col.ID, err = d.genGlobalID()
c.Assert(err, IsNil)
cols[i] = col
}
tblInfo.Columns = cols
return tblInfo
}
示例8: TestGroupBy
func (t *testGroupBySuite) TestGroupBy(c *C) {
tblPlan := &testTablePlan{groupByTestData, []string{"id", "name"}}
// test multiple fields
sl := &SelectList{
Fields: []*field.Field{
{
Expr: &expressions.Ident{
CIStr: model.NewCIStr("id"),
},
},
{
Expr: &expressions.Ident{
CIStr: model.NewCIStr("name"),
},
},
{
Expr: &expressions.Call{
F: "sum",
Args: []expression.Expression{
&expressions.Ident{
CIStr: model.NewCIStr("id"),
},
},
},
},
},
AggFields: map[int]struct{}{2: {}},
}
groupbyPlan := &GroupByDefaultPlan{
SelectList: sl,
Src: tblPlan,
By: []expression.Expression{
&expressions.Ident{
CIStr: model.NewCIStr("id"),
},
},
}
ret := map[int]string{}
groupbyPlan.Do(nil, func(id interface{}, data []interface{}) (bool, error) {
ret[data[0].(int)] = data[1].(string)
return true, nil
})
c.Assert(ret, HasLen, 3)
excepted := map[int]string{
10: "10",
40: "40",
60: "60",
}
c.Assert(ret, DeepEquals, excepted)
// test empty
tblPlan.rows = []*testRowData{}
groupbyPlan.Src = tblPlan
groupbyPlan.By = nil
groupbyPlan.Do(nil, func(id interface{}, data []interface{}) (bool, error) {
return true, nil
})
}
示例9: 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)
}
示例10: testCreateForeignKey
func (s *testForeighKeySuite) testCreateForeignKey(c *C, tblInfo *model.TableInfo, fkName string, keys []string, refTable string, refKeys []string, onDelete ast.ReferOptionType, onUpdate ast.ReferOptionType) *model.Job {
FKName := model.NewCIStr(fkName)
Keys := make([]model.CIStr, len(keys))
for i, key := range keys {
Keys[i] = model.NewCIStr(key)
}
RefTable := model.NewCIStr(refTable)
RefKeys := make([]model.CIStr, len(refKeys))
for i, key := range refKeys {
RefKeys[i] = model.NewCIStr(key)
}
fkInfo := &model.FKInfo{
Name: FKName,
RefTable: RefTable,
RefCols: RefKeys,
Cols: Keys,
OnDelete: int(onDelete),
OnUpdate: int(onUpdate),
State: model.StateNone,
}
job := &model.Job{
SchemaID: s.dbInfo.ID,
TableID: tblInfo.ID,
Type: model.ActionAddForeignKey,
Args: []interface{}{fkInfo},
}
err := s.d.doDDLJob(s.ctx, job)
c.Assert(err, IsNil)
return job
}
示例11: 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)
}
示例12: TestInfoTables
// Make sure that all tables of infomation_schema could be found in infoschema handle.
func (*testSuite) TestInfoTables(c *C) {
defer testleak.AfterTest(c)()
driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Assert(err, IsNil)
defer store.Close()
handle, err := infoschema.NewHandle(store)
c.Assert(err, IsNil)
builder, err := infoschema.NewBuilder(handle).InitWithDBInfos(nil, 0)
c.Assert(err, IsNil)
err = builder.Build()
c.Assert(err, IsNil)
is := handle.Get()
c.Assert(is, NotNil)
info_tables := []string{
"SCHEMATA",
"TABLES",
"COLUMNS",
"STATISTICS",
"CHARACTER_SETS",
"COLLATIONS",
"FILES",
"PROFILING",
"PARTITIONS",
"KEY_COLUMN_USAGE",
"REFERENTIAL_CONSTRAINTS",
}
for _, t := range info_tables {
tb, err1 := is.TableByName(model.NewCIStr(infoschema.Name), model.NewCIStr(t))
c.Assert(err1, IsNil)
c.Assert(tb, NotNil)
}
}
示例13: TestExpression
func (s *testExpressionSuite) TestExpression(c *C) {
defer testleak.AfterTest(c)()
var schema Schema
for i := 0; i < 3; i++ {
schema = append(schema, &Column{ColName: model.NewCIStr("t"), FromID: "mock", Position: i})
}
tc1 := &Column{FromID: "t", ColName: model.NewCIStr("c1")}
kc1 := &Column{FromID: "k", ColName: model.NewCIStr("c1")}
tc2 := &Column{FromID: "t", ColName: model.NewCIStr("c2")}
con := &Constant{Value: types.NewDatum(10)}
col := &ast.ColumnName{Name: model.NewCIStr("t")}
// t.c1 as t, t.c2 as t, 10 as t => error
index, err := getColIndex([]Expression{tc1, tc2, con}, schema, col)
c.Check(err, NotNil)
// t.c1 as t, 10 as t, t.c2 as t => 10
index, err = getColIndex([]Expression{tc1, con, tc2}, schema, col)
c.Assert(index, Equals, 1)
// t.c1 as t, t.c1 as t, 10 as t => 10
index, err = getColIndex([]Expression{tc1, tc1, con}, schema, col)
c.Assert(index, Equals, 2)
// t.c1 as t, k.c1 as t, 10 as t => error
index, err = getColIndex([]Expression{tc1, kc1, con}, schema, col)
c.Check(err, NotNil)
}
示例14: 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()
}
示例15: TestUniqueIndexMultipleNullEntries
func (ts *testSuite) TestUniqueIndexMultipleNullEntries(c *C) {
_, err := ts.se.Execute("CREATE TABLE test.t (a int primary key auto_increment, b varchar(255) unique)")
c.Assert(err, IsNil)
ctx := ts.se.(context.Context)
dom := sessionctx.GetDomain(ctx)
tb, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
c.Assert(tb.Meta().ID, Greater, int64(0))
c.Assert(tb.Meta().Name.L, Equals, "t")
c.Assert(tb.Meta(), NotNil)
c.Assert(tb.Indices(), NotNil)
c.Assert(string(tb.FirstKey()), Not(Equals), "")
c.Assert(string(tb.IndexPrefix()), Not(Equals), "")
c.Assert(string(tb.RecordPrefix()), Not(Equals), "")
c.Assert(tables.FindIndexByColName(tb, "b"), NotNil)
autoid, err := tb.AllocAutoID()
c.Assert(err, IsNil)
c.Assert(autoid, Greater, int64(0))
_, err = tb.AddRecord(ctx, types.MakeDatums(1, nil))
c.Assert(err, IsNil)
_, err = tb.AddRecord(ctx, types.MakeDatums(2, nil))
c.Assert(err, IsNil)
_, err = ts.se.Execute("drop table test.t")
c.Assert(err, IsNil)
}