github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/condition.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 func EmptyCond() SqlCondition { 8 return (*Condition)(nil) 9 } 10 11 type SqlCondition interface { 12 SqlExpr 13 SqlConditionMarker 14 15 And(cond SqlCondition) SqlCondition 16 Or(cond SqlCondition) SqlCondition 17 Xor(cond SqlCondition) SqlCondition 18 } 19 20 type SqlConditionMarker interface { 21 asCondition() 22 } 23 24 func AsCond(ex SqlExpr) *Condition { 25 return &Condition{expr: ex} 26 } 27 28 type Condition struct { 29 expr SqlExpr 30 SqlConditionMarker 31 } 32 33 func (c *Condition) Ex(ctx context.Context) *Ex { 34 if IsNilExpr(c.expr) { 35 return nil 36 } 37 return c.expr.Ex(ctx) 38 } 39 40 func (c *Condition) IsNil() bool { 41 return c == nil || IsNilExpr(c.expr) 42 } 43 44 func (c *Condition) And(cond SqlCondition) SqlCondition { 45 if IsNilExpr(cond) { 46 return c 47 } 48 return And(c, cond) 49 } 50 51 func (c *Condition) Or(cond SqlCondition) SqlCondition { 52 if IsNilExpr(cond) { 53 return c 54 } 55 return Or(c, cond) 56 } 57 58 func (c *Condition) Xor(cond SqlCondition) SqlCondition { 59 if IsNilExpr(cond) { 60 return c 61 } 62 return Xor(c, cond) 63 } 64 65 func And(conditions ...SqlCondition) SqlCondition { 66 return composedCondition("AND", filterNilCondition(conditions)...) 67 } 68 69 func Or(conditions ...SqlCondition) SqlCondition { 70 return composedCondition("OR", filterNilCondition(conditions)...) 71 } 72 73 func Xor(conditions ...SqlCondition) SqlCondition { 74 return composedCondition("XOR", filterNilCondition(conditions)...) 75 } 76 77 func filterNilCondition(conditions []SqlCondition) []SqlCondition { 78 finals := make([]SqlCondition, 0, len(conditions)) 79 80 for i := range conditions { 81 condition := conditions[i] 82 if IsNilExpr(condition) { 83 continue 84 } 85 finals = append(finals, condition) 86 } 87 88 return finals 89 } 90 91 func composedCondition(op string, conditions ...SqlCondition) SqlCondition { 92 return &ComposedCondition{op: op, conditions: conditions} 93 } 94 95 type ComposedCondition struct { 96 SqlConditionMarker 97 98 op string 99 conditions []SqlCondition 100 } 101 102 func (c *ComposedCondition) And(cond SqlCondition) SqlCondition { 103 return And(c, cond) 104 } 105 106 func (c *ComposedCondition) Or(cond SqlCondition) SqlCondition { 107 return Or(c, cond) 108 } 109 110 func (c *ComposedCondition) Xor(cond SqlCondition) SqlCondition { 111 return Xor(c, cond) 112 } 113 114 func (c *ComposedCondition) IsNil() bool { 115 if c == nil { 116 return true 117 } 118 if c.op == "" { 119 return true 120 } 121 122 isNil := true 123 124 for i := range c.conditions { 125 if !IsNilExpr(c.conditions[i]) { 126 isNil = false 127 continue 128 } 129 } 130 131 return isNil 132 } 133 134 func (c *ComposedCondition) Ex(ctx context.Context) *Ex { 135 e := Expr("") 136 e.Grow(len(c.conditions)) 137 138 for i := range c.conditions { 139 condition := c.conditions[i] 140 141 if i > 0 { 142 e.WriteQueryByte(' ') 143 e.WriteQuery(c.op) 144 e.WriteQueryByte(' ') 145 } 146 147 e.WriteGroup(func(e *Ex) { 148 e.WriteExpr(condition) 149 }) 150 } 151 152 return e.Ex(ctx) 153 }