Skip to content

Commit

Permalink
Merge pull request #16 from scylladb/mmt/counters
Browse files Browse the repository at this point in the history
qb advanced updates
  • Loading branch information
mmatczuk authored Sep 22, 2017
2 parents 9e40fcf + 557674a commit fd5efe4
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 175 deletions.
33 changes: 1 addition & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
* Queries with named parameters (:identifier) support
* Functions support
* Binding parameters form struct or map
* Scanning results into structs and slices
* Automatic query releasing
Expand Down Expand Up @@ -85,38 +86,6 @@ type Person struct {
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]}]
}

// 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).
Expand Down
23 changes: 20 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{
i := qb.Insert("gocqlx_test.person").Columns("first_name", "last_name", "email")
Expand Down Expand Up @@ -137,7 +153,7 @@ func TestExample(t *testing.T) {
}

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.
Expand All @@ -156,7 +172,7 @@ func TestExample(t *testing.T) {
}

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.
Expand All @@ -168,6 +184,7 @@ func TestExample(t *testing.T) {
}

stmt, names := qb.Select("gocqlx_test.person").
Columns("first_name").
Where(qb.Token("first_name").Gt()).
Limit(10).
ToCql()
Expand All @@ -180,7 +197,7 @@ func TestExample(t *testing.T) {
}

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.
Expand Down
118 changes: 59 additions & 59 deletions qb/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,8 @@ const (
type Cmp struct {
op op
column string
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)
}

// 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...)
name string
fn *Func
}

func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
Expand All @@ -94,19 +53,15 @@ func (c Cmp) writeCql(cql *bytes.Buffer) (names []string) {
cql.WriteString(" CONTAINS ")
}

if c.fn == "" {
if c.fn != nil {
names = append(names, c.fn.writeCql(cql)...)
} else {
cql.WriteByte('?')
if c.names == nil {
if c.name == "" {
names = append(names, c.column)
} 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
Expand All @@ -125,7 +80,16 @@ func EqNamed(column, name string) Cmp {
return Cmp{
op: eq,
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,
}
}

Expand All @@ -142,7 +106,16 @@ func LtNamed(column, name string) Cmp {
return Cmp{
op: lt,
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,
}
}

Expand All @@ -159,7 +132,16 @@ func LtOrEqNamed(column, name string) Cmp {
return Cmp{
op: leq,
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,
}
}

Expand All @@ -176,7 +158,16 @@ func GtNamed(column, name string) Cmp {
return Cmp{
op: gt,
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,
}
}

Expand All @@ -193,7 +184,16 @@ func GtOrEqNamed(column, name string) Cmp {
return Cmp{
op: geq,
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,
}
}

Expand All @@ -210,7 +210,7 @@ func InNamed(column, name string) Cmp {
return Cmp{
op: in,
column: column,
names: []string{name},
name: name,
}
}

Expand All @@ -227,7 +227,7 @@ func ContainsNamed(column, name string) Cmp {
return Cmp{
op: cnt,
column: column,
names: []string{name},
name: name,
}
}

Expand Down
13 changes: 4 additions & 9 deletions qb/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,24 @@ func TestCmp(t *testing.T) {

// Functions
{
C: Eq("eq").Func("fn", "arg0", "arg1"),
C: EqFunc("eq", Fn("fn", "arg0", "arg1")),
S: "eq=fn(?,?)",
N: []string{"arg0", "arg1"},
},
{
C: Eq("eq").MaxTimeuuid("arg0"),
C: EqFunc("eq", MaxTimeuuid("arg0")),
S: "eq=maxTimeuuid(?)",
N: []string{"arg0"},
},
{
C: Eq("eq").MinTimeuuid("arg0"),
C: EqFunc("eq", MinTimeuuid("arg0")),
S: "eq=minTimeuuid(?)",
N: []string{"arg0"},
},
{
C: Eq("eq").Now(),
C: EqFunc("eq", Now()),
S: "eq=now()",
},
{
C: Eq("eq").Token("arg0", "arg1"),
S: "eq=token(?,?)",
N: []string{"arg0", "arg1"},
},
}

buf := bytes.Buffer{}
Expand Down
51 changes: 51 additions & 0 deletions qb/func.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading

0 comments on commit fd5efe4

Please sign in to comment.