@@ -35,42 +35,41 @@ type Visitor struct {
3535 Analyzer model.Analyzer
3636}
3737
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- //}
38+ func (v * Visitor ) checkNilValue (in ast.Node ) {
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 = v.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+ // v.Analyzer.NullValueOperation = append(v.Analyzer.NullValueOperation, nullValueOperation)
50+ // }
51+ // }
52+ //}
7153
72- func (v * Visitor ) Enter (in ast.Node ) (ast.Node , bool ) {
73- // TableName
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 = v .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+ v .Analyzer .NullValueOperation = append (v .Analyzer .NullValueOperation , nullValueOperation )
66+ }
67+ }
68+ }
69+ }
70+ }
71+
72+ func (v * Visitor ) tableName (in ast.Node ) {
7473 if TableSource , ok := in .(* ast.TableSource ); ok {
7574 if len (v .Analyzer .TableName ) == 0 {
7675 if tableName , ok := TableSource .Source .(* ast.TableName ); ok {
@@ -83,9 +82,9 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
8382 }
8483 }
8584 }
85+ }
8686
87- //checkNilValue(in, &v.Analyzer)
88-
87+ func (v * Visitor ) stmtType (in ast.Node ) {
8988 if insertStmt , ok := in .(* ast.InsertStmt ); ok {
9089 // TODO : sub query
9190 if len (v .Analyzer .StmtType ) == 0 {
@@ -95,79 +94,79 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
9594 }
9695 }
9796 }
98-
9997 if _ , ok := in .(* ast.SelectStmt ); ok {
10098 // TODO : sub query
10199 if len (v .Analyzer .StmtType ) == 0 {
102100 v .Analyzer .StmtType = model .StmtTypeSelect
103101 }
104102 }
105-
106103 if _ , ok := in .(* ast.UpdateStmt ); ok {
107104 // TODO : sub query
108105 if len (v .Analyzer .StmtType ) == 0 {
109106 v .Analyzer .StmtType = model .StmtTypeUpdate
110107 }
111108 }
112-
113109 if _ , ok := in .(* ast.DeleteStmt ); ok {
114110 // TODO : sub query
115111 if len (v .Analyzer .StmtType ) == 0 {
116112 v .Analyzer .StmtType = model .StmtTypeDelete
117113 }
118114 }
115+ }
119116
120- // IsNullExpr
117+ func ( v * Visitor ) isNullExpr ( in ast. Node ) {
121118 if isNullExpr , ok := in .(* ast.IsNullExpr ); ok {
122119 if columnNameExpr , ok := isNullExpr .Expr .(* ast.ColumnNameExpr ); ok {
123120 v .Analyzer .NotNullColumns = append (
124121 v .Analyzer .NotNullColumns ,
125122 formatColumnName (columnNameExpr .Name .String (), v .Analyzer .TableName ))
126123 }
127124 }
125+ }
128126
129- // patternInExpr
127+ func ( v * Visitor ) patternInExpr ( in ast. Node ) {
130128 if patternInExpr , ok := in .(* ast.PatternInExpr ); ok {
131129 var operation model.AnalyzerOperation
132130 operation .Type = model .OpTypeIn
133-
134131 if columnNameExpr , ok := patternInExpr .Expr .(* ast.ColumnNameExpr ); ok {
135132 operation .Column = formatColumnName (columnNameExpr .Name .String (), v .Analyzer .TableName )
136133 }
137-
138134 if valueExpr , ok := patternInExpr .List [0 ].(* test_driver.ValueExpr ); ok {
139135 operation .Value = valueExpr .Datum .GetInt64 ()
140136 }
141-
142137 //if columnNameExpr, ok := patternInExpr.List[0].(*ast.ColumnNameExpr); ok {
143138 //operation.Value = columnNameExpr..GetInt64()
144139 //}
145-
146140 v .Analyzer .Operations = append (v .Analyzer .Operations , operation )
147141 }
142+ }
148143
149- // BinaryOperationExpr
144+ func ( v * Visitor ) binaryOperationExpr ( in ast. Node ) {
150145 if binaryOperationExpr , ok := in .(* ast.BinaryOperationExpr ); ok {
151146 if binaryOperationExpr .Op .String () == string (model .OpTypeEq ) {
152147 var operation model.AnalyzerOperation
153148 operation .Type = model .OpType (binaryOperationExpr .Op .String ())
154-
155149 if columnNameExpr , ok := binaryOperationExpr .L .(* ast.ColumnNameExpr ); ok {
156150 operation .Column = formatColumnName (columnNameExpr .Name .String (), v .Analyzer .TableName )
157151 }
158-
159152 if valueExpr , ok := binaryOperationExpr .R .(* test_driver.ValueExpr ); ok {
160153 operation .Value = valueExpr .Datum .GetValue ()
161154 }
162-
163155 if columnNameExpr , ok := binaryOperationExpr .R .(* ast.ColumnNameExpr ); ok {
164156 operation .Value = columnNameExpr .Name .String ()
165157 }
166-
167158 v .Analyzer .Operations = append (v .Analyzer .Operations , operation )
168159 }
169160 }
161+ }
170162
163+ func (v * Visitor ) Enter (in ast.Node ) (ast.Node , bool ) {
164+ v .tableName (in )
165+ v .stmtType (in )
166+ v .isNullExpr (in )
167+ v .patternInExpr (in )
168+ v .binaryOperationExpr (in )
169+ v .checkNilValue (in )
171170 return in , false
172171}
173172
0 commit comments