2020-02-02 09:40:44 +03:00
|
|
|
package clause
|
|
|
|
|
2020-09-02 15:09:51 +03:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-02-14 22:13:26 +03:00
|
|
|
const (
|
|
|
|
AndWithSpace = " AND "
|
|
|
|
OrWithSpace = " OR "
|
|
|
|
)
|
|
|
|
|
2020-02-02 09:40:44 +03:00
|
|
|
// Where where clause
|
|
|
|
type Where struct {
|
2020-02-07 18:45:35 +03:00
|
|
|
Exprs []Expression
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name where clause name
|
|
|
|
func (where Where) Name() string {
|
|
|
|
return "WHERE"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build build where clause
|
|
|
|
func (where Where) Build(builder Builder) {
|
2024-03-05 05:23:51 +03:00
|
|
|
if len(where.Exprs) == 1 {
|
|
|
|
if andCondition, ok := where.Exprs[0].(AndConditions); ok {
|
|
|
|
where.Exprs = andCondition.Exprs
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 11:42:21 +03:00
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
// Switch position if the first query expression is a single Or condition
|
|
|
|
for idx, expr := range where.Exprs {
|
2020-06-06 17:52:08 +03:00
|
|
|
if v, ok := expr.(OrConditions); !ok || len(v.Exprs) > 1 {
|
2020-02-07 18:45:35 +03:00
|
|
|
if idx != 0 {
|
|
|
|
where.Exprs[0], where.Exprs[idx] = where.Exprs[idx], where.Exprs[0]
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
break
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:13:26 +03:00
|
|
|
buildExprs(where.Exprs, builder, AndWithSpace)
|
2020-10-19 09:49:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildExprs(exprs []Expression, builder Builder, joinCond string) {
|
2020-09-02 15:09:51 +03:00
|
|
|
wrapInParentheses := false
|
2020-10-19 09:49:42 +03:00
|
|
|
|
|
|
|
for idx, expr := range exprs {
|
2020-06-06 17:52:08 +03:00
|
|
|
if idx > 0 {
|
|
|
|
if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
|
2022-02-14 22:13:26 +03:00
|
|
|
builder.WriteString(OrWithSpace)
|
2020-06-06 17:52:08 +03:00
|
|
|
} else {
|
2020-10-19 09:49:42 +03:00
|
|
|
builder.WriteString(joinCond)
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 17:52:08 +03:00
|
|
|
|
2020-10-19 09:49:42 +03:00
|
|
|
if len(exprs) > 1 {
|
2020-09-02 15:09:51 +03:00
|
|
|
switch v := expr.(type) {
|
|
|
|
case OrConditions:
|
|
|
|
if len(v.Exprs) == 1 {
|
|
|
|
if e, ok := v.Exprs[0].(Expr); ok {
|
2022-02-14 22:13:26 +03:00
|
|
|
sql := strings.ToUpper(e.SQL)
|
|
|
|
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
|
2020-09-02 15:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case AndConditions:
|
|
|
|
if len(v.Exprs) == 1 {
|
|
|
|
if e, ok := v.Exprs[0].(Expr); ok {
|
2022-02-14 22:13:26 +03:00
|
|
|
sql := strings.ToUpper(e.SQL)
|
|
|
|
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
|
2020-09-02 15:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case Expr:
|
2022-02-14 22:13:26 +03:00
|
|
|
sql := strings.ToUpper(v.SQL)
|
|
|
|
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
|
2021-12-21 14:50:00 +03:00
|
|
|
case NamedExpr:
|
2022-02-14 22:13:26 +03:00
|
|
|
sql := strings.ToUpper(v.SQL)
|
|
|
|
wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
|
2020-09-02 15:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wrapInParentheses {
|
2022-03-17 17:54:30 +03:00
|
|
|
builder.WriteByte('(')
|
2020-09-02 15:09:51 +03:00
|
|
|
expr.Build(builder)
|
2022-03-17 17:54:30 +03:00
|
|
|
builder.WriteByte(')')
|
2020-09-02 15:09:51 +03:00
|
|
|
wrapInParentheses = false
|
|
|
|
} else {
|
|
|
|
expr.Build(builder)
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeClause merge where clauses
|
|
|
|
func (where Where) MergeClause(clause *Clause) {
|
|
|
|
if w, ok := clause.Expression.(Where); ok {
|
2020-06-08 04:10:27 +03:00
|
|
|
exprs := make([]Expression, len(w.Exprs)+len(where.Exprs))
|
|
|
|
copy(exprs, w.Exprs)
|
|
|
|
copy(exprs[len(w.Exprs):], where.Exprs)
|
|
|
|
where.Exprs = exprs
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clause.Expression = where
|
|
|
|
}
|
|
|
|
|
|
|
|
func And(exprs ...Expression) Expression {
|
|
|
|
if len(exprs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-30 06:47:14 +03:00
|
|
|
|
|
|
|
if len(exprs) == 1 {
|
|
|
|
if _, ok := exprs[0].(OrConditions); !ok {
|
|
|
|
return exprs[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
return AndConditions{Exprs: exprs}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AndConditions struct {
|
|
|
|
Exprs []Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (and AndConditions) Build(builder Builder) {
|
|
|
|
if len(and.Exprs) > 1 {
|
2020-02-13 19:09:44 +03:00
|
|
|
builder.WriteByte('(')
|
2022-02-14 22:13:26 +03:00
|
|
|
buildExprs(and.Exprs, builder, AndWithSpace)
|
2020-02-13 19:09:44 +03:00
|
|
|
builder.WriteByte(')')
|
2020-10-19 09:49:42 +03:00
|
|
|
} else {
|
2022-02-14 22:13:26 +03:00
|
|
|
buildExprs(and.Exprs, builder, AndWithSpace)
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
func Or(exprs ...Expression) Expression {
|
|
|
|
if len(exprs) == 0 {
|
|
|
|
return nil
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
return OrConditions{Exprs: exprs}
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
type OrConditions struct {
|
|
|
|
Exprs []Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (or OrConditions) Build(builder Builder) {
|
|
|
|
if len(or.Exprs) > 1 {
|
2020-02-13 19:09:44 +03:00
|
|
|
builder.WriteByte('(')
|
2022-02-14 22:13:26 +03:00
|
|
|
buildExprs(or.Exprs, builder, OrWithSpace)
|
2020-02-13 19:09:44 +03:00
|
|
|
builder.WriteByte(')')
|
2020-10-19 09:49:42 +03:00
|
|
|
} else {
|
2022-02-14 22:13:26 +03:00
|
|
|
buildExprs(or.Exprs, builder, OrWithSpace)
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Not(exprs ...Expression) Expression {
|
|
|
|
if len(exprs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-12 11:42:21 +03:00
|
|
|
if len(exprs) == 1 {
|
|
|
|
if andCondition, ok := exprs[0].(AndConditions); ok {
|
|
|
|
exprs = andCondition.Exprs
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
return NotConditions{Exprs: exprs}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotConditions struct {
|
|
|
|
Exprs []Expression
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
func (not NotConditions) Build(builder Builder) {
|
2024-03-05 05:23:51 +03:00
|
|
|
anyNegationBuilder := false
|
|
|
|
for _, c := range not.Exprs {
|
|
|
|
if _, ok := c.(NegationExpressionBuilder); ok {
|
|
|
|
anyNegationBuilder = true
|
|
|
|
break
|
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
2020-09-02 15:09:51 +03:00
|
|
|
|
2024-03-05 05:23:51 +03:00
|
|
|
if anyNegationBuilder {
|
|
|
|
if len(not.Exprs) > 1 {
|
|
|
|
builder.WriteByte('(')
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
|
2024-03-05 05:23:51 +03:00
|
|
|
for idx, c := range not.Exprs {
|
|
|
|
if idx > 0 {
|
|
|
|
builder.WriteString(AndWithSpace)
|
|
|
|
}
|
|
|
|
|
|
|
|
if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
|
|
|
|
negationBuilder.NegationBuild(builder)
|
|
|
|
} else {
|
|
|
|
builder.WriteString("NOT ")
|
|
|
|
e, wrapInParentheses := c.(Expr)
|
|
|
|
if wrapInParentheses {
|
|
|
|
sql := strings.ToUpper(e.SQL)
|
|
|
|
if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses {
|
|
|
|
builder.WriteByte('(')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Build(builder)
|
|
|
|
|
|
|
|
if wrapInParentheses {
|
|
|
|
builder.WriteByte(')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(not.Exprs) > 1 {
|
|
|
|
builder.WriteByte(')')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
builder.WriteString("NOT ")
|
|
|
|
if len(not.Exprs) > 1 {
|
|
|
|
builder.WriteByte('(')
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, c := range not.Exprs {
|
|
|
|
if idx > 0 {
|
|
|
|
builder.WriteString(AndWithSpace)
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:09:51 +03:00
|
|
|
e, wrapInParentheses := c.(Expr)
|
|
|
|
if wrapInParentheses {
|
2022-02-14 22:13:26 +03:00
|
|
|
sql := strings.ToUpper(e.SQL)
|
|
|
|
if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses {
|
2020-09-02 15:09:51 +03:00
|
|
|
builder.WriteByte('(')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
c.Build(builder)
|
2020-09-02 15:09:51 +03:00
|
|
|
|
|
|
|
if wrapInParentheses {
|
|
|
|
builder.WriteByte(')')
|
|
|
|
}
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
2020-09-02 15:09:51 +03:00
|
|
|
|
2024-03-05 05:23:51 +03:00
|
|
|
if len(not.Exprs) > 1 {
|
|
|
|
builder.WriteByte(')')
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
}
|