| 41 | } |
| 42 | |
| 43 | func buildExprs(exprs []Expression, builder Builder, joinCond string) { |
| 44 | wrapInParentheses := false |
| 45 | |
| 46 | for idx, expr := range exprs { |
| 47 | if idx > 0 { |
| 48 | if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 { |
| 49 | builder.WriteString(OrWithSpace) |
| 50 | } else { |
| 51 | builder.WriteString(joinCond) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if len(exprs) > 1 { |
| 56 | switch v := expr.(type) { |
| 57 | case OrConditions: |
| 58 | if len(v.Exprs) == 1 { |
| 59 | if e, ok := v.Exprs[0].(Expr); ok { |
| 60 | sql := strings.ToUpper(e.SQL) |
| 61 | wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) |
| 62 | } |
| 63 | } |
| 64 | case AndConditions: |
| 65 | if len(v.Exprs) == 1 { |
| 66 | if e, ok := v.Exprs[0].(Expr); ok { |
| 67 | sql := strings.ToUpper(e.SQL) |
| 68 | wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) |
| 69 | } |
| 70 | } |
| 71 | case Expr: |
| 72 | sql := strings.ToUpper(v.SQL) |
| 73 | wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) |
| 74 | case NamedExpr: |
| 75 | sql := strings.ToUpper(v.SQL) |
| 76 | wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if wrapInParentheses { |
| 81 | builder.WriteByte('(') |
| 82 | expr.Build(builder) |
| 83 | builder.WriteByte(')') |
| 84 | wrapInParentheses = false |
| 85 | } else { |
| 86 | expr.Build(builder) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // MergeClause merge where clauses |
| 92 | func (where Where) MergeClause(clause *Clause) { |