Files
gocqlx/qb/cmp.go

242 lines
4.3 KiB
Go
Raw Normal View History

2017-07-27 13:39:27 +02:00
package qb
2017-08-30 10:04:33 +02:00
// Functions reference:
// http://cassandra.apache.org/doc/latest/cql/functions.html
import (
"bytes"
)
2017-07-27 13:39:27 +02:00
2017-07-28 10:18:38 +02:00
// op specifies Cmd operation type.
2017-07-27 13:39:27 +02:00
type op byte
const (
eq op = iota
lt
leq
gt
geq
in
cnt
)
2017-07-28 10:18:38 +02:00
// Cmp if a filtering comparator that is used in WHERE and IF clauses.
2017-07-27 13:39:27 +02:00
type Cmp struct {
op op
column string
2017-08-30 10:04:33 +02:00
fn string
names []string
}
// Func wraps comparator value with a custom function, fn is a function name,
// names are function arguments' bind names. For instance function:
//
// CREATE FUNCTION somefunction(somearg int, anotherarg text)
//
// can be used like this:
//
// stmt, names := qb.Select("table").
// Where(qb.Eq("t").Func("somefunction", "somearg", "anotherarg")).
// ToCql()
//
// q := gocqlx.Query(session.Query(stmt), names).BindMap(qb.M{
// "somearg": 1,
// "anotherarg": "text",
// })
func (c Cmp) Func(fn string, names ...string) Cmp {
c.fn = fn
c.names = names
return c
}
// MinTimeuuid sets minTimeuuid(?) compare value.
func (c Cmp) MinTimeuuid(name string) Cmp {
return c.Func("minTimeuuid", name)
}
// MaxTimeuuid sets maxTimeuuid(?) compare value.
func (c Cmp) MaxTimeuuid(name string) Cmp {
return c.Func("maxTimeuuid", name)
2017-07-27 13:39:27 +02:00
}
2017-08-30 10:04:33 +02:00
// Now sets now() compare value.
func (c Cmp) Now() Cmp {
return c.Func("now")
}
// Token sets Token(?,?...) compare value.
func (c Cmp) Token(names ...string) Cmp {
return c.Func("token", names...)
}
func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
cql.WriteString(c.column)
switch c.op {
2017-07-27 13:39:27 +02:00
case eq:
cql.WriteByte('=')
case lt:
cql.WriteByte('<')
case leq:
cql.WriteByte('<')
cql.WriteByte('=')
case gt:
cql.WriteByte('>')
case geq:
cql.WriteByte('>')
cql.WriteByte('=')
case in:
cql.WriteString(" IN ")
case cnt:
cql.WriteString(" CONTAINS ")
}
2017-08-30 10:04:33 +02:00
if c.fn == "" {
cql.WriteByte('?')
if c.names == nil {
names = append(names, c.column)
} else {
names = append(names, c.names...)
}
} else {
cql.WriteString(c.fn)
cql.WriteByte('(')
placeholders(cql, len(c.names))
cql.WriteByte(')')
names = append(names, c.names...)
}
return
2017-07-27 13:39:27 +02:00
}
2017-07-28 10:18:38 +02:00
// Eq produces column=?.
2017-07-27 13:39:27 +02:00
func Eq(column string) Cmp {
return Cmp{
op: eq,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// EqNamed produces column=? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func EqNamed(column, name string) Cmp {
return Cmp{
op: eq,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// Lt produces column<?.
2017-07-27 13:39:27 +02:00
func Lt(column string) Cmp {
return Cmp{
op: lt,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// LtNamed produces column<? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func LtNamed(column, name string) Cmp {
return Cmp{
op: lt,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// LtOrEq produces column<=?.
2017-07-27 13:39:27 +02:00
func LtOrEq(column string) Cmp {
return Cmp{
op: leq,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// LtOrEqNamed produces column<=? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func LtOrEqNamed(column, name string) Cmp {
return Cmp{
op: leq,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// Gt produces column>?.
2017-07-27 13:39:27 +02:00
func Gt(column string) Cmp {
return Cmp{
op: gt,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// GtNamed produces column>? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func GtNamed(column, name string) Cmp {
return Cmp{
op: gt,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// GtOrEq produces column>=?.
2017-07-27 13:39:27 +02:00
func GtOrEq(column string) Cmp {
return Cmp{
op: geq,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// GtOrEqNamed produces column>=? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func GtOrEqNamed(column, name string) Cmp {
return Cmp{
op: geq,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// In produces column IN ?.
2017-07-27 13:39:27 +02:00
func In(column string) Cmp {
return Cmp{
op: in,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// InNamed produces column IN ? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func InNamed(column, name string) Cmp {
return Cmp{
op: in,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
2017-07-28 10:18:38 +02:00
// Contains produces column CONTAINS ?.
2017-07-27 13:39:27 +02:00
func Contains(column string) Cmp {
return Cmp{
op: cnt,
column: column,
}
}
2017-07-28 10:18:38 +02:00
// ContainsNamed produces column CONTAINS ? with a custom parameter name.
2017-07-27 13:39:27 +02:00
func ContainsNamed(column, name string) Cmp {
return Cmp{
op: cnt,
column: column,
2017-08-30 10:04:33 +02:00
names: []string{name},
2017-07-27 13:39:27 +02:00
}
}
type cmps []Cmp
func (cs cmps) writeCql(cql *bytes.Buffer) (names []string) {
for i, c := range cs {
2017-08-30 10:04:33 +02:00
names = append(names, c.writeCql(cql)...)
2017-07-27 13:39:27 +02:00
if i < len(cs)-1 {
cql.WriteString(" AND ")
}
}
cql.WriteByte(' ')
return
}