Skip to content

Commit ac01c66

Browse files
author
tkc
committed
Refactoring depend on linter issues
1 parent 9327ea8 commit ac01c66

File tree

13 files changed

+153
-144
lines changed

13 files changed

+153
-144
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
}

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package mysql
22

33
import (
4-
"gorm.io/gorm"
54
"sql-dog/src/domain/model"
6-
)
75

8-
const logTableName = "general_log"
6+
"gorm.io/gorm"
7+
)
98

109
type generalLogRepository struct {
1110
db *gorm.DB
@@ -25,14 +24,13 @@ func NewGeneralLogRepository(
2524
}
2625

2726
func (r *generalLogRepository) Clear() error {
28-
r.db.Exec("truncate table general_log")
29-
return nil
27+
return r.db.Exec("truncate table general_log").Error
3028
}
3129

3230
func (r *generalLogRepository) GetQueries() ([]string, error) {
3331
var logs []model.GeneralLog
3432
if err := r.db.
35-
Table(logTableName).
33+
Table("general_log").
3634
Select("command_type, argument").
3735
Where("command_type in ('Execute', 'Query')").
3836
Find(&logs).Error; err != nil {

src/infrastructure/datastore/mysql/handler.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ func NewMySQLHandler(username, password, host string, port int) (*gorm.DB, func(
1818
)
1919
}
2020

21-
//func NewMySQLHandler() (*gorm.DB, func() error, error) {
22-
// return newSQLHandler(
23-
// "root",
24-
// "password",
25-
// "localhost",
26-
// 3306,
27-
// "mysql",
28-
// )
29-
//}
30-
3121
func newSQLHandler(userName, password, host string, port int, dbname string) (*gorm.DB, func() error, error) {
3222
if _, err := time.LoadLocation("UTC"); err != nil {
3323
return nil, nil, err
@@ -56,6 +46,5 @@ func newSQLHandler(userName, password, host string, port int, dbname string) (*g
5646
return nil, nil, err
5747
}
5848

59-
conn.Set("gorm:table_options", "ENGINE=InnoDB")
6049
return conn, db.Close, nil
6150
}

src/usecase/presenter/report.go

Lines changed: 3 additions & 2 deletions
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{}
@@ -15,7 +16,7 @@ func NewReportPresenter() ReportPresenter {
1516
func (p reportPresenter) Show(reports []model.Report) {
1617
if len(reports) == 0 {
1718
beer := emoji.Sprint(":beer:")
18-
color.Magenta(beer + beer + beer + "No Report !!")
19+
color.Magenta(beer + "No Report !!")
1920
}
2021

2122
for _, report := range reports {

src/usecase/services/analyzer_service.go

Lines changed: 57 additions & 57 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,53 @@ 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+
// TODO : `checkNilValue` is unused (deadcode)
39+
//func checkNilValue(in ast.Node, analyzer *model.Analyzer) {
40+
// if patternInExpr, ok := in.(*ast.PatternInExpr); ok {
41+
// if valueExpr, ok := patternInExpr.List[0].(*test_driver.ValueExpr); ok {
42+
// if valueExpr.Datum.GetValue() == nil {
43+
// var nullValueOperation model.AnalyzerNullValueOperation
44+
// nullValueOperation.TableName = analyzer.TableName
45+
// nullValueOperation.Value = valueExpr.Datum.GetValue()
46+
// nullValueOperation.Type = model.OpTypeIn
47+
// if columnNameExpr, ok := patternInExpr.Expr.(*ast.ColumnNameExpr); ok {
48+
// nullValueOperation.Column = columnNameExpr.Name.String()
49+
// }
50+
// analyzer.NullValueOperation = append(analyzer.NullValueOperation, nullValueOperation)
51+
// }
52+
// }
53+
// }
54+
//
55+
// if binaryOperationExpr, ok := in.(*ast.BinaryOperationExpr); ok {
56+
// if binaryOperationExpr.Op.String() == string(model.OpTypeEq) {
57+
// if valueExpr, ok := binaryOperationExpr.R.(*test_driver.ValueExpr); ok {
58+
// if valueExpr.Datum.GetValue() == nil {
59+
// var nullValueOperation model.AnalyzerNullValueOperation
60+
// nullValueOperation.TableName = analyzer.TableName
61+
// nullValueOperation.Value = valueExpr.Datum.GetValue()
62+
// nullValueOperation.Type = model.OpTypeEq
63+
// if columnNameExpr, ok := binaryOperationExpr.R.(*ast.ColumnNameExpr); ok {
64+
// nullValueOperation.Column = columnNameExpr.Name.String()
65+
// }
66+
// analyzer.NullValueOperation = append(analyzer.NullValueOperation, nullValueOperation)
67+
// }
68+
// }
69+
// }
70+
// }
71+
//}
7472

7573
func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
76-
77-
// TableName
78-
if tableName, ok := in.(*ast.TableName); ok {
79-
// TODO : sub query
74+
// Set tableName
75+
if TableSource, ok := in.(*ast.TableSource); ok {
8076
if len(v.Analyzer.TableName) == 0 {
81-
v.Analyzer.TableName = tableName.Name.String()
77+
if tableName, ok := TableSource.Source.(*ast.TableName); ok {
78+
if len(v.Analyzer.TableName) == 0 {
79+
v.Analyzer.TableName = tableName.Name.String()
80+
}
81+
}
82+
if len(TableSource.AsName.String()) > 0 {
83+
v.Analyzer.TableName = TableSource.AsName.String()
84+
}
8285
}
8386
}
8487

@@ -120,26 +123,25 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
120123
if columnNameExpr, ok := isNullExpr.Expr.(*ast.ColumnNameExpr); ok {
121124
v.Analyzer.NotNullColumns = append(
122125
v.Analyzer.NotNullColumns,
123-
formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName))
126+
v.formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName))
124127
}
125128
}
126129

127-
// patternInExpr
130+
// PatternInExpr
128131
if patternInExpr, ok := in.(*ast.PatternInExpr); ok {
129-
130132
var operation model.AnalyzerOperation
131133
operation.Type = model.OpTypeIn
132134

133135
if columnNameExpr, ok := patternInExpr.Expr.(*ast.ColumnNameExpr); ok {
134-
operation.Column = formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName)
136+
operation.Column = v.formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName)
135137
}
136138

137139
if valueExpr, ok := patternInExpr.List[0].(*test_driver.ValueExpr); ok {
138-
operation.Value = valueExpr.Datum.GetInt64()
140+
operation.Value = valueExpr.Datum.GetInt64()
139141
}
140142

141143
//if columnNameExpr, ok := patternInExpr.List[0].(*ast.ColumnNameExpr); ok {
142-
//operation.Value = columnNameExpr..GetInt64()
144+
//operation.Value = columnNameExpr..GetInt64()
143145
//}
144146

145147
v.Analyzer.Operations = append(v.Analyzer.Operations, operation)
@@ -148,13 +150,11 @@ func (v *Visitor) Enter(in ast.Node) (ast.Node, bool) {
148150
// BinaryOperationExpr
149151
if binaryOperationExpr, ok := in.(*ast.BinaryOperationExpr); ok {
150152
if binaryOperationExpr.Op.String() == string(model.OpTypeEq) {
151-
152153
var operation model.AnalyzerOperation
153154
operation.Type = model.OpType(binaryOperationExpr.Op.String())
154155

155156
if columnNameExpr, ok := binaryOperationExpr.L.(*ast.ColumnNameExpr); ok {
156-
// TODO : table name
157-
operation.Column = formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName)
157+
operation.Column = v.formatColumnName(columnNameExpr.Name.String(), v.Analyzer.TableName)
158158
}
159159

160160
if valueExpr, ok := binaryOperationExpr.R.(*test_driver.ValueExpr); ok {
@@ -176,7 +176,7 @@ func (v *Visitor) Leave(in ast.Node) (ast.Node, bool) {
176176
return in, true
177177
}
178178

179-
func formatColumnName(column string, tableName string) string {
179+
func (v *Visitor) formatColumnName(column string, tableName string) string {
180180
slice := strings.Split(column, ".")
181181
if len(slice) == 1 {
182182
return column
@@ -187,7 +187,7 @@ func formatColumnName(column string, tableName string) string {
187187
return column
188188
}
189189

190-
func debug(in interface{}) {
191-
log.Print("---debug---")
192-
log.Print(reflect.TypeOf(in))
193-
}
190+
//func (v *Visitor) debug(in interface{}) {
191+
// log.Print("---debug---")
192+
// log.Print(reflect.TypeOf(in))
193+
//}

0 commit comments

Comments
 (0)