Exemple #1
0
func (s *testColumnSuite) colDefStrToFieldType(c *C, str string) *types.FieldType {
	sqlA := "alter table t modify column a " + str
	stmt, err := parser.New().ParseOneStmt(sqlA, "", "")
	c.Assert(err, IsNil)
	colDef := stmt.(*ast.AlterTableStmt).Specs[0].NewColumn
	col, _, err := columnDefToCol(nil, 0, colDef)
	c.Assert(err, IsNil)
	return &col.FieldType
}
Exemple #2
0
// Parse parses a query string to raw ast.StmtNode.
func Parse(ctx context.Context, src string) ([]ast.StmtNode, error) {
	log.Debug("compiling", src)
	charset, collation := getCtxCharsetInfo(ctx)
	stmts, err := parser.New().Parse(src, charset, collation)
	if err != nil {
		log.Warnf("compiling %s, error: %v", src, err)
		return nil, errors.Trace(err)
	}
	return stmts, nil
}
Exemple #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),
		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
}
Exemple #4
0
func (ts *testMiscSuite) TestDMLVistorCover(c *C) {
	sql := `delete from somelog where user = '******' order by timestamp_column limit 1;
delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3.id;
select * from t where exists(select * from t k where t.c = k.c having sum(c) = 1);
insert into t_copy select * from t where t.x > 5;
(select a from t1 where a=10 and b=1) union (select a from t2 where a=11 and b=2) order by a limit 10;
update t1 set col1 = col1 + 1, col2 = col1;
show create table t;
load data infile '/tmp/t.csv' into table t fields terminated by 'ab' enclosed by 'b';`

	parser := parser.New()
	stmts, err := parser.Parse(sql, "", "")
	c.Assert(err, IsNil)
	for _, stmt := range stmts {
		stmt.Accept(visitor{})
		stmt.Accept(visitor1{})
	}
}
Exemple #5
0
func (s *testSuite) SetUpSuite(c *C) {
	s.Parser = parser.New()
	flag.Lookup("mockTikv")
	useMockTikv := *mockTikv
	if useMockTikv {
		store, err := tikv.NewMockTikvStore()
		c.Assert(err, IsNil)
		s.store = store
		tidb.SetSchemaLease(0)
	} else {
		store, err := tidb.NewStore("memory://test/test")
		c.Assert(err, IsNil)
		s.store = store
	}
	logLevel := os.Getenv("log_level")
	log.SetLevelByString(logLevel)
	executor.BaseLookupTableTaskSize = 2
}
Exemple #6
0
// Create a new session on store but only do ddl works.
func (s *testSessionSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
	ss := &session{
		values:      make(map[fmt.Stringer]interface{}),
		store:       store,
		parser:      parser.New(),
		sessionVars: variable.NewSessionVars(),
	}
	ss.SetValue(context.Initing, true)
	domain, err := domap.Get(store)
	c.Assert(err, IsNil)
	sessionctx.BindDomain(ss, domain)
	sessionMu.Lock()
	defer sessionMu.Unlock()
	b, err := checkBootstrapped(ss)
	c.Assert(b, IsFalse)
	c.Assert(err, IsNil)
	doDDLWorks(ss)
	// Leave dml unfinished.
}
Exemple #7
0
func createSession(store kv.Storage) (*session, error) {
	s := &session{
		values:      make(map[fmt.Stringer]interface{}),
		store:       store,
		maxRetryCnt: 10,
		parser:      parser.New(),
		sessionVars: variable.NewSessionVars(),
	}
	domain, err := domap.Get(store)
	if err != nil {
		return nil, errors.Trace(err)
	}
	sessionctx.BindDomain(s, domain)
	// session implements variable.GlobalVarAccessor. Bind it to ctx.
	s.sessionVars.GlobalVarsAccessor = s

	// TODO: Add auth here
	privChecker := &privileges.UserPrivileges{}
	privilege.BindPrivilegeChecker(s, privChecker)
	return s, nil
}
Exemple #8
0
func main() {
	p := parser.New()
	stmt, err := p.ParseOneStmt(sql, "", "")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%#v\n\n", stmt)
	selectStmt := stmt.(*ast.SelectStmt)

	fmt.Printf("selectStmt.From.TableRefs.Left is %#v\n", selectStmt.From.TableRefs.Left)
	fmt.Printf("selectStmt.From.TableRefs.Right is %#v\n\n", selectStmt.From.TableRefs.Right)

	tableSource := selectStmt.From.TableRefs.Left.(*ast.TableSource)
	fmt.Printf("left tableSource.Source is %#v\n", tableSource.Source)
	tableName := tableSource.Source.(*ast.TableName)
	fmt.Printf("left tableName.Name.O is %#v\n", tableName.Name.O)
	tableSource = selectStmt.From.TableRefs.Right.(*ast.TableSource)
	fmt.Printf("right tableSource.Source is %#v\n", tableSource.Source)
	tableName = tableSource.Source.(*ast.TableName)
	fmt.Printf("right tableName.Name.O is %#v\n", tableName.Name.O)
}
Exemple #9
0
func (ts *testMiscSuite) TestDDLVisitorCover(c *C) {
	sql := `
create table t (c1 smallint unsigned, c2 int unsigned);
alter table t add column a smallint unsigned after b;
create index t_i on t (id);
create database test character set utf8;
drop database test;
drop index t_i on t;
drop table t;
truncate t;
create table t (
jobAbbr char(4) not null,
constraint foreign key (jobabbr) references ffxi_jobtype (jobabbr) on delete cascade on update cascade
);
`
	parser := parser.New()
	stmts, err := parser.Parse(sql, "", "")
	c.Assert(err, IsNil)
	for _, stmt := range stmts {
		stmt.Accept(visitor{})
		stmt.Accept(visitor1{})
	}
}
Exemple #10
0
// Create a new session on store but only do ddl works.
func (s *testSessionSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
	ss := &session{
		values: make(map[fmt.Stringer]interface{}),
		store:  store,
		sid:    atomic.AddInt64(&sessionID, 1),
		parser: parser.New(),
	}
	ss.SetValue(context.Initing, true)
	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.
}
Exemple #11
0
func (s *testPlanSuite) SetUpSuite(c *C) {
	s.Parser = parser.New()
}
Exemple #12
0
// DoPrepare prepares the statement, it can be called multiple times without
// side effect.
func (e *PrepareExec) DoPrepare() {
	vars := e.Ctx.GetSessionVars()
	if e.ID != 0 {
		// Must be the case when we retry a prepare.
		// Make sure it is idempotent.
		_, ok := vars.PreparedStmts[e.ID]
		if ok {
			return
		}
	}
	charset, collation := vars.GetCharsetInfo()
	var (
		stmts []ast.StmtNode
		err   error
	)
	if sqlParser, ok := e.Ctx.(sqlexec.SQLParser); ok {
		stmts, err = sqlParser.ParseSQL(e.SQLText, charset, collation)
	} else {
		stmts, err = parser.New().Parse(e.SQLText, charset, collation)
	}
	if err != nil {
		e.Err = errors.Trace(err)
		return
	}
	if len(stmts) != 1 {
		e.Err = ErrPrepareMulti
		return
	}
	stmt := stmts[0]
	if _, ok := stmt.(ast.DDLNode); ok {
		e.Err = ErrPrepareDDL
		return
	}
	var extractor paramMarkerExtractor
	stmt.Accept(&extractor)

	// The parameter markers are appended in visiting order, which may not
	// be the same as the position order in the query string. We need to
	// sort it by position.
	sorter := &paramMarkerSorter{markers: extractor.markers}
	sort.Sort(sorter)
	e.ParamCount = len(sorter.markers)
	prepared := &Prepared{
		Stmt:          stmt,
		Params:        sorter.markers,
		SchemaVersion: e.IS.SchemaMetaVersion(),
	}

	err = plan.PrepareStmt(e.IS, e.Ctx, stmt)
	if err != nil {
		e.Err = errors.Trace(err)
		return
	}

	if e.ID == 0 {
		e.ID = vars.GetNextPreparedStmtID()
	}
	if e.Name != "" {
		vars.PreparedStmtNameToID[e.Name] = e.ID
	}
	vars.PreparedStmts[e.ID] = prepared
}
Exemple #13
0
func (s *testEvaluatorSuite) SetUpSuite(c *C) {
	s.Parser = parser.New()
}
Exemple #14
0
func (ts *testFlagSuite) SetUpSuite(c *C) {
	ts.Parser = parser.New()
}
Exemple #15
0
func (s *testNameResolverSuite) SetUpSuite(c *C) {
	s.Parser = parser.New()
}
Exemple #16
0
func (s *testEvaluatorSuite) SetUpSuite(c *C) {
	s.Parser = parser.New()
	s.ctx = mock.NewContext()
}