Skip to content

Commit 15ecb8d

Browse files
author
Tkc
authored
Merge pull request #3 from tkc/feat/null_value_check
Add null value report
2 parents 97bdb5e + 633a4af commit 15ecb8d

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed

src/domain/model/model.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func (v ValidatorNode) HasError() bool {
6464
if v.HasInsertColumnsError() {
6565
return true
6666
}
67+
if v.HasNullValueOperationError() {
68+
return true
69+
}
6770
return false
6871
}
6972

@@ -94,6 +97,10 @@ func (v ValidatorNode) HasInsertColumnsError() bool {
9497
return false
9598
}
9699

100+
func (v ValidatorNode) HasNullValueOperationError() bool {
101+
return len(v.NullValueOperation) > 0
102+
}
103+
97104
type Report struct {
98105
Analyzer Analyzer
99106
ValidatorNode *ValidatorNode

src/usecase/presenter/report.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func (p reportPresenter) Show(reports []model.Report) {
5959

6060
if len(report.ValidatorNode.NullValueOperation) > 0 {
6161
color.Magenta("NullValueOperation")
62+
for _, notNullColumn := range report.ValidatorNode.NullValueOperation {
63+
color.White(notNullColumn.Column)
64+
}
6265
}
6366
}
6467
}

src/usecase/services/analyzer_service.go

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/usecase/services/validation_service.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
const errorMessageNullColumn = "fail null column check : "
88
const errorMessageInsertColumn = "fail insert column check :"
99
const errorMessageOperation = "fail operations check : "
10-
11-
//const errorMessageNilValueOperation = "fail nil value check : "
10+
const errorMessageNilValueOperation = "fail nil value check : "
1211

1312
type validateService struct{}
1413

@@ -30,25 +29,26 @@ func (v validateService) Validates(analyzers []model.Analyzer, validator model.V
3029
return reports
3130
}
3231

33-
func validate(analyzer model.Analyzer, node *model.ValidatorNode, ignores []string) *model.Report {
32+
func isIgnoreTable(analyzer model.Analyzer, node *model.ValidatorNode, ignores []string) bool {
3433
for _, ignore := range ignores {
3534
if analyzer.SQL == ignore {
36-
return nil
35+
return true
3736
}
3837
}
39-
4038
if analyzer.TableName != node.TableName {
41-
return nil
39+
return true
4240
}
43-
4441
stmtTypeMatch := false
4542
for _, stmtTypes := range node.StmtTypePattern {
4643
if analyzer.StmtType == stmtTypes {
4744
stmtTypeMatch = true
4845
}
4946
}
47+
return !stmtTypeMatch
48+
}
5049

51-
if !stmtTypeMatch {
50+
func validate(analyzer model.Analyzer, node *model.ValidatorNode, ignores []string) *model.Report {
51+
if isIgnoreTable(analyzer, node, ignores) {
5252
return nil
5353
}
5454

@@ -94,6 +94,13 @@ func validate(analyzer model.Analyzer, node *model.ValidatorNode, ignores []stri
9494
}
9595
}
9696

97+
if len(analyzer.NullValueOperation) > 0 {
98+
node.NullValueOperation = analyzer.NullValueOperation
99+
for _, analyzerOperation := range analyzer.NullValueOperation {
100+
node.Messages = append(node.Messages, errorMessageNilValueOperation+analyzerOperation.Column)
101+
}
102+
}
103+
97104
if node.HasError() {
98105
return &model.Report{
99106
Analyzer: analyzer,

0 commit comments

Comments
 (0)