Skip to content

Commit

Permalink
qb: using ttl timestamp
Browse files Browse the repository at this point in the history
Fixes: #40
  • Loading branch information
mmatczuk committed May 25, 2018
1 parent e8ffe4a commit fa402f3
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 136 deletions.
9 changes: 4 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ func TestExample(t *testing.T) {
{
stmt, names := qb.Insert("gocqlx_test.person").
Columns("first_name", "last_name", "email").
TTL().
TTL(86400 * time.Second).
Timestamp(time.Now()).
ToCql()

q := gocqlx.Query(session.Query(stmt), names).BindStructMap(p, qb.M{
"_ttl": qb.TTL(86400 * time.Second),
})
if err := q.ExecRelease(); err != nil {
err := gocqlx.Query(session.Query(stmt), names).BindStruct(p).ExecRelease()
if err != nil {
t.Fatal(err)
}
}
Expand Down
26 changes: 20 additions & 6 deletions qb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package qb
import (
"bytes"
"fmt"
"time"
)

// BATCH reference:
Expand Down Expand Up @@ -98,14 +99,27 @@ func (b *BatchBuilder) Counter() *BatchBuilder {
return b
}

// Timestamp sets a USING TIMESTAMP clause on the query.
func (b *BatchBuilder) Timestamp() *BatchBuilder {
b.using.timestamp = true
// TTL adds USING TTL clause to the query.
func (b *BatchBuilder) TTL(d time.Duration) *BatchBuilder {
b.using.TTL(d)
return b
}

// TTL sets a USING TTL clause on the query.
func (b *BatchBuilder) TTL() *BatchBuilder {
b.using.ttl = true
// TTLNamed adds USING TTL clause to the query with a custom parameter name.
func (b *BatchBuilder) TTLNamed(name string) *BatchBuilder {
b.using.TTLNamed(name)
return b
}

// Timestamp adds USING TIMESTAMP clause to the query.
func (b *BatchBuilder) Timestamp(t time.Time) *BatchBuilder {
b.using.Timestamp(t)
return b
}

// TimestampNamed adds a USING TIMESTAMP clause to the query with a custom
// parameter name.
func (b *BatchBuilder) TimestampNamed(name string) *BatchBuilder {
b.using.TimestampNamed(name)
return b
}
17 changes: 13 additions & 4 deletions qb/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package qb

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -53,15 +54,23 @@ func TestBatchBuilder(t *testing.T) {
},
// Add TTL
{
B: Batch().TTL(),
B: Batch().TTL(time.Second),
S: "BEGIN BATCH USING TTL 1 APPLY BATCH ",
},
{
B: Batch().TTLNamed("ttl"),
S: "BEGIN BATCH USING TTL ? APPLY BATCH ",
N: []string{"_ttl"},
N: []string{"ttl"},
},
// Add TIMESTAMP
{
B: Batch().Timestamp(),
B: Batch().Timestamp(time.Date(2005, 05, 05, 0, 0, 0, 0, time.UTC)),
S: "BEGIN BATCH USING TIMESTAMP 1115251200000000 APPLY BATCH ",
},
{
B: Batch().TimestampNamed("ts"),
S: "BEGIN BATCH USING TIMESTAMP ? APPLY BATCH ",
N: []string{"_ts"},
N: []string{"ts"},
},
}

Expand Down
22 changes: 22 additions & 0 deletions qb/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,25 @@ func (cs cmps) writeCql(cql *bytes.Buffer) (names []string) {
cql.WriteByte(' ')
return
}

type where cmps

func (w where) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 {
return
}

cql.WriteString("WHERE ")
return cmps(w).writeCql(cql)
}

type _if cmps

func (w _if) writeCql(cql *bytes.Buffer) (names []string) {
if len(w) == 0 {
return
}

cql.WriteString("IF ")
return cmps(w).writeCql(cql)
}
14 changes: 11 additions & 3 deletions qb/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package qb

import (
"bytes"
"time"
)

// DeleteBuilder builds CQL DELETE statements.
Expand Down Expand Up @@ -65,9 +66,16 @@ func (b *DeleteBuilder) Columns(columns ...string) *DeleteBuilder {
return b
}

// Timestamp sets a USING TIMESTAMP clause on the query.
func (b *DeleteBuilder) Timestamp() *DeleteBuilder {
b.using.timestamp = true
// Timestamp adds USING TIMESTAMP clause to the query.
func (b *DeleteBuilder) Timestamp(t time.Time) *DeleteBuilder {
b.using.Timestamp(t)
return b
}

// TimestampNamed adds a USING TIMESTAMP clause to the query with a custom
// parameter name.
func (b *DeleteBuilder) TimestampNamed(name string) *DeleteBuilder {
b.using.TimestampNamed(name)
return b
}

Expand Down
12 changes: 9 additions & 3 deletions qb/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package qb

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -50,9 +51,14 @@ func TestDeleteBuilder(t *testing.T) {
},
// Add TIMESTAMP
{
B: Delete("cycling.cyclist_name").Where(w).Timestamp(),
B: Delete("cycling.cyclist_name").Where(w).Timestamp(time.Date(2005, 05, 05, 0, 0, 0, 0, time.UTC)),
S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP 1115251200000000 WHERE id=? ",
N: []string{"expr"},
},
{
B: Delete("cycling.cyclist_name").Where(w).TimestampNamed("ts"),
S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP ? WHERE id=? ",
N: []string{"_ts", "expr"},
N: []string{"ts", "expr"},
},
// Add IF EXISTS
{
Expand All @@ -68,7 +74,7 @@ func TestDeleteBuilder(t *testing.T) {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
t.Error(diff, names)
}
}
}
65 changes: 0 additions & 65 deletions qb/expr.go

This file was deleted.

26 changes: 20 additions & 6 deletions qb/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package qb

import (
"bytes"
"time"
)

// initializer specifies an value for a column in an insert operation.
Expand Down Expand Up @@ -119,14 +120,27 @@ func (b *InsertBuilder) Unique() *InsertBuilder {
return b
}

// Timestamp sets a USING TIMESTAMP clause on the query.
func (b *InsertBuilder) Timestamp() *InsertBuilder {
b.using.timestamp = true
// TTL adds USING TTL clause to the query.
func (b *InsertBuilder) TTL(d time.Duration) *InsertBuilder {
b.using.TTL(d)
return b
}

// TTL sets a USING TTL clause on the query.
func (b *InsertBuilder) TTL() *InsertBuilder {
b.using.ttl = true
// TTLNamed adds USING TTL clause to the query with a custom parameter name.
func (b *InsertBuilder) TTLNamed(name string) *InsertBuilder {
b.using.TTLNamed(name)
return b
}

// Timestamp adds USING TIMESTAMP clause to the query.
func (b *InsertBuilder) Timestamp(t time.Time) *InsertBuilder {
b.using.Timestamp(t)
return b
}

// TimestampNamed adds a USING TIMESTAMP clause to the query with a custom
// parameter name.
func (b *InsertBuilder) TimestampNamed(name string) *InsertBuilder {
b.using.TimestampNamed(name)
return b
}
19 changes: 15 additions & 4 deletions qb/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package qb

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -49,15 +50,25 @@ func TestInsertBuilder(t *testing.T) {
},
// Add TTL
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TTL(),
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TTL(time.Second),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TTL 1 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TTLNamed("ttl"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TTL ? ",
N: []string{"id", "user_uuid", "firstname", "_ttl"},
N: []string{"id", "user_uuid", "firstname", "ttl"},
},
// Add TIMESTAMP
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timestamp(),
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timestamp(time.Date(2005, 05, 05, 0, 0, 0, 0, time.UTC)),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP 1115251200000000 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TimestampNamed("ts"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP ? ",
N: []string{"id", "user_uuid", "firstname", "_ts"},
N: []string{"id", "user_uuid", "firstname", "ts"},
},
// Add IF NOT EXISTS
{
Expand Down
26 changes: 20 additions & 6 deletions qb/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package qb

import (
"bytes"
"time"
)

// assignment specifies an assignment in a set operation.
Expand Down Expand Up @@ -78,15 +79,28 @@ func (b *UpdateBuilder) Table(table string) *UpdateBuilder {
return b
}

// Timestamp sets a USING TIMESTAMP clause on the query.
func (b *UpdateBuilder) Timestamp() *UpdateBuilder {
b.using.timestamp = true
// TTL adds USING TTL clause to the query.
func (b *UpdateBuilder) TTL(d time.Duration) *UpdateBuilder {
b.using.TTL(d)
return b
}

// TTL sets a USING TTL clause on the query.
func (b *UpdateBuilder) TTL() *UpdateBuilder {
b.using.ttl = true
// TTLNamed adds USING TTL clause to the query with a custom parameter name.
func (b *UpdateBuilder) TTLNamed(name string) *UpdateBuilder {
b.using.TTLNamed(name)
return b
}

// Timestamp adds USING TIMESTAMP clause to the query.
func (b *UpdateBuilder) Timestamp(t time.Time) *UpdateBuilder {
b.using.Timestamp(t)
return b
}

// TimestampNamed adds a USING TIMESTAMP clause to the query with a custom
// parameter name.
func (b *UpdateBuilder) TimestampNamed(name string) *UpdateBuilder {
b.using.TimestampNamed(name)
return b
}

Expand Down
Loading

0 comments on commit fa402f3

Please sign in to comment.