33
README.md
33
README.md
@@ -15,6 +15,7 @@ hood it uses `sqlx/reflectx` package so `sqlx` models will also work with `gocql
|
|||||||
|
|
||||||
* Builders for `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH`
|
* Builders for `SELECT`, `INSERT`, `UPDATE` `DELETE` and `BATCH`
|
||||||
* Queries with named parameters (:identifier) support
|
* Queries with named parameters (:identifier) support
|
||||||
|
* Functions support
|
||||||
* Binding parameters form struct or map
|
* Binding parameters form struct or map
|
||||||
* Scanning results into structs and slices
|
* Scanning results into structs and slices
|
||||||
* Automatic query releasing
|
* Automatic query releasing
|
||||||
@@ -85,38 +86,6 @@ type Person struct {
|
|||||||
t.Log(people)
|
t.Log(people)
|
||||||
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Ian Citizen [ian.citzen@gocqlx_test.com]}]
|
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Ian Citizen [ian.citzen@gocqlx_test.com]}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch insert two rows in a single query, advanced struct binding.
|
|
||||||
{
|
|
||||||
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
|
||||||
|
|
||||||
stmt, names := qb.Batch().
|
|
||||||
AddWithPrefix("a", i).
|
|
||||||
AddWithPrefix("b", i).
|
|
||||||
ToCql()
|
|
||||||
|
|
||||||
batch := struct {
|
|
||||||
A Person
|
|
||||||
B Person
|
|
||||||
}{
|
|
||||||
A: Person{
|
|
||||||
"Igy",
|
|
||||||
"Citizen",
|
|
||||||
[]string{"igy.citzen@gocqlx_test.com"},
|
|
||||||
},
|
|
||||||
B: Person{
|
|
||||||
"Ian",
|
|
||||||
"Citizen",
|
|
||||||
[]string{"ian.citzen@gocqlx_test.com"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
q := gocqlx.Query(session.Query(stmt), names).BindStruct(&batch)
|
|
||||||
|
|
||||||
if err := q.ExecRelease(); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
See more examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
|
See more examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
|
||||||
|
|||||||
@@ -89,6 +89,22 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Advanced update, adding and removing elements to collections and counters.
|
||||||
|
{
|
||||||
|
stmt, names := qb.Update("gocqlx_test.person").
|
||||||
|
AddNamed("email", "new_email").
|
||||||
|
Where(qb.Eq("first_name"), qb.Eq("last_name")).
|
||||||
|
ToCql()
|
||||||
|
|
||||||
|
q := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
|
||||||
|
"new_email": []string{"patricia2.citzen@gocqlx_test.com", "patricia3.citzen@gocqlx_test.com"},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := q.ExecRelease(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Batch insert two rows in a single query, advanced struct binding.
|
// Batch insert two rows in a single query, advanced struct binding.
|
||||||
{
|
{
|
||||||
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
|
||||||
@@ -137,7 +153,7 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Log(p)
|
t.Log(p)
|
||||||
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]}
|
// {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com patricia2.citzen@gocqlx_test.com patricia3.citzen@gocqlx_test.com]}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select, load all the results into a slice.
|
// Select, load all the results into a slice.
|
||||||
@@ -156,7 +172,7 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Log(people)
|
t.Log(people)
|
||||||
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Ian Citizen [ian.citzen@gocqlx_test.com]}]
|
// [{Ian Citizen [ian.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]} {Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com patricia2.citzen@gocqlx_test.com patricia3.citzen@gocqlx_test.com]}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Easy token based pagination.
|
// Easy token based pagination.
|
||||||
@@ -168,6 +184,7 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stmt, names := qb.Select("gocqlx_test.person").
|
stmt, names := qb.Select("gocqlx_test.person").
|
||||||
|
Columns("first_name").
|
||||||
Where(qb.Token("first_name").Gt()).
|
Where(qb.Token("first_name").Gt()).
|
||||||
Limit(10).
|
Limit(10).
|
||||||
ToCql()
|
ToCql()
|
||||||
@@ -180,7 +197,7 @@ func TestExample(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Log(people)
|
t.Log(people)
|
||||||
// [{Patricia Citizen [patricia.citzen@gocqlx_test.com patricia1.citzen@gocqlx_test.com]} {Igy Citizen [igy.citzen@gocqlx_test.com]}]
|
// [{Patricia []} {Igy []}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Named query compilation.
|
// Named query compilation.
|
||||||
|
|||||||
118
qb/cmp.go
118
qb/cmp.go
@@ -28,49 +28,8 @@ const (
|
|||||||
type Cmp struct {
|
type Cmp struct {
|
||||||
op op
|
op op
|
||||||
column string
|
column string
|
||||||
fn string
|
name string
|
||||||
names []string
|
fn *Func
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
|
||||||
@@ -94,19 +53,15 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
|
|||||||
cql.WriteString(" CONTAINS ")
|
cql.WriteString(" CONTAINS ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.fn == "" {
|
if c.fn != nil {
|
||||||
|
names = append(names, c.fn.writeCql(cql)...)
|
||||||
|
} else {
|
||||||
cql.WriteByte('?')
|
cql.WriteByte('?')
|
||||||
if c.names == nil {
|
if c.name == "" {
|
||||||
names = append(names, c.column)
|
names = append(names, c.column)
|
||||||
} else {
|
} else {
|
||||||
names = append(names, c.names...)
|
names = append(names, c.name)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
cql.WriteString(c.fn)
|
|
||||||
cql.WriteByte('(')
|
|
||||||
placeholders(cql, len(c.names))
|
|
||||||
cql.WriteByte(')')
|
|
||||||
names = append(names, c.names...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -125,7 +80,16 @@ func EqNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: eq,
|
op: eq,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EqFunc produces column=someFunc(?...).
|
||||||
|
func EqFunc(column string, fn *Func) Cmp {
|
||||||
|
return Cmp{
|
||||||
|
op: eq,
|
||||||
|
column: column,
|
||||||
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +106,16 @@ func LtNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: lt,
|
op: lt,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LtFunc produces column<someFunc(?...).
|
||||||
|
func LtFunc(column string, fn *Func) Cmp {
|
||||||
|
return Cmp{
|
||||||
|
op: lt,
|
||||||
|
column: column,
|
||||||
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +132,16 @@ func LtOrEqNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: leq,
|
op: leq,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LtOrEqFunc produces column<=someFunc(?...).
|
||||||
|
func LtOrEqFunc(column string, fn *Func) Cmp {
|
||||||
|
return Cmp{
|
||||||
|
op: leq,
|
||||||
|
column: column,
|
||||||
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,7 +158,16 @@ func GtNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: gt,
|
op: gt,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GtFunc produces column>someFunc(?...).
|
||||||
|
func GtFunc(column string, fn *Func) Cmp {
|
||||||
|
return Cmp{
|
||||||
|
op: gt,
|
||||||
|
column: column,
|
||||||
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +184,16 @@ func GtOrEqNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: geq,
|
op: geq,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GtOrEqFunc produces column>=someFunc(?...).
|
||||||
|
func GtOrEqFunc(column string, fn *Func) Cmp {
|
||||||
|
return Cmp{
|
||||||
|
op: geq,
|
||||||
|
column: column,
|
||||||
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +210,7 @@ func InNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: in,
|
op: in,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +227,7 @@ func ContainsNamed(column, name string) Cmp {
|
|||||||
return Cmp{
|
return Cmp{
|
||||||
op: cnt,
|
op: cnt,
|
||||||
column: column,
|
column: column,
|
||||||
names: []string{name},
|
name: name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,29 +93,24 @@ func TestCmp(t *testing.T) {
|
|||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
{
|
{
|
||||||
C: Eq("eq").Func("fn", "arg0", "arg1"),
|
C: EqFunc("eq", Fn("fn", "arg0", "arg1")),
|
||||||
S: "eq=fn(?,?)",
|
S: "eq=fn(?,?)",
|
||||||
N: []string{"arg0", "arg1"},
|
N: []string{"arg0", "arg1"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
C: Eq("eq").MaxTimeuuid("arg0"),
|
C: EqFunc("eq", MaxTimeuuid("arg0")),
|
||||||
S: "eq=maxTimeuuid(?)",
|
S: "eq=maxTimeuuid(?)",
|
||||||
N: []string{"arg0"},
|
N: []string{"arg0"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
C: Eq("eq").MinTimeuuid("arg0"),
|
C: EqFunc("eq", MinTimeuuid("arg0")),
|
||||||
S: "eq=minTimeuuid(?)",
|
S: "eq=minTimeuuid(?)",
|
||||||
N: []string{"arg0"},
|
N: []string{"arg0"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
C: Eq("eq").Now(),
|
C: EqFunc("eq", Now()),
|
||||||
S: "eq=now()",
|
S: "eq=now()",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
C: Eq("eq").Token("arg0", "arg1"),
|
|
||||||
S: "eq=token(?,?)",
|
|
||||||
N: []string{"arg0", "arg1"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
buf := bytes.Buffer{}
|
||||||
|
|||||||
51
qb/func.go
Normal file
51
qb/func.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
// Functions reference:
|
||||||
|
// https://cassandra.apache.org/doc/latest/cql/functions.html
|
||||||
|
|
||||||
|
// Func is a custom database function invocation that can be use in a comparator
|
||||||
|
// or update statement.
|
||||||
|
type Func struct {
|
||||||
|
// function name
|
||||||
|
Name string
|
||||||
|
// name of the function parameters
|
||||||
|
ParamNames []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Func) writeCql(cql *bytes.Buffer) (names []string) {
|
||||||
|
cql.WriteString(f.Name)
|
||||||
|
cql.WriteByte('(')
|
||||||
|
placeholders(cql, len(f.ParamNames))
|
||||||
|
cql.WriteByte(')')
|
||||||
|
names = append(names, f.ParamNames...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fn creates Func.
|
||||||
|
func Fn(name string, paramNames ...string) *Func {
|
||||||
|
return &Func{
|
||||||
|
Name: name,
|
||||||
|
ParamNames: paramNames,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MinTimeuuid produces minTimeuuid(?).
|
||||||
|
func MinTimeuuid(name string) *Func {
|
||||||
|
return Fn("minTimeuuid", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxTimeuuid produces maxTimeuuid(?).
|
||||||
|
func MaxTimeuuid(name string) *Func {
|
||||||
|
return Fn("maxTimeuuid", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now produces now().
|
||||||
|
func Now() *Func {
|
||||||
|
return Fn("now")
|
||||||
|
}
|
||||||
82
qb/token.go
82
qb/token.go
@@ -9,110 +9,72 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TokenBuilder helps implement pagination using token function.
|
||||||
|
type TokenBuilder []string
|
||||||
|
|
||||||
// Token creates a new TokenBuilder.
|
// Token creates a new TokenBuilder.
|
||||||
func Token(columns ...string) TokenBuilder {
|
func Token(columns ...string) TokenBuilder {
|
||||||
return TokenBuilder(columns)
|
return TokenBuilder(columns)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TokenBuilder helps implement pagination using token function.
|
|
||||||
type TokenBuilder []string
|
|
||||||
|
|
||||||
// Eq produces token(column)=token(?).
|
// Eq produces token(column)=token(?).
|
||||||
func (t TokenBuilder) Eq() Cmp {
|
func (t TokenBuilder) Eq() Cmp {
|
||||||
return Cmp{
|
return t.cmp(eq, nil)
|
||||||
op: eq,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: t,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EqNamed produces token(column)=token(?) with a custom parameter name.
|
// EqNamed produces token(column)=token(?) with a custom parameter name.
|
||||||
func (t TokenBuilder) EqNamed(names ...string) Cmp {
|
func (t TokenBuilder) EqNamed(names ...string) Cmp {
|
||||||
return Cmp{
|
return t.cmp(eq, names)
|
||||||
op: eq,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: names,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lt produces token(column)<token(?).
|
// Lt produces token(column)<token(?).
|
||||||
func (t TokenBuilder) Lt() Cmp {
|
func (t TokenBuilder) Lt() Cmp {
|
||||||
return Cmp{
|
return t.cmp(lt, nil)
|
||||||
op: lt,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: t,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LtNamed produces token(column)<token(?) with a custom parameter name.
|
// LtNamed produces token(column)<token(?) with a custom parameter name.
|
||||||
func (t TokenBuilder) LtNamed(names ...string) Cmp {
|
func (t TokenBuilder) LtNamed(names ...string) Cmp {
|
||||||
return Cmp{
|
return t.cmp(lt, names)
|
||||||
op: lt,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: names,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LtOrEq produces token(column)<=token(?).
|
// LtOrEq produces token(column)<=token(?).
|
||||||
func (t TokenBuilder) LtOrEq() Cmp {
|
func (t TokenBuilder) LtOrEq() Cmp {
|
||||||
return Cmp{
|
return t.cmp(leq, nil)
|
||||||
op: leq,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: t,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LtOrEqNamed produces token(column)<=token(?) with a custom parameter name.
|
// LtOrEqNamed produces token(column)<=token(?) with a custom parameter name.
|
||||||
func (t TokenBuilder) LtOrEqNamed(names ...string) Cmp {
|
func (t TokenBuilder) LtOrEqNamed(names ...string) Cmp {
|
||||||
return Cmp{
|
return t.cmp(leq, names)
|
||||||
op: leq,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: names,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gt produces token(column)>token(?).
|
// Gt produces token(column)>token(?).
|
||||||
func (t TokenBuilder) Gt() Cmp {
|
func (t TokenBuilder) Gt() Cmp {
|
||||||
return Cmp{
|
return t.cmp(gt, nil)
|
||||||
op: gt,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: t,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GtNamed produces token(column)>token(?) with a custom parameter name.
|
// GtNamed produces token(column)>token(?) with a custom parameter name.
|
||||||
func (t TokenBuilder) GtNamed(names ...string) Cmp {
|
func (t TokenBuilder) GtNamed(names ...string) Cmp {
|
||||||
return Cmp{
|
return t.cmp(gt, names)
|
||||||
op: gt,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: names,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GtOrEq produces token(column)>=token(?).
|
// GtOrEq produces token(column)>=token(?).
|
||||||
func (t TokenBuilder) GtOrEq() Cmp {
|
func (t TokenBuilder) GtOrEq() Cmp {
|
||||||
return Cmp{
|
return t.cmp(geq, nil)
|
||||||
op: geq,
|
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
|
||||||
names: t,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GtOrEqNamed produces token(column)>=token(?) with a custom parameter name.
|
// GtOrEqNamed produces token(column)>=token(?) with a custom parameter name.
|
||||||
func (t TokenBuilder) GtOrEqNamed(names ...string) Cmp {
|
func (t TokenBuilder) GtOrEqNamed(names ...string) Cmp {
|
||||||
|
return t.cmp(geq, names)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TokenBuilder) cmp(op op, names []string) Cmp {
|
||||||
|
s := names
|
||||||
|
if s == nil {
|
||||||
|
s = t
|
||||||
|
}
|
||||||
return Cmp{
|
return Cmp{
|
||||||
op: geq,
|
op: op,
|
||||||
fn: "token",
|
|
||||||
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
|
||||||
names: names,
|
fn: Fn("token", s...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
91
qb/update.go
91
qb/update.go
@@ -9,16 +9,41 @@ package qb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// assignment specifies an assignment in a set operation.
|
||||||
|
type assignment struct {
|
||||||
|
column string
|
||||||
|
name string
|
||||||
|
expr bool
|
||||||
|
fn *Func
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a assignment) writeCql(cql *bytes.Buffer) (names []string) {
|
||||||
|
cql.WriteString(a.column)
|
||||||
|
switch {
|
||||||
|
case a.expr:
|
||||||
|
names = append(names, a.name)
|
||||||
|
case a.fn != nil:
|
||||||
|
cql.WriteByte('=')
|
||||||
|
names = append(names, a.fn.writeCql(cql)...)
|
||||||
|
default:
|
||||||
|
cql.WriteByte('=')
|
||||||
|
cql.WriteByte('?')
|
||||||
|
names = append(names, a.name)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateBuilder builds CQL UPDATE statements.
|
// UpdateBuilder builds CQL UPDATE statements.
|
||||||
type UpdateBuilder struct {
|
type UpdateBuilder struct {
|
||||||
table string
|
table string
|
||||||
using using
|
using using
|
||||||
columns columns
|
assignments []assignment
|
||||||
where where
|
where where
|
||||||
_if _if
|
_if _if
|
||||||
exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update returns a new UpdateBuilder with the given table name.
|
// Update returns a new UpdateBuilder with the given table name.
|
||||||
@@ -39,14 +64,12 @@ func (b *UpdateBuilder) ToCql() (stmt string, names []string) {
|
|||||||
names = append(names, b.using.writeCql(&cql)...)
|
names = append(names, b.using.writeCql(&cql)...)
|
||||||
|
|
||||||
cql.WriteString("SET ")
|
cql.WriteString("SET ")
|
||||||
for i, c := range b.columns {
|
for i, a := range b.assignments {
|
||||||
cql.WriteString(c)
|
names = append(names, a.writeCql(&cql)...)
|
||||||
cql.WriteString("=?")
|
if i < len(b.assignments)-1 {
|
||||||
if i < len(b.columns)-1 {
|
|
||||||
cql.WriteByte(',')
|
cql.WriteByte(',')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
names = append(names, b.columns...)
|
|
||||||
cql.WriteByte(' ')
|
cql.WriteByte(' ')
|
||||||
|
|
||||||
names = append(names, b.where.writeCql(&cql)...)
|
names = append(names, b.where.writeCql(&cql)...)
|
||||||
@@ -80,7 +103,51 @@ func (b *UpdateBuilder) TTL() *UpdateBuilder {
|
|||||||
|
|
||||||
// Set adds SET clauses to the query.
|
// Set adds SET clauses to the query.
|
||||||
func (b *UpdateBuilder) Set(columns ...string) *UpdateBuilder {
|
func (b *UpdateBuilder) Set(columns ...string) *UpdateBuilder {
|
||||||
b.columns = append(b.columns, columns...)
|
for _, c := range columns {
|
||||||
|
b.assignments = append(b.assignments, assignment{
|
||||||
|
column: c,
|
||||||
|
name: c,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFunc adds SET column=someFunc(?...) clause to the query.
|
||||||
|
func (b *UpdateBuilder) SetFunc(column string, fn *Func) *UpdateBuilder {
|
||||||
|
b.assignments = append(b.assignments, assignment{column: column, fn: fn})
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds SET column=column+? clauses to the query.
|
||||||
|
func (b *UpdateBuilder) Add(column string) *UpdateBuilder {
|
||||||
|
return b.AddNamed(column, column)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddNamed adds SET column=column+? clauses to the query with a custom
|
||||||
|
// parameter name.
|
||||||
|
func (b *UpdateBuilder) AddNamed(column, name string) *UpdateBuilder {
|
||||||
|
b.assignments = append(b.assignments, assignment{
|
||||||
|
column: fmt.Sprint(column, "=", column, "+?"),
|
||||||
|
name: name,
|
||||||
|
expr: true,
|
||||||
|
})
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove adds SET column=column-? clauses to the query.
|
||||||
|
func (b *UpdateBuilder) Remove(column string) *UpdateBuilder {
|
||||||
|
return b.RemoveNamed(column, column)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveNamed adds SET column=column-? clauses to the query with a custom
|
||||||
|
// parameter name.
|
||||||
|
func (b *UpdateBuilder) RemoveNamed(column, name string) *UpdateBuilder {
|
||||||
|
b.assignments = append(b.assignments, assignment{
|
||||||
|
column: fmt.Sprint(column, "=", column, "-?"),
|
||||||
|
name: name,
|
||||||
|
expr: true,
|
||||||
|
})
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,36 @@ func TestUpdateBuilder(t *testing.T) {
|
|||||||
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=?,stars=? WHERE id=? ",
|
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=?,stars=? WHERE id=? ",
|
||||||
N: []string{"id", "user_uuid", "firstname", "stars", "expr"},
|
N: []string{"id", "user_uuid", "firstname", "stars", "expr"},
|
||||||
},
|
},
|
||||||
|
// Add SET SetFunc
|
||||||
|
{
|
||||||
|
B: Update("cycling.cyclist_name").SetFunc("user_uuid", Fn("someFunc", "param_0", "param_1")).Where(w).Set("stars"),
|
||||||
|
S: "UPDATE cycling.cyclist_name SET user_uuid=someFunc(?,?),stars=? WHERE id=? ",
|
||||||
|
N: []string{"param_0", "param_1", "stars", "expr"},
|
||||||
|
},
|
||||||
|
// Add SET Add
|
||||||
|
{
|
||||||
|
B: Update("cycling.cyclist_name").Add("total").Where(w),
|
||||||
|
S: "UPDATE cycling.cyclist_name SET total=total+? WHERE id=? ",
|
||||||
|
N: []string{"total", "expr"},
|
||||||
|
},
|
||||||
|
// Add SET AddNamed
|
||||||
|
{
|
||||||
|
B: Update("cycling.cyclist_name").AddNamed("total", "inc").Where(w),
|
||||||
|
S: "UPDATE cycling.cyclist_name SET total=total+? WHERE id=? ",
|
||||||
|
N: []string{"inc", "expr"},
|
||||||
|
},
|
||||||
|
// Add SET Remove
|
||||||
|
{
|
||||||
|
B: Update("cycling.cyclist_name").Remove("total").Where(w),
|
||||||
|
S: "UPDATE cycling.cyclist_name SET total=total-? WHERE id=? ",
|
||||||
|
N: []string{"total", "expr"},
|
||||||
|
},
|
||||||
|
// Add SET RemoveNamed
|
||||||
|
{
|
||||||
|
B: Update("cycling.cyclist_name").RemoveNamed("total", "dec").Where(w),
|
||||||
|
S: "UPDATE cycling.cyclist_name SET total=total-? WHERE id=? ",
|
||||||
|
N: []string{"dec", "expr"},
|
||||||
|
},
|
||||||
// Add WHERE
|
// Add WHERE
|
||||||
{
|
{
|
||||||
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w, Gt("firstname")),
|
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w, Gt("firstname")),
|
||||||
|
|||||||
Reference in New Issue
Block a user