Skip to content

Commit 3cc6423

Browse files
author
ffffwh
committed
replace skipQueryEvent() (partially)
from string based matching to ast.
1 parent fc62b51 commit 3cc6423

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

drivers/mysql/mysql/binlog/binlog_reader.go

+20-31
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,12 @@ func ToColumnValuesV2(abstractValues []interface{}) *common.ColumnValues {
420420

421421
// If isDDL, a sql correspond to a table item, aka len(tables) == len(sqls).
422422
type parseDDLResult struct {
423-
isDDL bool
424-
table common.SchemaTable
423+
isDDL bool
424+
table common.SchemaTable
425425
extraTables []common.SchemaTable
426-
sql string
427-
ast ast.StmtNode
426+
sql string
427+
ast ast.StmtNode
428+
isExpand bool
428429
}
429430

430431
// StreamEvents
@@ -474,15 +475,15 @@ func (b *BinlogReader) handleEvent(ev *replication.BinlogEvent, entriesChannel c
474475
b.hasBeginQuery = true
475476
} else {
476477
if strings.ToUpper(query) == "COMMIT" || !b.hasBeginQuery {
478+
ddlInfo, err := resolveDDLSQL(currentSchema, query, b.skipQueryDDL)
479+
477480
var skipExpandSyntax bool
478481
if b.mysqlContext.ExpandSyntaxSupport {
479482
skipExpandSyntax = false
480483
} else {
481-
skipExpandSyntax = isExpandSyntaxQuery(query)
484+
skipExpandSyntax = isExpandSyntaxQuery(query) || ddlInfo.isExpand
482485
}
483486

484-
ddlInfo, err := resolveDDLSQL(currentSchema, query, b.skipQueryDDL)
485-
486487
schema := b.findSchema(currentSchema)
487488
currentSchemaRename := currentSchema
488489
if schema.TableSchemaRename != "" {
@@ -1026,6 +1027,15 @@ func resolveDDLSQL(currentSchema string, sql string,
10261027
setTable(v.Table.Schema.O, v.Table.Name.O)
10271028
case *ast.AlterTableStmt:
10281029
setTable(v.Table.Schema.O, v.Table.Name.O)
1030+
case *ast.RevokeStmt, *ast.RevokeRoleStmt:
1031+
result.isExpand = true
1032+
case *ast.SetPwdStmt:
1033+
result.isExpand = true
1034+
case *ast.FlushStmt:
1035+
switch v.Tp {
1036+
case ast.FlushPrivileges:
1037+
result.isExpand = true
1038+
}
10291039
case *ast.DropTableStmt:
10301040
var newTables []*ast.TableName
10311041
for i, t := range v.Tables {
@@ -1060,6 +1070,7 @@ func resolveDDLSQL(currentSchema string, sql string,
10601070
}
10611071
case *ast.CreateUserStmt, *ast.GrantStmt, *ast.DropUserStmt, *ast.AlterUserStmt:
10621072
setTable("mysql", "user")
1073+
result.isExpand = true
10631074
case *ast.RenameTableStmt:
10641075
setTable(v.OldTable.Schema.O, v.OldTable.Name.O)
10651076
// TODO handle extra tables in v.TableToTables[1:]
@@ -1094,39 +1105,17 @@ func (b *BinlogReader) skipQueryDDL(schema string, tableName string) bool {
10941105
func isExpandSyntaxQuery(sql string) bool {
10951106
sql = strings.ToLower(sql)
10961107

1097-
if strings.HasPrefix(sql, "flush privileges") {
1098-
return true
1099-
}
1100-
if strings.HasPrefix(sql, "alter user") {
1101-
return true
1102-
}
1103-
if strings.HasPrefix(sql, "create user") {
1104-
return true
1105-
}
1108+
// TODO mod pingcap/parser to support these DDLs
1109+
// and use ast instead of string-matching
11061110
if strings.HasPrefix(sql, "create function") {
11071111
return true
11081112
}
11091113
if strings.HasPrefix(sql, "create procedure") {
11101114
return true
11111115
}
1112-
if strings.HasPrefix(sql, "drop user") {
1113-
return true
1114-
}
1115-
if strings.HasPrefix(sql, "delete from mysql.user") {
1116-
return true
1117-
}
1118-
if strings.HasPrefix(sql, "grant") {
1119-
return true
1120-
}
11211116
if strings.HasPrefix(sql, "rename user") {
11221117
return true
11231118
}
1124-
if strings.HasPrefix(sql, "revoke") {
1125-
return true
1126-
}
1127-
if strings.HasPrefix(sql, "set password") {
1128-
return true
1129-
}
11301119

11311120
return false
11321121
}

0 commit comments

Comments
 (0)