-
Notifications
You must be signed in to change notification settings - Fork 2
/
select.go
275 lines (222 loc) · 6.76 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
package qb
// SelectQuery represents a query that returns data
type SelectQuery interface {
Query
getSQL(SQLBuilder, bool) (string, []interface{})
SubQuery(...*Field) *SubQuery
CTE(...*Field) *CTE
Fields() []Field
}
// ReturningBuilder builds a query with a RETURNING statement
type ReturningBuilder struct {
Query Query
fields []Field
}
// Returning creates a RETURNING or OUTPUT query
func Returning(q Query, f ...Field) SelectQuery {
return ReturningBuilder{q, f}
}
// SQL returns a query string and a list of values
func (q ReturningBuilder) SQL(b SQLBuilder) (string, []interface{}) {
return q.getSQL(b, false)
}
func (q ReturningBuilder) getSQL(b SQLBuilder, aliasFields bool) (string, []interface{}) {
s, v := b.Context.Driver.Returning(b, q.Query, q.fields)
return s, append(v, *b.Context.Values...)
}
// Fields returns a list of the fields used in the query
func (q ReturningBuilder) Fields() []Field {
return q.fields
}
// SubQuery converts the SelectQuery to a SubQuery for use in further queries
func (q ReturningBuilder) SubQuery(fields ...*Field) *SubQuery {
return newSubQuery(q, fields)
}
// CTE creates a new CTE (WITH) Query
func (q ReturningBuilder) CTE(fields ...*Field) *CTE {
return newCTE(q, fields)
}
// SelectBuilder builds a SELECT query
type SelectBuilder struct {
source Source
fields []Field
where []Condition
joins []join
order []FieldOrder
group []Field
having []Condition
tables []Source
limit int
offset int
}
// NewSelectBuilder retruns a new SelectBuilder
func NewSelectBuilder(f []Field, src Source) *SelectBuilder {
return &SelectBuilder{fields: f, source: src, tables: []Source{src}}
}
// Where adds conditions to the WHERE clause
func (q *SelectBuilder) Where(c ...Condition) *SelectBuilder {
q.where = append(q.where, c...)
return q
}
// InnerJoin adds an INNER JOIN clause to the query
func (q *SelectBuilder) InnerJoin(f1, f2 Field, c ...Condition) *SelectBuilder {
return q.join(JoinInner, f1, f2, c)
}
// CrossJoin adds a CROSS JOIN clause to the query
func (q *SelectBuilder) CrossJoin(s Source) *SelectBuilder {
q.joins = append(q.joins, join{JoinCross, s, nil})
q.tables = append(q.tables, s)
return q
}
// LeftJoin adds a LEFT JOIN clause to the query
func (q *SelectBuilder) LeftJoin(f1, f2 Field, c ...Condition) *SelectBuilder {
return q.join(JoinLeft, f1, f2, c)
}
// RightJoin adds a RIGHT JOIN clause to the query
func (q *SelectBuilder) RightJoin(f1, f2 Field, c ...Condition) *SelectBuilder {
return q.join(JoinRight, f1, f2, c)
}
// ManualJoin manually joins a table
// Only use this if you know what you are doing
func (q *SelectBuilder) ManualJoin(t Join, s Source, c ...Condition) *SelectBuilder {
q.joins = append(q.joins, join{t, s, c})
q.tables = append(q.tables, s)
return q
}
func (q *SelectBuilder) join(t Join, f1, f2 Field, c []Condition) *SelectBuilder {
var new Source
exists := 0
for _, v := range q.tables {
if src := getParent(f1); src == v {
exists++
new = getParent(f2)
}
if src := getParent(f2); src == v {
exists++
new = getParent(f1)
}
}
if exists == 0 {
panic(`None of these tables are present in the query`)
}
if exists > 1 {
panic(`Both tables already joined`)
}
return q.ManualJoin(t, new, append(c, eq(f1, f2))...)
}
// GroupBy adds a GROUP BY clause to the query
func (q *SelectBuilder) GroupBy(f ...Field) *SelectBuilder {
q.group = f
return q
}
// Having adds a HAVING clause to the query
func (q *SelectBuilder) Having(c ...Condition) *SelectBuilder {
q.having = append(q.having, c...)
return q
}
// OrderBy adds a ORDER BY clause to the query
func (q *SelectBuilder) OrderBy(o ...FieldOrder) *SelectBuilder {
q.order = o
return q
}
// Limit adds a LIMIT clause to the query
func (q *SelectBuilder) Limit(i int) *SelectBuilder {
q.limit = i
return q
}
// Offset adds a OFFSET clause to the query
func (q *SelectBuilder) Offset(i int) *SelectBuilder {
q.offset = i
return q
}
// CTE creates a new CTE (WITH) Query
func (q *SelectBuilder) CTE(fields ...*Field) *CTE {
return newCTE(q, fields)
}
// SQL returns a query string and a list of values
func (q *SelectBuilder) SQL(b SQLBuilder) (string, []interface{}) {
return q.getSQL(b, false)
}
func (q *SelectBuilder) getSQL(b SQLBuilder, aliasFields bool) (string, []interface{}) {
oldAlias := b.Context.alias
if _, ok := oldAlias.(*noAlias); ok {
b.Context.alias = AliasGenerator()
}
for _, v := range q.tables {
_ = b.Context.Alias(v)
}
b.Select(aliasFields, q.fields...)
b.From(q.source)
b.Join(q.joins...)
b.Where(q.where...)
b.GroupBy(q.group...)
b.Having(q.having...)
b.OrderBy(q.order...)
b.LimitOffset(q.limit, q.offset)
b.Context.alias = oldAlias
return b.w.String(), *b.Context.Values
}
// SubQuery converts the SelectQuery to a SubQuery for use in further queries
func (q *SelectBuilder) SubQuery(fields ...*Field) *SubQuery {
return newSubQuery(q, fields)
}
// Fields returns a list of the fields used in the query
func (q *SelectBuilder) Fields() []Field {
return q.fields
}
////////////////////////////
type combinedQuery struct {
combineType string
queries []SelectQuery
}
func (q combinedQuery) getSQL(b SQLBuilder, aliasFields bool) (string, []interface{}) {
s := ``
for k, v := range q.queries {
var sql string
if k == 0 {
sql, _ = v.getSQL(b, aliasFields)
} else {
s += ` ` + q.combineType + ` `
sql, _ = v.getSQL(b, false)
}
s += getSubQuerySQL(sql)
}
return s + NEWLINE, *b.Context.Values
}
func (q combinedQuery) SQL(b SQLBuilder) (string, []interface{}) {
return q.getSQL(b, false)
}
func (q combinedQuery) Fields() []Field {
return q.queries[0].Fields()
}
func (q combinedQuery) CTE(fields ...*Field) *CTE {
return newCTE(q, fields)
}
func (q combinedQuery) SubQuery(fields ...*Field) *SubQuery {
return newSubQuery(q, fields)
}
////////////////////////
// UnionAll combines queries with an UNION ALL
func UnionAll(q ...SelectQuery) SelectQuery {
return combinedQuery{combineType: `UNION ALL`, queries: q}
}
// Union combines queries with an UNION
func Union(q ...SelectQuery) SelectQuery {
return combinedQuery{combineType: `UNION`, queries: q}
}
// ExceptAll combines queries with an EXCEPT ALL
func ExceptAll(q1, q2 SelectQuery) SelectQuery {
return combinedQuery{combineType: `EXCEPT ALL`, queries: []SelectQuery{q1, q2}}
}
// Except combines queries with an EXCEPT
func Except(q1, q2 SelectQuery) SelectQuery {
return combinedQuery{combineType: `EXCEPT`, queries: []SelectQuery{q1, q2}}
}
// IntersectAll combines queries with an INTERSECT ALL
func IntersectAll(q1, q2 SelectQuery) SelectQuery {
return combinedQuery{combineType: `INTERSECT ALL`, queries: []SelectQuery{q1, q2}}
}
// Intersect combines queries with an INTERSECT
func Intersect(q1, q2 SelectQuery) SelectQuery {
return combinedQuery{combineType: `INTERSECT`, queries: []SelectQuery{q1, q2}}
}