forked from umisama/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
select.go
365 lines (320 loc) · 6.85 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
package sqlbuilder
// SelectStatement represents a SELECT statement.
type SelectStatement struct {
columns selectColumnList
from Table
where Condition
distinct bool
groupBy []serializable
orderBy []serializable
limit int
offset int
having Condition
err error
}
// Select returns new SELECT statement with from as FROM clause.
func Select(from Table) *SelectStatement {
if from == nil {
return &SelectStatement{
err: newError("table is nil."),
}
}
return &SelectStatement{
from: from,
}
}
// Columns set columns for select.
// Get all columns (use *) if it is not setted.
func (b *SelectStatement) Columns(columns ...Column) *SelectStatement {
if b.err != nil {
return b
}
for _, col := range columns {
if !b.from.hasColumn(col) {
b.err = newError("column not found in FROM.")
return b
}
}
b.columns = selectColumnList(columns)
return b
}
// Where sets WHERE clause. The cond is filter condition.
func (b *SelectStatement) Where(cond Condition) *SelectStatement {
if b.err != nil {
return b
}
for _, col := range cond.columns() {
if !b.from.hasColumn(col) {
b.err = newError("column not found in FROM.")
return b
}
}
b.where = cond
return b
}
// Distinct sets DISTINCT clause.
func (b *SelectStatement) Distinct() *SelectStatement {
if b.err != nil {
return b
}
b.distinct = true
return b
}
// GroupBy sets "GROUP BY" clause by the columns.
func (b *SelectStatement) GroupBy(columns ...Column) *SelectStatement {
if b.err != nil {
return b
}
ex_column := make([]serializable, len(columns))
for i := range columns {
ex_column[i] = columns[i]
}
b.groupBy = ex_column
return b
}
// GroupBy sets "HAVING" clause with the cond.
func (b *SelectStatement) Having(cond Condition) *SelectStatement {
if b.err != nil {
return b
}
b.having = cond
return b
}
// OrderBy sets "ORDER BY" clause. Use descending order if the desc is true, by the columns.
func (b *SelectStatement) OrderBy(desc bool, columns ...Column) *SelectStatement {
if b.err != nil {
return b
}
if b.orderBy == nil {
b.orderBy = make([]serializable, 0)
}
for _, c := range columns {
b.orderBy = append(b.orderBy, newOrderBy(desc, c))
}
return b
}
// Limit sets LIMIT clause.
func (b *SelectStatement) Limit(limit int) *SelectStatement {
if b.err != nil {
return b
}
b.limit = limit
return b
}
// Offset sets OFFSET clause.
func (b *SelectStatement) Offset(offset int) *SelectStatement {
if b.err != nil {
return b
}
b.offset = offset
return b
}
func (b *SelectStatement) serialize(bldr *builder) {
if b.err != nil {
bldr.SetError(b.err)
return
}
// SELECT COLUMN
bldr.Append("SELECT ")
if b.distinct {
bldr.Append("DISTINCT ")
}
bldr.AppendItem(b.columns)
// FROM
bldr.Append(" FROM ")
bldr.AppendItem(b.from)
// WHERE
if b.where != nil {
bldr.Append(" WHERE ")
bldr.AppendItem(b.where)
}
// GROUP BY
if b.groupBy != nil {
bldr.Append(" GROUP BY ")
bldr.AppendItems(b.groupBy, ",")
}
// HAVING
if b.having != nil {
if b.groupBy == nil {
bldr.SetError(newError("GROUP BY clause is not found."))
}
bldr.Append(" HAVING ")
bldr.AppendItem(b.having)
}
// ORDER BY
if b.orderBy != nil {
bldr.Append(" ORDER BY ")
bldr.AppendItems(b.orderBy, ", ")
}
// LIMIT
if b.limit != 0 {
bldr.Append(" LIMIT ")
bldr.AppendValue(b.limit)
}
// Offset
if b.offset != 0 {
bldr.Append(" OFFSET ")
bldr.AppendValue(b.offset)
}
return
}
// ToSql generates query string, placeholder arguments, and returns err on errors.
func (b *SelectStatement) ToSql() (query string, args []interface{}, err error) {
bldr := newBuilder()
bldr.AppendItem(b)
return bldr.Query(), bldr.Args(), bldr.Err()
}
func (m *SelectStatement) ToSubquery(alias string) Table {
return newSubquery(m, alias)
}
type subquery struct {
stat *SelectStatement
alias string
err error
}
func newSubquery(s *SelectStatement, alias string) *subquery {
m := &subquery{
stat: s,
alias: alias,
}
if len(alias) == 0 {
m.err = newError("alias is empty.")
}
return m
}
func (m *subquery) serialize(bldr *builder) {
if m.err != nil {
bldr.SetError(m.err)
}
bldr.Append("( ")
bldr.AppendItem(m.stat)
bldr.Append(" ) AS " + m.alias)
return
}
func (m *subquery) Name() string {
return m.alias
}
func (m *subquery) C(name string) Column {
for _, col := range m.stat.columns {
if ac, ok := col.(aliasedColumn); ok {
if ac.column_alias() == name {
return col.config().toColumn(m)
}
}
if col.column_name() == name {
return col.config().toColumn(m)
}
}
return newErrorColumn(newError("column %s was not found.", name))
}
func (m *subquery) Columns() []Column {
l := make([]Column, len(m.stat.columns))
for _, col := range m.stat.columns {
if _, ok := col.(aliasedColumn); ok {
l = append(l, col.config().toColumn(m))
}
l = append(l, col.config().toColumn(m))
}
return nil
}
func (m *subquery) Option() *TableOption {
return nil
}
func (m *subquery) InnerJoin(Table, Condition) Table {
m.err = newError("subquery can not join.")
return m
}
func (m *subquery) LeftOuterJoin(Table, Condition) Table {
m.err = newError("subquery can not join.")
return m
}
func (m *subquery) RightOuterJoin(Table, Condition) Table {
m.err = newError("subquery can not join.")
return m
}
func (m *subquery) FullOuterJoin(Table, Condition) Table {
m.err = newError("subquery can not join.")
return m
}
func (m *subquery) hasColumn(trg Column) bool {
if cimpl, ok := trg.(*columnImpl); ok {
if trg == Star {
return true
}
if cimpl.table != m {
return false
}
for _, col := range m.stat.columns {
if col.column_name() == trg.column_name() {
return true
}
}
return false
}
if acol, ok := trg.(*aliasColumn); ok {
if acol.column.(*columnImpl).table != m {
return false
}
for _, col := range m.stat.columns {
if col.column_name() == trg.column_name() {
return true
}
}
return false
}
if sqlfn, ok := trg.(*sqlFuncImpl); ok {
for _, fncol := range sqlfn.columns() {
find := false
for _, col := range m.stat.columns {
if col.column_name() == fncol.column_name() {
find = true
}
}
if !find {
return false
}
}
return true
}
return false
}
type selectColumnList []Column
func (l selectColumnList) serialize(bldr *builder) {
if len(l) == 0 {
bldr.AppendItem(Star)
return
}
first := true
for _, col := range l {
if first {
first = false
} else {
bldr.Append(", ")
}
if ac, ok := col.(aliasedColumn); ok {
bldr.AppendItem(ac.source())
bldr.Append(" AS ")
bldr.AppendItem(col)
} else {
bldr.AppendItem(col)
}
}
}
type orderBy struct {
column Column
desc bool
}
func newOrderBy(desc bool, column Column) *orderBy {
return &orderBy{
column: column,
desc: desc,
}
}
func (m *orderBy) serialize(bldr *builder) {
bldr.AppendItem(m.column)
if m.desc {
bldr.Append(" DESC")
} else {
bldr.Append(" ASC")
}
}