Files
gocqlx/qb/select.go

237 lines
5.2 KiB
Go
Raw Normal View History

2017-09-21 21:43:27 +02:00
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.
2017-07-27 12:28:01 +02:00
package qb
// SELECT reference:
2017-07-28 10:18:38 +02:00
// https://cassandra.apache.org/doc/latest/cql/dml.html#select
2017-07-27 12:28:01 +02:00
import (
"bytes"
"fmt"
)
2017-07-28 10:18:38 +02:00
// Order specifies sorting order.
2017-07-27 12:28:01 +02:00
type Order bool
const (
2017-07-28 10:18:38 +02:00
// ASC is ascending order
ASC Order = true
// DESC is descending order
DESC = false
2017-07-27 12:28:01 +02:00
)
func (o Order) String() string {
if o {
return "ASC"
}
return "DESC"
}
2017-07-28 10:18:38 +02:00
// SelectBuilder builds CQL SELECT statements.
2017-07-27 12:28:01 +02:00
type SelectBuilder struct {
table string
columns columns
distinct columns
where where
groupBy columns
orderBy columns
2017-07-27 12:28:01 +02:00
limit uint
limitPerPartition uint
allowFiltering bool
bypassCache bool
2019-05-13 14:01:13 +02:00
json bool
2017-07-27 12:28:01 +02:00
}
// Select returns a new SelectBuilder with the given table name.
func Select(table string) *SelectBuilder {
return &SelectBuilder{
table: table,
}
}
2017-07-28 10:18:38 +02:00
// ToCql builds the query into a CQL string and named args.
func (b *SelectBuilder) ToCql() (stmt string, names []string) {
2017-07-27 12:28:01 +02:00
cql := bytes.Buffer{}
cql.WriteString("SELECT ")
2019-05-13 14:01:13 +02:00
if b.json {
cql.WriteString("JSON ")
}
2017-07-27 12:28:01 +02:00
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)
}
2017-07-27 12:28:01 +02:00
case len(b.columns) == 0:
cql.WriteByte('*')
default:
b.columns.writeCql(&cql)
}
cql.WriteString(" FROM ")
cql.WriteString(b.table)
cql.WriteByte(' ')
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 {
2017-07-27 12:28:01 +02:00
cql.WriteString("ORDER BY ")
b.orderBy.writeCql(&cql)
cql.WriteByte(' ')
2017-07-27 12:28:01 +02:00
}
if b.limit != 0 {
cql.WriteString("LIMIT ")
cql.WriteString(fmt.Sprint(b.limit))
cql.WriteByte(' ')
}
if b.limitPerPartition != 0 {
cql.WriteString("PER PARTITION LIMIT ")
cql.WriteString(fmt.Sprint(b.limitPerPartition))
cql.WriteByte(' ')
}
if b.allowFiltering {
cql.WriteString("ALLOW FILTERING ")
}
if b.bypassCache {
cql.WriteString("BYPASS CACHE ")
}
2017-07-27 12:28:01 +02:00
stmt = cql.String()
return
}
// From sets the table to be selected from.
func (b *SelectBuilder) From(table string) *SelectBuilder {
b.table = table
return b
}
2019-05-13 14:01:13 +02:00
// Json sets the clause of the query.
func (b *SelectBuilder) Json() *SelectBuilder {
b.json = true
return b
}
2017-07-28 10:18:38 +02:00
// Columns adds result columns to the query.
2017-07-27 12:28:01 +02:00
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
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
}
2017-07-28 10:18:38 +02:00
// Distinct sets DISTINCT clause on the query.
func (b *SelectBuilder) Distinct(columns ...string) *SelectBuilder {
2017-07-27 12:28:01 +02:00
b.distinct = append(b.distinct, columns...)
return b
}
2017-07-28 10:18:38 +02:00
// Where adds an expression to the WHERE clause of the query. Expressions are
// ANDed together in the generated CQL.
2017-07-27 13:39:27 +02:00
func (b *SelectBuilder) Where(w ...Cmp) *SelectBuilder {
b.where = append(b.where, w...)
2017-07-27 12:28:01 +02:00
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.
2017-07-28 10:18:38 +02:00
func (b *SelectBuilder) GroupBy(columns ...string) *SelectBuilder {
2017-07-27 12:28:01 +02:00
b.groupBy = append(b.groupBy, columns...)
return b
}
2017-07-28 10:18:38 +02:00
// OrderBy sets ORDER BY clause on the query.
2017-07-27 12:28:01 +02:00
func (b *SelectBuilder) OrderBy(column string, o Order) *SelectBuilder {
b.orderBy = append(b.orderBy, column+" "+o.String())
2017-07-27 12:28:01 +02:00
return b
}
2017-07-28 10:18:38 +02:00
// Limit sets a LIMIT clause on the query.
2017-07-27 12:28:01 +02:00
func (b *SelectBuilder) Limit(limit uint) *SelectBuilder {
b.limit = limit
return b
}
2017-07-28 10:18:38 +02:00
// LimitPerPartition sets a PER PARTITION LIMIT clause on the query.
2017-07-27 12:28:01 +02:00
func (b *SelectBuilder) LimitPerPartition(limit uint) *SelectBuilder {
b.limitPerPartition = limit
return b
}
2017-07-28 10:18:38 +02:00
// AllowFiltering sets a ALLOW FILTERING clause on the query.
2017-07-27 12:28:01 +02:00
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) *SelectBuilder {
b.Columns(name + "(" + column + ")")
return b
}