Skip to content

Commit bfde33e

Browse files
author
Tkc
authored
Merge pull request #5 from tkc/feat/join_table
Feat join table analyzer
2 parents b1d44d6 + 9e50338 commit bfde33e

32 files changed

+1710
-496
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea/
1+
.idea/
2+
config.yaml
3+
linter.yaml

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ linters-settings:
99
# threshold: 100
1010
funlen:
1111
lines: 500
12-
statements: 50
12+
statements: 80
1313
# goconst:
1414
# min-len: 2
1515
# min-occurrences: 2

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lint:
2+
golangci-lint run

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ This app will run an analysis on the log of the sql execution, go to mysql datab
1212
```sql
1313
SET GLOBAL general_log = 'ON';
1414
SET GLOBAL log_output='TABLE';
15+
16+
SET GLOBAL slow_query_log = 'ON';
17+
SET GLOBAL long_query_time = 0;
1518
```
1619

1720

cmd/clean/main.go

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

33
import (
4-
"sql-dog/src/infrastructure/datastore/mysql"
4+
"github.com/tkc/sql-dog/config"
5+
"github.com/tkc/sql-dog/src/infrastructure/datastore/mysql"
56
)
67

78
func main() {
9+
conf, err := config.ReadConfig()
10+
if err != nil {
11+
panic(err)
12+
}
13+
814
handler, _, _ := mysql.NewMySQLHandler(
9-
"root",
10-
"password",
11-
"localhost",
12-
3306)
15+
conf.Username,
16+
conf.Password,
17+
conf.Host,
18+
conf.Port,
19+
conf.RootDatabase)
1320
repo := mysql.NewGeneralLogRepository(handler)
1421
if err := repo.Clear(); err != nil {
1522
panic(err)

cmd/emulate/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"github.com/tkc/sql-dog/config"
5+
"github.com/tkc/sql-dog/src/infrastructure/datastore/mysql"
6+
"github.com/tkc/sql-dog/src/usecase/services"
7+
)
8+
9+
func main() {
10+
conf, err := config.ReadConfig()
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
handler, _, _ := mysql.NewMySQLHandler(
16+
conf.Username,
17+
conf.Password,
18+
conf.Host,
19+
conf.Port,
20+
conf.ServiceDatabase)
21+
22+
repo := mysql.NewEmulateRepository(handler)
23+
emulateService := services.NewEmulateService(repo)
24+
emulateService.Insert()
25+
}

cmd/lint/main.go

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,29 @@
11
package main
22

33
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"
4+
"github.com/tkc/sql-dog/config"
5+
"github.com/tkc/sql-dog/src/infrastructure/datastore/mysql"
6+
"github.com/tkc/sql-dog/src/usecase/presenter"
7+
"github.com/tkc/sql-dog/src/usecase/services"
88
)
99

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-
},
10+
func main() {
11+
validation, err := config.ReadLintConfig("./linter.yaml")
12+
if err != nil {
13+
panic(err)
3414
}
35-
}
3615

37-
func main() {
38-
v := createValidation()
16+
conf, err := config.ReadConfig()
17+
if err != nil {
18+
panic(err)
19+
}
3920

4021
handler, _, _ := mysql.NewMySQLHandler(
41-
"root",
42-
"password",
43-
"localhost",
44-
3306)
22+
conf.Username,
23+
conf.Password,
24+
conf.Host,
25+
conf.Port,
26+
conf.RootDatabase)
4527

4628
reportService := services.NewReportService(
4729
mysql.NewGeneralLogRepository(handler),
@@ -50,5 +32,5 @@ func main() {
5032
presenter.NewReportPresenter(),
5133
)
5234

53-
reportService.Show(v)
35+
reportService.Show(*validation)
5436
}

config.sample.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
username : "root"
2+
password : "password"
3+
host : "localhost"
4+
port : 3306
5+
6+
rootDatabase : "mysql"
7+
serviceDatabase : "service_database_name"

config/config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"io/ioutil"
5+
6+
"github.com/goccy/go-yaml"
7+
)
8+
9+
type Config struct {
10+
Username string
11+
Password string
12+
Host string
13+
Port int
14+
RootDatabase string `yaml:"rootDatabase"`
15+
ServiceDatabase string `yaml:"serviceDatabase"`
16+
}
17+
18+
func ReadConfig() (*Config, error) {
19+
buf, err := ioutil.ReadFile("./config.yaml")
20+
if err != nil {
21+
return nil, err
22+
}
23+
var config Config
24+
if err := yaml.Unmarshal(buf, &config); err != nil {
25+
panic(err)
26+
}
27+
return &config, nil
28+
}

config/config_linter.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package config
2+
3+
import (
4+
"io/ioutil"
5+
6+
"github.com/goccy/go-yaml"
7+
"github.com/tkc/sql-dog/src/domain/model"
8+
)
9+
10+
type LintConfig struct {
11+
Ignores []string `yaml:"ignores"`
12+
Tables []Table `yaml:"tables"`
13+
}
14+
15+
type Table struct {
16+
Name string `yaml:"name"`
17+
StmtTypes []model.StmtType `yaml:"stmtTypePatterns"`
18+
Operations []string `yaml:"mustSelectColumns"`
19+
InsertColumns []string `yaml:"mustInsertColumns"`
20+
NotNullColumns []string `yaml:"notNullColumns"`
21+
}
22+
23+
func (c *LintConfig) ConvertToValidators() (*model.Validator, error) {
24+
var nodes []model.ValidatorNode
25+
26+
for _, t := range c.Tables {
27+
nodes = append(nodes,
28+
model.ValidatorNode{
29+
TableName: t.Name,
30+
StmtTypePattern: t.StmtTypes,
31+
Operations: t.Operations,
32+
InsertColumns: t.InsertColumns,
33+
NotNullColumns: t.NotNullColumns,
34+
})
35+
}
36+
37+
return &model.Validator{
38+
Ignores: c.Ignores,
39+
Nodes: nodes,
40+
}, nil
41+
}
42+
43+
func ReadLintConfig(path string) (*model.Validator, error) {
44+
buf, err := ioutil.ReadFile(path)
45+
if err != nil {
46+
return nil, err
47+
}
48+
var config LintConfig
49+
if err := yaml.Unmarshal(buf, &config); err != nil {
50+
panic(err)
51+
}
52+
return config.ConvertToValidators()
53+
}

0 commit comments

Comments
 (0)