Skip to content

Commit 03d4818

Browse files
authored
feat: add method for getting batched statements (#318)
* feat: add method for getting batched statements Adds a GetBatchedStatements function that returns the currently buffered statements that will be executed if RunBatch is called. This gives applications the possibility of inspecting the list of buffered statements before actually executing them. * chore: use slices.Clone
1 parent 5cb6fc6 commit 03d4818

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

driver.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math/big"
2323
"reflect"
2424
"regexp"
25+
"slices"
2526
"strconv"
2627
"strings"
2728
"sync"
@@ -535,6 +536,10 @@ type SpannerConn interface {
535536
InDDLBatch() bool
536537
// InDMLBatch returns true if the connection is currently in a DML batch.
537538
InDMLBatch() bool
539+
// GetBatchedStatements returns a copy of the statements that are currently
540+
// buffered to be executed as a DML or DDL batch. It returns an empty slice
541+
// if no batch is active, or if there are no statements buffered.
542+
GetBatchedStatements() []spanner.Statement
538543

539544
// RetryAbortsInternally returns true if the connection automatically
540545
// retries all aborted transactions.
@@ -755,6 +760,13 @@ func (c *conn) InDMLBatch() bool {
755760
return (c.batch != nil && c.batch.tp == dml) || (c.inReadWriteTransaction() && c.tx.(*readWriteTransaction).batch != nil)
756761
}
757762

763+
func (c *conn) GetBatchedStatements() []spanner.Statement {
764+
if c.batch == nil || c.batch.statements == nil {
765+
return []spanner.Statement{}
766+
}
767+
return slices.Clone(c.batch.statements)
768+
}
769+
758770
func (c *conn) inBatch() bool {
759771
return c.InDDLBatch() || c.InDMLBatch()
760772
}

driver_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"database/sql/driver"
2020
"net"
21+
"reflect"
2122
"testing"
2223
"time"
2324

@@ -425,6 +426,52 @@ func TestConn_NonDmlStatementsInDmlBatch(t *testing.T) {
425426
}
426427
}
427428

429+
func TestConn_GetBatchedStatements(t *testing.T) {
430+
t.Parallel()
431+
432+
ctx := context.Background()
433+
c := &conn{}
434+
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
435+
t.Fatal("conn should return an empty slice when no batch is active")
436+
}
437+
if err := c.StartBatchDDL(); err != nil {
438+
t.Fatal(err)
439+
}
440+
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
441+
t.Fatal("conn should return an empty slice when a batch contains no statements")
442+
}
443+
if _, err := c.ExecContext(ctx, "create table table1", []driver.NamedValue{}); err != nil {
444+
t.Fatal(err)
445+
}
446+
if _, err := c.ExecContext(ctx, "create table table2", []driver.NamedValue{}); err != nil {
447+
t.Fatal(err)
448+
}
449+
batchedStatements := c.GetBatchedStatements()
450+
if !reflect.DeepEqual([]spanner.Statement{
451+
{SQL: "create table table1", Params: map[string]interface{}{}},
452+
{SQL: "create table table2", Params: map[string]interface{}{}},
453+
}, batchedStatements) {
454+
t.Errorf("unexpected batched statements: %v", batchedStatements)
455+
}
456+
457+
// Changing the returned slice does not change the batched statements.
458+
batchedStatements[0] = spanner.Statement{SQL: "drop table table1"}
459+
batchedStatements2 := c.GetBatchedStatements()
460+
if !reflect.DeepEqual([]spanner.Statement{
461+
{SQL: "create table table1", Params: map[string]interface{}{}},
462+
{SQL: "create table table2", Params: map[string]interface{}{}},
463+
}, batchedStatements2) {
464+
t.Errorf("unexpected batched statements: %v", batchedStatements2)
465+
}
466+
467+
if err := c.AbortBatch(); err != nil {
468+
t.Fatal(err)
469+
}
470+
if !reflect.DeepEqual(c.GetBatchedStatements(), []spanner.Statement{}) {
471+
t.Fatal("conn should return an empty slice when no batch is active")
472+
}
473+
}
474+
428475
func TestConn_GetCommitTimestampAfterAutocommitDml(t *testing.T) {
429476
want := time.Now()
430477
c := &conn{

0 commit comments

Comments
 (0)