From 16ddae8dfadc4417a15f5a9f7c4d093657111c40 Mon Sep 17 00:00:00 2001 From: XuShuo Date: Tue, 30 Apr 2024 22:28:09 +0800 Subject: [PATCH] fix: fix '||' in Where condition but not wrap with brackets (#7002) --- clause/where.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/clause/where.go b/clause/where.go index 2c3c90f1..8e740ce9 100644 --- a/clause/where.go +++ b/clause/where.go @@ -5,8 +5,10 @@ import ( ) const ( - AndWithSpace = " AND " - OrWithSpace = " OR " + AndWithSpace = " AND " + OrWithSpace = " OR " + AndSymbolWithSpace = " && " + OrSymbolWithSpace = " || " ) // Where where clause @@ -58,22 +60,26 @@ func buildExprs(exprs []Expression, builder Builder, joinCond string) { if len(v.Exprs) == 1 { if e, ok := v.Exprs[0].(Expr); ok { sql := strings.ToUpper(e.SQL) - wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) + wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) || + strings.Contains(sql, AndSymbolWithSpace) || strings.Contains(sql, OrSymbolWithSpace) } } case AndConditions: if len(v.Exprs) == 1 { if e, ok := v.Exprs[0].(Expr); ok { sql := strings.ToUpper(e.SQL) - wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) + wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) || + strings.Contains(sql, AndSymbolWithSpace) || strings.Contains(sql, OrSymbolWithSpace) } } case Expr: sql := strings.ToUpper(v.SQL) - wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) + wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) || + strings.Contains(sql, AndSymbolWithSpace) || strings.Contains(sql, OrSymbolWithSpace) case NamedExpr: sql := strings.ToUpper(v.SQL) - wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) + wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) || + strings.Contains(sql, AndSymbolWithSpace) || strings.Contains(sql, OrSymbolWithSpace) } }