-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathselect.go
283 lines (237 loc) · 6.65 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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
package qb
// SELECT reference:
// https://cassandra.apache.org/doc/latest/cql/dml.html#select
import (
"bytes"
"context"
"time"
"github.com/scylladb/gocqlx/v3"
)
// Order specifies sorting order.
type Order bool
const (
// ASC is ascending order
ASC Order = true
// DESC is descending order
DESC Order = false
)
func (o Order) String() string {
if o {
return "ASC"
}
return "DESC"
}
// SelectBuilder builds CQL SELECT statements.
type SelectBuilder struct {
limit limit
limitPerPartition limit
table string
where where
groupBy columns
orderBy columns
columns columns
distinct columns
using using
allowFiltering bool
bypassCache bool
json bool
}
// Select returns a new SelectBuilder with the given table name.
func Select(table string) *SelectBuilder {
return &SelectBuilder{
table: table,
}
}
// ToCql builds the query into a CQL string and named args.
func (b *SelectBuilder) ToCql() (stmt string, names []string) {
cql := bytes.Buffer{}
cql.WriteString("SELECT ")
if b.json {
cql.WriteString("JSON ")
}
switch {
case len(b.distinct) > 0:
cql.WriteString("DISTINCT ")
b.distinct.writeCql(&cql)
case len(b.groupBy) > 0:
b.groupBy.writeCql(&cql)
if len(b.columns) != 0 {
cql.WriteByte(',')
b.columns.writeCql(&cql)
}
case len(b.columns) == 0:
cql.WriteByte('*')
default:
b.columns.writeCql(&cql)
}
cql.WriteString(" FROM ")
cql.WriteString(b.table)
cql.WriteByte(' ')
names = append(names, b.where.writeCql(&cql)...)
if len(b.groupBy) > 0 {
cql.WriteString("GROUP BY ")
b.groupBy.writeCql(&cql)
cql.WriteByte(' ')
}
if len(b.orderBy) > 0 {
cql.WriteString("ORDER BY ")
b.orderBy.writeCql(&cql)
cql.WriteByte(' ')
}
names = append(names, b.limitPerPartition.writeCql(&cql)...)
names = append(names, b.limit.writeCql(&cql)...)
if b.allowFiltering {
cql.WriteString("ALLOW FILTERING ")
}
if b.bypassCache {
cql.WriteString("BYPASS CACHE ")
}
names = append(names, b.using.writeCql(&cql)...)
stmt = cql.String()
return
}
// Query returns query built on top of current SelectBuilder state.
func (b *SelectBuilder) Query(session gocqlx.Session) *gocqlx.Queryx {
return session.Query(b.ToCql())
}
// QueryContext returns query wrapped with context built on top of current SelectBuilder state.
func (b *SelectBuilder) QueryContext(ctx context.Context, session gocqlx.Session) *gocqlx.Queryx {
return b.Query(session).WithContext(ctx)
}
// From sets the table to be selected from.
func (b *SelectBuilder) From(table string) *SelectBuilder {
b.table = table
return b
}
// Json sets the clause of the query.
func (b *SelectBuilder) Json() *SelectBuilder { // nolint: revive
b.json = true
return b
}
// Columns adds result columns to the query.
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
if len(b.columns) == 0 {
b.columns = columns
} else {
b.columns = append(b.columns, columns...)
}
return b
}
// As is a helper for adding a column AS name result column to the query.
func As(column, name string) string {
return column + " AS " + name
}
// Distinct sets DISTINCT clause on the query.
func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder {
if len(b.where) == 0 {
b.distinct = columns
} else {
b.distinct = append(b.distinct, columns...)
}
return b
}
// Timeout adds USING TIMEOUT clause to the query.
func (b *SelectBuilder) Timeout(d time.Duration) *SelectBuilder {
b.using.Timeout(d)
return b
}
// TimeoutNamed adds a USING TIMEOUT clause to the query with a custom
// parameter name.
func (b *SelectBuilder) TimeoutNamed(name string) *SelectBuilder {
b.using.TimeoutNamed(name)
return b
}
// Where adds an expression to the WHERE clause of the query. Expressions are
// ANDed together in the generated CQL.
func (b *SelectBuilder) Where(w ...Cmp) *SelectBuilder {
if len(b.where) == 0 {
b.where = w
} else {
b.where = append(b.where, w...)
}
return b
}
// GroupBy sets GROUP BY clause on the query. Columns must be a primary key,
// this will automatically add the the columns as first selectors.
func (b *SelectBuilder) GroupBy(columns ...string) *SelectBuilder {
if len(b.groupBy) == 0 {
b.groupBy = columns
} else {
b.groupBy = append(b.groupBy, columns...)
}
return b
}
// OrderBy sets ORDER BY clause on the query.
func (b *SelectBuilder) OrderBy(column string, o Order) *SelectBuilder {
b.orderBy = append(b.orderBy, column+" "+o.String())
return b
}
// Limit sets a LIMIT clause on the query.
func (b *SelectBuilder) Limit(limit uint) *SelectBuilder {
b.limit = limitLit(limit, false)
return b
}
// LimitNamed produces LIMIT ? clause with a custom parameter name.
func (b *SelectBuilder) LimitNamed(name string) *SelectBuilder {
b.limit = limitNamed(name, false)
return b
}
// LimitPerPartition sets a PER PARTITION LIMIT clause on the query.
func (b *SelectBuilder) LimitPerPartition(limit uint) *SelectBuilder {
b.limitPerPartition = limitLit(limit, true)
return b
}
// LimitPerPartitionNamed produces PER PARTITION LIMIT ? clause with a custom parameter name.
func (b *SelectBuilder) LimitPerPartitionNamed(name string) *SelectBuilder {
b.limitPerPartition = limitNamed(name, true)
return b
}
// AllowFiltering sets a ALLOW FILTERING clause on the query.
func (b *SelectBuilder) AllowFiltering() *SelectBuilder {
b.allowFiltering = true
return b
}
// BypassCache sets a BYPASS CACHE clause on the query.
//
// BYPASS CACHE is a feature specific to ScyllaDB.
// See https://docs.scylladb.com/getting-started/dml/#bypass-cache
func (b *SelectBuilder) BypassCache() *SelectBuilder {
b.bypassCache = true
return b
}
// Count produces 'count(column)'.
func (b *SelectBuilder) Count(column string) *SelectBuilder {
b.fn("count", column)
return b
}
// CountAll produces 'count(*)'.
func (b *SelectBuilder) CountAll() *SelectBuilder {
b.Count("*")
return b
}
// Min produces 'min(column)' aggregation function.
func (b *SelectBuilder) Min(column string) *SelectBuilder {
b.fn("min", column)
return b
}
// Max produces 'max(column)' aggregation function.
func (b *SelectBuilder) Max(column string) *SelectBuilder {
b.fn("max", column)
return b
}
// Avg produces 'avg(column)' aggregation function.
func (b *SelectBuilder) Avg(column string) *SelectBuilder {
b.fn("avg", column)
return b
}
// Sum produces 'sum(column)' aggregation function.
func (b *SelectBuilder) Sum(column string) *SelectBuilder {
b.fn("sum", column)
return b
}
func (b *SelectBuilder) fn(name, column string) {
b.Columns(name + "(" + column + ")")
}