提交 fb13892e 编写于 作者: martianzhang's avatar martianzhang

fix #221 build pass with test cases

上级 d50ad5b7
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/pingcap/parser/ast"
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
...@@ -1691,42 +1692,52 @@ func MergeAlterTables(sqls ...string) map[string]string { ...@@ -1691,42 +1692,52 @@ func MergeAlterTables(sqls ...string) map[string]string {
for _, sql := range sqls { for _, sql := range sqls {
sql = strings.Trim(sql, common.Config.Delimiter) sql = strings.Trim(sql, common.Config.Delimiter)
stmt, _ := sqlparser.Parse(sql) stmts, err := TiParse(sql, "", "")
if err != nil {
common.Log.Warn(err.Error())
continue
}
// stmt, _ := sqlparser.Parse(sql)
alterSQL := "" alterSQL := ""
dbName := "" dbName := ""
tableName := "" tableName := ""
for _, stmt := range stmts {
switch n := stmt.(type) { switch n := stmt.(type) {
case *sqlparser.DDL: case *ast.AlterTableStmt:
// 注意: 表名和库名不区分大小写 // 注意: 表名和库名不区分大小写
tableName = strings.ToLower(n.Table.Name.String()) tableName = n.Table.Name.L
dbName = strings.ToLower(n.Table.Qualifier.String()) dbName = n.Table.Schema.L
switch n.Action {
case "rename":
if alterExp.MatchString(sql) { if alterExp.MatchString(sql) {
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, "")) common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, "")) alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else if renameExp.MatchString(sql) {
common.Log.Debug("rename renameExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else {
common.Log.Warn("rename not match: ALTER %v %v", tableName, sql)
} }
case "alter": case *ast.CreateIndexStmt:
if alterExp.MatchString(sql) { tableName = n.Table.Name.L
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, "")) dbName = n.Table.Schema.L
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else if createIndexExp.MatchString(sql) {
buf := createIndexExp.ReplaceAllString(sql, "") buf := createIndexExp.ReplaceAllString(sql, "")
idxName := strings.TrimSpace(indexNameExp.FindString(buf)) idxName := strings.TrimSpace(indexNameExp.FindString(buf))
buf = string([]byte(buf)[strings.Index(buf, "("):]) buf = string([]byte(buf)[strings.Index(buf, "("):])
common.Log.Error(buf) common.Log.Error(buf)
common.Log.Debug("alter createIndexExp: ALTER %v ADD INDEX %v %v", tableName, "ADD INDEX", idxName, buf) common.Log.Debug("alter createIndexExp: ALTER %v ADD INDEX %v %v", tableName, "ADD INDEX", idxName, buf)
alterSQL = fmt.Sprint("ADD INDEX", " "+idxName+" ", buf) alterSQL = fmt.Sprint("ADD INDEX", " "+idxName+" ", buf)
case *ast.RenameTableStmt:
// 注意: 表名和库名不区分大小写
tableName = n.OldTable.Name.L
dbName = n.OldTable.Schema.L
if alterExp.MatchString(sql) {
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else if renameExp.MatchString(sql) {
common.Log.Debug("rename renameExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else {
common.Log.Warn("rename not match: ALTER %v %v", tableName, sql)
} }
default: default:
} }
} }
if alterSQL != "" && tableName != "" && tableName != "dual" { if alterSQL != "" && tableName != "" && tableName != "dual" {
if dbName == "" { if dbName == "" {
alterSQLs["`"+tableName+"`"] = append(alterSQLs["`"+tableName+"`"], alterSQL) alterSQLs["`"+tableName+"`"] = append(alterSQLs["`"+tableName+"`"], alterSQL)
......
...@@ -660,7 +660,7 @@ func TestMergeAlterTables(t *testing.T) { ...@@ -660,7 +660,7 @@ func TestMergeAlterTables(t *testing.T) {
// table name quote in back ticks // table name quote in back ticks
"alter table `t3`add index `idx_a`(a)", "alter table `t3`add index `idx_a`(a)",
"alter table`t3`drop index`idx_b`(b)", "alter table`t3`drop index`idx_b`",
} }
alterSQLs := MergeAlterTables(sqls...) alterSQLs := MergeAlterTables(sqls...)
......
`customer` : ALTER TABLE `customer` ADD INDEX part_of_name (name(10)) ; `customer` : ALTER TABLE `customer` ADD INDEX part_of_name (name(10)) ;
`db`.`old_table` : ALTER TABLE `db`.`old_table` RENAME new_table ;
`old_table` : ALTER TABLE `old_table` RENAME TO new_table, RENAME AS new_table ;
`sakila`.`t1` : ALTER TABLE `sakila`.`t1` add index `idx_col`(`col`), add UNIQUE index `idx_col`(`col`), add index `idx_ID`(`ID`) ; `sakila`.`t1` : ALTER TABLE `sakila`.`t1` add index `idx_col`(`col`), add UNIQUE index `idx_col`(`col`), add index `idx_ID`(`ID`) ;
`t1` : ALTER TABLE `t1` RENAME INDEX idx_a TO idx_b, RENAME KEY idx_a TO idx_b, MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column', CHANGE b a INT NOT NULL ; `t1` : ALTER TABLE `t1` RENAME COLUMN a TO b, RENAME INDEX idx_a TO idx_b, RENAME KEY idx_a TO idx_b, MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column', CHANGE b a INT NOT NULL ;
`t2` : ALTER TABLE `t2` DROP COLUMN c, DROP COLUMN d, ADD COLUMN C int, ADD COLUMN D int FIRST, ADD COLUMN E int AFTER D ; `t2` : ALTER TABLE `t2` DROP COLUMN c, DROP COLUMN d, ADD COLUMN C int, ADD COLUMN D int FIRST, ADD COLUMN E int AFTER D ;
`t3` : ALTER TABLE `t3` add index `idx_a`(a), drop index`idx_b`(b) ; `t3` : ALTER TABLE `t3` add index `idx_a`(a), drop index`idx_b` ;
`test_bb` : ALTER TABLE `test_bb` ADD INDEX idx_test_cca (test_cc) ; `test_bb` : ALTER TABLE `test_bb` ADD INDEX idx_test_cca (test_cc) ;
...@@ -8,7 +8,10 @@ ...@@ -8,7 +8,10 @@
resultSetNode: ast.resultSetNode{}, resultSetNode: ast.resultSetNode{},
SelectStmtOpts: &ast.SelectStmtOpts{ SelectStmtOpts: &ast.SelectStmtOpts{
Distinct: false, Distinct: false,
SQLBigResult: false,
SQLBufferResult: false,
SQLCache: true, SQLCache: true,
SQLSmallResult: false,
CalcFoundRows: false, CalcFoundRows: false,
StraightJoin: false, StraightJoin: false,
Priority: 0, Priority: 0,
......
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
{ {
"text": "select 1", "text": "select 1",
"resultFields": null, "resultFields": null,
"SQLBigResult": false,
"SQLBufferResult": false,
"SQLCache": true, "SQLCache": true,
"SQLSmallResult": false,
"CalcFoundRows": false, "CalcFoundRows": false,
"StraightJoin": false, "StraightJoin": false,
"Priority": 0, "Priority": 0,
......
...@@ -210,7 +210,7 @@ ...@@ -210,7 +210,7 @@
} }
[]ast.Token{ []ast.Token{
{Type:57348, Val:"select", i:0}, {Type:57348, Val:"select", i:0},
{Type:57593, Val:"sql_calc_found_rows", i:0}, {Type:57588, Val:"sql_calc_found_rows", i:0},
{Type:57396, Val:"col", i:0}, {Type:57396, Val:"col", i:0},
{Type:57353, Val:"from", i:0}, {Type:57353, Val:"from", i:0},
{Type:57396, Val:"tbl", i:0}, {Type:57396, Val:"tbl", i:0},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册