-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselect.go
380 lines (332 loc) · 10.5 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
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
package sol
import (
"fmt"
"strings"
"github.com/aodin/sol/dialect"
)
// Selectable is an interface that allows both tables and columns to be
// selected. It is implemented by TableElem and ColumnElem.
type Selectable interface {
Columns() []ColumnElem
}
// SelectStmt is the internal representation of an SQL SELECT statement.
type SelectStmt struct {
ConditionalStmt
alias string
tables []Tabular
columns ColumnSet
joins []JoinClause
groupBy ColumnSet
having Clause
orderBy []OrderedColumn
isDistinct bool
distincts ColumnSet
limit int
offset int
}
// Since SELECT statements can be used in FROM clauses, SelectStmt must
// implement the Tabular interface
var _ Tabular = SelectStmt{}
// String outputs the parameter-less SELECT statement in a neutral dialect.
func (stmt SelectStmt) String() string {
compiled, _ := stmt.Compile(&defaultDialect{}, Params())
return compiled
}
// As sets the alias for this statement
func (stmt SelectStmt) As(alias string) SelectStmt {
stmt.alias = alias
return stmt
}
// TODO create a TableSet type?
func (stmt SelectStmt) compileTables(d dialect.Dialect, ps *Parameters) ([]string, error) {
names := make([]string, len(stmt.tables))
var err error
for i, table := range stmt.tables {
if names[i], err = table.Compile(d, ps); err != nil {
return nil, err
}
}
return names, nil
}
func (stmt SelectStmt) Compile(d dialect.Dialect, ps *Parameters) (string, error) {
// Return immediately if there are existing errors
if err := stmt.Error(); err != nil {
return "", err
}
// Being building the statement
compiled := []string{SELECT}
if stmt.isDistinct {
compiled = append(compiled, DISTINCT)
if stmt.distincts.Exists() {
compiled = append(compiled, fmt.Sprintf(
"ON (%s)", strings.Join(stmt.distincts.FullNames(), ", "),
))
}
}
selections, err := stmt.columns.Compile(d, ps)
if err != nil {
return "", nil
}
tables, err := stmt.compileTables(d, ps)
if err != nil {
return "", nil
}
compiled = append(compiled, selections, FROM, strings.Join(tables, ", "))
if len(stmt.joins) != 0 {
for _, j := range stmt.joins {
jc, err := j.Compile(d, ps)
if err != nil {
return "", err
}
compiled = append(compiled, jc)
}
}
if stmt.where != nil {
conditional, err := stmt.where.Compile(d, ps)
if err != nil {
return "", err
}
compiled = append(compiled, WHERE, conditional)
}
if stmt.groupBy.Exists() {
cc, err := stmt.groupBy.Compile(d, ps)
if err != nil {
return "", err
}
compiled = append(compiled, GROUPBY, cc)
}
if stmt.having != nil {
conditional, err := stmt.having.Compile(d, ps)
if err != nil {
return "", err
}
compiled = append(compiled, HAVING, conditional)
}
if len(stmt.orderBy) > 0 {
order := make([]string, len(stmt.orderBy))
for i, ord := range stmt.orderBy {
order[i], _ = ord.Compile(d, ps)
}
compiled = append(compiled, ORDERBY, strings.Join(order, ", "))
}
if stmt.limit != 0 {
compiled = append(compiled, LIMIT, fmt.Sprintf("%d", stmt.limit))
}
if stmt.offset != 0 {
compiled = append(compiled, OFFSET, fmt.Sprintf("%d", stmt.offset))
}
if stmt.alias != "" {
return fmt.Sprintf(
"(%s) AS %s",
strings.Join(compiled, WHITESPACE), stmt.alias,
), nil
}
return strings.Join(compiled, WHITESPACE), nil
}
// Columns returns all the columns within the SELECT. This method implements
// the Tabular interface
func (stmt SelectStmt) Columns() []ColumnElem {
return stmt.columns.All()
}
// Name returns the name of the statement's alias or the name of its
// first table. It will return nothing if neither an alias or
// any tables exist.
func (stmt SelectStmt) Name() string {
if stmt.alias != "" {
return stmt.alias
}
if len(stmt.tables) != 0 {
return stmt.tables[0].Name()
}
return ""
}
func (stmt SelectStmt) Table() *TableElem {
// Create a new table from the statement's selections
// TODO this ignores an error!
columns, _ := stmt.columns.MakeUnique()
return &TableElem{
name: stmt.Name(),
columns: columns,
}
}
func (stmt SelectStmt) hasTable(name string) bool {
for _, table := range stmt.tables {
if table.Name() == name {
return true
}
}
return false
}
// From manually specifies the SelectStmt's FROM clause
func (stmt SelectStmt) From(tables ...Tabular) SelectStmt {
stmt.tables = tables
return stmt
}
// All removes the DISTINCT clause from the SELECT statement.
func (stmt SelectStmt) All() SelectStmt {
stmt.isDistinct = false
stmt.distincts = Columns() // reset
return stmt
}
// Distinct adds a DISTINCT clause to the SELECT statement. If any
// column are provided, the clause will be compiled as a DISTINCT ON.
func (stmt SelectStmt) Distinct(columns ...Columnar) SelectStmt {
stmt.isDistinct = true
// Since the ColumnSet is not unique, any errors can be ignored
stmt.distincts, _ = stmt.distincts.Add(columns...)
return stmt
}
func (stmt SelectStmt) join(table Tabular, method string, clauses ...Clause) SelectStmt {
stmt.joins = append(
stmt.joins,
JoinClause{
method: method,
table: table,
ArrayClause: ArrayClause{clauses: clauses, sep: " AND "},
},
)
return stmt
}
// CrossJoin adds a CROSS JOIN ... clause to the SELECT statement.
func (stmt SelectStmt) CrossJoin(table Tabular) SelectStmt {
return stmt.join(table, CROSSJOIN)
}
// InnerJoin adds an INNER JOIN ... ON ... clause to the SELECT statement.
// If no clauses are given, it will assume the clause is NATURAL.
func (stmt SelectStmt) InnerJoin(table Tabular, clauses ...Clause) SelectStmt {
return stmt.join(table, INNERJOIN, clauses...)
}
// LeftOuterJoin adds a LEFT OUTER JOIN ... ON ... clause to the SELECT
// statement. If no clauses are given, it will assume the clause is NATURAL.
func (stmt SelectStmt) LeftOuterJoin(table Tabular, clauses ...Clause) SelectStmt {
return stmt.join(table, LEFTOUTERJOIN, clauses...)
}
// RightOuterJoin adds a RIGHT OUTER JOIN ... ON ... clause to the SELECT
// statement. If no clauses are given, it will assume the clause is NATURAL.
func (stmt SelectStmt) RightOuterJoin(table Tabular, clauses ...Clause) SelectStmt {
return stmt.join(table, RIGHTOUTERJOIN, clauses...)
}
// FullOuterJoin adds a FULL OUTER JOIN ... ON ... clause to the SELECT
// statement. If no clauses are given, it will assume the clause is NATURAL.
func (stmt SelectStmt) FullOuterJoin(table Tabular, clauses ...Clause) SelectStmt {
return stmt.join(table, FULLOUTERJOIN, clauses...)
}
// Where adds a conditional clause to the SELECT statement. Only one WHERE
// is allowed per statement. Additional calls to Where will overwrite the
// existing WHERE clause.
func (stmt SelectStmt) Where(conditions ...Clause) SelectStmt {
if len(conditions) > 1 {
// By default, multiple where clauses will be joined using AllOf
stmt.where = AllOf(conditions...)
} else if len(conditions) == 1 {
stmt.where = conditions[0]
} else {
// Clear the existing conditions
stmt.where = nil
}
return stmt
}
// GroupBy adds a GROUP BY to the SELECT statement. Only one GROUP BY
// is allowed per statement. Additional calls to GroupBy will overwrite the
// existing GROUP BY clause.
func (stmt SelectStmt) GroupBy(columns ...Columnar) SelectStmt {
// Since the ColumnSet is not unique, any errors can be ignored
stmt.groupBy, _ = stmt.groupBy.Add(columns...)
return stmt
}
// Having adds a conditional clause to the SELECT statement. Only one HAVING
// is allowed per statement. Additional calls to Having will overwrite the
// existing HAVING clause.
func (stmt SelectStmt) Having(conditions ...Clause) SelectStmt {
if len(conditions) > 1 {
// By default, multiple having clauses will be joined using AllOf
stmt.having = AllOf(conditions...)
} else if len(conditions) == 1 {
stmt.having = conditions[0]
} else {
// Clear the existing conditions
stmt.having = nil
}
return stmt
}
// OrderBy adds an ORDER BY to the SELECT statement. Only one ORDER BY
// is allowed per statement. Additional calls to OrderBy will overwrite the
// existing ORDER BY clause.
func (stmt SelectStmt) OrderBy(ords ...Orderable) SelectStmt {
stmt.orderBy = make([]OrderedColumn, len(ords))
// Since columns may be given without an ordering method, perform the
// orderable conversion whether or not it is already ordered
for i, column := range ords {
stmt.orderBy[i] = column.Orderable()
}
return stmt
}
// Limit sets the limit of the SELECT statement.
func (stmt SelectStmt) Limit(limit int) SelectStmt {
// TODO Error (or warning) if limit was already set
stmt.limit = limit
return stmt
}
// Offset sets the offset of the SELECT statement.
func (stmt SelectStmt) Offset(offset int) SelectStmt {
// TODO Error (or warning) if offset was already set
stmt.offset = offset
return stmt
}
// SelectTable creates a SELECT statement from the given table and its
// columns. Any additional selections will not have their table added to
// the SelectStmt's tables field - they must be added manually or through
// a join. To perform selections using cartesian logic, use Select() instead.
func SelectTable(table Tabular, selects ...Selectable) (stmt SelectStmt) {
stmt.tables = []Tabular{table}
// Add the columns from the initial table
stmt.columns = Columns(table.Columns()...)
// Add any additional selections
for _, selection := range selects {
if selection == nil {
stmt.AddMeta("sol: received a nil selectable in SelectTable()")
return
}
for _, column := range selection.Columns() {
if column.IsInvalid() {
stmt.AddMeta(
"sol: cannot select invalid column %s", column.FullName(),
)
return
}
// Since selections do not need to be unique, any errors
// from the ColumnSet can be ignored
stmt.columns, _ = stmt.columns.Add(column)
}
}
return
}
// Select create a SELECT statement from the given columns and tables.
func Select(selections ...Selectable) (stmt SelectStmt) {
columns := []ColumnElem{} // Holds columns until validated
for _, selection := range selections {
if selection == nil {
stmt.AddMeta("sol: received a nil selectable in Select()")
return
}
columns = append(columns, selection.Columns()...)
}
if len(columns) == 0 {
stmt.AddMeta("sol: Select() must be given at least one column")
return
}
for _, column := range columns {
if column.IsInvalid() {
stmt.AddMeta("sol: column %s does not exist", column.FullName())
return
}
// Since selections do not need to be unique, any errors
// from the ColumnSet can be ignored
stmt.columns, _ = stmt.columns.Add(column)
// Add the table to the stmt tables if it does not already exist
if !stmt.hasTable(column.Table().Name()) {
stmt.tables = append(stmt.tables, column.Table())
}
}
return
}