Skip to content

Commit 94c2485

Browse files
author
tkc
committed
Refactoring depend on linter issues
1 parent f1db08b commit 94c2485

File tree

13 files changed

+154
-96
lines changed

13 files changed

+154
-96
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
![sql-dog](https://github.com/tkc/sql-dog/workflows/sql-dog/badge.svg?branch=master)
2+
![reviewdog](https://github.com/tkc/sql-dog/workflows/reviewdog/badge.svg)
23

34
# sql-dog
45

@@ -25,13 +26,13 @@ https://github.com/tkc/sql-dog/blob/master/cmd/lint/main.go#L12-L49
2526
run query analyzer and show report.
2627

2728
```bash
28-
$ go run ./cmd/lint
29+
$ go run ./cmd/lint/main.go
2930
```
3031

3132
clear general_log table records.
3233

3334
```bash
34-
$ go run ./cmd/clean
35+
$ go run ./cmd/clean/main.go
3536
```
3637

3738
### Features

cmd/clean/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ func main() {
1111
"localhost",
1212
3306)
1313
repo := mysql.NewGeneralLogRepository(handler)
14-
repo.Clear()
14+
if err := repo.Clear(); err != nil {
15+
panic(err)
16+
}
1517
}

cmd/lint/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"sql-dog/src/domain/model"
5+
"sql-dog/src/infrastructure/datastore/mysql"
6+
"sql-dog/src/usecase/presenter"
7+
"sql-dog/src/usecase/services"
8+
)
9+
10+
func createValidation() model.Validator {
11+
return model.Validator{
12+
Ignores: []string{
13+
"DELETE FROM Ignores table_name",
14+
},
15+
Nodes: []model.ValidatorNode{
16+
{
17+
TableName: "table_name",
18+
Operations: []model.ValidateOperation{
19+
{
20+
Type: model.OpTypeEq,
21+
Column: "require_column_a",
22+
},
23+
{
24+
Type: model.OpTypeEq,
25+
Column: "require_column_b",
26+
},
27+
},
28+
StmtTypePattern: []model.StmtType{
29+
model.StmtTypeSelect,
30+
model.StmtTypeDelete,
31+
},
32+
},
33+
},
34+
}
35+
}
36+
37+
func main() {
38+
v := createValidation()
39+
40+
handler, _, _ := mysql.NewMySQLHandler(
41+
"root",
42+
"password",
43+
"localhost",
44+
3306)
45+
46+
reportService := services.NewReportService(
47+
mysql.NewGeneralLogRepository(handler),
48+
services.NewAnalyzerService(),
49+
services.NewValidatesService(),
50+
presenter.NewReportPresenter(),
51+
)
52+
53+
reportService.Show(v)
54+
}

src/domain/model/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package model
22

33
import (
4+
// test_driver
45
_ "github.com/pingcap/parser/test_driver"
56
)
67

src/infrastructure/datastore/mysql/general_log_repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package mysql
22

33
import (
4-
"gorm.io/gorm"
54
"sql-dog/src/domain/model"
5+
6+
"gorm.io/gorm"
67
)
78

89
const logTableName = "general_log"

src/usecase/presenter/report.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package presenter
22

33
import (
4+
"sql-dog/src/domain/model"
5+
46
"github.com/fatih/color"
57
"github.com/kyokomi/emoji"
6-
"sql-dog/src/domain/model"
78
)
89

910
type reportPresenter struct{}

src/usecase/services/analyzer_service.go

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package services
22

33
import (
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

1512
type 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

7572
func (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+
//}

src/usecase/services/analyzer_service_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package services
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"log"
65
"sql-dog/src/domain/model"
76
"testing"
7+
8+
"github.com/stretchr/testify/assert"
89
)
910

1011
func TestSelectQueries(t *testing.T) {
@@ -29,7 +30,7 @@ func TestSelectQueries(t *testing.T) {
2930
{
3031
Type: model.OpTypeIn,
3132
Column: "c_a",
32-
Value: int64(1),
33+
Value: int64(1),
3334
},
3435
},
3536
StmtType: model.StmtTypeSelect,
@@ -44,12 +45,12 @@ func TestSelectQueries(t *testing.T) {
4445
{
4546
Type: model.OpTypeEq,
4647
Column: "c_a",
47-
Value: int64(1),
48+
Value: int64(1),
4849
},
4950
{
5051
Type: model.OpTypeIn,
5152
Column: "c_b",
52-
Value: int64(1),
53+
Value: int64(1),
5354
},
5455
},
5556
StmtType: model.StmtTypeSelect,
@@ -64,49 +65,57 @@ func TestSelectQueries(t *testing.T) {
6465
{
6566
Type: model.OpTypeEq,
6667
Column: "c_a",
67-
Value: int64(1),
68+
Value: int64(1),
6869
},
6970
{
7071
Type: model.OpTypeEq,
7172
Column: "c_b",
72-
Value: int64(1),
73+
Value: int64(1),
7374
},
7475
{
7576
Type: model.OpTypeEq,
7677
Column: "c_c",
77-
Value: int64(1),
78+
Value: int64(1),
7879
},
7980
},
8081
StmtType: model.StmtTypeSelect,
8182
NotNullColumns: []string{"deleted_at"},
8283
},
8384
},
8485
{
85-
query: "select * from table_a as tb_a, (SELECT c_a ,min(c_b) as from table_b group by c_c) as tb_b where c_a = 1 and c_b in (1) and c_c = 1",
86+
query: "select * from table_a where c_a = 1 and c_b in (1) and c_c = 1",
8687
analyzer: model.Analyzer{
8788
TableName: "table_a",
8889
Operations: []model.AnalyzerOperation{
8990
{
9091
Type: model.OpTypeEq,
9192
Column: "c_a",
92-
Value: int64(1),
93-
93+
Value: int64(1),
9494
},
9595
{
9696
Type: model.OpTypeIn,
9797
Column: "c_b",
98-
Value: int64(1),
98+
Value: int64(1),
9999
},
100100
{
101101
Type: model.OpTypeEq,
102102
Column: "c_c",
103-
Value: int64(1),
103+
Value: int64(1),
104104
},
105105
},
106106
StmtType: model.StmtTypeSelect,
107107
NotNullColumns: nil,
108108
},
109109
},
110+
{
111+
query: "select * from table_a as test WHERE `deleted_at` IS NULL",
112+
analyzer: model.Analyzer{
113+
TableName: "test",
114+
Operations: nil,
115+
StmtType: model.StmtTypeSelect,
116+
NotNullColumns: []string{"deleted_at"},
117+
},
118+
},
110119
}
111120

112121
analyzerService := NewAnalyzerService()
@@ -163,4 +172,3 @@ func TestInsertQueries(t *testing.T) {
163172
assert.Equal(t, c.analyzer.InsertColumns, analyzer.InsertColumns)
164173
}
165174
}
166-

src/usecase/services/interfaces.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package services
22

33
import (
4-
"github.com/pingcap/parser/ast"
54
"sql-dog/src/domain/model"
5+
6+
"github.com/pingcap/parser/ast"
67
)
78

89
type AnalyzerService interface {
@@ -14,6 +15,6 @@ type ReportService interface {
1415
Show(validator model.Validator)
1516
}
1617

17-
type ValidatesService interface {
18+
type ValidateService interface {
1819
Validates(analyzers []model.Analyzer, validator model.Validator) []model.Report
1920
}

0 commit comments

Comments
 (0)