diff --git a/session/rewrite.go b/session/rewrite.go index a3a2d3fc..5e329fb1 100644 --- a/session/rewrite.go +++ b/session/rewrite.go @@ -17,6 +17,7 @@ package session import ( + "fmt" "vitess.io/vitess/go/vt/sqlparser" ) @@ -104,3 +105,42 @@ func insert2Select(stmt *sqlparser.Insert) string { return "select 1 from DUAL" } + +// select2Count : SELECT 转成 COUNT语句 +func (rw *Rewrite) select2Count() string { + if rw.Stmt == nil { + return fmt.Sprintf("SELECT COUNT(1) FROM (%s)t", rw.SQL) + } + // log.Infof("%#v", rw.Stmt) + + switch stmt := rw.Stmt.(type) { + case *sqlparser.Select: + if stmt.Distinct != "" || stmt.GroupBy != nil || stmt.Having != nil { + return fmt.Sprintf("SELECT COUNT(1) FROM (%s)t", rw.SQL) + } + + newSQL := &sqlparser.Select{ + SelectExprs: []sqlparser.SelectExpr{ + &sqlparser.AliasedExpr{ + Expr: &sqlparser.FuncExpr{ + Name: sqlparser.NewColIdent("count"), + Exprs: []sqlparser.SelectExpr{ + new(sqlparser.StarExpr), + }, + }, + }, + }, + Distinct: stmt.Distinct, + From: stmt.From, + Where: stmt.Where, + GroupBy: stmt.GroupBy, + Having: stmt.Having, + Limit: stmt.Limit, + } + return sqlparser.String(newSQL) + // case *sqlparser.Union, *sqlparser.ParenSelect: + // return fmt.Sprintf("SELECT COUNT(1) FROM (%s)t", rw.SQL) + default: + return fmt.Sprintf("SELECT COUNT(1) FROM (%s)t", rw.SQL) + } +} diff --git a/session/session_inception.go b/session/session_inception.go index e0763859..9e2498d3 100644 --- a/session/session_inception.go +++ b/session/session_inception.go @@ -49,6 +49,7 @@ import ( "github.com/hanchuanchuan/goInception/util/auth" "github.com/hanchuanchuan/goInception/util/sqlexec" "github.com/hanchuanchuan/goInception/util/stringutil" + // "vitess.io/vitess/go/vt/sqlparser" // "github.com/hanchuanchuan/parser/ast" "github.com/jinzhu/gorm" "github.com/percona/go-mysql/query" @@ -126,6 +127,9 @@ type sourceOptions struct { // DDL/DML分隔功能 split bool + + // 使用count(*)计算受影响行数 + realRowCount bool } // ExplainInfo 执行计划信息 @@ -2235,7 +2239,8 @@ func (s *session) parseOptions(sql string) { Print: viper.GetBool("queryPrint"), - split: viper.GetBool("split"), + split: viper.GetBool("split"), + realRowCount: viper.GetBool("realRowCount"), } if s.opt.split || s.opt.check || s.opt.Print { @@ -5736,6 +5741,73 @@ func (s *session) getExplainInfo(sql string, sqlId string) { } } +// getRealRowCount: 获取真正的受影响行数 +func (s *session) getRealRowCount(sql string, sqlId string) { + + if s.hasError() { + return + } + + var newRecord *Record + if s.Inc.EnableFingerprint && sqlId != "" { + newRecord = &Record{ + Buf: new(bytes.Buffer), + } + } + r := s.myRecord + + var value int + rows, err := s.Raw(sql) + if rows != nil { + defer rows.Close() + } + + if err != nil { + // log.Error(err) + log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) + if myErr, ok := err.(*mysqlDriver.MySQLError); ok { + s.AppendErrorMessage(myErr.Message) + if newRecord != nil { + newRecord.AppendErrorMessage(myErr.Message) + } + } else { + s.AppendErrorMessage(err.Error()) + if newRecord != nil { + newRecord.AppendErrorMessage(myErr.Message) + } + } + return + } else { + for rows.Next() { + rows.Scan(&value) + } + } + + // log.Info(sql) + // log.Info(value) + + r.AffectedRows = value + if newRecord != nil { + newRecord.AffectedRows = r.AffectedRows + } + + if s.Inc.MaxUpdateRows > 0 && r.AffectedRows > int(s.Inc.MaxUpdateRows) { + switch r.Type.(type) { + case *ast.DeleteStmt, *ast.UpdateStmt: + s.AppendErrorNo(ER_UDPATE_TOO_MUCH_ROWS, + r.AffectedRows, s.Inc.MaxUpdateRows) + if newRecord != nil { + newRecord.AppendErrorNo(ER_UDPATE_TOO_MUCH_ROWS, + r.AffectedRows, s.Inc.MaxUpdateRows) + } + } + } + + if newRecord != nil { + s.sqlFingerprint[sqlId] = newRecord + } +} + func (s *session) explainOrAnalyzeSql(sql string) { // // 如果没有表结构,或者新增表 or 新增列时,不做explain @@ -5749,38 +5821,62 @@ func (s *session) explainOrAnalyzeSql(sql string) { return } - if s.DBVersion < 50600 { + if s.opt.realRowCount { + // dml转换成select rw, err := NewRewrite(sql) if err != nil { log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) s.AppendErrorMessage(err.Error()) } else { - rw, err = rw.Rewrite() + rw, err = rw.RewriteDML2Select() if err != nil { log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) s.AppendErrorMessage(err.Error()) } else { - sql = rw.NewSQL - if sql == "" { - return + stmt, err := NewRewrite(rw.NewSQL) + if err != nil { + log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) + s.AppendErrorMessage(err.Error()) + } else { + sql = stmt.select2Count() + // log.Info(sql) + s.getRealRowCount(sql, sqlId) + } + } + } + return + } else { + if s.DBVersion < 50600 { + rw, err := NewRewrite(sql) + if err != nil { + log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) + s.AppendErrorMessage(err.Error()) + } else { + rw, err = rw.RewriteDML2Select() + if err != nil { + log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err) + s.AppendErrorMessage(err.Error()) + } else { + sql = rw.NewSQL + if sql == "" { + return + } } } } - } - - var explain []string - if s.isMiddleware() { - explain = append(explain, s.opt.middlewareExtend) - } + var explain []string - explain = append(explain, "EXPLAIN ") - explain = append(explain, sql) + if s.isMiddleware() { + explain = append(explain, s.opt.middlewareExtend) + } - // rows := s.getExplainInfo(strings.Join(explain, "")) - s.getExplainInfo(strings.Join(explain, ""), sqlId) + explain = append(explain, "EXPLAIN ") + explain = append(explain, sql) - // s.AnlyzeExplain(rows) + // rows := s.getExplainInfo(strings.Join(explain, "")) + s.getExplainInfo(strings.Join(explain, ""), sqlId) + } } func (s *session) AnlyzeExplain(rows []ExplainInfo) { diff --git a/session/session_inception_backup_test.go b/session/session_inception_backup_test.go index ed0f624c..023a5d2a 100644 --- a/session/session_inception_backup_test.go +++ b/session/session_inception_backup_test.go @@ -50,6 +50,8 @@ type testSessionIncBackupSuite struct { sqlMode string // 时间戳类型是否需要明确指定默认值 explicitDefaultsForTimestamp bool + + realRowCount bool } func (s *testSessionIncBackupSuite) SetUpSuite(c *C) { @@ -58,6 +60,8 @@ func (s *testSessionIncBackupSuite) SetUpSuite(c *C) { c.Skip("skipping test; in TRAVIS mode") } + s.realRowCount = true + testleak.BeforeTest() s.cluster = mocktikv.NewCluster() mocktikv.BootstrapWithSingleStore(s.cluster) @@ -120,12 +124,12 @@ func (s *testSessionIncBackupSuite) TearDownTest(c *C) { } func (s *testSessionIncBackupSuite) makeSQL(c *C, tk *testkit.TestKit, sql string) *testkit.Result { - a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=1;--port=3306;--enable-ignore-warnings;*/ + a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=1;--port=3306;--enable-ignore-warnings;real_row_count=%v;*/ inception_magic_start; use test_inc; %s; inception_magic_commit;` - res := tk.MustQueryInc(fmt.Sprintf(a, sql)) + res := tk.MustQueryInc(fmt.Sprintf(a, s.realRowCount, sql)) // 需要成功执行 for _, row := range res.Rows() { diff --git a/session/session_inception_exec_test.go b/session/session_inception_exec_test.go index a84534d8..9314dbab 100644 --- a/session/session_inception_exec_test.go +++ b/session/session_inception_exec_test.go @@ -48,6 +48,8 @@ type testSessionIncExecSuite struct { sqlMode string // 时间戳类型是否需要明确指定默认值 explicitDefaultsForTimestamp bool + + realRowCount bool } func (s *testSessionIncExecSuite) SetUpSuite(c *C) { @@ -56,6 +58,7 @@ func (s *testSessionIncExecSuite) SetUpSuite(c *C) { c.Skip("skipping test; in TRAVIS mode") } + s.realRowCount = true testleak.BeforeTest() s.cluster = mocktikv.NewCluster() mocktikv.BootstrapWithSingleStore(s.cluster) @@ -116,16 +119,16 @@ func (s *testSessionIncExecSuite) TearDownTest(c *C) { } } -func makeExecSQL(tk *testkit.TestKit, sql string) *testkit.Result { +func (s *testSessionIncExecSuite) makeExecSQL(tk *testkit.TestKit, sql string) *testkit.Result { session.CheckAuditSetting(config.GetGlobalConfig()) - a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ + a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=0;--port=3306;--enable-ignore-warnings;real_row_count=%v;*/ inception_magic_start; use test_inc; %s; inception_magic_commit;` - return tk.MustQueryInc(fmt.Sprintf(a, sql)) + return tk.MustQueryInc(fmt.Sprintf(a, s.realRowCount, sql)) } func (s *testSessionIncExecSuite) testErrorCode(c *C, sql string, errors ...*session.SQLError) { @@ -135,7 +138,7 @@ func (s *testSessionIncExecSuite) testErrorCode(c *C, sql string, errors ...*ses session.CheckAuditSetting(config.GetGlobalConfig()) - res := makeExecSQL(s.tk, sql) + res := s.makeExecSQL(s.tk, sql) row := res.Rows()[int(s.tk.Se.AffectedRows())-1] errCode := 0 @@ -172,10 +175,9 @@ func (s *testSessionIncExecSuite) TestBegin(c *C) { c.Skip("skipping test; in TRAVIS mode") } - tk := testkit.NewTestKitWithInit(c, s.store) - res := tk.MustQueryInc("drop table if exists t1;create table t1(id int);") + res := s.tk.MustQueryInc("drop table if exists t1;create table t1(id int);") - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + c.Assert(len(res.Rows()), Equals, 1, Commentf("%v", res.Rows())) for _, row := range res.Rows() { c.Assert(row[2], Equals, "2") @@ -184,10 +186,9 @@ func (s *testSessionIncExecSuite) TestBegin(c *C) { } func (s *testSessionIncExecSuite) TestNoSourceInfo(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - res := tk.MustQueryInc("inception_magic_start;\ncreate table t1(id int);") + res := s.tk.MustQueryInc("inception_magic_start;\ncreate table t1(id int);") - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + c.Assert(int(s.tk.Se.AffectedRows()), Equals, 1) for _, row := range res.Rows() { c.Assert(row[2], Equals, "2") @@ -196,11 +197,10 @@ func (s *testSessionIncExecSuite) TestNoSourceInfo(c *C) { } func (s *testSessionIncExecSuite) TestWrongDBName(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - res := tk.MustQueryInc(`/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ + res := s.tk.MustQueryInc(`/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ inception_magic_start;create table t1(id int);inception_magic_commit;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + c.Assert(int(s.tk.Se.AffectedRows()), Equals, 1) for _, row := range res.Rows() { c.Assert(row[2], Equals, "2") @@ -209,11 +209,10 @@ inception_magic_start;create table t1(id int);inception_magic_commit;`) } func (s *testSessionIncExecSuite) TestEnd(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - res := tk.MustQueryInc(`/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ + res := s.tk.MustQueryInc(`/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ inception_magic_start;use test_inc;create table t1(id int);`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 3) + c.Assert(int(s.tk.Se.AffectedRows()), Equals, 3) row := res.Rows()[2] c.Assert(row[2], Equals, "2") @@ -221,7 +220,6 @@ inception_magic_start;use test_inc;create table t1(id int);`) } func (s *testSessionIncExecSuite) TestCreateTable(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -236,8 +234,8 @@ func (s *testSessionIncExecSuite) TestCreateTable(c *C) { s.testErrorCode(c, sql) // 表存在 - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int);create table t1(id int);") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);create table t1(id int);") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Table 't1' already exists.") @@ -248,32 +246,32 @@ func (s *testSessionIncExecSuite) TestCreateTable(c *C) { // 主键 config.GetGlobalConfig().Inc.CheckPrimaryKey = true - res = makeExecSQL(tk, "drop table t1;create table t1(id int);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table t1;create table t1(id int);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set a primary key for table 't1'.") config.GetGlobalConfig().Inc.CheckPrimaryKey = false // 数据类型 警告 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 bit);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 bit);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c1'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 enum('red', 'blue', 'black'));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 enum('red', 'blue', 'black'));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c1'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 set('red', 'blue', 'black'));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 set('red', 'blue', 'black'));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c1'.") // char列建议 config.GetGlobalConfig().Inc.MaxCharLength = 100 - res = makeExecSQL(tk, `drop table if exists t1;create table t1(id int,c1 char(200));`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, `drop table if exists t1;create table t1(id int,c1 char(200));`) + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set column 'c1' to VARCHAR type.") @@ -297,15 +295,15 @@ func (s *testSessionIncExecSuite) TestCreateTable(c *C) { config.GetGlobalConfig().Inc.EnableIdentiferKeyword = false config.GetGlobalConfig().Inc.CheckIdentifier = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int, TABLES varchar(20),`c1$` varchar(20),c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int, TABLES varchar(20),`c1$` varchar(20),c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Identifier 'TABLES' is keyword in MySQL.\nIdentifier 'c1$' is invalid, valid options: [a-z|A-Z|0-9|_].\nIdentifier name 'c1234567890123456789012345678901234567890123456789012345678901234567890' is too long.") // 列注释 config.GetGlobalConfig().Inc.CheckColumnComment = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(20));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(20));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Column 'c1' in table 't1' have no comments.") @@ -313,39 +311,39 @@ func (s *testSessionIncExecSuite) TestCreateTable(c *C) { // 表注释 config.GetGlobalConfig().Inc.CheckTableComment = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(20));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(20));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set comments for table 't1'.") config.GetGlobalConfig().Inc.CheckTableComment = false - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(20));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(20));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // 无效默认值 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int default '');") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int default '');") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Invalid default value for column 'c1'.") // blob/text字段 config.GetGlobalConfig().Inc.EnableBlobType = false - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 blob, c2 text);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 blob, c2 text);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Type blob/text is used in column 'c1'.\nType blob/text is used in column 'c2'.") config.GetGlobalConfig().Inc.EnableBlobType = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 blob not null);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 blob not null);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "TEXT/BLOB Column 'c1' in table 't1' can't been not null.") // 检查默认值 config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(10));") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(10));") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set Default value for column 'c1' in table 't1'") config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false @@ -353,27 +351,27 @@ func (s *testSessionIncExecSuite) TestCreateTable(c *C) { // 支持innodb引擎 config.GetGlobalConfig().Inc.EnableSetEngine = true config.GetGlobalConfig().Inc.SupportEngine = "innodb" - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = myisam;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = myisam;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Set engine to one of 'innodb'") // 禁止设置存储引擎 config.GetGlobalConfig().Inc.EnableSetEngine = false - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Cannot set engine 't1'") // 允许设置存储引擎 config.GetGlobalConfig().Inc.EnableSetEngine = true config.GetGlobalConfig().Inc.SupportEngine = "innodb" - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // 时间戳 timestamp默认值 @@ -547,8 +545,8 @@ primary key(id)) comment 'test';` s.testErrorCode(c, sql) } - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 int);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 int);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // 测试表名大小写 @@ -558,7 +556,6 @@ primary key(id)) comment 'test';` } func (s *testSessionIncExecSuite) TestDropTable(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -572,13 +569,12 @@ func (s *testSessionIncExecSuite) TestDropTable(c *C) { config.GetGlobalConfig().Inc.EnableDropTable = true - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int);drop table t1;") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);drop table t1;") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") } func (s *testSessionIncExecSuite) TestAlterTableAddColumn(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -592,48 +588,48 @@ func (s *testSessionIncExecSuite) TestAlterTableAddColumn(c *C) { sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int;" s.testErrorCode(c, sql) - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int;alter table t1 add column c1 int;") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int;alter table t1 add column c1 int;") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't1.c1' have existed.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int first;alter table t1 add column c2 int after c1;") + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int first;alter table t1 add column c2 int after c1;") for _, row := range res.Rows() { c.Assert(row[2], Not(Equals), "2") } // after 不存在的列 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 int after c1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 int after c1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't1.c1' not existed.") // 数据类型 警告 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 bit;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 bit;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c2'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 enum('red', 'blue', 'black');") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 enum('red', 'blue', 'black');") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c2'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 set('red', 'blue', 'black');") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c2 set('red', 'blue', 'black');") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'c2'.") // char列建议 config.GetGlobalConfig().Inc.MaxCharLength = 100 - res = makeExecSQL(tk, `drop table if exists t1;create table t1(id int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;create table t1(id int); alter table t1 add column c1 char(200); alter table t1 add column c2 varchar(200);`) - row = res.Rows()[int(tk.Se.AffectedRows())-2] + row = res.Rows()[int(s.tk.Se.AffectedRows())-2] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set column 'c1' to VARCHAR type.") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // 字符集 @@ -651,53 +647,53 @@ func (s *testSessionIncExecSuite) TestAlterTableAddColumn(c *C) { config.GetGlobalConfig().Inc.EnableIdentiferKeyword = false config.GetGlobalConfig().Inc.CheckIdentifier = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column TABLES varchar(20);alter table t1 add column `c1$` varchar(20);alter table t1 add column c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20);") - row = res.Rows()[int(tk.Se.AffectedRows())-3] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column TABLES varchar(20);alter table t1 add column `c1$` varchar(20);alter table t1 add column c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-3] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Identifier 'TABLES' is keyword in MySQL.") - row = res.Rows()[int(tk.Se.AffectedRows())-2] + row = res.Rows()[int(s.tk.Se.AffectedRows())-2] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Identifier 'c1$' is invalid, valid options: [a-z|A-Z|0-9|_].") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Identifier name 'c1234567890123456789012345678901234567890123456789012345678901234567890' is too long.") // 列注释 config.GetGlobalConfig().Inc.CheckColumnComment = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(20);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(20);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Column 'c1' in table 't1' have no comments.") config.GetGlobalConfig().Inc.CheckColumnComment = false // 无效默认值 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int default '';") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int default '';") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Invalid default value for column 'c1'.") // blob/text字段 config.GetGlobalConfig().Inc.EnableBlobType = false - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob;alter table t1 add column c2 text;") - row = res.Rows()[int(tk.Se.AffectedRows())-2] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob;alter table t1 add column c2 text;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-2] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Type blob/text is used in column 'c1'.") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Type blob/text is used in column 'c2'.") config.GetGlobalConfig().Inc.EnableBlobType = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob not null;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob not null;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "TEXT/BLOB Column 'c1' in table 't1' can't been not null.") // 检查默认值 config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(10);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(10);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set Default value for column 'c1' in table 't1'") config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false @@ -757,30 +753,28 @@ func (s *testSessionIncExecSuite) TestAlterTableAddColumn(c *C) { } func (s *testSessionIncExecSuite) TestAlterTableAlterColumn(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved }() - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id set default '';") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id set default '';") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Invalid default value for column 'id'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id set default '1';") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id set default '1';") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id drop default ;alter table t1 alter column id set default '1';") - row = res.Rows()[int(tk.Se.AffectedRows())-2] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 alter column id drop default ;alter table t1 alter column id set default '1';") + row = res.Rows()[int(s.tk.Se.AffectedRows())-2] c.Assert(row[2], Equals, "0") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") } func (s *testSessionIncExecSuite) TestAlterTableModifyColumn(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -790,48 +784,48 @@ func (s *testSessionIncExecSuite) TestAlterTableModifyColumn(c *C) { config.GetGlobalConfig().Inc.CheckTableComment = false sql := "" - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int first;") + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int first;") for _, row := range res.Rows() { c.Assert(row[2], Not(Equals), "2") } - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column id int after c1;") + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column id int after c1;") for _, row := range res.Rows() { c.Assert(row[2], Not(Equals), "2") } // after 不存在的列 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);alter table t1 modify column c1 int after id;") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);alter table t1 modify column c1 int after id;") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't1.c1' not existed.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int after id1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int after id1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't1.id1' not existed.") // 数据类型 警告 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id bit);alter table t1 modify column id bit;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id bit);alter table t1 modify column id bit;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'id'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id enum('red', 'blue'));alter table t1 modify column id enum('red', 'blue', 'black');") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id enum('red', 'blue'));alter table t1 modify column id enum('red', 'blue', 'black');") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'id'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id set('red'));alter table t1 modify column id set('red', 'blue', 'black');") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id set('red'));alter table t1 modify column id set('red', 'blue', 'black');") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Not supported data type on field: 'id'.") // char列建议 config.GetGlobalConfig().Inc.MaxCharLength = 100 - res = makeExecSQL(tk, `drop table if exists t1;create table t1(id int,c1 char(10)); + res = s.makeExecSQL(s.tk, `drop table if exists t1;create table t1(id int,c1 char(10)); alter table t1 modify column c1 char(200);`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set column 'c1' to VARCHAR type.") @@ -848,40 +842,40 @@ func (s *testSessionIncExecSuite) TestAlterTableModifyColumn(c *C) { // 列注释 config.GetGlobalConfig().Inc.CheckColumnComment = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 varchar(10));alter table t1 modify column c1 varchar(20);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 varchar(10));alter table t1 modify column c1 varchar(20);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Column 'c1' in table 't1' have no comments.") config.GetGlobalConfig().Inc.CheckColumnComment = false // 无效默认值 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int default '';") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 modify column c1 int default '';") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Invalid default value for column 'c1'.") // blob/text字段 config.GetGlobalConfig().Inc.EnableBlobType = false - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 varchar(10));alter table t1 modify column c1 blob;alter table t1 modify column c1 text;") - row = res.Rows()[int(tk.Se.AffectedRows())-2] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 varchar(10));alter table t1 modify column c1 blob;alter table t1 modify column c1 text;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-2] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Type blob/text is used in column 'c1'.") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Type blob/text is used in column 'c1'.") config.GetGlobalConfig().Inc.EnableBlobType = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 blob);alter table t1 modify column c1 blob not null;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 blob);alter table t1 modify column c1 blob not null;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "TEXT/BLOB Column 'c1' in table 't1' can't been not null.") // 检查默认值 config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 varchar(5));alter table t1 modify column c1 varchar(10);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 varchar(5));alter table t1 modify column c1 varchar(10);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Set Default value for column 'c1' in table 't1'") config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false @@ -891,12 +885,12 @@ func (s *testSessionIncExecSuite) TestAlterTableModifyColumn(c *C) { s.testErrorCode(c, sql, session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "int(11)", "varchar(10)")) - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 char(100));alter table t1 modify column c1 char(20);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 char(100));alter table t1 modify column c1 char(20);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(c1 varchar(100));alter table t1 modify column c1 varchar(10);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(c1 varchar(100));alter table t1 modify column c1 varchar(10);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") if s.getDBVersion(c) >= 50600 { @@ -920,20 +914,19 @@ func (s *testSessionIncExecSuite) TestAlterTableModifyColumn(c *C) { } func (s *testSessionIncExecSuite) TestAlterTableDropColumn(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved }() sql := "" - res := makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c2;") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c2;") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't1.c2' not existed.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // // drop column @@ -947,7 +940,6 @@ func (s *testSessionIncExecSuite) TestAlterTableDropColumn(c *C) { } func (s *testSessionIncExecSuite) TestInsert(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -959,35 +951,35 @@ func (s *testSessionIncExecSuite) TestInsert(c *C) { sql := "" // 表不存在 - res := makeExecSQL(tk, "insert into t1 values(1,1);") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "insert into t1 values(1,1);") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Table 'test_inc.t1' doesn't exist.") // 列数不匹配 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1,1);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1,1);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column count doesn't match value count at row 1.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1),(2,1);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1),(2,1);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column count doesn't match value count at row 2.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int not null);insert into t1(id,c1) select 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int not null);insert into t1(id,c1) select 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column count doesn't match value count at row 1.") // 列重复 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id,id) values(1,1);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id,id) values(1,1);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 'id' specified twice in table 't1'.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id,id) select 1,1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id,id) select 1,1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 'id' specified twice in table 't1'.") @@ -1006,19 +998,19 @@ func (s *testSessionIncExecSuite) TestInsert(c *C) { session.NewErr(session.ER_WITH_INSERT_VALUES)) // 列不允许为空 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int not null);insert into t1(id,c1) values(1,null);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int not null);insert into t1(id,c1) values(1,null);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 'test_inc.t1.c1' cannot be null in 1 row.") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int not null default 1);insert into t1(id,c1) values(1,null);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int not null default 1);insert into t1(id,c1) values(1,null);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 'test_inc.t1.c1' cannot be null in 1 row.") // insert select 表不存在 - res = makeExecSQL(tk, "drop table if exists t1;drop table if exists t2;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t2;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;drop table if exists t2;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t2;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Table 'test_inc.t2' doesn't exist.") @@ -1031,28 +1023,28 @@ func (s *testSessionIncExecSuite) TestInsert(c *C) { // limit config.GetGlobalConfig().Inc.CheckDMLLimit = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t1 limit 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t1 limit 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Limit is not allowed in update/delete statement.") config.GetGlobalConfig().Inc.CheckDMLLimit = false // order by rand() // config.GetGlobalConfig().Inc.CheckDMLOrderBy = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t1 order by rand();") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t1 order by rand();") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Order by rand is not allowed in select statement.") // config.GetGlobalConfig().Inc.CheckDMLOrderBy = false // 受影响行数 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1 values(1,1),(2,2);") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);insert into t1 values(1,1),(2,2);") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "2") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int );insert into t1(id,c1) select 1,null;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "1") @@ -1062,7 +1054,6 @@ func (s *testSessionIncExecSuite) TestInsert(c *C) { } func (s *testSessionIncExecSuite) TestUpdate(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -1085,24 +1076,24 @@ func (s *testSessionIncExecSuite) TestUpdate(c *C) { s.testErrorCode(c, sql, session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c2")) - res := makeExecSQL(tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); + res := s.makeExecSQL(s.tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); create table t2(id int primary key,c1 int,c2 int); update t1 inner join t2 on t1.id=t2.id2 set t1.c1=t2.c1 where c11=1;`) - row := res.Rows()[int(tk.Se.AffectedRows())-1] + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't2.id2' not existed.\nColumn 'c11' not existed.") - res = makeExecSQL(tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); create table t2(id int primary key,c1 int,c2 int); update t1,t2 t3 set t1.c1=t2.c3 where t1.id=t3.id;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't2.c3' not existed.") - res = makeExecSQL(tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); create table t2(id int primary key,c1 int,c2 int); update t1,t2 t3 set t1.c1=t2.c3 where t1.id=t3.id;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't2.c3' not existed.") @@ -1116,39 +1107,38 @@ func (s *testSessionIncExecSuite) TestUpdate(c *C) { // limit config.GetGlobalConfig().Inc.CheckDMLLimit = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1 limit 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1 limit 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Limit is not allowed in update/delete statement.") config.GetGlobalConfig().Inc.CheckDMLLimit = false // order by rand() config.GetGlobalConfig().Inc.CheckDMLOrderBy = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1 order by rand();") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1 order by rand();") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Order by is not allowed in update/delete statement.") config.GetGlobalConfig().Inc.CheckDMLOrderBy = false // 受影响行数 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);update t1 set c1 = 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "0") - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int primary key,c1 int);insert into t1 values(1,1),(2,2);update t1 set c1 = 1 where id = 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int primary key,c1 int);insert into t1 values(1,1),(2,2);update t1 set c1 = 1 where id = 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "0", Commentf("%v", res.Rows())) - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int primary key,c1 int);insert into t1 values(1,1),(2,2);update t1 set c1 = 10 where id = 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int primary key,c1 int);insert into t1 values(1,1),(2,2);update t1 set c1 = 10 where id = 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "1", Commentf("%v", res.Rows())) } func (s *testSessionIncExecSuite) TestDelete(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) saved := config.GetGlobalConfig().Inc defer func() { config.GetGlobalConfig().Inc = saved @@ -1163,18 +1153,18 @@ func (s *testSessionIncExecSuite) TestDelete(c *C) { s.testErrorCode(c, sql) // 表不存在 - res := makeExecSQL(tk, "delete from t1 where c1 = 1;") - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.makeExecSQL(s.tk, "delete from t1 where c1 = 1;") + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Table 'test_inc.t1' doesn't exist.") - // res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int);delete from t1 where c1 = 1;") - // row = res.Rows()[int(tk.Se.AffectedRows())-1] + // res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int);delete from t1 where c1 = 1;") + // row = res.Rows()[int(s.tk.Se.AffectedRows())-1] // c.Assert(row[2], Equals, "2") // c.Assert(row[4], Equals, "Column 'c1' not existed.") - // res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where c1 = 1 and c2 = 1;") - // row = res.Rows()[int(tk.Se.AffectedRows())-1] + // res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where c1 = 1 and c2 = 1;") + // row = res.Rows()[int(s.tk.Se.AffectedRows())-1] // c.Assert(row[2], Equals, "2") // c.Assert(row[4], Equals, "Column 't1.c2' not existed.") @@ -1188,49 +1178,49 @@ func (s *testSessionIncExecSuite) TestDelete(c *C) { // limit config.GetGlobalConfig().Inc.CheckDMLLimit = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1 limit 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1 limit 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Limit is not allowed in update/delete statement.") config.GetGlobalConfig().Inc.CheckDMLLimit = false // order by rand() config.GetGlobalConfig().Inc.CheckDMLOrderBy = true - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1 order by rand();") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1 order by rand();") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "1") c.Assert(row[4], Equals, "Order by is not allowed in update/delete statement.") config.GetGlobalConfig().Inc.CheckDMLOrderBy = false // 表不存在 - res = makeExecSQL(tk, `drop table if exists t1; + res = s.makeExecSQL(s.tk, `drop table if exists t1; delete from t1 where id1 =1;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Table 'test_inc.t1' doesn't exist.") - res = makeExecSQL(tk, `drop table if exists t1;create table t1(id int,c1 int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;create table t1(id int,c1 int); delete from t1 where id1 =1;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 'id1' not existed.") - res = makeExecSQL(tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); create table t2(id int primary key,c1 int,c2 int); delete t2 from t1 inner join t2 on t1.id=t2.id2 where c11=1;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "2") c.Assert(row[4], Equals, "Column 't2.id2' not existed.") - res = makeExecSQL(tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); + res = s.makeExecSQL(s.tk, `drop table if exists t1;drop table if exists t2;create table t1(id int primary key,c1 int); create table t2(id int primary key,c1 int,c2 int); delete t2 from t1 inner join t2 on t1.id=t2.id where t1.c1=1;`) - row = res.Rows()[int(tk.Se.AffectedRows())-1] + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") // 受影响行数 - res = makeExecSQL(tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1;") - row = res.Rows()[int(tk.Se.AffectedRows())-1] + res = s.makeExecSQL(s.tk, "drop table if exists t1;create table t1(id int,c1 int);delete from t1 where id = 1;") + row = res.Rows()[int(s.tk.Se.AffectedRows())-1] c.Assert(row[2], Equals, "0") c.Assert(row[6], Equals, "0") } @@ -1365,20 +1355,18 @@ func (s *testSessionIncExecSuite) TestAlterTableDropIndex(c *C) { } func (s *testSessionIncExecSuite) TestShowVariables(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - sql := "" sql = "inception show variables;" - tk.MustQueryInc(sql) - c.Assert(tk.Se.AffectedRows(), GreaterEqual, uint64(102)) + s.tk.MustQueryInc(sql) + c.Assert(s.tk.Se.AffectedRows(), GreaterEqual, uint64(102)) sql = "inception get variables;" - tk.MustQueryInc(sql) - c.Assert(tk.Se.AffectedRows(), GreaterEqual, uint64(102)) + s.tk.MustQueryInc(sql) + c.Assert(s.tk.Se.AffectedRows(), GreaterEqual, uint64(102)) sql = "inception show variables like 'backup_password';" - res := tk.MustQueryInc(sql) - row := res.Rows()[int(tk.Se.AffectedRows())-1] + res := s.tk.MustQueryInc(sql) + row := res.Rows()[int(s.tk.Se.AffectedRows())-1] if row[1].(string) != "" { c.Assert(row[1].(string)[:1], Equals, "*") } @@ -1386,8 +1374,6 @@ func (s *testSessionIncExecSuite) TestShowVariables(c *C) { // 无法审核,原因是需要自己做SetSessionManager // func (s *testSessionIncExecSuite) TestShowProcesslist(c *C) { -// tk := testkit.NewTestKitWithInit(c, s.store) - // sql := "" // sql = "inception show processlist;" // tk.MustQueryInc(sql) @@ -1398,37 +1384,35 @@ func (s *testSessionIncExecSuite) TestShowVariables(c *C) { // c.Assert(tk.Se.AffectedRows(), GreaterEqual, 1) // sql = "inception show variables like 'backup_password';" -// res := tk.MustQueryInc(sql) -// row := res.Rows()[int(tk.Se.AffectedRows())-1] +// res := s.tk.MustQueryInc(sql) +// row := res.Rows()[int(s.tk.Se.AffectedRows())-1] // if row[1].(string) != "" { // c.Assert(row[1].(string)[:1], Equals, "*") // } // } func (s *testSessionIncExecSuite) TestSetVariables(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - sql := "" sql = "inception show variables;" - tk.MustQueryInc(sql) - c.Assert(tk.Se.AffectedRows(), GreaterEqual, uint64(102)) + s.tk.MustQueryInc(sql) + c.Assert(s.tk.Se.AffectedRows(), GreaterEqual, uint64(102)) // 不区分session和global.所有会话全都global级别 - tk.MustExecInc("inception set global max_keys = 20;") - tk.MustExecInc("inception set session max_keys = 10;") - result := tk.MustQueryInc("inception show variables like 'max_keys';") + s.tk.MustExecInc("inception set global max_keys = 20;") + s.tk.MustExecInc("inception set session max_keys = 10;") + result := s.tk.MustQueryInc("inception show variables like 'max_keys';") result.Check(testkit.Rows("max_keys 10")) - tk.MustExecInc("inception set ghost_default_retries = 70;") - result = tk.MustQueryInc("inception show variables like 'ghost_default_retries';") + s.tk.MustExecInc("inception set ghost_default_retries = 70;") + result = s.tk.MustQueryInc("inception show variables like 'ghost_default_retries';") result.Check(testkit.Rows("ghost_default_retries 70")) - tk.MustExecInc("inception set osc_max_thread_running = 100;") - result = tk.MustQueryInc("inception show variables like 'osc_max_thread_running';") + s.tk.MustExecInc("inception set osc_max_thread_running = 100;") + result = s.tk.MustQueryInc("inception show variables like 'osc_max_thread_running';") result.Check(testkit.Rows("osc_max_thread_running 100")) // 无效参数 - res, err := tk.ExecInc("inception set osc_max_thread_running1 = 100;") + res, err := s.tk.ExecInc("inception set osc_max_thread_running1 = 100;") c.Assert(err, NotNil) c.Assert(err.Error(), Equals, "无效参数") if res != nil { @@ -1436,7 +1420,7 @@ func (s *testSessionIncExecSuite) TestSetVariables(c *C) { } // 无效参数 - res, err = tk.ExecInc("inception set osc_max_thread_running = 'abc';") + res, err = s.tk.ExecInc("inception set osc_max_thread_running = 'abc';") c.Assert(err, NotNil) c.Assert(err.Error(), Equals, "[variable:1232]Incorrect argument type to variable 'osc_max_thread_running'") if res != nil { @@ -1481,7 +1465,7 @@ func (s *testSessionIncExecSuite) getDBVersion(c *C) int { sql := "show variables like 'version'" - res := makeExecSQL(s.tk, sql) + res := s.makeExecSQL(s.tk, sql) c.Assert(int(s.tk.Se.AffectedRows()), Equals, 2) row := res.Rows()[int(s.tk.Se.AffectedRows())-1] @@ -1524,7 +1508,7 @@ func (s *testSessionIncExecSuite) getSQLMode(c *C) string { sql := "show variables like 'sql_mode'" - res := makeExecSQL(s.tk, sql) + res := s.makeExecSQL(s.tk, sql) c.Assert(int(s.tk.Se.AffectedRows()), Equals, 2) row := res.Rows()[int(s.tk.Se.AffectedRows())-1] @@ -1553,7 +1537,7 @@ func (s *testSessionIncExecSuite) getExplicitDefaultsForTimestamp(c *C) bool { sql := "show variables where Variable_name='explicit_defaults_for_timestamp';" - res := makeExecSQL(s.tk, sql) + res := s.makeExecSQL(s.tk, sql) c.Assert(int(s.tk.Se.AffectedRows()), Equals, 2) row := res.Rows()[int(s.tk.Se.AffectedRows())-1] diff --git a/session/session_inception_test.go b/session/session_inception_test.go index 56654ed1..1fc8e5c0 100644 --- a/session/session_inception_test.go +++ b/session/session_inception_test.go @@ -52,6 +52,8 @@ type testSessionIncSuite struct { explicitDefaultsForTimestamp bool rows [][]interface{} + + realRowCount bool } func (s *testSessionIncSuite) SetUpSuite(c *C) { @@ -59,6 +61,7 @@ func (s *testSessionIncSuite) SetUpSuite(c *C) { if testing.Short() { c.Skip("skipping test; in TRAVIS mode") } + s.realRowCount = true testleak.BeforeTest() s.cluster = mocktikv.NewCluster() @@ -265,23 +268,23 @@ func (s *testSessionIncSuite) makeSQL(sql string) *testkit.Result { tk := s.tk session.CheckAuditSetting(config.GetGlobalConfig()) - a := `/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ + a := `/*--user=test;--password=test;--host=127.0.0.1;--check=1;--backup=0;--port=3306;--enable-ignore-warnings;real_row_count=%v;*/ inception_magic_start; use test_inc; %s; inception_magic_commit;` - return tk.MustQueryInc(fmt.Sprintf(a, sql)) + return tk.MustQueryInc(fmt.Sprintf(a, s.realRowCount, sql)) } func (s *testSessionIncSuite) execSQL(c *C, sql string) *testkit.Result { config.GetGlobalConfig().Inc.EnableDropTable = true session.CheckAuditSetting(config.GetGlobalConfig()) - a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=0;--port=3306;--enable-ignore-warnings;*/ + a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=0;--port=3306;--enable-ignore-warnings;real_row_count=%v;*/ inception_magic_start; use test_inc; %s; inception_magic_commit;` - res := s.tk.MustQueryInc(fmt.Sprintf(a, sql)) + res := s.tk.MustQueryInc(fmt.Sprintf(a, s.realRowCount, sql)) for _, row := range res.Rows() { c.Assert(row[2], Not(Equals), "2", Commentf("%v", row)) @@ -1427,7 +1430,11 @@ insert into t1 values(1),(2),(3);` sql = `drop table if exists t2;create table t2 like t1; insert into t2 select id from t1;` s.testErrorCode(c, sql) - s.testAffectedRows(c, 1) + if s.realRowCount { + s.testAffectedRows(c, 0) + } else { + s.testAffectedRows(c, 1) + } config.GetGlobalConfig().Inc.EnableSelectStar = true s.execSQL(c, "drop table if exists tt1;create table tt1(id int,c1 int);insert into tt1 values(1,1);")