Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Query.Consistency() and .MapScanCAS() #8

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ type Query interface {
// were selected, ErrNotFound is returned.
MapScan(m map[string]interface{}) error

// MapScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
// statement containing an IF clause). If the transaction fails because
// the existing values did not match, the previous values will be stored
// in dest map.
//
// As for INSERT .. IF NOT EXISTS, previous values will be returned as if
// SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
// column mismatching. MapScanCAS is added to capture them safely.
MapScanCAS(dest map[string]interface{}) (applied bool, err error)

// Scan executes the query, copies the columns of the first selected row
// into the values pointed at by dest and discards the rest. If no rows
// were selected, ErrNotFound is returned.
Expand All @@ -48,6 +58,9 @@ type Query interface {

// SetConsistency sets the consistency level for this query.
SetConsistency(c gocql.Consistency)

// SetConsistency sets the consistency level for this query.
willfaught marked this conversation as resolved.
Show resolved Hide resolved
Consistency(c gocql.Consistency) Query
}

var (
Expand Down Expand Up @@ -90,6 +103,12 @@ func (m QueryMock) MapScan(mm map[string]interface{}) error {
return m.Called(mm).Error(0)
}

// MapScan implements Query.
func (m QueryMock) MapScanCAS(mm map[string]interface{}) (bool, error) {
call := m.Called(mm)
return call.Bool(0), call.Error(1)
}

// Scan implements Query.
func (m QueryMock) Scan(dest ...interface{}) error {
return m.Called(dest).Error(0)
Expand All @@ -108,6 +127,10 @@ func (m QueryMock) SetConsistency(c gocql.Consistency) {
m.Called(c)
}

func (m QueryMock) Consistency(c gocql.Consistency) Query {
return m.Called(c).Get(0).(Query)
}

type query struct {
q *gocql.Query
}
Expand Down Expand Up @@ -151,3 +174,12 @@ func (q query) GetConsistency() gocql.Consistency {
func (q query) SetConsistency(c gocql.Consistency) {
q.q.SetConsistency(c)
}

func (q query) Consistency(c gocql.Consistency) Query {
q.q = q.q.Consistency(c)
return q
willfaught marked this conversation as resolved.
Show resolved Hide resolved
}

func (q query) MapScanCAS(dest map[string]interface{}) (applied bool, err error) {
return q.q.MapScanCAS(dest)
}
5 changes: 4 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package gockle
import (
"context"
"fmt"
"github.com/gocql/gocql"
"reflect"
"testing"

"github.com/gocql/gocql"
)

func TestQuery(t *testing.T) {
Expand Down Expand Up @@ -73,9 +74,11 @@ func TestQueryMock(t *testing.T) {
{"Iter", nil, []interface{}{it}},
{"MapScan", []interface{}{map[string]interface{}(nil)}, []interface{}{nil}},
{"MapScan", []interface{}{map[string]interface{}{"a": 1}}, []interface{}{e}},
{"MapScanCAS", []interface{}{map[string]interface{}{"a": 1}}, []interface{}{true, e}},
{"Release", nil, nil},
{"GetConsistency", nil, []interface{}{gocql.Quorum}},
{"SetConsistency", []interface{}{gocql.One}, nil},
{"Consistency", []interface{}{gocql.One}, []interface{}{m}},
})
}

Expand Down