Skip to content

Commit 62493fb

Browse files
committed
chore:optimize code , remove outline break
1 parent 4c25158 commit 62493fb

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

sqle/driver/mysql/rule/rule.go

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,8 +2682,7 @@ func checkDMLWithBatchInsertMaxLimits(input *RuleHandlerInput) error {
26822682

26832683
func checkWhereExistFunc(input *RuleHandlerInput) error {
26842684
tables := []*ast.TableName{}
2685-
switch stmt := input.Node.(type) {
2686-
case *ast.SelectStmt:
2685+
hasExistFunc := func(stmt *ast.SelectStmt) bool {
26872686
selectExtractor := util.SelectStmtExtractor{}
26882687
stmt.Accept(&selectExtractor)
26892688
for _, selectStmt := range selectExtractor.SelectStmts {
@@ -2705,9 +2704,18 @@ func checkWhereExistFunc(input *RuleHandlerInput) error {
27052704
}
27062705

27072706
if checkExistFunc(input.Ctx, input.Rule, input.Res, tables, selectStmt.Where) {
2708-
break
2707+
return true
27092708
}
27102709
}
2710+
2711+
return false
2712+
}
2713+
2714+
switch stmt := input.Node.(type) {
2715+
case *ast.SelectStmt:
2716+
if hasExistFunc(stmt) {
2717+
break
2718+
}
27112719
case *ast.UpdateStmt:
27122720
if stmt.Where != nil {
27132721
tableSources := util.GetTableSources(stmt.TableRefs.TableRefs)
@@ -2724,30 +2732,9 @@ func checkWhereExistFunc(input *RuleHandlerInput) error {
27242732
checkExistFunc(input.Ctx, input.Rule, input.Res, util.GetTables(stmt.TableRefs.TableRefs), stmt.Where)
27252733
}
27262734
case *ast.UnionStmt:
2727-
outerBreaker:
2728-
for _, selectStmtList := range stmt.SelectList.Selects {
2729-
selectExtractor := util.SelectStmtExtractor{}
2730-
selectStmtList.Accept(&selectExtractor)
2731-
for _, selectStmt := range selectExtractor.SelectStmts {
2732-
if selectStmt.From == nil || selectStmt.Where == nil {
2733-
continue
2734-
}
2735-
2736-
tableSources := util.GetTableSources(selectStmt.From.TableRefs)
2737-
if len(tableSources) < 1 {
2738-
continue
2739-
}
2740-
2741-
for _, tableSource := range tableSources {
2742-
switch source := tableSource.Source.(type) {
2743-
case *ast.TableName:
2744-
tables = append(tables, source)
2745-
}
2746-
}
2747-
2748-
if checkExistFunc(input.Ctx, input.Rule, input.Res, tables, selectStmt.Where) {
2749-
break outerBreaker
2750-
}
2735+
for _, selectStmt := range stmt.SelectList.Selects {
2736+
if hasExistFunc(selectStmt) {
2737+
break
27512738
}
27522739
}
27532740
default:
@@ -2779,25 +2766,32 @@ func checkExistFunc(ctx *session.Context, rule driverV2.Rule, res *driverV2.Audi
27792766
}
27802767

27812768
func checkWhereColumnImplicitConversion(input *RuleHandlerInput) error {
2782-
switch stmt := input.Node.(type) {
2783-
case *ast.SelectStmt:
2769+
hasWhereColumnImplicitConversionFunc := func(stmt *ast.SelectStmt) bool {
27842770
selectExtractor := util.SelectStmtExtractor{}
27852771
stmt.Accept(&selectExtractor)
27862772
for _, selectStmt := range selectExtractor.SelectStmts {
2787-
if selectStmt.Where == nil || selectStmt.From == nil {
2773+
if selectStmt.From == nil || selectStmt.Where == nil {
27882774
continue
27892775
}
27902776

27912777
tableSources := util.GetTableSources(selectStmt.From.TableRefs)
2792-
// not select from table statement
27932778
if len(tableSources) < 1 {
27942779
continue
27952780
}
27962781

27972782
if checkWhereColumnImplicitConversionFunc(input.Ctx, input.Rule, input.Res, tableSources, selectStmt.Where) {
2798-
break
2783+
return true
27992784
}
28002785
}
2786+
2787+
return false
2788+
}
2789+
2790+
switch stmt := input.Node.(type) {
2791+
case *ast.SelectStmt:
2792+
if hasWhereColumnImplicitConversionFunc(stmt) {
2793+
break
2794+
}
28012795
case *ast.UpdateStmt:
28022796
if stmt.Where != nil {
28032797
tableSources := util.GetTableSources(stmt.TableRefs.TableRefs)
@@ -2809,23 +2803,9 @@ func checkWhereColumnImplicitConversion(input *RuleHandlerInput) error {
28092803
checkWhereColumnImplicitConversionFunc(input.Ctx, input.Rule, input.Res, tableSources, stmt.Where)
28102804
}
28112805
case *ast.UnionStmt:
2812-
outerBreaker:
2813-
for _, selectStmtList := range stmt.SelectList.Selects {
2814-
selectExtractor := util.SelectStmtExtractor{}
2815-
selectStmtList.Accept(&selectExtractor)
2816-
for _, selectStmt := range selectExtractor.SelectStmts {
2817-
if selectStmt.From == nil || selectStmt.Where == nil {
2818-
continue
2819-
}
2820-
2821-
tableSources := util.GetTableSources(selectStmt.From.TableRefs)
2822-
if len(tableSources) < 1 {
2823-
continue
2824-
}
2825-
2826-
if checkWhereColumnImplicitConversionFunc(input.Ctx, input.Rule, input.Res, tableSources, selectStmt.Where) {
2827-
break outerBreaker
2828-
}
2806+
for _, selectStmt := range stmt.SelectList.Selects {
2807+
if hasWhereColumnImplicitConversionFunc(selectStmt) {
2808+
break
28292809
}
28302810
}
28312811
default:

0 commit comments

Comments
 (0)