forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_plan_filters.go
230 lines (199 loc) · 6.86 KB
/
query_plan_filters.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gorp
import (
"bytes"
"reflect"
)
// A Filter is a type that can be used as a sub-section of a where
// clause.
type Filter interface {
// Where should take a structColumnMap, a dialect, and the index
// to start binding at, and return the string to be added to the
// where clause, a slice of query arguments in the where clause,
// and any errors encountered.
Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error)
}
// A MultiFilter is a filter that can also accept additional filters.
type MultiFilter interface {
Filter
Add(filters ...Filter)
}
// A combinedFilter is a filter that has more than one sub-filter.
// This is mainly for things like AND or OR operations.
type combinedFilter struct {
subFilters []Filter
}
// joinFilters joins all of the sub-filters' where clauses into a
// single where clause.
func (filter *combinedFilter) joinFilters(separator string, structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
buffer := bytes.Buffer{}
args := make([]interface{}, 0, len(filter.subFilters))
if len(filter.subFilters) > 1 {
buffer.WriteString("(")
}
for index, subFilter := range filter.subFilters {
nextWhere, nextArgs, err := subFilter.Where(structMap, dialect, startBindIdx+len(args))
if err != nil {
return "", nil, err
}
args = append(args, nextArgs...)
if index != 0 {
buffer.WriteString(separator)
}
buffer.WriteString(nextWhere)
}
if len(filter.subFilters) > 1 {
buffer.WriteString(")")
}
return buffer.String(), args, nil
}
// Add adds one or more filters to the slice of sub-filters.
func (filter *combinedFilter) Add(filters ...Filter) {
filter.subFilters = append(filter.subFilters, filters...)
}
// An andFilter is a combinedFilter that will have its sub-filters
// joined using AND.
type andFilter struct {
combinedFilter
}
func (filter *andFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
return filter.joinFilters(" and ", structMap, dialect, startBindIdx)
}
// An orFilter is a combinedFilter that will have its sub-filters
// joined using OR.
type orFilter struct {
combinedFilter
}
func (filter *orFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
return filter.joinFilters(" or ", structMap, dialect, startBindIdx)
}
// A joinFilter is an andFilter used for ON clauses. It contains the
// name of the table that this filter is for, to make generating a
// join clause simple.
type joinFilter struct {
andFilter
quotedJoinTable string
}
// JoinClause on a joinFilter will return the full join clause for use
// in a SELECT statement.
func (filter *joinFilter) JoinClause(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
join := " inner join " + filter.quotedJoinTable
on, args, err := filter.andFilter.Where(structMap, dialect, startBindIdx)
if err != nil {
return "", nil, err
}
if on != "" {
join += " on " + on
}
return join, args, nil
}
// A comparisonFilter is a filter that compares two values.
type comparisonFilter struct {
left interface{}
comparison string
right interface{}
}
func (filter *comparisonFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
args := make([]interface{}, 0, 2)
comparison := bytes.Buffer{}
if reflect.ValueOf(filter.left).Kind() == reflect.Ptr {
column, err := structMap.tableColumnForPointer(filter.left)
if err != nil {
return "", nil, err
}
comparison.WriteString(column)
} else {
bindVar := dialect.BindVar(startBindIdx + len(args))
comparison.WriteString(bindVar)
args = append(args, filter.left)
}
comparison.WriteString(filter.comparison)
if reflect.ValueOf(filter.right).Kind() == reflect.Ptr {
column, err := structMap.tableColumnForPointer(filter.right)
if err != nil {
return "", nil, err
}
comparison.WriteString(column)
} else {
bindVar := dialect.BindVar(startBindIdx + len(args))
comparison.WriteString(bindVar)
args = append(args, filter.right)
}
return comparison.String(), args, nil
}
// A notFilter is a filter that inverts another filter.
type notFilter struct {
filter Filter
}
func (filter *notFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
whereStr, args, err := filter.filter.Where(structMap, dialect, startBindIdx)
if err != nil {
return "", nil, err
}
return "NOT " + whereStr, args, nil
}
// A nullFilter is a filter that compares a field to null
type nullFilter struct {
addr interface{}
}
func (filter *nullFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
column, err := structMap.tableColumnForPointer(filter.addr)
if err != nil {
return "", nil, err
}
return column + " IS NULL", nil, nil
}
// A notNullFilter is a filter that compares a field to null
type notNullFilter struct {
addr interface{}
}
func (filter *notNullFilter) Where(structMap structColumnMap, dialect Dialect, startBindIdx int) (string, []interface{}, error) {
column, err := structMap.tableColumnForPointer(filter.addr)
if err != nil {
return "", nil, err
}
return column + " IS NOT NULL", nil, nil
}
// Or returns a filter that will OR all passed in filters
func Or(filters ...Filter) Filter {
return &orFilter{combinedFilter{filters}}
}
// And returns a filter that will AND all passed in filters
func And(filters ...Filter) Filter {
return &andFilter{combinedFilter{filters}}
}
// Not returns a filter that will NOT the passed in filter
func Not(filter Filter) Filter {
return ¬Filter{filter}
}
// Null returns a filter for fieldPtr IS NULL
func Null(fieldPtr interface{}) Filter {
return &nullFilter{fieldPtr}
}
// NotNull returns a filter for fieldPtr IS NOT NULL
func NotNull(fieldPtr interface{}) Filter {
return ¬NullFilter{fieldPtr}
}
// Equal returns a filter for fieldPtr == value
func Equal(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "=", value}
}
// NotEqual returns a filter for fieldPtr != value
func NotEqual(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "<>", value}
}
// Less returns a filter for fieldPtr < value
func Less(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "<", value}
}
// LessOrEqual returns a filter for fieldPtr <= value
func LessOrEqual(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "<=", value}
}
// Greater returns a filter for fieldPtr > value
func Greater(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "=", value}
}
// GreaterOrEqual returns a filter for fieldPtr >= value
func GreaterOrEqual(fieldPtr interface{}, value interface{}) Filter {
return &comparisonFilter{fieldPtr, "=", value}
}