forked from mailru/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
302 lines (261 loc) · 6.47 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
package dbr
// SelectStmt builds `SELECT ...`
type SelectStmt interface {
Builder
From(table interface{}) SelectStmt
Distinct() SelectStmt
Prewhere(query interface{}, value ...interface{}) SelectStmt
Where(query interface{}, value ...interface{}) SelectStmt
Having(query interface{}, value ...interface{}) SelectStmt
GroupBy(col ...string) SelectStmt
OrderAsc(col string) SelectStmt
OrderDesc(col string) SelectStmt
Limit(n uint64) SelectStmt
Offset(n uint64) SelectStmt
ForUpdate() SelectStmt
Join(table, on interface{}) SelectStmt
LeftJoin(table, on interface{}) SelectStmt
RightJoin(table, on interface{}) SelectStmt
FullJoin(table, on interface{}) SelectStmt
As(alias string) Builder
}
type selectStmt struct {
raw
IsDistinct bool
Column []interface{}
Table interface{}
JoinTable []Builder
PrewhereCond []Builder
WhereCond []Builder
Group []Builder
HavingCond []Builder
Order []Builder
LimitCount int64
OffsetCount int64
IsForUpdate bool
}
// Build builds `SELECT ...` in dialect
func (b *selectStmt) Build(d Dialect, buf Buffer) error {
if b.raw.Query != "" {
return b.raw.Build(d, buf)
}
if len(b.Column) == 0 {
return ErrColumnNotSpecified
}
buf.WriteString("SELECT ")
if b.IsDistinct {
buf.WriteString("DISTINCT ")
}
for i, col := range b.Column {
if i > 0 {
buf.WriteString(", ")
}
switch col := col.(type) {
case string:
buf.WriteString(col)
default:
buf.WriteString(placeholder)
buf.WriteValue(col)
}
}
if b.Table != nil {
buf.WriteString(" FROM ")
switch table := b.Table.(type) {
case string:
buf.WriteString(table)
default:
buf.WriteString(placeholder)
buf.WriteValue(table)
}
if len(b.JoinTable) > 0 {
for _, join := range b.JoinTable {
err := join.Build(d, buf)
if err != nil {
return err
}
}
}
}
if len(b.PrewhereCond) > 0 {
keyword := d.Prewhere()
if len(keyword) == 0 {
return ErrPrewhereNotSupported
}
buf.WriteString(" ")
buf.WriteString(keyword)
buf.WriteString(" ")
err := And(b.PrewhereCond...).Build(d, buf)
if err != nil {
return err
}
}
if len(b.WhereCond) > 0 {
buf.WriteString(" WHERE ")
err := And(b.WhereCond...).Build(d, buf)
if err != nil {
return err
}
}
if len(b.Group) > 0 {
buf.WriteString(" GROUP BY ")
for i, group := range b.Group {
if i > 0 {
buf.WriteString(", ")
}
err := group.Build(d, buf)
if err != nil {
return err
}
}
}
if len(b.HavingCond) > 0 {
buf.WriteString(" HAVING ")
err := And(b.HavingCond...).Build(d, buf)
if err != nil {
return err
}
}
if len(b.Order) > 0 {
buf.WriteString(" ORDER BY ")
for i, order := range b.Order {
if i > 0 {
buf.WriteString(", ")
}
err := order.Build(d, buf)
if err != nil {
return err
}
}
}
if b.LimitCount >= 0 {
buf.WriteString(" ")
buf.WriteString(d.Limit(b.OffsetCount, b.LimitCount))
}
if b.IsForUpdate {
buf.WriteString(" FOR UPDATE")
}
return nil
}
// Select creates a SelectStmt
func Select(column ...interface{}) SelectStmt {
return createSelectStmt(column)
}
func createSelectStmt(column []interface{}) *selectStmt {
return &selectStmt{
Column: column,
LimitCount: -1,
OffsetCount: -1,
}
}
// From specifies table
func (b *selectStmt) From(table interface{}) SelectStmt {
b.Table = table
return b
}
// SelectBySql creates a SelectStmt from raw query
func SelectBySql(query string, value ...interface{}) SelectStmt {
return createSelectStmtBySQL(query, value)
}
func createSelectStmtBySQL(query string, value []interface{}) *selectStmt {
return &selectStmt{
raw: raw{
Query: query,
Value: value,
},
LimitCount: -1,
OffsetCount: -1,
}
}
// Distinct adds `DISTINCT`
func (b *selectStmt) Distinct() SelectStmt {
b.IsDistinct = true
return b
}
// Prewhere adds a prewhere condition
// For example clickhouse PREWHERE:
// https://clickhouse.yandex/docs/en/query_language/select/#prewhere-clause
func (b *selectStmt) Prewhere(query interface{}, value ...interface{}) SelectStmt {
switch query := query.(type) {
case string:
b.PrewhereCond = append(b.PrewhereCond, Expr(query, value...))
case Builder:
b.PrewhereCond = append(b.PrewhereCond, query)
}
return b
}
// Where adds a where condition
func (b *selectStmt) Where(query interface{}, value ...interface{}) SelectStmt {
switch query := query.(type) {
case string:
b.WhereCond = append(b.WhereCond, Expr(query, value...))
case Builder:
b.WhereCond = append(b.WhereCond, query)
}
return b
}
// Having adds a having condition
func (b *selectStmt) Having(query interface{}, value ...interface{}) SelectStmt {
switch query := query.(type) {
case string:
b.HavingCond = append(b.HavingCond, Expr(query, value...))
case Builder:
b.HavingCond = append(b.HavingCond, query)
}
return b
}
// GroupBy specifies columns for grouping
func (b *selectStmt) GroupBy(col ...string) SelectStmt {
for _, group := range col {
b.Group = append(b.Group, Expr(group))
}
return b
}
// OrderAsc specifies columns for ordering in asc direction
func (b *selectStmt) OrderAsc(col string) SelectStmt {
b.Order = append(b.Order, order(col, asc))
return b
}
// OrderDesc specifies columns for ordering in desc direction
func (b *selectStmt) OrderDesc(col string) SelectStmt {
b.Order = append(b.Order, order(col, desc))
return b
}
// Limit adds LIMIT
func (b *selectStmt) Limit(n uint64) SelectStmt {
b.LimitCount = int64(n)
return b
}
// Offset adds OFFSET, works only if LIMIT is set
func (b *selectStmt) Offset(n uint64) SelectStmt {
b.OffsetCount = int64(n)
return b
}
// ForUpdate adds `FOR UPDATE`
func (b *selectStmt) ForUpdate() SelectStmt {
b.IsForUpdate = true
return b
}
// Join joins table on condition
func (b *selectStmt) Join(table, on interface{}) SelectStmt {
b.JoinTable = append(b.JoinTable, join(inner, table, on))
return b
}
// LeftJoin joins table on condition via LEFT JOIN
func (b *selectStmt) LeftJoin(table, on interface{}) SelectStmt {
b.JoinTable = append(b.JoinTable, join(left, table, on))
return b
}
// RightJoin joins table on condition via RIGHT JOIN
func (b *selectStmt) RightJoin(table, on interface{}) SelectStmt {
b.JoinTable = append(b.JoinTable, join(right, table, on))
return b
}
// FullJoin joins table on condition via FULL JOIN
func (b *selectStmt) FullJoin(table, on interface{}) SelectStmt {
b.JoinTable = append(b.JoinTable, join(full, table, on))
return b
}
// As creates alias for select statement
func (b *selectStmt) As(alias string) Builder {
return as(b, alias)
}