This commit is contained in:
Michał Matczuk
2017-07-27 13:39:27 +02:00
parent 546e11d58f
commit 2afe71e2b9
10 changed files with 363 additions and 156 deletions

171
qb/cmp.go Normal file
View File

@@ -0,0 +1,171 @@
package qb
import "bytes"
type op byte
const (
eq op = iota
lt
leq
gt
geq
in
cnt
)
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
}
func Eq(column string) Cmp {
return Cmp{
op: eq,
column: column,
name: column,
}
}
func EqNamed(column, name string) Cmp {
return Cmp{
op: eq,
column: column,
name: name,
}
}
func Lt(column string) Cmp {
return Cmp{
op: lt,
column: column,
name: column,
}
}
func LtNamed(column, name string) Cmp {
return Cmp{
op: lt,
column: column,
name: name,
}
}
func LtOrEq(column string) Cmp {
return Cmp{
op: leq,
column: column,
name: column,
}
}
func LtOrEqNamed(column, name string) Cmp {
return Cmp{
op: leq,
column: column,
name: name,
}
}
func Gt(column string) Cmp {
return Cmp{
op: gt,
column: column,
name: column,
}
}
func GtNamed(column, name string) Cmp {
return Cmp{
op: gt,
column: column,
name: name,
}
}
func GtOrEq(column string) Cmp {
return Cmp{
op: geq,
column: column,
name: column,
}
}
func GtOrEqNamed(column, name string) Cmp {
return Cmp{
op: geq,
column: column,
name: name,
}
}
func In(column string) Cmp {
return Cmp{
op: in,
column: column,
name: column,
}
}
func InNamed(column, name string) Cmp {
return Cmp{
op: in,
column: column,
name: name,
}
}
func Contains(column string) Cmp {
return Cmp{
op: cnt,
column: column,
name: column,
}
}
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
}

114
qb/cmp_test.go Normal file
View File

@@ -0,0 +1,114 @@
package qb
import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCmp(t *testing.T) {
table := []struct {
C Cmp
S string
N string
}{
{
C: Eq("eq"),
S: "eq=?",
N: "eq",
},
{
C: EqNamed("eq", "name"),
S: "eq=?",
N: "name",
},
{
C: Lt("lt"),
S: "lt<?",
N: "lt",
},
{
C: LtNamed("lt", "name"),
S: "lt<?",
N: "name",
},
{
C: LtOrEq("lt"),
S: "lt<=?",
N: "lt",
},
{
C: LtOrEqNamed("lt", "name"),
S: "lt<=?",
N: "name",
},
{
C: Gt("gt"),
S: "gt>?",
N: "gt",
},
{
C: GtNamed("gt", "name"),
S: "gt>?",
N: "name",
},
{
C: GtOrEq("gt"),
S: "gt>=?",
N: "gt",
},
{
C: GtOrEqNamed("gt", "name"),
S: "gt>=?",
N: "name",
},
{
C: In("in"),
S: "in IN ?",
N: "in",
},
{
C: InNamed("in", "name"),
S: "in IN ?",
N: "name",
},
{
C: Contains("cnt"),
S: "cnt CONTAINS ?",
N: "cnt",
},
{
C: ContainsNamed("cnt", "name"),
S: "cnt CONTAINS ?",
N: "name",
},
}
buf := bytes.Buffer{}
for _, test := range table {
buf.Reset()
name := test.C.writeCql(&buf)
if diff := cmp.Diff(test.S, buf.String()); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, name); diff != "" {
t.Error(diff)
}
}
}
func BenchmarkCmp(b *testing.B) {
buf := bytes.Buffer{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
c := cmps{
Eq("id"),
Lt("user_uuid"),
LtOrEq("firstname"),
Gt("stars"),
}
c.writeCql(&buf)
}
}

View File

@@ -77,13 +77,13 @@ func (b *DeleteBuilder) Timestamp(t time.Time) *DeleteBuilder {
return b return b
} }
func (b *DeleteBuilder) Where(e ...expr) *DeleteBuilder { func (b *DeleteBuilder) Where(w ...Cmp) *DeleteBuilder {
b.where = append(b.where, e...) b.where = append(b.where, w...)
return b return b
} }
func (b *DeleteBuilder) If(e ...expr) *DeleteBuilder { func (b *DeleteBuilder) If(w ...Cmp) *DeleteBuilder {
b._if = append(b._if, e...) b._if = append(b._if, w...)
return b return b
} }

View File

@@ -8,10 +8,7 @@ import (
) )
func TestDeleteBuilder(t *testing.T) { func TestDeleteBuilder(t *testing.T) {
m := mockExpr{ w := EqNamed("id", "expr")
cql: "expr",
names: []string{"expr"},
}
table := []struct { table := []struct {
B *DeleteBuilder B *DeleteBuilder
@@ -20,56 +17,44 @@ func TestDeleteBuilder(t *testing.T) {
}{ }{
// Basic test for delete // Basic test for delete
{ {
B: Delete("cycling.cyclist_name").Where(m), B: Delete("cycling.cyclist_name").Where(w),
S: "DELETE FROM cycling.cyclist_name WHERE expr ", S: "DELETE FROM cycling.cyclist_name WHERE id=? ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Change table name // Change table name
{ {
B: Delete("cycling.cyclist_name").Where(m).From("Foobar"), B: Delete("cycling.cyclist_name").Where(w).From("Foobar"),
S: "DELETE FROM Foobar WHERE expr ", S: "DELETE FROM Foobar WHERE id=? ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add column // Add column
{ {
B: Delete("cycling.cyclist_name").Where(m).Columns("stars"), B: Delete("cycling.cyclist_name").Where(w).Columns("stars"),
S: "DELETE stars FROM cycling.cyclist_name WHERE expr ", S: "DELETE stars FROM cycling.cyclist_name WHERE id=? ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add WHERE // Add WHERE
{ {
B: Delete("cycling.cyclist_name").Where(m).Where(mockExpr{ B: Delete("cycling.cyclist_name").Where(w, Gt("firstname")),
cql: "expr_1", S: "DELETE FROM cycling.cyclist_name WHERE id=? AND firstname>? ",
names: []string{"expr_1"}, N: []string{"expr", "firstname"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "DELETE FROM cycling.cyclist_name WHERE expr AND expr_1 AND expr_2 ",
N: []string{"expr", "expr_1", "expr_2"},
}, },
// Add IF // Add IF
{ {
B: Delete("cycling.cyclist_name").Where(m).If(mockExpr{ B: Delete("cycling.cyclist_name").Where(w).If(Gt("firstname")),
cql: "expr_1", S: "DELETE FROM cycling.cyclist_name WHERE id=? IF firstname>? ",
names: []string{"expr_1"}, N: []string{"expr", "firstname"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "DELETE FROM cycling.cyclist_name WHERE expr IF expr_1 AND expr_2 ",
N: []string{"expr", "expr_1", "expr_2"},
}, },
// Add TIMESTAMP // Add TIMESTAMP
{ {
B: Delete("cycling.cyclist_name").Where(m).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)), B: Delete("cycling.cyclist_name").Where(w).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)),
S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP 123456789 WHERE expr ", S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP 123456789 WHERE id=? ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add IF EXISTS // Add IF EXISTS
{ {
B: Delete("cycling.cyclist_name").Where(m).Existing(), B: Delete("cycling.cyclist_name").Where(w).Existing(),
S: "DELETE FROM cycling.cyclist_name WHERE expr IF EXISTS ", S: "DELETE FROM cycling.cyclist_name WHERE id=? IF EXISTS ",
N: []string{"expr"}, N: []string{"expr"},
}, },
} }
@@ -89,12 +74,8 @@ func TestDeleteBuilder(t *testing.T) {
} }
func BenchmarkDeleteBuilder(b *testing.B) { func BenchmarkDeleteBuilder(b *testing.B) {
m := mockExpr{
cql: "expr",
names: []string{"expr"},
}
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Delete("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(m) Delete("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(Eq("id"))
} }
} }

View File

@@ -3,16 +3,9 @@ package qb
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"strings"
"time" "time"
) )
type expr interface {
// WriteCql writes a CQL representation of the expr to a buffer and returns
// slice of parameter names.
WriteCql(cql *bytes.Buffer) (names []string)
}
type columns []string type columns []string
func (cols columns) writeCql(cql *bytes.Buffer) { func (cols columns) writeCql(cql *bytes.Buffer) {
@@ -30,21 +23,26 @@ type using struct {
} }
func (u using) writeCql(cql *bytes.Buffer) { func (u using) writeCql(cql *bytes.Buffer) {
var v []string ts := !u.timestamp.IsZero()
if !u.timestamp.IsZero() {
v = append(v, fmt.Sprint("TIMESTAMP ", u.timestamp.UnixNano()/1000)) if ts {
cql.WriteString("USING TIMESTAMP ")
cql.WriteString(fmt.Sprint(u.timestamp.UnixNano() / 1000))
cql.WriteByte(' ')
} }
if u.ttl != 0 { if u.ttl != 0 {
v = append(v, fmt.Sprint("TTL ", int(u.ttl.Seconds()))) if ts {
cql.WriteString("AND TTL ")
} else {
cql.WriteString("USING TTL ")
} }
if len(v) > 0 { cql.WriteString(fmt.Sprint(int(u.ttl.Seconds())))
cql.WriteString("USING ")
cql.WriteString(strings.Join(v, ","))
cql.WriteByte(' ') cql.WriteByte(' ')
} }
} }
type where []expr type where cmps
func (w where) writeCql(cql *bytes.Buffer) (names []string) { func (w where) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 { if len(w) == 0 {
@@ -52,10 +50,10 @@ func (w where) writeCql(cql *bytes.Buffer) (names []string) {
} }
cql.WriteString("WHERE ") cql.WriteString("WHERE ")
return writeCql(w, cql) return cmps(w).writeCql(cql)
} }
type _if []expr type _if cmps
func (w _if) writeCql(cql *bytes.Buffer) (names []string) { func (w _if) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 { if len(w) == 0 {
@@ -63,16 +61,5 @@ func (w _if) writeCql(cql *bytes.Buffer) (names []string) {
} }
cql.WriteString("IF ") cql.WriteString("IF ")
return writeCql(w, cql) return cmps(w).writeCql(cql)
}
func writeCql(es []expr, cql *bytes.Buffer) (names []string) {
for i, c := range es {
names = append(names, c.WriteCql(cql)...)
if i < len(es)-1 {
cql.WriteString(" AND ")
}
}
cql.WriteByte(' ')
return
} }

View File

@@ -1,14 +0,0 @@
package qb
import "bytes"
type mockExpr struct {
cql string
names []string
}
func (m mockExpr) WriteCql(cql *bytes.Buffer) (names []string) {
cql.WriteString(m.cql)
names = m.names
return
}

View File

@@ -120,8 +120,8 @@ func (b *SelectBuilder) Distinct(columns... string) *SelectBuilder {
return b return b
} }
func (b *SelectBuilder) Where(e ...expr) *SelectBuilder { func (b *SelectBuilder) Where(w ...Cmp) *SelectBuilder {
b.where = append(b.where, e...) b.where = append(b.where, w...)
return b return b
} }

View File

@@ -7,10 +7,7 @@ import (
) )
func TestSelectBuilder(t *testing.T) { func TestSelectBuilder(t *testing.T) {
m := mockExpr{ w := EqNamed("id", "expr")
cql: "expr",
names: []string{"expr"},
}
table := []struct { table := []struct {
B *SelectBuilder B *SelectBuilder
@@ -39,15 +36,9 @@ func TestSelectBuilder(t *testing.T) {
}, },
// Add WHERE // Add WHERE
{ {
B: Select("cycling.cyclist_name").Where(m).Where(mockExpr{ B: Select("cycling.cyclist_name").Where(w, Gt("firstname")),
cql: "expr_1", S: "SELECT * FROM cycling.cyclist_name WHERE id=? AND firstname>? ",
names: []string{"expr_1"}, N: []string{"expr", "firstname"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "SELECT * FROM cycling.cyclist_name WHERE expr AND expr_1 AND expr_2 ",
N: []string{"expr", "expr_1", "expr_2"},
}, },
// Add GROUP BY // Add GROUP BY
{ {
@@ -56,26 +47,26 @@ func TestSelectBuilder(t *testing.T) {
}, },
// Add ORDER BY // Add ORDER BY
{ {
B: Select("cycling.cyclist_name").Where(m).OrderBy("firstname", ASC), B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC),
S: "SELECT * FROM cycling.cyclist_name WHERE expr ORDER BY firstname ASC ", S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add LIMIT // Add LIMIT
{ {
B: Select("cycling.cyclist_name").Where(m).Limit(10), B: Select("cycling.cyclist_name").Where(w).Limit(10),
S: "SELECT * FROM cycling.cyclist_name WHERE expr LIMIT 10 ", S: "SELECT * FROM cycling.cyclist_name WHERE id=? LIMIT 10 ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add PER PARTITION LIMIT // Add PER PARTITION LIMIT
{ {
B: Select("cycling.cyclist_name").Where(m).LimitPerPartition(10), B: Select("cycling.cyclist_name").Where(w).LimitPerPartition(10),
S: "SELECT * FROM cycling.cyclist_name WHERE expr PER PARTITION LIMIT 10 ", S: "SELECT * FROM cycling.cyclist_name WHERE id=? PER PARTITION LIMIT 10 ",
N: []string{"expr"}, N: []string{"expr"},
}, },
// Add ALLOW FILTERING // Add ALLOW FILTERING
{ {
B: Select("cycling.cyclist_name").Where(m).AllowFiltering(), B: Select("cycling.cyclist_name").Where(w).AllowFiltering(),
S: "SELECT * FROM cycling.cyclist_name WHERE expr ALLOW FILTERING ", S: "SELECT * FROM cycling.cyclist_name WHERE id=? ALLOW FILTERING ",
N: []string{"expr"}, N: []string{"expr"},
}, },
} }
@@ -95,12 +86,8 @@ func TestSelectBuilder(t *testing.T) {
} }
func BenchmarkSelectBuilder(b *testing.B) { func BenchmarkSelectBuilder(b *testing.B) {
m := mockExpr{
cql: "expr",
names: []string{"expr"},
}
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(m) Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(Eq("id"))
} }
} }

View File

@@ -94,13 +94,13 @@ func (b *UpdateBuilder) Set(columns ...string) *UpdateBuilder {
return b return b
} }
func (b *UpdateBuilder) Where(e ...expr) *UpdateBuilder { func (b *UpdateBuilder) Where(w ...Cmp) *UpdateBuilder {
b.where = append(b.where, e...) b.where = append(b.where, w...)
return b return b
} }
func (b *UpdateBuilder) If(e ...expr) *UpdateBuilder { func (b *UpdateBuilder) If(w ...Cmp) *UpdateBuilder {
b._if = append(b._if, e...) b._if = append(b._if, w...)
return b return b
} }

View File

@@ -8,10 +8,7 @@ import (
) )
func TestUpdateBuilder(t *testing.T) { func TestUpdateBuilder(t *testing.T) {
m := mockExpr{ w := EqNamed("id", "expr")
cql: "expr",
names: []string{"expr"},
}
table := []struct { table := []struct {
B *UpdateBuilder B *UpdateBuilder
@@ -20,62 +17,50 @@ func TestUpdateBuilder(t *testing.T) {
}{ }{
// Basic test for update // Basic test for update
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr ", S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"}, N: []string{"id", "user_uuid", "firstname", "expr"},
}, },
// Change table name // Change table name
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Table("Foobar"), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Table("Foobar"),
S: "UPDATE Foobar SET id=?,user_uuid=?,firstname=? WHERE expr ", S: "UPDATE Foobar SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"}, N: []string{"id", "user_uuid", "firstname", "expr"},
}, },
// Add SET // Add SET
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Set("stars"), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Set("stars"),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=?,stars=? WHERE expr ", 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 WHERE // Add WHERE
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Where(mockExpr{ B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w, Gt("firstname")),
cql: "expr_1", S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? AND firstname>? ",
names: []string{"expr_1"}, N: []string{"id", "user_uuid", "firstname", "expr", "firstname"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr AND expr_1 AND expr_2 ",
N: []string{"id", "user_uuid", "firstname", "expr", "expr_1", "expr_2"},
}, },
// Add IF // Add IF
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).If(mockExpr{ B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).If(Gt("firstname")),
cql: "expr_1", S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? IF firstname>? ",
names: []string{"expr_1"}, N: []string{"id", "user_uuid", "firstname", "expr", "firstname"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr IF expr_1 AND expr_2 ",
N: []string{"id", "user_uuid", "firstname", "expr", "expr_1", "expr_2"},
}, },
// Add TTL // Add TTL
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).TTL(time.Second * 86400), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TTL(time.Second * 86400),
S: "UPDATE cycling.cyclist_name USING TTL 86400 SET id=?,user_uuid=?,firstname=? WHERE expr ", S: "UPDATE cycling.cyclist_name USING TTL 86400 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"}, N: []string{"id", "user_uuid", "firstname", "expr"},
}, },
// Add TIMESTAMP // Add TIMESTAMP
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)),
S: "UPDATE cycling.cyclist_name USING TIMESTAMP 123456789 SET id=?,user_uuid=?,firstname=? WHERE expr ", S: "UPDATE cycling.cyclist_name USING TIMESTAMP 123456789 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"}, N: []string{"id", "user_uuid", "firstname", "expr"},
}, },
// Add IF EXISTS // Add IF EXISTS
{ {
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Existing(), B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Existing(),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr IF EXISTS ", S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? IF EXISTS ",
N: []string{"id", "user_uuid", "firstname", "expr"}, N: []string{"id", "user_uuid", "firstname", "expr"},
}, },
} }
@@ -95,12 +80,8 @@ func TestUpdateBuilder(t *testing.T) {
} }
func BenchmarkUpdateBuilder(b *testing.B) { func BenchmarkUpdateBuilder(b *testing.B) {
m := mockExpr{
cql: "expr",
names: []string{"expr"},
}
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname", "stars").Where(m) Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname", "stars").Where(Eq("id"))
} }
} }