Files
gocqlx/qb/cmp.go

188 lines
3.0 KiB
Go
Raw Normal View History

2017-07-27 13:39:27 +02:00
package qb
import "bytes"
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
name string
}
func (cmp Cmp) writeCql(cql *bytes.Buffer) string {
cql.WriteString(cmp.column)
switch cmp.op {
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 ")
}
cql.WriteByte('?')
return cmp.name
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
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,
name: 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,
name: name,
}
}
type cmps []Cmp
func (cs cmps) writeCql(cql *bytes.Buffer) (names []string) {
for i, c := range cs {
names = append(names, c.writeCql(cql))
if i < len(cs)-1 {
cql.WriteString(" AND ")
}
}
cql.WriteByte(' ')
return
}