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