Skip to content

Commit

Permalink
Merge pull request #164 from hanchuanchuan/update-osc-ddl-parse
Browse files Browse the repository at this point in the history
fix: 优化使用pt工具时注释导致alter子句解析失败的问题
  • Loading branch information
hanchuanchuan authored Mar 6, 2020
2 parents 6af334c + 7c3baf2 commit bc41cdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
11 changes: 9 additions & 2 deletions session/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ func (s *session) getAlterTablePostPart(sql string, isPtOSC bool) string {

// getAlterPartSql 获取alter子句部分
func (s *session) getAlterPartSql(sql string) (string, bool) {
sql = strings.Replace(sql, "\n", " ", -1)
sql = strings.Replace(sql, "\r", " ", -1)
// sql = strings.Replace(sql, "\n", " ", -1)
// sql = strings.Replace(sql, "\r", " ", -1)

charsetInfo, collation := s.sessionVars.GetCharsetInfo()
stmtNodes, _, err := s.parser.Parse(sql, charsetInfo, collation)
Expand All @@ -865,7 +865,14 @@ func (s *session) getAlterPartSql(sql string) (string, bool) {
var builder strings.Builder
var columns []string

if len(stmtNodes) == 0 {
s.AppendErrorMessage(fmt.Sprintf("未正确解析ALTER语句: %s", sql))
log.Error(fmt.Sprintf("未正确解析ALTER语句: %s", sql))
return "", false
}

for _, stmtNode := range stmtNodes {

switch node := stmtNode.(type) {
case *ast.AlterTableStmt:
for _, alter := range node.Specs {
Expand Down
21 changes: 15 additions & 6 deletions session/session_inception_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,23 +1261,29 @@ func (s *testSessionIncExecSuite) TestAlterTablePtOSC(c *C) {
s.mustRunExec(c, sql)

// 删除后添加列
sql = "alter table t1 drop column c1;alter table t1 add column c1 varchar(20);"
sql = `# 这是一条注释
alter table t1 drop column c1;alter table t1 add column c1 varchar(20);`
s.testErrorCode(c, sql)

sql = "alter table t1 drop column c1,add column c1 varchar(20);"
sql = `/* 这是一条注释 */
alter table t1 drop column c1,add column c1 varchar(20);`
s.testErrorCode(c, sql)

sql = "alter table t1 drop column c1,add column c1 varchar(20) comment '123';"
s.testErrorCode(c, sql)

sql = "alter table t1 add column c2 varchar(20) comment '!@#$%^&*()_+[]{}\\|;:\",.<>/?';"
sql = `-- 这是一条注释
alter table t1 add column c2 varchar(20) comment '!@#$%^&*()_+[]{}\\|;:",.<>/?';`
s.testErrorCode(c, sql)

sql = "alter table t1 add column c3 varchar(20) comment \"!@#$%^&*()_+[]{}\\|;:',.<>/?\";"
s.testErrorCode(c, sql)

sql = "alter table t1 add column `c4` varchar(20) comment \"!@#$%^&*()_+[]{}\\|;:',.<>/?\";"
s.testErrorCode(c, sql)

sql = "alter table t1 add column `c5` varchar(20) comment \"!@#$%^&*()_+[]{}\\|;:',.<>/?\"; -- 测试注释"
s.testErrorCode(c, sql)
}

func (s *testSessionIncExecSuite) TestAlterTableGhost(c *C) {
Expand All @@ -1299,16 +1305,19 @@ func (s *testSessionIncExecSuite) TestAlterTableGhost(c *C) {
s.mustRunExec(c, sql)

// 删除后添加列
sql = "alter table t1 drop column c1;alter table t1 add column c1 varchar(20);"
sql = `# 这是一条注释
alter table t1 drop column c1;alter table t1 add column c1 varchar(20);`
s.testErrorCode(c, sql)

sql = "alter table t1 drop column c1,add column c1 varchar(20);"
sql = `/* 这是一条注释 */
alter table t1 drop column c1,add column c1 varchar(20);`
s.testErrorCode(c, sql)

sql = "alter table t1 drop column c1,add column c1 varchar(20) comment '123';"
s.testErrorCode(c, sql)

sql = "alter table t1 add column c2 varchar(20) comment '!@#$%^&*()_+[]{}\\|;:\",.<>/?';"
sql = `-- 这是一条注释
alter table t1 add column c2 varchar(20) comment '!@#$%^&*()_+[]{}\\|;:",.<>/?';`
s.testErrorCode(c, sql)

sql = "alter table t1 add column c3 varchar(20) comment \"!@#$%^&*()_+[]{}\\|;:',.<>/?\";"
Expand Down

0 comments on commit bc41cdd

Please sign in to comment.