Skip to content

Commit

Permalink
query: fixes pagination with query binding
Browse files Browse the repository at this point in the history
Paging state was not reset in query::Bind which effectively broke
paging when Bind was used.
  • Loading branch information
dahankzter committed Oct 18, 2019
1 parent a9de648 commit 38bd5c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,50 @@ func TestPaging(t *testing.T) {
}
}

func TestPagingWithBind(t *testing.T) {
session := createSession(t)
defer session.Close()

if session.cfg.ProtoVersion == 1 {
t.Skip("Paging not supported. Please use Cassandra >= 2.0")
}

if err := createTable(session, "CREATE TABLE gocql_test.paging_bind (id int, val int, primary key(id,val))"); err != nil {
t.Fatal("create table:", err)
}
for i := 0; i < 100; i++ {
if err := session.Query("INSERT INTO paging_bind (id,val) VALUES (?,?)", 1,i).Exec(); err != nil {
t.Fatal("insert:", err)
}
}

q := session.Query("SELECT val FROM paging_bind WHERE id = ? AND val < ?",1, 50).PageSize(10)
iter := q.Iter()
var id int
count := 0
for iter.Scan(&id) {
count++
}
if err := iter.Close(); err != nil {
t.Fatal("close:", err)
}
if count != 50 {
t.Fatalf("expected %d, got %d", 50, count)
}

iter = q.Bind(1, 20).Iter()
count = 0
for iter.Scan(&id) {
count++
}
if count != 20 {
t.Fatalf("expected %d, got %d", 20, count)
}
if err := iter.Close(); err != nil {
t.Fatal("close:", err)
}
}

func TestCAS(t *testing.T) {
cluster := createCluster()
cluster.SerialConsistency = LocalSerial
Expand Down
1 change: 1 addition & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ func (q *Query) Idempotent(value bool) *Query {
// to an existing query instance.
func (q *Query) Bind(v ...interface{}) *Query {
q.values = v
q.pageState = nil
return q
}

Expand Down

0 comments on commit 38bd5c2

Please sign in to comment.