-
Notifications
You must be signed in to change notification settings - Fork 11
/
select.go
315 lines (250 loc) · 6.92 KB
/
select.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package builder
import (
"fmt"
"github.com/ulule/loukoum/v3/parser"
"github.com/ulule/loukoum/v3/stmt"
"github.com/ulule/loukoum/v3/types"
)
// Select is a builder used for "SELECT" query.
type Select struct {
query stmt.Select
}
// NewSelect creates a new Select.
func NewSelect() Select {
return Select{}
}
// DistinctOn adds a DISTINCT ON clause to the query.
func (b Select) DistinctOn(args ...interface{}) Select {
if !b.query.GroupBy.IsEmpty() {
panic("loukoum: select builder has distinct on clause already defined")
}
columns := ToColumns(args)
distinctOn := stmt.NewDistinctOn(columns)
if distinctOn.IsEmpty() {
panic("loukoum: given distinct on clause is undefined")
}
b.query.DistinctOn = distinctOn
return b
}
// Distinct adds a DISTINCT clause to the query.
func (b Select) Distinct() Select {
b.query.Distinct = true
return b
}
// Columns adds result columns to the query.
func (b Select) Columns(args ...interface{}) Select {
if len(b.query.Expressions) != 0 {
panic("loukoum: select builder has columns already defined")
}
if len(args) == 0 {
args = []interface{}{"*"}
}
b.query.Expressions = ToSelectExpressions(args)
return b
}
// From sets the FROM clause of the query.
func (b Select) From(arg ...interface{}) Select {
if !b.query.From.IsEmpty() {
panic("loukoum: select builder has from clause already defined")
}
b.query.From = ToFrom(arg...)
return b
}
// Join adds a JOIN clause to the query.
func (b Select) Join(args ...interface{}) Select {
switch len(args) {
case 1:
return b.join1(args)
case 2:
return b.join2(args)
case 3:
return b.join3(args)
default:
panic("loukoum: given join clause is invalid")
}
}
func (b Select) join1(args []interface{}) Select {
join := stmt.Join{}
switch value := args[0].(type) {
case string:
join = parser.MustParseJoin(value)
case stmt.Join:
join = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as join clause", args[0]))
}
if join.IsEmpty() {
panic("loukoum: given join clause is undefined")
}
b.query.Joins = append(b.query.Joins, join)
return b
}
func (b Select) join2(args []interface{}) Select {
join := handleSelectJoin(args)
if join.IsEmpty() {
panic("loukoum: given join clause is undefined")
}
b.query.Joins = append(b.query.Joins, join)
return b
}
func (b Select) join3(args []interface{}) Select {
join := handleSelectJoin(args)
switch value := args[2].(type) {
case types.JoinType:
join.Type = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as join clause", args[1]))
}
if join.IsEmpty() {
panic("loukoum: given join clause is undefined")
}
b.query.Joins = append(b.query.Joins, join)
return b
}
// With adds WITH clauses.
func (b Select) With(args ...stmt.WithQuery) Select {
if b.query.With.IsEmpty() {
b.query.With = stmt.NewWith(args)
return b
}
b.query.With.Queries = append(b.query.With.Queries, args...)
return b
}
// Where adds WHERE clauses.
func (b Select) Where(condition stmt.Expression) Select {
if condition == nil {
panic("loukoum: condition must be not nil")
}
if b.query.Where.IsEmpty() {
b.query.Where = stmt.NewWhere(condition)
return b
}
return b.And(condition)
}
// And adds AND WHERE conditions.
func (b Select) And(condition stmt.Expression) Select {
if condition == nil {
panic("loukoum: condition must be not nil")
}
b.query.Where = b.query.Where.And(condition)
return b
}
// Or adds OR WHERE conditions.
func (b Select) Or(condition stmt.Expression) Select {
b.query.Where = b.query.Where.Or(condition)
return b
}
// GroupBy adds GROUP BY clauses.
func (b Select) GroupBy(args ...interface{}) Select {
if !b.query.GroupBy.IsEmpty() {
panic("loukoum: select builder has group by clause already defined")
}
columns := ToColumns(args)
group := stmt.NewGroupBy(columns)
if group.IsEmpty() {
panic("loukoum: given join clause is undefined")
}
b.query.GroupBy = group
return b
}
// Having adds HAVING clauses.
func (b Select) Having(condition stmt.Expression) Select {
if !b.query.Having.IsEmpty() {
panic("loukoum: select builder has having clause already defined")
}
b.query.Having = stmt.NewHaving(condition)
return b
}
// OrderBy adds ORDER BY clauses.
func (b Select) OrderBy(orders ...stmt.Order) Select {
b.query.OrderBy.Orders = append(b.query.OrderBy.Orders, orders...)
return b
}
// Limit adds LIMIT clause.
func (b Select) Limit(value interface{}) Select {
if !b.query.Limit.IsEmpty() {
panic("loukoum: select builder has limit clause already defined")
}
b.query.Limit = ToLimit(value)
return b
}
// Offset adds OFFSET clause.
func (b Select) Offset(value interface{}) Select {
if !b.query.Offset.IsEmpty() {
panic("loukoum: select builder has offset clause already defined")
}
b.query.Offset = ToOffset(value)
return b
}
// Suffix adds given clauses as suffixes.
func (b Select) Suffix(suffix interface{}) Select {
if !b.query.Offset.IsEmpty() {
panic("loukoum: select builder has suffixes clauses already defined")
}
b.query.Suffix = ToSuffix(suffix)
return b
}
// Prefix adds given clauses as prefixes.
func (b Select) Prefix(prefix interface{}) Select {
if !b.query.Offset.IsEmpty() {
panic("loukoum: select builder has prefixes clauses already defined")
}
b.query.Prefix = ToPrefix(prefix)
return b
}
// Comment adds comment to the query.
func (b Select) Comment(comment string) Select {
b.query.Comment = stmt.NewComment(comment)
return b
}
// String returns the underlying query as a raw statement.
// This function should be used for debugging since it doesn't escape anything and is completely
// vulnerable to SQL injection.
// You should use either NamedQuery() or Query()...
func (b Select) String() string {
ctx := &types.RawContext{}
b.query.Write(ctx)
return ctx.Query()
}
// NamedQuery returns the underlying query as a named statement.
func (b Select) NamedQuery() (string, map[string]interface{}) {
ctx := &types.NamedContext{}
b.query.Write(ctx)
return ctx.Query(), ctx.Values()
}
// Query returns the underlying query as a regular statement.
func (b Select) Query() (string, []interface{}) {
ctx := &types.StdContext{}
b.query.Write(ctx)
return ctx.Query(), ctx.Values()
}
// Statement returns underlying statement.
func (b Select) Statement() stmt.Statement {
return b.query
}
func handleSelectJoin(args []interface{}) stmt.Join {
join := stmt.Join{}
table := stmt.Table{}
switch value := args[0].(type) {
case string:
table = stmt.NewTable(value)
case stmt.Table:
table = value
default:
panic(fmt.Sprintf("loukoum: cannot use %T as table argument for join clause", args[0]))
}
switch value := args[1].(type) {
case string:
join = parser.MustParseJoin(value)
case stmt.OnClause:
join = stmt.NewInnerJoin(table, value)
case stmt.InfixOnExpression:
join = stmt.NewInnerJoin(table, value)
default:
panic(fmt.Sprintf("loukoum: cannot use %T as condition for join clause", args[1]))
}
join.Table = table
return join
}
// Ensure that Select is a Builder
var _ Builder = Select{}