11package services
22
33import (
4- "github.com/pingcap/parser/ast"
5- "github.com/pingcap/parser/test_driver"
6- _ "github.com/pingcap/parser/test_driver"
7- "log"
8- "reflect"
94 "sql-dog/src/domain/model"
105 "strings"
116
127 "github.com/pingcap/parser"
8+ "github.com/pingcap/parser/ast"
9+ "github.com/pingcap/parser/test_driver"
1310)
1411
1512type analyzerService struct {}
@@ -38,47 +35,52 @@ type Visitor struct {
3835 Analyzer model.Analyzer
3936}
4037
41- func checkNilValue (in ast.Node , analyzer * model.Analyzer ) {
42- if patternInExpr , ok := in .(* ast.PatternInExpr ); ok {
43- if valueExpr , ok := patternInExpr .List [0 ].(* test_driver.ValueExpr ); ok {
44- if valueExpr .Datum .GetValue () == nil {
45- var nullValueOperation model.AnalyzerNullValueOperation
46- nullValueOperation .TableName = analyzer .TableName
47- nullValueOperation .Value = valueExpr .Datum .GetValue ()
48- nullValueOperation .Type = model .OpTypeIn
49- if columnNameExpr , ok := patternInExpr .Expr .(* ast.ColumnNameExpr ); ok {
50- nullValueOperation .Column = columnNameExpr .Name .String ()
51- }
52- analyzer .NullValueOperation = append (analyzer .NullValueOperation , nullValueOperation )
53- }
54- }
55- }
56-
57- if binaryOperationExpr , ok := in .(* ast.BinaryOperationExpr ); ok {
58- if binaryOperationExpr .Op .String () == string (model .OpTypeEq ) {
59- if valueExpr , ok := binaryOperationExpr .R .(* test_driver.ValueExpr ); ok {
60- if valueExpr .Datum .GetValue () == nil {
61- var nullValueOperation model.AnalyzerNullValueOperation
62- nullValueOperation .TableName = analyzer .TableName
63- nullValueOperation .Value = valueExpr .Datum .GetValue ()
64- nullValueOperation .Type = model .OpTypeEq
65- if columnNameExpr , ok := binaryOperationExpr .R .(* ast.ColumnNameExpr ); ok {
66- nullValueOperation .Column = columnNameExpr .Name .String ()
67- }
68- analyzer .NullValueOperation = append (analyzer .NullValueOperation , nullValueOperation )
69- }
70- }
71- }
72- }
73- }
38+ // func checkNilValue(in ast.Node, analyzer *model.Analyzer) {
39+ // if patternInExpr, ok := in.(*ast.PatternInExpr); ok {
40+ // if valueExpr, ok := patternInExpr.List[0].(*test_driver.ValueExpr); ok {
41+ // if valueExpr.Datum.GetValue() == nil {
42+ // var nullValueOperation model.AnalyzerNullValueOperation
43+ // nullValueOperation.TableName = analyzer.TableName
44+ // nullValueOperation.Value = valueExpr.Datum.GetValue()
45+ // nullValueOperation.Type = model.OpTypeIn
46+ // if columnNameExpr, ok := patternInExpr.Expr.(*ast.ColumnNameExpr); ok {
47+ // nullValueOperation.Column = columnNameExpr.Name.String()
48+ // }
49+ // analyzer.NullValueOperation = append(analyzer.NullValueOperation, nullValueOperation)
50+ // }
51+ // }
52+ // }
53+ //
54+ // if binaryOperationExpr, ok := in.(*ast.BinaryOperationExpr); ok {
55+ // if binaryOperationExpr.Op.String() == string(model.OpTypeEq) {
56+ // if valueExpr, ok := binaryOperationExpr.R.(*test_driver.ValueExpr); ok {
57+ // if valueExpr.Datum.GetValue() == nil {
58+ // var nullValueOperation model.AnalyzerNullValueOperation
59+ // nullValueOperation.TableName = analyzer.TableName
60+ // nullValueOperation.Value = valueExpr.Datum.GetValue()
61+ // nullValueOperation.Type = model.OpTypeEq
62+ // if columnNameExpr, ok := binaryOperationExpr.R.(*ast.ColumnNameExpr); ok {
63+ // nullValueOperation.Column = columnNameExpr.Name.String()
64+ // }
65+ // analyzer.NullValueOperation = append(analyzer.NullValueOperation, nullValueOperation)
66+ // }
67+ // }
68+ // }
69+ // }
70+ // }
7471
7572func (v * Visitor ) Enter (in ast.Node ) (ast.Node , bool ) {
76-
7773 // TableName
78- if tableName , ok := in .(* ast.TableName ); ok {
79- // TODO : sub query
74+ if TableSource , ok := in .(* ast.TableSource ); ok {
8075 if len (v .Analyzer .TableName ) == 0 {
81- v .Analyzer .TableName = tableName .Name .String ()
76+ if tableName , ok := TableSource .Source .(* ast.TableName ); ok {
77+ if len (v .Analyzer .TableName ) == 0 {
78+ v .Analyzer .TableName = tableName .Name .String ()
79+ }
80+ }
81+ if len (TableSource .AsName .String ()) > 0 {
82+ v .Analyzer .TableName = TableSource .AsName .String ()
83+ }
8284 }
8385 }
8486
@@ -126,7 +128,6 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
126128
127129 // patternInExpr
128130 if patternInExpr , ok := in .(* ast.PatternInExpr ); ok {
129-
130131 var operation model.AnalyzerOperation
131132 operation .Type = model .OpTypeIn
132133
@@ -135,11 +136,11 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
135136 }
136137
137138 if valueExpr , ok := patternInExpr .List [0 ].(* test_driver.ValueExpr ); ok {
138- operation .Value = valueExpr .Datum .GetInt64 ()
139+ operation .Value = valueExpr .Datum .GetInt64 ()
139140 }
140141
141142 //if columnNameExpr, ok := patternInExpr.List[0].(*ast.ColumnNameExpr); ok {
142- //operation.Value = columnNameExpr..GetInt64()
143+ //operation.Value = columnNameExpr..GetInt64()
143144 //}
144145
145146 v .Analyzer .Operations = append (v .Analyzer .Operations , operation )
@@ -148,12 +149,10 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
148149 // BinaryOperationExpr
149150 if binaryOperationExpr , ok := in .(* ast.BinaryOperationExpr ); ok {
150151 if binaryOperationExpr .Op .String () == string (model .OpTypeEq ) {
151-
152152 var operation model.AnalyzerOperation
153153 operation .Type = model .OpType (binaryOperationExpr .Op .String ())
154154
155155 if columnNameExpr , ok := binaryOperationExpr .L .(* ast.ColumnNameExpr ); ok {
156- // TODO : table name
157156 operation .Column = formatColumnName (columnNameExpr .Name .String (), v .Analyzer .TableName )
158157 }
159158
@@ -187,7 +186,7 @@ func formatColumnName(column string, tableName string) string {
187186 return column
188187}
189188
190- func debug (in interface {}) {
191- log .Print ("---debug---" )
192- log .Print (reflect .TypeOf (in ))
193- }
189+ // func debug(in interface{}) {
190+ // log.Print("---debug---")
191+ // log.Print(reflect.TypeOf(in))
192+ // }
0 commit comments