forked from doug-martin/goqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_select.go
405 lines (358 loc) · 13 KB
/
dataset_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package goqu
import (
"fmt"
)
var (
conditioned_join_types = map[JoinType]bool{
INNER_JOIN: true,
FULL_OUTER_JOIN: true,
RIGHT_OUTER_JOIN: true,
LEFT_OUTER_JOIN: true,
FULL_JOIN: true,
RIGHT_JOIN: true,
LEFT_JOIN: true,
}
)
//Adds columns to the SELECT clause. See examples
//You can pass in the following.
// string: Will automatically be turned into an identifier
// Dataset: Will use the SQL generated from that Dataset. If the dataset is aliased it will use that alias as the column name.
// LiteralExpression: (See Literal) Will use the literal SQL
// SqlFunction: (See Func, MIN, MAX, COUNT....)
// Struct: If passing in an instance of a struct, we will parse the struct for the column names to select. See examples
func (me *Dataset) Select(selects ...interface{}) *Dataset {
ret := me.copy()
ret.clauses.SelectDistinct = nil
ret.clauses.Select = cols(selects...)
return ret
}
//Adds columns to the SELECT DISTINCT clause. See examples
//You can pass in the following.
// string: Will automatically be turned into an identifier
// Dataset: Will use the SQL generated from that Dataset. If the dataset is aliased it will use that alias as the column name.
// LiteralExpression: (See Literal) Will use the literal SQL
// SqlFunction: (See Func, MIN, MAX, COUNT....)
// Struct: If passing in an instance of a struct, we will parse the struct for the column names to select. See examples
func (me *Dataset) SelectDistinct(selects ...interface{}) *Dataset {
ret := me.copy()
ret.clauses.Select = nil
ret.clauses.SelectDistinct = cols(selects...)
return ret
}
//Resets to SELECT *. If the SelectDistinct was used the returned Dataset will have the the dataset set to SELECT *. See examples.
func (me *Dataset) ClearSelect() *Dataset {
ret := me.copy()
ret.clauses.Select = cols(Literal("*"))
ret.clauses.SelectDistinct = nil
return ret
}
//Returns true if using default SELECT *
func (me *Dataset) isDefaultSelect() bool {
ret := false
if me.clauses.Select != nil {
selects := me.clauses.Select.Columns()
if len(selects) == 1 {
if l, ok := selects[0].(LiteralExpression); ok && l.Literal() == "*" {
ret = true
}
}
}
return ret
}
//Adds columns to the SELECT clause. See examples
//You can pass in the following.
// string: Will automatically be turned into an identifier
// Dataset: Will use the SQL generated from that Dataset. If the dataset is aliased it will use that alias as the column name.
// LiteralExpression: (See Literal) Will use the literal SQL
// SqlFunction: (See Func, MIN, MAX, COUNT....)
func (me *Dataset) SelectAppend(selects ...interface{}) *Dataset {
ret := me.copy()
if ret.clauses.SelectDistinct != nil {
ret.clauses.SelectDistinct = ret.clauses.SelectDistinct.Append(cols(selects...).Columns()...)
} else {
ret.clauses.Select = ret.clauses.Select.Append(cols(selects...).Columns()...)
}
return ret
}
//Adds a FROM clause. This return a new dataset with the original sources replaced. See examples.
//You can pass in the following.
// string: Will automatically be turned into an identifier
// Dataset: Will be added as a sub select. If the Dataset is not aliased it will automatically be aliased
// LiteralExpression: (See Literal) Will use the literal SQL
func (me *Dataset) From(from ...interface{}) *Dataset {
ret := me.copy()
var sources []interface{}
numSources := 0
for _, source := range from {
if d, ok := source.(*Dataset); ok && d.clauses.Alias == nil {
numSources++
sources = append(sources, d.As(fmt.Sprintf("t%d", numSources)))
} else {
sources = append(sources, source)
}
}
ret.clauses.From = cols(sources...)
return ret
}
//Returns a new Dataset with the current one as an source. If the current Dataset is not aliased (See Dataset#As) then it will automatically be aliased. See examples.
func (me *Dataset) FromSelf() *Dataset {
builder := Dataset{}
builder.database = me.database
builder.adapter = me.adapter
builder.clauses = clauses{
Select: cols(Star()),
}
return builder.From(me)
}
//Alias to InnerJoin. See examples.
func (me *Dataset) Join(table Expression, condition joinExpression) *Dataset {
return me.InnerJoin(table, condition)
}
//Adds an INNER JOIN clause. See examples.
func (me *Dataset) InnerJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(INNER_JOIN, table, condition)
}
//Adds a FULL OUTER JOIN clause. See examples.
func (me *Dataset) FullOuterJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(FULL_OUTER_JOIN, table, condition)
}
//Adds a RIGHT OUTER JOIN clause. See examples.
func (me *Dataset) RightOuterJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(RIGHT_OUTER_JOIN, table, condition)
}
//Adds a LEFT OUTER JOIN clause. See examples.
func (me *Dataset) LeftOuterJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(LEFT_OUTER_JOIN, table, condition)
}
//Adds a FULL JOIN clause. See examples.
func (me *Dataset) FullJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(FULL_JOIN, table, condition)
}
//Adds a RIGHT JOIN clause. See examples.
func (me *Dataset) RightJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(RIGHT_JOIN, table, condition)
}
//Adds a LEFT JOIN clause. See examples.
func (me *Dataset) LeftJoin(table Expression, condition joinExpression) *Dataset {
return me.joinTable(LEFT_JOIN, table, condition)
}
//Adds a NATURAL JOIN clause. See examples.
func (me *Dataset) NaturalJoin(table Expression) *Dataset {
return me.joinTable(NATURAL_JOIN, table, nil)
}
//Adds a NATURAL LEFT JOIN clause. See examples.
func (me *Dataset) NaturalLeftJoin(table Expression) *Dataset {
return me.joinTable(NATURAL_LEFT_JOIN, table, nil)
}
//Adds a NATURAL RIGHT JOIN clause. See examples.
func (me *Dataset) NaturalRightJoin(table Expression) *Dataset {
return me.joinTable(NATURAL_RIGHT_JOIN, table, nil)
}
//Adds a NATURAL FULL JOIN clause. See examples.
func (me *Dataset) NaturalFullJoin(table Expression) *Dataset {
return me.joinTable(NATURAL_FULL_JOIN, table, nil)
}
//Adds a CROSS JOIN clause. See examples.
func (me *Dataset) CrossJoin(table Expression) *Dataset {
return me.joinTable(CROSS_JOIN, table, nil)
}
//Joins this Datasets table with another
func (me *Dataset) joinTable(joinType JoinType, table Expression, condition joinExpression) *Dataset {
ret := me.copy()
isConditioned := conditioned_join_types[joinType]
ret.clauses.Joins = append(ret.clauses.Joins, JoiningClause{JoinType: joinType, IsConditioned: isConditioned, Table: table, Condition: condition})
return ret
}
//Adds a WHERE clause. See examples.
func (me *Dataset) Where(expressions ...Expression) *Dataset {
expLen := len(expressions)
if expLen > 0 {
ret := me.copy()
if ret.clauses.Where == nil {
ret.clauses.Where = And(expressions...)
} else {
ret.clauses.Where = ret.clauses.Where.Append(expressions...)
}
return ret
}
return me
}
//Removes the WHERE clause. See examples.
func (me *Dataset) ClearWhere() *Dataset {
ret := me.copy()
ret.clauses.Where = nil
return ret
}
//Adds a GROUP BY clause. See examples.
func (me *Dataset) GroupBy(groupBy ...interface{}) *Dataset {
ret := me.copy()
ret.clauses.GroupBy = cols(groupBy...)
return ret
}
//Adds a HAVING clause. See examples.
func (me *Dataset) Having(expressions ...Expression) *Dataset {
expLen := len(expressions)
if expLen > 0 {
ret := me.copy()
if ret.clauses.Having == nil {
ret.clauses.Having = And(expressions...)
} else {
ret.clauses.Having = ret.clauses.Having.Append(expressions...)
}
return ret
}
return me
}
//Adds a ORDER clause. If the ORDER is currently set it replaces it. See examples.
func (me *Dataset) Order(order ...OrderedExpression) *Dataset {
ret := me.copy()
ret.clauses.Order = orderList(order...)
return ret
}
//Adds a more columns to the current ORDER BY clause. If no order has be previously specified it is the same as calling Order. See examples.
func (me *Dataset) OrderAppend(order ...OrderedExpression) *Dataset {
if me.clauses.Order == nil {
return me.Order(order...)
} else {
ret := me.copy()
ret.clauses.Order = ret.clauses.Order.Append(orderList(order...).Columns()...)
return ret
}
return me
}
//Removes the ORDER BY clause. See examples.
func (me *Dataset) ClearOrder() *Dataset {
ret := me.copy()
ret.clauses.Order = nil
return ret
}
//Adds a LIMIT clause. If the LIMIT is currently set it replaces it. See examples.
func (me *Dataset) Limit(limit uint) *Dataset {
ret := me.copy()
if limit > 0 {
ret.clauses.Limit = limit
} else {
ret.clauses.Limit = nil
}
return ret
}
//Adds a LIMIT ALL clause. If the LIMIT is currently set it replaces it. See examples.
func (me *Dataset) LimitAll() *Dataset {
ret := me.copy()
ret.clauses.Limit = Literal("ALL")
return ret
}
//Removes the LIMIT clause.
func (me *Dataset) ClearLimit() *Dataset {
return me.Limit(0)
}
//Adds an OFFSET clause. If the OFFSET is currently set it replaces it. See examples.
func (me *Dataset) Offset(offset uint) *Dataset {
ret := me.copy()
ret.clauses.Offset = offset
return ret
}
//Removes the OFFSET clause from the Dataset
func (me *Dataset) ClearOffset() *Dataset {
return me.Offset(0)
}
//Creates an UNION statement with another dataset.
// If this or the other dataset has a limit or offset it will use that dataset as a subselect in the FROM clause. See examples.
func (me *Dataset) Union(other *Dataset) *Dataset {
ret := me.compoundFromSelf()
ret.clauses.Compounds = append(ret.clauses.Compounds, Union(other.compoundFromSelf()))
return ret
}
//Creates an UNION ALL statement with another dataset.
// If this or the other dataset has a limit or offset it will use that dataset as a subselect in the FROM clause. See examples.
func (me *Dataset) UnionAll(other *Dataset) *Dataset {
ret := me.compoundFromSelf()
ret.clauses.Compounds = append(ret.clauses.Compounds, UnionAll(other.compoundFromSelf()))
return ret
}
//Creates an INTERSECT statement with another dataset.
// If this or the other dataset has a limit or offset it will use that dataset as a subselect in the FROM clause. See examples.
func (me *Dataset) Intersect(other *Dataset) *Dataset {
ret := me.compoundFromSelf()
ret.clauses.Compounds = append(ret.clauses.Compounds, Intersect(other.compoundFromSelf()))
return ret
}
//Creates an INTERSECT ALL statement with another dataset.
// If this or the other dataset has a limit or offset it will use that dataset as a subselect in the FROM clause. See examples.
func (me *Dataset) IntersectAll(other *Dataset) *Dataset {
ret := me.compoundFromSelf()
ret.clauses.Compounds = append(ret.clauses.Compounds, IntersectAll(other.compoundFromSelf()))
return ret
}
//Used internally to determine if the dataset needs to use iteself as a source.
//If the dataset has an order or limit it will select from itself
func (me *Dataset) compoundFromSelf() *Dataset {
if me.clauses.Order != nil || me.clauses.Limit != nil {
return me.FromSelf()
}
return me.copy()
}
//Adds a RETURNING clause to the dataset if the adapter supports it. Typically used for INSERT, UPDATE or DELETE. See examples.
func (me *Dataset) Returning(returning ...interface{}) *Dataset {
ret := me.copy()
ret.clauses.Returning = cols(returning...)
return ret
}
//Sets the alias for this dataset. This is typically used when using a Dataset as a subselect. See examples.
func (me *Dataset) As(alias string) *Dataset {
ret := me.copy()
ret.clauses.Alias = I(alias)
return ret
}
//Generates a SELECT sql statement, if Prepared has been called with true then the parameters will not be interpolated. See examples.
//
//Errors:
// * There is an error generating the SQL
func (me *Dataset) ToSql() (string, []interface{}, error) {
buf := NewSqlBuilder(me.isPrepared)
if err := me.selectSqlWriteTo(buf); err != nil {
return "", nil, err
}
sql, args := buf.ToSql()
return sql, args, nil
}
//Does actual sql generation of sql, accepts an sql builder so other methods can call when creating subselects and needing prepared sql.
func (me *Dataset) selectSqlWriteTo(buf *SqlBuilder) error {
if err := me.adapter.CommonTablesSql(buf, me.clauses.CommonTables); err != nil {
return err
}
if me.clauses.SelectDistinct != nil {
if err := me.adapter.SelectDistinctSql(buf, me.clauses.SelectDistinct); err != nil {
return err
}
} else {
if err := me.adapter.SelectSql(buf, me.clauses.Select); err != nil {
return err
}
}
if err := me.adapter.FromSql(buf, me.clauses.From); err != nil {
return err
}
if err := me.adapter.JoinSql(buf, me.clauses.Joins); err != nil {
return err
}
if err := me.adapter.WhereSql(buf, me.clauses.Where); err != nil {
return err
}
if err := me.adapter.GroupBySql(buf, me.clauses.GroupBy); err != nil {
return err
}
if err := me.adapter.HavingSql(buf, me.clauses.Having); err != nil {
return err
}
if err := me.adapter.CompoundsSql(buf, me.clauses.Compounds); err != nil {
return err
}
if err := me.adapter.OrderSql(buf, me.clauses.Order); err != nil {
return err
}
if err := me.adapter.LimitSql(buf, me.clauses.Limit); err != nil {
return err
}
return me.adapter.OffsetSql(buf, me.clauses.Offset)
}