Skip to content

Commit

Permalink
-report-type add ast-json, tiast-json
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Dec 22, 2018
1 parent 0acae27 commit b237d5e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
34 changes: 18 additions & 16 deletions ast/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package ast

import (
"encoding/json"

"github.com/XiaoMi/soar/common"

"github.com/kr/pretty"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"

// for pincap parser
_ "github.com/pingcap/tidb/types/parser_driver"
)
Expand All @@ -43,20 +46,19 @@ func PrintPrettyStmtNode(sql, charset, collation string) {
}
}

// TiVisitor TODO:
type TiVisitor struct {
EnterFunc func(node ast.Node) bool
LeaveFunc func(node ast.Node) bool
}

// Enter TODO:
func (visitor *TiVisitor) Enter(n ast.Node) (node ast.Node, skip bool) {
skip = visitor.EnterFunc(n)
return
}

// Leave TODO:
func (visitor *TiVisitor) Leave(n ast.Node) (node ast.Node, ok bool) {
ok = visitor.LeaveFunc(n)
return
// StmtNode2JSON TiParse AST tree into json format
func StmtNode2JSON(sql, charset, collation string) string {
var str string
tree, err := TiParse(sql, charset, collation)
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
}
54 changes: 54 additions & 0 deletions ast/vitess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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
}
22 changes: 11 additions & 11 deletions cmd/soar/soar.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/kr/pretty"
"github.com/percona/go-mysql/query"
"github.com/ziutek/mymysql/mysql"
"vitess.io/vitess/go/vt/sqlparser"
)

func main() {
Expand Down Expand Up @@ -143,20 +142,21 @@ func main() {
fmt.Println(ast.Compress(sql) + common.Config.Delimiter)
continue
case "ast":
// SQL 抽象语法树
var tree sqlparser.Statement
tree, err = sqlparser.Parse(sql)
if err != nil {
fmt.Println(err)
} else {
_, err = pretty.Println(tree)
common.LogIfWarn(err, "")
}
// print vitess AST data struct
ast.PrintPrettyVitessStmtNode(sql)
continue
case "ast-json":
// print vitess SQL AST into json format
fmt.Println(ast.VitessStmtNode2JSON(sql))
continue
case "tiast":
// TiDB SQL 抽象语法树
// print TiDB AST data struct
ast.PrintPrettyStmtNode(sql, "", "")
continue
case "tiast-json":
// print TiDB SQL AST into json format
fmt.Println(ast.StmtNode2JSON(sql, "", ""))
continue
case "tokenize":
// SQL 切词
_, err = pretty.Println(ast.Tokenize(sql))
Expand Down

0 comments on commit b237d5e

Please sign in to comment.