Skip to content

Commit

Permalink
Merge pull request #123 from actiontech/support_multi_rule_params
Browse files Browse the repository at this point in the history
Support multi rule params
  • Loading branch information
drivebyer authored Dec 3, 2021
2 parents 8b92b58 + 0c95258 commit 47d737a
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 295 deletions.
95 changes: 67 additions & 28 deletions sqle/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ type RuleParam struct {

type RuleParams []*RuleParam



func (r *RuleParams) SetParamValue(key, value string) error {
paramNotFoundErrMsg := "param %s not found"
if r == nil {
Expand All @@ -104,50 +102,91 @@ func (r *RuleParams) SetParamValue(key, value string) error {
return fmt.Errorf(paramNotFoundErrMsg, key)
}

func (r *RuleParams) GetParamValue(key string) (string, bool) {
func (r *RuleParams) GetParam(key string) *RuleParam {
if r == nil {
return "", false
return nil
}
for _, p := range *r {
if p.Key == key {
return p.Value, true
return p
}
}
return "", false
return nil
}

type Rule struct {
Name string
Desc string

// Category is the category of the rule. Such as "Naming Conventions"...
// Rules will be displayed on the SQLE rule list page by category.
Category string

Level RuleLevel
Value string
func (r *RuleParam) String() string {
if r == nil {
return ""
}
return r.Value
}

func (r *Rule) GetValueInt(defaultRule *Rule) int64 {
value := r.GetValue()
i, err := strconv.ParseInt(value, 10, 64)
if err == nil {
return i
func (r *RuleParam) Int() int {
if r == nil {
return 0
}
i, err = strconv.ParseInt(defaultRule.GetValue(), 10, 64)
if err == nil {
return i
i, err := strconv.Atoi(r.Value)
if err != nil {
return 0
}
return 0
return i
}

func (r *Rule) GetValue() string {
func (r *RuleParam) Bool() bool {
if r == nil {
return ""
return false
}
return r.Value
b, err := strconv.ParseBool(r.Value)
if err != nil {
return false
}
return b
}

type Rule struct {
Name string
Desc string

// Category is the category of the rule. Such as "Naming Conventions"...
// Rules will be displayed on the SQLE rule list page by category.
Category string
Level RuleLevel
Params RuleParams
}

//func (r *Rule) GetValueInt(defaultRule *Rule) int64 {
// value := r.getValue(DefaultSingleParamKeyName, defaultRule)
// i, err := strconv.ParseInt(value, 10, 64)
// if err != nil {
// return 0
// }
// return i
//}
//
//func (r *Rule) GetSingleValue() string {
// value, _ := r.Params.GetParamValue(DefaultSingleParamKeyName)
// return value
//}
//
//func (r *Rule) GetSingleValueInt() int {
// value := r.GetSingleValue()
// i, err := strconv.Atoi(value)
// if err != nil {
// return 0
// }
// return i
//}

//func (r *Rule) getValue(key string, defaultRule *Rule) string {
// var value string
// var exist bool
// value, exist = r.Params.GetParamValue(key)
// if !exist {
// value, _ = defaultRule.Params.GetParamValue(key)
// }
// return value
//}

// Config difine the configuration for driver.
type Config struct {
DSN *DSN
Expand Down
52 changes: 26 additions & 26 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestCheckObjectNameUsingKeywordOffline(t *testing.T) {
"INDEX `show` (v1)"+
")ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=\"unit test\";",
newTestResult().addResult(rulepkg.DDLCheckObjectNameUsingKeyword, "select, create, show").
addResult(rulepkg.DDLCheckIndexPrefix),
addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)
}

Expand All @@ -251,7 +251,7 @@ v1 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: columns length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -262,7 +262,7 @@ id bigint unsigned NOT NULL AUTO_INCREMENT COMMENT "unit test",
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: index length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -274,37 +274,37 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id),
INDEX idx_%s (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table: table length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 RENAME %s;`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:Add column length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 ADD COLUMN %s varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:change column length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 CHANGE COLUMN v1 %s varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table: Add index length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 ADD index idx_%s (v1);`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:rename index length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 RENAME index idx_1 TO idx_%s;`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)
}

Expand Down Expand Up @@ -438,7 +438,7 @@ INDEX idx_5 (id),
INDEX idx_6 (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexCount),
newTestResult().addResult(rulepkg.DDLCheckIndexCount, 5),
)
}

Expand Down Expand Up @@ -472,7 +472,7 @@ PRIMARY KEY (id),
INDEX idx_1 (id,v1,v2,v3,v4,v5)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckCompositeIndexMax),
newTestResult().addResult(rulepkg.DDLCheckCompositeIndexMax, 3),
)
}

Expand Down Expand Up @@ -616,21 +616,21 @@ PRIMARY KEY (id),
INDEX index_1 (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

runDefaultRulesInspectCase(t, "alter_table: index prefix not idx_", DefaultMysqlInspectOffline(),
`
ALTER TABLE exist_db.exist_tb_1 ADD INDEX index_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

runDefaultRulesInspectCase(t, "create_index: index prefix not idx_", DefaultMysqlInspectOffline(),
`
CREATE INDEX index_1 ON exist_db.exist_tb_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

for _, sql := range []string{
Expand Down Expand Up @@ -658,21 +658,21 @@ PRIMARY KEY (id),
UNIQUE INDEX index_1 (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

runSingleRuleInspectCase(rule, t, "alter_table: unique index prefix not uniq_", DefaultMysqlInspectOffline(),
`
ALTER TABLE exist_db.exist_tb_1 ADD UNIQUE INDEX index_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

runSingleRuleInspectCase(rule, t, "create_index: unique index prefix not uniq_", DefaultMysqlInspectOffline(),
`
CREATE UNIQUE INDEX index_1 ON exist_db.exist_tb_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

for _, sql := range []string{
Expand Down Expand Up @@ -890,12 +890,12 @@ insert into exist_db.exist_tb_1 (id,v1,v2) values (?,?,?),(?,?,?);
func TestCheckBatchInsertListsMaxOffline(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckBatchInsertListsMax].Rule
// defult 5000, unit testing :4
rule.Value = "4"
rule.Params.SetParamValue(rulepkg.DefaultSingleParamKeyName, "4")
runSingleRuleInspectCase(rule, t, "insert:check batch insert lists max", DefaultMysqlInspectOffline(),
`
insert into exist_db.exist_tb_1 (id,v1,v2) values (1,"1","1"),(2,"2","2"),(3,"3","3"),(4,"4","4"),(5,"5","5");
`,
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, rule.Value),
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, 4),
)

runSingleRuleInspectCase(rule, t, "insert: passing the check batch insert lists max", DefaultMysqlInspectOffline(),
Expand All @@ -909,12 +909,12 @@ insert into exist_db.exist_tb_1 (id,v1,v2) values (1,"1","1"),(2,"2","2"),(3,"3"
func TestCheckBatchInsertListsMax_FPOffline(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckBatchInsertListsMax].Rule
// defult 5000, unit testing :4
rule.Value = "4"
rule.Params.SetParamValue(rulepkg.DefaultSingleParamKeyName, "4")
runSingleRuleInspectCase(rule, t, "[fp]insert:check batch insert lists max", DefaultMysqlInspectOffline(),
`
insert into exist_db.exist_tb_1 (id,v1,v2) values (?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?);
`,
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, rule.Value),
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, 4),
)

runSingleRuleInspectCase(rule, t, "[fp]insert: passing the check batch insert lists max", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -1449,7 +1449,7 @@ func TestCheckNeedlessFuncOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc))
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc, "sha(),sqrt(),md5()"))
}

for desc, sql := range map[string]string{
Expand All @@ -1475,7 +1475,7 @@ func TestCheckNeedlessFunc_FPOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc))
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc, "sha(),sqrt(),md5()"))
}

for desc, sql := range map[string]string{
Expand All @@ -1501,7 +1501,7 @@ func TestCheckDatabaseSuffixOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DDLCheckDatabaseSuffix))
newTestResult().addResult(rulepkg.DDLCheckDatabaseSuffix, "_DB"))
}

for desc, sql := range map[string]string{
Expand Down Expand Up @@ -1690,7 +1690,7 @@ JOIN exist_db.exist_tb_4 ON exist_db.exist_tb_3.id = exist_db.exist_tb_4.id
desc,
inspector,
sql,
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables))
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables, 3))
}

for desc, sql := range map[string]string{
Expand Down Expand Up @@ -1745,7 +1745,7 @@ WHERE exist_db.exist_tb_1.v1 = ? AND exist_db.exist_tb_1.v2 = ?
desc,
inspector,
sql,
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables))
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables, 3))
}

for desc, sql := range map[string]string{
Expand Down
Loading

0 comments on commit 47d737a

Please sign in to comment.