Skip to content

Commit 7353ad6

Browse files
committed
Exec() method for batch was added & Query() method was refactored
1 parent 34fdeeb commit 7353ad6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

example_batch_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package gocql_test
33
import (
44
"context"
55
"fmt"
6-
"github.com/gocql/gocql"
76
"log"
7+
"testing"
8+
9+
"github.com/gocql/gocql"
810
)
911

1012
// Example_batch demonstrates how to execute a batch of statements.
11-
func Example_batch() {
13+
func TestExample_batch(t *testing.T) {
1214
/* The example assumes the following CQL was used to setup the keyspace:
1315
create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
1416
create table example.batches(pk int, ck int, description text, PRIMARY KEY(pk, ck));
@@ -35,11 +37,19 @@ func Example_batch() {
3537
Args: []interface{}{1, 3, "1.3"},
3638
Idempotent: true,
3739
})
40+
3841
err = session.ExecuteBatch(b)
3942
if err != nil {
4043
log.Fatal(err)
4144
}
4245

46+
err = b.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 4, "1.4").
47+
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 5, "1.5").
48+
Exec()
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
4353
scanner := session.Query("SELECT pk, ck, description FROM example.batches").Iter().Scanner()
4454
for scanner.Next() {
4555
var pk, ck int32
@@ -52,4 +62,6 @@ func Example_batch() {
5262
}
5363
// 1 2 1.2
5464
// 1 3 1.3
65+
// 1 4 1.4
66+
// 1 5 1.5
5567
}

session.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,11 @@ func (b *Batch) execute(ctx context.Context, conn *Conn) *Iter {
711711
return conn.executeBatch(ctx, b)
712712
}
713713

714+
func (b *Batch) Exec() error {
715+
iter := b.session.executeBatch(b)
716+
return iter.Close()
717+
}
718+
714719
func (s *Session) executeBatch(batch *Batch) *Iter {
715720
// fail fast
716721
if s.Closed() {
@@ -1840,8 +1845,9 @@ func (b *Batch) SpeculativeExecutionPolicy(sp SpeculativeExecutionPolicy) *Batch
18401845
}
18411846

18421847
// Query adds the query to the batch operation
1843-
func (b *Batch) Query(stmt string, args ...interface{}) {
1848+
func (b *Batch) Query(stmt string, args ...interface{}) *Batch {
18441849
b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
1850+
return b
18451851
}
18461852

18471853
// Bind adds the query to the batch operation and correlates it with a binding callback

0 commit comments

Comments
 (0)