forked from goal-web/querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhere.go
191 lines (163 loc) · 4.58 KB
/
where.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package querybuilder
import (
"fmt"
"github.com/goal-web/contracts"
)
type Where struct {
field string
condition string
arg string
}
func (this *Where) String() string {
if this == nil {
return ""
}
return fmt.Sprintf("%s %s %s", this.field, this.condition, this.arg)
}
type Wheres struct {
subWheres map[contracts.WhereJoinType][]*Wheres
wheres map[contracts.WhereJoinType][]*Where
}
func (this *Wheres) IsEmpty() bool {
return len(this.subWheres) == 0 && len(this.wheres) == 0
}
func (this Wheres) getSubStringers(whereType contracts.WhereJoinType) []fmt.Stringer {
stringers := make([]fmt.Stringer, 0)
for _, where := range this.subWheres[whereType] {
stringers = append(stringers, where)
}
return stringers
}
func (this Wheres) getStringers(whereType contracts.WhereJoinType) []fmt.Stringer {
stringers := make([]fmt.Stringer, 0)
for _, where := range this.wheres[whereType] {
stringers = append(stringers, where)
}
return stringers
}
func (this *Wheres) getSubWheres(whereType contracts.WhereJoinType) string {
return JoinSubStringerArray(this.getSubStringers(whereType), string(whereType))
}
func (this *Wheres) getWheres(whereType contracts.WhereJoinType) string {
return JoinStringerArray(this.getStringers(whereType), string(whereType))
}
func (this *Wheres) String() (result string) {
if this == nil || this.IsEmpty() {
return ""
}
result = this.getSubWheres(contracts.And)
andWheres := this.getWheres(contracts.And)
if result != "" {
if andWheres != "" {
result = fmt.Sprintf("%s and %s", result, andWheres)
}
} else {
result = andWheres
}
orSubWheres := this.getSubWheres(contracts.Or)
if result == "" {
result = orSubWheres
} else if orSubWheres != "" {
result = fmt.Sprintf("%s or %s", result, orSubWheres)
}
orWheres := this.getWheres(contracts.Or)
if result == "" {
result = orWheres
} else if orWheres != "" {
result = fmt.Sprintf("%s or %s", result, orWheres)
}
return
}
func (this *Builder) WhereFunc(callback contracts.QueryFunc, whereType ...contracts.WhereJoinType) contracts.QueryBuilder {
subBuilder := &Builder{
wheres: &Wheres{
wheres: map[contracts.WhereJoinType][]*Where{},
subWheres: map[contracts.WhereJoinType][]*Wheres{},
},
bindings: map[bindingType][]interface{}{},
}
callback(subBuilder)
if len(whereType) == 0 {
this.wheres.subWheres[contracts.And] = append(this.wheres.subWheres[contracts.And], subBuilder.getWheres())
} else {
this.wheres.subWheres[whereType[0]] = append(this.wheres.subWheres[whereType[0]], subBuilder.getWheres())
}
return this.addBinding(whereBinding, subBuilder.GetBindings()...)
}
func (this *Builder) WhereFields(fields contracts.Fields) contracts.QueryBuilder {
for column, value := range fields {
this.Where(column, value)
}
return this
}
func (this *Builder) OrWhereFunc(callback contracts.QueryFunc) contracts.QueryBuilder {
return this.WhereFunc(callback, contracts.Or)
}
func (this *Builder) Where(field string, args ...interface{}) contracts.QueryBuilder {
var (
arg interface{}
condition = "="
whereType = contracts.And
)
switch len(args) {
case 1:
arg = args[0]
case 2:
condition = args[0].(string)
arg = args[1]
case 3:
condition = args[0].(string)
arg = args[1]
whereType = args[2].(contracts.WhereJoinType)
}
raw, bindings := this.prepareArgs(condition, arg)
this.wheres.wheres[whereType] = append(this.wheres.wheres[whereType], &Where{
field: field,
condition: condition,
arg: raw,
})
return this.addBinding(whereBinding, bindings...)
}
func (this *Builder) OrWhere(field string, args ...interface{}) contracts.QueryBuilder {
var (
arg interface{}
condition = "="
)
switch len(args) {
case 1:
arg = args[0]
case 2:
condition = args[0].(string)
arg = args[1]
default:
condition = args[0].(string)
arg = args[1]
}
raw, bindings := this.prepareArgs(condition, arg)
this.wheres.wheres[contracts.Or] = append(this.wheres.wheres[contracts.Or], &Where{
field: field,
condition: condition,
arg: raw,
})
return this.addBinding(whereBinding, bindings...)
}
func JoinStringerArray(arr []fmt.Stringer, sep string) (result string) {
for index, stringer := range arr {
if index == 0 {
result = stringer.String()
} else {
result = fmt.Sprintf("%s %s %s", result, sep, stringer.String())
}
}
return
}
func JoinSubStringerArray(arr []fmt.Stringer, sep string) (result string) {
for index, stringer := range arr {
if index == 0 {
result = fmt.Sprintf("(%s)", stringer.String())
} else {
result = fmt.Sprintf("%s %s (%s)", result, sep, stringer.String())
}
}
return
}