Skip to content

Commit 5a44475

Browse files
author
tengu-alt
committed
Exec() method for batch was added & Query() method was refactored
1 parent 974fa12 commit 5a44475

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

batch_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,24 @@ func TestBatch_Errors(t *testing.T) {
4848
}
4949

5050
b := session.NewBatch(LoggedBatch)
51-
b.Query("SELECT * FROM batch_errors WHERE id=2 AND val=?", nil)
52-
if err := session.ExecuteBatch(b); err == nil {
51+
b = b.Query("SELECT * FROM gocql_test.batch_errors WHERE id=2 AND val=?", nil)
52+
if err := b.Exec(); err == nil {
5353
t.Fatal("expected to get error for invalid query in batch")
5454
}
5555
}
5656

5757
func TestBatch_WithTimestamp(t *testing.T) {
58-
session := createSession(t)
59-
defer session.Close()
58+
cluster := NewCluster("172.17.0.2:9042")
59+
cluster.Keyspace = "example"
60+
cluster.Consistency = Quorum
61+
cluster.ProtoVersion = 4
62+
// connect to the cluster
63+
session, err := cluster.CreateSession()
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
//session := createSession(t)
68+
//defer session.Close()
6069

6170
if session.cfg.ProtoVersion < protoVersion3 {
6271
t.Skip("Batch timestamps are only available on protocol >= 3")
@@ -70,13 +79,15 @@ func TestBatch_WithTimestamp(t *testing.T) {
7079

7180
b := session.NewBatch(LoggedBatch)
7281
b.WithTimestamp(micros)
73-
b.Query("INSERT INTO batch_ts (id, val) VALUES (?, ?)", 1, "val")
74-
if err := session.ExecuteBatch(b); err != nil {
82+
b = b.Query("INSERT INTO gocql_test.batch_ts (id, val) VALUES (?, ?)", 1, "val")
83+
b = b.Query("INSERT INTO gocql_test.batch_ts (id, val) VALUES (?, ?)", 2, "val")
84+
85+
if err := b.Exec(); err != nil {
7586
t.Fatal(err)
7687
}
7788

7889
var storedTs int64
79-
if err := session.Query(`SELECT writetime(val) FROM batch_ts WHERE id = ?`, 1).Scan(&storedTs); err != nil {
90+
if err := session.Query(`SELECT writetime(val) FROM gocql_test.batch_ts WHERE id = ?`, 1).Scan(&storedTs); err != nil {
8091
t.Fatal(err)
8192
}
8293

example_batch_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ func Example_batch() {
6060
Args: []interface{}{1, 3, "1.3"},
6161
Idempotent: true,
6262
})
63+
6364
err = session.ExecuteBatch(b)
6465
if err != nil {
6566
log.Fatal(err)
6667
}
6768

69+
err = b.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 4, "1.4").
70+
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 5, "1.5").
71+
Exec()
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
6876
scanner := session.Query("SELECT pk, ck, description FROM example.batches").Iter().Scanner()
6977
for scanner.Next() {
7078
var pk, ck int32
@@ -77,4 +85,6 @@ func Example_batch() {
7785
}
7886
// 1 2 1.2
7987
// 1 3 1.3
88+
// 1 4 1.4
89+
// 1 5 1.5
8090
}

session.go

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

734+
func (b *Batch) Exec() error {
735+
iter := b.session.executeBatch(b)
736+
return iter.Close()
737+
}
738+
734739
func (s *Session) executeBatch(batch *Batch) *Iter {
735740
// fail fast
736741
if s.Closed() {
@@ -1860,8 +1865,9 @@ func (b *Batch) SpeculativeExecutionPolicy(sp SpeculativeExecutionPolicy) *Batch
18601865
}
18611866

18621867
// Query adds the query to the batch operation
1863-
func (b *Batch) Query(stmt string, args ...interface{}) {
1868+
func (b *Batch) Query(stmt string, args ...interface{}) *Batch {
18641869
b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
1870+
return b
18651871
}
18661872

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

0 commit comments

Comments
 (0)