Skip to content

Commit b12fcac

Browse files
committed
Exec() method for batch was added & Query() method was refactored
1 parent 974fa12 commit b12fcac

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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)