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

-report-type add ast-json, tiast-json

上级 0acae27f
...@@ -17,11 +17,14 @@ ...@@ -17,11 +17,14 @@
package ast package ast
import ( import (
"encoding/json"
"github.com/XiaoMi/soar/common" "github.com/XiaoMi/soar/common"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/pingcap/parser" "github.com/pingcap/parser"
"github.com/pingcap/parser/ast" "github.com/pingcap/parser/ast"
// for pincap parser // for pincap parser
_ "github.com/pingcap/tidb/types/parser_driver" _ "github.com/pingcap/tidb/types/parser_driver"
) )
...@@ -43,20 +46,19 @@ func PrintPrettyStmtNode(sql, charset, collation string) { ...@@ -43,20 +46,19 @@ func PrintPrettyStmtNode(sql, charset, collation string) {
} }
} }
// TiVisitor TODO: // StmtNode2JSON TiParse AST tree into json format
type TiVisitor struct { func StmtNode2JSON(sql, charset, collation string) string {
EnterFunc func(node ast.Node) bool var str string
LeaveFunc func(node ast.Node) bool tree, err := TiParse(sql, charset, collation)
} if err != nil {
common.Log.Warning(err.Error())
// Enter TODO: } else {
func (visitor *TiVisitor) Enter(n ast.Node) (node ast.Node, skip bool) { b, err := json.MarshalIndent(tree, "", " ")
skip = visitor.EnterFunc(n) if err != nil {
return common.Log.Error(err.Error())
} } else {
str = string(b)
// Leave TODO: }
func (visitor *TiVisitor) Leave(n ast.Node) (node ast.Node, ok bool) { }
ok = visitor.LeaveFunc(n) return str
return
} }
/*
* Copyright 2018 Xiaomi, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ast
import (
"encoding/json"
"github.com/XiaoMi/soar/common"
"github.com/kr/pretty"
"vitess.io/vitess/go/vt/sqlparser"
)
// PrintPrettyVitessStmtNode print vitess AST struct data
func PrintPrettyVitessStmtNode(sql string) {
tree, err := sqlparser.Parse(sql)
if err != nil {
common.Log.Warning(err.Error())
} else {
_, err = pretty.Println(tree)
common.LogIfWarn(err, "")
}
}
// VitessStmtNode2JSON vitess AST tree into json format
func VitessStmtNode2JSON(sql string) string {
var str string
tree, err := sqlparser.Parse(sql)
if err != nil {
common.Log.Warning(err.Error())
} else {
b, err := json.MarshalIndent(tree, "", " ")
if err != nil {
common.Log.Error(err.Error())
} else {
str = string(b)
}
}
return str
}
...@@ -31,7 +31,6 @@ import ( ...@@ -31,7 +31,6 @@ import (
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/percona/go-mysql/query" "github.com/percona/go-mysql/query"
"github.com/ziutek/mymysql/mysql" "github.com/ziutek/mymysql/mysql"
"vitess.io/vitess/go/vt/sqlparser"
) )
func main() { func main() {
...@@ -143,20 +142,21 @@ func main() { ...@@ -143,20 +142,21 @@ func main() {
fmt.Println(ast.Compress(sql) + common.Config.Delimiter) fmt.Println(ast.Compress(sql) + common.Config.Delimiter)
continue continue
case "ast": case "ast":
// SQL 抽象语法树 // print vitess AST data struct
var tree sqlparser.Statement ast.PrintPrettyVitessStmtNode(sql)
tree, err = sqlparser.Parse(sql) continue
if err != nil { case "ast-json":
fmt.Println(err) // print vitess SQL AST into json format
} else { fmt.Println(ast.VitessStmtNode2JSON(sql))
_, err = pretty.Println(tree)
common.LogIfWarn(err, "")
}
continue continue
case "tiast": case "tiast":
// TiDB SQL 抽象语法树 // print TiDB AST data struct
ast.PrintPrettyStmtNode(sql, "", "") ast.PrintPrettyStmtNode(sql, "", "")
continue continue
case "tiast-json":
// print TiDB SQL AST into json format
fmt.Println(ast.StmtNode2JSON(sql, "", ""))
continue
case "tokenize": case "tokenize":
// SQL 切词 // SQL 切词
_, err = pretty.Println(ast.Tokenize(sql)) _, err = pretty.Println(ast.Tokenize(sql))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册