-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexpr.go
More file actions
271 lines (222 loc) · 5.62 KB
/
expr.go
File metadata and controls
271 lines (222 loc) · 5.62 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package q2sql
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/velmie/qparser"
)
// Sqlizer return an SQL with the list of arguments
type Sqlizer interface {
ToSQL() (string, []interface{}, error)
}
// Eq - Equality: field = value
type Eq struct {
Field string
Value interface{}
}
func (eq *Eq) ToSQL() (string, []interface{}, error) {
return eq.Field + " = ?", []interface{}{eq.Value}, nil
}
// Neq - Non-equality: field != value
type Neq struct {
Field string
Value interface{}
}
func (neq *Neq) ToSQL() (string, []interface{}, error) {
return neq.Field + " != ?", []interface{}{neq.Value}, nil
}
// Lt - Less than: field < value
type Lt struct {
Field string
Value interface{}
}
func (lt *Lt) ToSQL() (string, []interface{}, error) {
return lt.Field + " < ?", []interface{}{lt.Value}, nil
}
// Le - Less than or equal to: field < value
type Le struct {
Field string
Value interface{}
}
func (le *Le) ToSQL() (string, []interface{}, error) {
return le.Field + " <= ?", []interface{}{le.Value}, nil
}
// Gt - Greater than: field > value
type Gt struct {
Field string
Value interface{}
}
func (gt *Gt) ToSQL() (string, []interface{}, error) {
return gt.Field + " > ?", []interface{}{gt.Value}, nil
}
// Ge - Greater than or equal to: field >= value
type Ge struct {
Field string
Value interface{}
}
func (ge *Ge) ToSQL() (string, []interface{}, error) {
return ge.Field + " >= ?", []interface{}{ge.Value}, nil
}
// In - Equals one value from set: field IN (value, value2)
type In struct {
Field string
Values []interface{}
}
func (in *In) ToSQL() (string, []interface{}, error) {
if len(in.Values) == 0 {
return "", nil, fmt.Errorf("'In' condition requires at least one value for field %s", in.Field)
}
return in.Field + " IN (?" + strings.Repeat(",?", len(in.Values)-1) + ")", in.Values, nil
}
// NotIn - Not in a set of values: field NOT IN (value, value2)
type NotIn struct {
Field string
Values []interface{}
}
func (n *NotIn) ToSQL() (string, []interface{}, error) {
if len(n.Values) == 0 {
return "", nil, fmt.Errorf("'NotIn' condition requires at least one value for field %s", n.Field)
}
return n.Field + " NOT IN (?" + strings.Repeat(",?", len(n.Values)-1) + ")", n.Values, nil
}
// Like - contains text: field like value
type Like struct {
Field string
Value interface{}
}
func (l *Like) ToSQL() (string, []interface{}, error) {
return l.Field + " LIKE ?", []interface{}{l.Value}, nil
}
// IsNull - Equal to null: field is null
type IsNull string
func (i IsNull) ToSQL() (string, []interface{}, error) {
return string(i) + " IS NULL", nil, nil
}
// IsNotNull - Not equal to null: field is not null
type IsNotNull string
func (i IsNotNull) ToSQL() (string, []interface{}, error) {
return string(i) + " IS NOT NULL", nil, nil
}
// Or connects multiple expressions with the "OR" statement
type Or []Sqlizer
func (or Or) ToSQL() (sql string, args []interface{}, err error) {
if len(or) == 0 {
return "", nil, fmt.Errorf("'Or' requires at least one condition")
}
group := len(or) > 1
expr := new(bytes.Buffer)
args = make([]interface{}, 0)
if group {
expr.WriteByte('(')
}
args, err = appendToSQL(or, expr, " OR ", args)
if err != nil {
return
}
if group {
expr.WriteByte(')')
}
sql = expr.String()
return
}
// And connects multiple expressions with the "AND" statement
type And []Sqlizer
func (and And) ToSQL() (sql string, args []interface{}, err error) {
if len(and) == 0 {
return "", nil, fmt.Errorf("'And' requires at least one condition")
}
group := len(and) > 1
expr := new(bytes.Buffer)
args = make([]interface{}, 0)
if group {
expr.WriteByte('(')
}
args, err = appendToSQL(and, expr, " AND ", args)
if err != nil {
return "", nil, err
}
if group {
expr.WriteByte(')')
}
return expr.String(), args, nil
}
// RawSQLWithArgs is a free form sql with possible arguments
type RawSQLWithArgs struct {
SQL string
Args []interface{}
}
func (r *RawSQLWithArgs) ToSQL() (string, []interface{}, error) {
return r.SQL, r.Args, nil
}
// Columns is a helper that simplifies columns list creation
type Columns []string
func (s Columns) String() string {
columns := ""
if len(s) > 0 {
columns = strings.Join(s, ", ")
}
return columns
}
func (s Columns) ToSQL() (string, []interface{}, error) {
return s.String(), nil, nil
}
// OrderBy is a helper that simplifies creation
// of the "ORDER BY" SQL statement
type OrderBy []qparser.Sort
func (s OrderBy) String() string {
if len(s) == 0 {
return ""
}
var sb strings.Builder
for i := 0; i < len(s); i++ {
if i != 0 {
sb.WriteString(", ")
}
sb.WriteString(s[i].FieldName)
sb.WriteString(" ")
sb.WriteString(s[i].Order.String())
}
return sb.String()
}
func (s OrderBy) ToSQL() (string, []interface{}, error) {
return s.String(), nil, nil
}
// Not - Negates a single expression: NOT (expression)
type Not struct {
Expr Sqlizer
}
func (n *Not) ToSQL() (string, []interface{}, error) {
sql, args, err := n.Expr.ToSQL()
if err != nil {
return "", nil, err
}
return "NOT (" + sql + ")", args, nil
}
// RawSQL is a raw SQL string without arguments
type RawSQL string
func (s RawSQL) ToSQL() (string, []interface{}, error) {
return string(s), nil, nil
}
func appendToSQL(parts []Sqlizer, w io.Writer, sep string, args []interface{}) ([]interface{}, error) {
for i, p := range parts {
partSQL, partArgs, err := p.ToSQL()
if err != nil {
return nil, err
}
if partSQL == "" {
continue
}
if i > 0 {
if _, inErr := io.WriteString(w, sep); inErr != nil {
return nil, inErr
}
}
_, err = io.WriteString(w, partSQL)
if err != nil {
return nil, err
}
args = append(args, partArgs...)
}
return args, nil
}