@@ -5,60 +5,18 @@ import (
5
5
"strings"
6
6
)
7
7
8
- type clauseKind uint
9
-
10
- type fromClause struct {
11
- table string
12
- }
13
-
14
- type intoClause struct {
15
- table string
16
- }
17
-
18
- type limitClause int64
19
-
20
- type offsetClause int64
21
-
22
- type orderClause struct {
23
- cols []string
24
- dir string
25
- }
26
-
27
- type returningClause struct {
28
- cols []string
29
- }
30
-
31
- type setClause struct {
32
- col string
33
- expr Expr
34
- }
35
-
36
- type unionClause struct {
37
- q Query
38
- }
39
-
40
- type valuesClause struct {
41
- items []string
42
- args []interface {}
43
- }
44
-
45
- type whereClause struct {
46
- conjunction string
47
- op string
48
- left Expr
49
- right Expr
50
- }
51
-
52
- // Clause is a Query expression that will typically represent one of the
8
+ // clause is a Query expression that will typically represent one of the
53
9
// following SQL clauses, FROM, LIMIT, OFFSET, ORDER BY, UNION, VALUES,
54
10
// WHERE, RETURNING, and SET.
55
- type Clause interface {
11
+ type clause interface {
56
12
Expr
57
13
58
- // Kind returns the kind of the current Clause .
59
- Kind () clauseKind
14
+ // kind returns the kind of the current clause .
15
+ kind () clauseKind
60
16
}
61
17
18
+ type clauseKind uint
19
+
62
20
//go:generate stringer -type clauseKind -linecomment
63
21
const (
64
22
_FromClause clauseKind = iota // FROM
@@ -72,17 +30,6 @@ const (
72
30
_SetClause // SET
73
31
)
74
32
75
- var (
76
- _ Clause = (* fromClause )(nil )
77
- _ Clause = (* limitClause )(nil )
78
- _ Clause = (* offsetClause )(nil )
79
- _ Clause = (* orderClause )(nil )
80
- _ Clause = (* unionClause )(nil )
81
- _ Clause = (* whereClause )(nil )
82
- _ Clause = (* returningClause )(nil )
83
- _ Clause = (* setClause )(nil )
84
- )
85
-
86
33
func realWhere (conjunction string , left Expr , op string , right Expr ) Option {
87
34
return func (q Query ) Query {
88
35
leftArgs := left .Args ()
@@ -107,6 +54,24 @@ func realWhere(conjunction string, left Expr, op string, right Expr) Option {
107
54
}
108
55
}
109
56
57
+ // Where appends a WHERE clause to the Query. This will append the arguments
58
+ // of the given expression to the Query too. By default this will use AND for
59
+ // conjoining multiple WHERE clauses.
60
+ func Where (col , op string , expr Expr ) Option {
61
+ return func (q Query ) Query {
62
+ return realWhere ("AND" , Ident (col ), op , expr )(q )
63
+ }
64
+ }
65
+
66
+ // OrWhere appends a WHERE clause to the Query. This will append the arguments
67
+ // of the given expression to the Query too. This will use OR for conjoining
68
+ // with a preceding WHERE clause.
69
+ func OrWhere (col , op string , expr Expr ) Option {
70
+ return func (q Query ) Query {
71
+ return realWhere ("OR" , Ident (col ), op , expr )(q )
72
+ }
73
+ }
74
+
110
75
// From appends a FROM clause for the given table to the Query.
111
76
func From (table string ) Option {
112
77
return func (q Query ) Query {
@@ -201,60 +166,98 @@ func Values(vals ...interface{}) Option {
201
166
}
202
167
}
203
168
204
- // Where appends a WHERE clause to the Query. This will append the arguments
205
- // of the given expression to the Query too. By default this will use AND for
206
- // conjoining multiple WHERE clauses.
207
- func Where (col , op string , expr Expr ) Option {
208
- return func (q Query ) Query {
209
- return realWhere ("AND" , Ident (col ), op , expr )(q )
210
- }
169
+ type fromClause struct {
170
+ table string
211
171
}
212
172
213
- // OrWhere appends a WHERE clause to the Query. This will append the arguments
214
- // of the given expression to the Query too. This will use OR for conjoining
215
- // with a preceding WHERE clause.
216
- func OrWhere (col , op string , expr Expr ) Option {
217
- return func (q Query ) Query {
218
- return realWhere ("OR" , Ident (col ), op , expr )(q )
219
- }
220
- }
173
+ var _ clause = (* fromClause )(nil )
221
174
222
175
func (c fromClause ) Args () []interface {} { return nil }
223
176
func (c fromClause ) Build () string { return c .table }
224
- func (c fromClause ) Kind () clauseKind { return _FromClause }
177
+ func (c fromClause ) kind () clauseKind { return _FromClause }
178
+
179
+ type limitClause int64
180
+
181
+ var _ clause = (* limitClause )(nil )
225
182
226
183
func (c limitClause ) Args () []interface {} { return nil }
227
184
func (c limitClause ) Build () string { return strconv .FormatInt (int64 (c ), 10 ) }
228
- func (c limitClause ) Kind () clauseKind { return _LimitClause }
185
+ func (c limitClause ) kind () clauseKind { return _LimitClause }
186
+
187
+ type offsetClause int64
188
+
189
+ var _ clause = (* offsetClause )(nil )
229
190
230
191
func (c offsetClause ) Args () []interface {} { return nil }
231
192
func (c offsetClause ) Build () string { return strconv .FormatInt (int64 (c ), 10 ) }
232
- func (c offsetClause ) Kind () clauseKind { return _OffsetClause }
193
+ func (c offsetClause ) kind () clauseKind { return _OffsetClause }
194
+
195
+ type orderClause struct {
196
+ cols []string
197
+ dir string
198
+ }
199
+
200
+ var _ clause = (* orderClause )(nil )
233
201
234
202
func (c orderClause ) Args () []interface {} { return nil }
235
203
func (c orderClause ) Build () string { return strings .Join (c .cols , ", " ) + " " + c .dir }
236
- func (c orderClause ) Kind () clauseKind { return _OrderClause }
204
+ func (c orderClause ) kind () clauseKind { return _OrderClause }
205
+
206
+ type returningClause struct {
207
+ cols []string
208
+ }
209
+
210
+ var _ clause = (* returningClause )(nil )
237
211
238
212
func (c returningClause ) Args () []interface {} { return nil }
239
213
func (c returningClause ) Build () string { return strings .Join (c .cols , ", " ) }
240
- func (c returningClause ) Kind () clauseKind { return _ReturningClause }
214
+ func (c returningClause ) kind () clauseKind { return _ReturningClause }
215
+
216
+ type setClause struct {
217
+ col string
218
+ expr Expr
219
+ }
220
+
221
+ var _ clause = (* setClause )(nil )
241
222
242
223
func (c setClause ) Args () []interface {} { return nil }
243
224
func (c setClause ) Build () string { return c .col + " = " + c .expr .Build () }
244
- func (c setClause ) Kind () clauseKind { return _SetClause }
225
+ func (c setClause ) kind () clauseKind { return _SetClause }
226
+
227
+ type unionClause struct {
228
+ q Query
229
+ }
230
+
231
+ var _ clause = (* unionClause )(nil )
245
232
246
233
func (c unionClause ) Args () []interface {} { return nil }
247
- func (c unionClause ) Kind () clauseKind { return _UnionClause }
248
234
func (c unionClause ) Build () string { return c .q .buildInitial () }
235
+ func (c unionClause ) kind () clauseKind { return _UnionClause }
236
+
237
+ type valuesClause struct {
238
+ items []string
239
+ args []interface {}
240
+ }
241
+
242
+ var _ clause = (* valuesClause )(nil )
249
243
250
244
func (c valuesClause ) Args () []interface {} { return c .args }
251
245
func (c valuesClause ) Build () string { return "(" + strings .Join (c .items , ", " ) + ")" }
252
- func (c valuesClause ) Kind () clauseKind { return _ValuesClause }
246
+ func (c valuesClause ) kind () clauseKind { return _ValuesClause }
247
+
248
+ type whereClause struct {
249
+ conjunction string
250
+ op string
251
+ left Expr
252
+ right Expr
253
+ }
254
+
255
+ var _ clause = (* whereClause )(nil )
253
256
254
257
func (c whereClause ) Args () []interface {} { return nil }
255
258
256
259
func (c whereClause ) Build () string {
257
260
return c .left .Build () + " " + c .op + " " + c .right .Build ()
258
261
}
259
262
260
- func (c whereClause ) Kind () clauseKind { return _WhereClause }
263
+ func (c whereClause ) kind () clauseKind { return _WhereClause }
0 commit comments