Skip to content

Commit

Permalink
Fixed the use of "or" to be " OR ", to account for words that contain…
Browse files Browse the repository at this point in the history
… "or" or "and" (e.g., 'score', 'band') in a sql statement as the name of a field.
  • Loading branch information
sammyrnycreal authored and jinzhu committed Feb 20, 2022
1 parent 48ced75 commit 5edc781
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
39 changes: 22 additions & 17 deletions clause/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"strings"
)

const (
AndWithSpace = " AND "
OrWithSpace = " OR "
)

// Where where clause
type Where struct {
Exprs []Expression
Expand All @@ -26,7 +31,7 @@ func (where Where) Build(builder Builder) {
}
}

buildExprs(where.Exprs, builder, " AND ")
buildExprs(where.Exprs, builder, AndWithSpace)
}

func buildExprs(exprs []Expression, builder Builder, joinCond string) {
Expand All @@ -35,7 +40,7 @@ func buildExprs(exprs []Expression, builder Builder, joinCond string) {
for idx, expr := range exprs {
if idx > 0 {
if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
builder.WriteString(" OR ")
builder.WriteString(OrWithSpace)
} else {
builder.WriteString(joinCond)
}
Expand All @@ -46,23 +51,23 @@ func buildExprs(exprs []Expression, builder Builder, joinCond string) {
case OrConditions:
if len(v.Exprs) == 1 {
if e, ok := v.Exprs[0].(Expr); ok {
sql := strings.ToLower(e.SQL)
wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
sql := strings.ToUpper(e.SQL)
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
}
}
case AndConditions:
if len(v.Exprs) == 1 {
if e, ok := v.Exprs[0].(Expr); ok {
sql := strings.ToLower(e.SQL)
wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
sql := strings.ToUpper(e.SQL)
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
}
}
case Expr:
sql := strings.ToLower(v.SQL)
wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
sql := strings.ToUpper(v.SQL)
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
case NamedExpr:
sql := strings.ToLower(v.SQL)
wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
sql := strings.ToUpper(v.SQL)
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
}
}

Expand Down Expand Up @@ -110,10 +115,10 @@ type AndConditions struct {
func (and AndConditions) Build(builder Builder) {
if len(and.Exprs) > 1 {
builder.WriteByte('(')
buildExprs(and.Exprs, builder, " AND ")
buildExprs(and.Exprs, builder, AndWithSpace)
builder.WriteByte(')')
} else {
buildExprs(and.Exprs, builder, " AND ")
buildExprs(and.Exprs, builder, AndWithSpace)
}
}

Expand All @@ -131,10 +136,10 @@ type OrConditions struct {
func (or OrConditions) Build(builder Builder) {
if len(or.Exprs) > 1 {
builder.WriteByte('(')
buildExprs(or.Exprs, builder, " OR ")
buildExprs(or.Exprs, builder, OrWithSpace)
builder.WriteByte(')')
} else {
buildExprs(or.Exprs, builder, " OR ")
buildExprs(or.Exprs, builder, OrWithSpace)
}
}

Expand All @@ -156,7 +161,7 @@ func (not NotConditions) Build(builder Builder) {

for idx, c := range not.Exprs {
if idx > 0 {
builder.WriteString(" AND ")
builder.WriteString(AndWithSpace)
}

if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
Expand All @@ -165,8 +170,8 @@ func (not NotConditions) Build(builder Builder) {
builder.WriteString("NOT ")
e, wrapInParentheses := c.(Expr)
if wrapInParentheses {
sql := strings.ToLower(e.SQL)
if wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or"); wrapInParentheses {
sql := strings.ToUpper(e.SQL)
if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses {
builder.WriteByte('(')
}
}
Expand Down
39 changes: 39 additions & 0 deletions clause/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ func TestWhere(t *testing.T) {
"SELECT * FROM `users` WHERE (`age` = ? OR `name` <> ?)",
[]interface{}{18, "jinzhu"},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
Exprs: []clause.Expression{clause.Not(clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}), clause.And(clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}, WithoutParentheses: false})},
}},
"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND `age` <= ?) AND `score` <= ?",
[]interface{}{"1", 18, 100},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
Exprs: []clause.Expression{clause.Not(clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}), clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}, WithoutParentheses: false}},
}},
"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND `age` <= ?) AND `score` <= ?",
[]interface{}{"1", 18, 100},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
Exprs: []clause.Expression{clause.Not(clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}), clause.Or(clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}, WithoutParentheses: false})},
}},
"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND `age` <= ?) OR `score` <= ?",
[]interface{}{"1", 18, 100},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
Exprs: []clause.Expression{
clause.And(clause.Not(clause.Eq{Column: clause.PrimaryColumn, Value: "1"}),
clause.And(clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}, WithoutParentheses: false})),
},
}},
"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND `score` <= ?)",
[]interface{}{"1", 100},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
Exprs: []clause.Expression{clause.Not(clause.Eq{Column: clause.PrimaryColumn, Value: "1"},
clause.And(clause.Expr{SQL: "`score` <= ?", Vars: []interface{}{100}, WithoutParentheses: false}))},
}},
"SELECT * FROM `users` WHERE (`users`.`id` <> ? AND NOT `score` <= ?)",
[]interface{}{"1", 100},
},
}

for idx, result := range results {
Expand Down

0 comments on commit 5edc781

Please sign in to comment.