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

fix -report-type qurery-type bug

  GRANT SELECT ON *.* to user@'host'
  REVOKE INSERT ON *.* FROM 'jeffrey'@'localhost';
上级 e3496fd7
SELECT
SELECT
GRANT
REVOKE
SELECT
SELECT
SELECT
SELECT
......
......@@ -446,6 +446,7 @@ var mySQLKeywords = map[string]string{
"geometry": "GEOMETRY",
"geometrycollection": "GEOMETRYCOLLECTION",
"global": "GLOBAL",
"grant": "GRANT",
"group": "GROUP",
"group_concat": "GROUP_CONCAT",
"having": "HAVING",
......@@ -512,6 +513,7 @@ var mySQLKeywords = map[string]string{
"reorganize": "REORGANIZE",
"repair": "REPAIR",
"replace": "REPLACE",
"revoke": "REVOKE",
"right": "RIGHT",
"rlike": "REGEXP",
"rollback": "ROLLBACK",
......@@ -989,13 +991,14 @@ func NewLines(buf []byte) int {
// QueryType get query type such as SELECT/INSERT/DELETE/CREATE/ALTER
func QueryType(sql string) string {
var typ string
tokens := Tokenizer(sql)
tokens := Tokenize(sql)
for _, token := range tokens {
if val, ok := mySQLKeywords[token.Val]; ok {
typ = val
break
// use strings.Fields for 'ALTER TABLE' token split
for _, tk := range strings.Fields(strings.TrimSpace(token.Val)) {
if val, ok := mySQLKeywords[strings.ToLower(tk)]; ok {
return val
}
}
}
return typ
return ""
}
......@@ -241,8 +241,14 @@ func TestNewLines(t *testing.T) {
func TestQueryType(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
var testSQLs = []string{
`/*comment*/ select 1`,
`(select 1)`,
`grant select on *.* to user@'localhost'`,
`REVOKE INSERT ON *.* FROM 'jeffrey'@'localhost';`,
}
// fmt.Println(testSQLs[len(testSQLs)-1])
// fmt.Println(QueryType(testSQLs[len(testSQLs)-1]))
// return
err := common.GoldenDiff(func() {
for _, buf := range append(testSQLs, common.TestSQLs...) {
fmt.Println(QueryType(buf))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册