Support for literals in INSERT and UPDATE and comparisons

The 'value' interface represents a CQL value for use in a comparison,
update, or intitialization operation. A consistent interface for this
allows us to easily support specifying default-named, custom-named,
literal, and evaluated-function values in all these contexts.

Parameters to Func should probably also be values to support full
composition, but that would be a breaking change because Func's
properties are exposed.

The value interface could itself be exposed if we wanted to allow
clients to pass their own values to SetValue, etc, but for now it is a
package-internal abstraction.

BLA
This commit is contained in:
Josh Giles
2017-10-29 13:18:49 -04:00
committed by Michał Matczuk
parent 9fa5432a65
commit 12d360a0c3
8 changed files with 286 additions and 63 deletions

114
qb/cmp.go
View File

@@ -4,9 +4,6 @@
package qb
// Functions reference:
// http://cassandra.apache.org/doc/latest/cql/functions.html
import (
"bytes"
)
@@ -28,8 +25,7 @@ const (
type Cmp struct {
op op
column string
name string
fn *Func
value value
}
func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
@@ -52,19 +48,7 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
case cnt:
cql.WriteString(" CONTAINS ")
}
if c.fn != nil {
names = append(names, c.fn.writeCql(cql)...)
} else {
cql.WriteByte('?')
if c.name == "" {
names = append(names, c.column)
} else {
names = append(names, c.name)
}
}
return
return c.value.writeCql(cql)
}
// Eq produces column=?.
@@ -72,6 +56,7 @@ func Eq(column string) Cmp {
return Cmp{
op: eq,
column: column,
value: param(column),
}
}
@@ -80,7 +65,16 @@ func EqNamed(column, name string) Cmp {
return Cmp{
op: eq,
column: column,
name: name,
value: param(name),
}
}
// EqLit produces column=literal and does not add a parameter to the query.
func EqLit(column, literal string) Cmp {
return Cmp{
op: eq,
column: column,
value: lit(literal),
}
}
@@ -89,7 +83,7 @@ func EqFunc(column string, fn *Func) Cmp {
return Cmp{
op: eq,
column: column,
fn: fn,
value: fn,
}
}
@@ -98,6 +92,7 @@ func Lt(column string) Cmp {
return Cmp{
op: lt,
column: column,
value: param(column),
}
}
@@ -106,7 +101,16 @@ func LtNamed(column, name string) Cmp {
return Cmp{
op: lt,
column: column,
name: name,
value: param(name),
}
}
// LtLit produces column<literal and does not add a parameter to the query.
func LtLit(column, literal string) Cmp {
return Cmp{
op: lt,
column: column,
value: lit(literal),
}
}
@@ -115,7 +119,7 @@ func LtFunc(column string, fn *Func) Cmp {
return Cmp{
op: lt,
column: column,
fn: fn,
value: fn,
}
}
@@ -124,6 +128,7 @@ func LtOrEq(column string) Cmp {
return Cmp{
op: leq,
column: column,
value: param(column),
}
}
@@ -132,7 +137,16 @@ func LtOrEqNamed(column, name string) Cmp {
return Cmp{
op: leq,
column: column,
name: name,
value: param(name),
}
}
// LtOrEqLit produces column<=literal and does not add a parameter to the query.
func LtOrEqLit(column, literal string) Cmp {
return Cmp{
op: leq,
column: column,
value: lit(literal),
}
}
@@ -141,7 +155,7 @@ func LtOrEqFunc(column string, fn *Func) Cmp {
return Cmp{
op: leq,
column: column,
fn: fn,
value: fn,
}
}
@@ -150,6 +164,7 @@ func Gt(column string) Cmp {
return Cmp{
op: gt,
column: column,
value: param(column),
}
}
@@ -158,7 +173,16 @@ func GtNamed(column, name string) Cmp {
return Cmp{
op: gt,
column: column,
name: name,
value: param(name),
}
}
// GtLit produces column>literal and does not add a parameter to the query.
func GtLit(column, literal string) Cmp {
return Cmp{
op: gt,
column: column,
value: lit(literal),
}
}
@@ -167,7 +191,7 @@ func GtFunc(column string, fn *Func) Cmp {
return Cmp{
op: gt,
column: column,
fn: fn,
value: fn,
}
}
@@ -176,6 +200,7 @@ func GtOrEq(column string) Cmp {
return Cmp{
op: geq,
column: column,
value: param(column),
}
}
@@ -184,7 +209,16 @@ func GtOrEqNamed(column, name string) Cmp {
return Cmp{
op: geq,
column: column,
name: name,
value: param(name),
}
}
// GtOrEqLit produces column>=literal and does not add a parameter to the query.
func GtOrEqLit(column, literal string) Cmp {
return Cmp{
op: geq,
column: column,
value: lit(literal),
}
}
@@ -193,7 +227,7 @@ func GtOrEqFunc(column string, fn *Func) Cmp {
return Cmp{
op: geq,
column: column,
fn: fn,
value: fn,
}
}
@@ -202,6 +236,7 @@ func In(column string) Cmp {
return Cmp{
op: in,
column: column,
value: param(column),
}
}
@@ -210,7 +245,16 @@ func InNamed(column, name string) Cmp {
return Cmp{
op: in,
column: column,
name: name,
value: param(name),
}
}
// InLit produces column IN literal and does not add a parameter to the query.
func InLit(column, literal string) Cmp {
return Cmp{
op: in,
column: column,
value: lit(literal),
}
}
@@ -219,6 +263,7 @@ func Contains(column string) Cmp {
return Cmp{
op: cnt,
column: column,
value: param(column),
}
}
@@ -227,7 +272,16 @@ func ContainsNamed(column, name string) Cmp {
return Cmp{
op: cnt,
column: column,
name: name,
value: param(name),
}
}
// ContainsLit produces column CONTAINS literal and does not add a parameter to the query.
func ContainsLit(column, literal string) Cmp {
return Cmp{
op: cnt,
column: column,
value: lit(literal),
}
}