From ac810d3e98140a17a4f4c9c33d21a883d1f53d0c Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Fri, 15 Feb 2019 10:01:49 +0800 Subject: [PATCH] add new -report-type `query-type` echo "select * from tb" | soar -report-type query-type --- ast/testdata/TestQueryType.golden | 86 +++++++++++++++++++++++++++++++ ast/token.go | 13 +++++ ast/token_test.go | 16 ++++++ cmd/soar/soar.go | 4 ++ cmd/soar/soar_test.go | 2 +- common/config.go | 5 ++ doc/report_type.md | 8 +++ 7 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 ast/testdata/TestQueryType.golden diff --git a/ast/testdata/TestQueryType.golden b/ast/testdata/TestQueryType.golden new file mode 100644 index 0000000..7b9b540 --- /dev/null +++ b/ast/testdata/TestQueryType.golden @@ -0,0 +1,86 @@ +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +DELETE +DELETE +DELETE +DELETE +DELETE +UPDATE +UPDATE +UPDATE +UPDATE +INSERT +INSERT +INSERT +INSERT +REPLACE +REPLACE +REPLACE +REPLACE +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +SELECT +ALTER +ALTER +ALTER +SELECT +CREATE +SELECT +ALTER diff --git a/ast/token.go b/ast/token.go index 4267e83..9021661 100644 --- a/ast/token.go +++ b/ast/token.go @@ -986,3 +986,16 @@ func NewLines(buf []byte) int { } return newLines } + +// QueryType get query type such as SELECT/INSERT/DELETE/CREATE/ALTER +func QueryType(sql string) string { + var typ string + tokens := Tokenizer(sql) + for _, token := range tokens { + if val, ok := mySQLKeywords[token.Val]; ok { + typ = val + break + } + } + return typ +} diff --git a/ast/token_test.go b/ast/token_test.go index 2c273d2..d72e718 100644 --- a/ast/token_test.go +++ b/ast/token_test.go @@ -237,3 +237,19 @@ func TestNewLines(t *testing.T) { } common.Log.Debug("Exiting function: %s", common.GetFunctionName()) } + +func TestQueryType(t *testing.T) { + common.Log.Debug("Entering function: %s", common.GetFunctionName()) + var testSQLs = []string{ + `(select 1)`, + } + err := common.GoldenDiff(func() { + for _, buf := range append(testSQLs, common.TestSQLs...) { + fmt.Println(QueryType(buf)) + } + }, t.Name(), update) + if nil != err { + t.Fatal(err) + } + common.Log.Debug("Exiting function: %s", common.GetFunctionName()) +} diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 5bfa8b2..2c67c5e 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -163,6 +163,10 @@ func main() { _, err = pretty.Println(ast.Tokenize(sql)) common.LogIfWarn(err, "") continue + case "query-type": + // query type by first key word + fmt.Println(ast.QueryType(sql)) + continue default: // SQL 签名 id = query.Id(fingerprint) diff --git a/cmd/soar/soar_test.go b/cmd/soar/soar_test.go index 9fbae11..c538e5a 100644 --- a/cmd/soar/soar_test.go +++ b/cmd/soar/soar_test.go @@ -63,7 +63,7 @@ func Test_Main_More(_ *testing.T) { orgRerportType := common.Config.ReportType for _, typ := range []string{ "json", "html", "markdown", "fingerprint", "compress", "pretty", "rewrite", - "ast", "tiast", "ast-json", "tiast-json", "tokenize", "lint", "tables", + "ast", "tiast", "ast-json", "tiast-json", "tokenize", "lint", "tables", "query-type", } { common.Config.ReportType = typ main() diff --git a/common/config.go b/common/config.go index fec8d19..973ab54 100644 --- a/common/config.go +++ b/common/config.go @@ -884,6 +884,11 @@ var ReportTypes = []ReportType{ Description: "以 JSON 格式输出 SQL 使用的库表名", Example: `echo "select * from film" | soar -report-type meta`, }, + { + Name: "query-type", + Description: "SQL 语句的请求类型", + Example: `echo "select * from film" | soar -report-type query-type`, + }, { Name: "fingerprint", Description: "输出SQL的指纹", diff --git a/doc/report_type.md b/doc/report_type.md index 67794a3..1c19fb0 100644 --- a/doc/report_type.md +++ b/doc/report_type.md @@ -66,6 +66,14 @@ echo "select * from film" | soar -report-type tiast-json ```bash echo "select * from film" | soar -report-type meta ``` +## query-type +* **Description**:SQL 语句的请求类型 + +* **Example**: + +```bash +echo "select * from film" | soar -report-type query-type +``` ## fingerprint * **Description**:输出SQL的指纹 -- GitLab