Skip to content

Commit

Permalink
use BatchUpdateWithOptions instead of Update to apply DML (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwata authored Feb 28, 2022
1 parent ce72f07 commit 9f346f2
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pkg/spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,30 @@ func (c *Client) ApplyDMLFile(ctx context.Context, ddl []byte, partitioned bool,
func (c *Client) ApplyDML(ctx context.Context, statements []string, priority PriorityType) (int64, error) {
p := priorityPBOf(priority)
numAffectedRows := int64(0)
_, err := c.spannerClient.ReadWriteTransactionWithOptions(ctx, func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
for _, s := range statements {
num, err := tx.Update(ctx, spanner.Statement{
SQL: s,
_, err := c.spannerClient.ReadWriteTransactionWithOptions(
ctx,
func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
stmts := make([]spanner.Statement, len(statements))
for i, s := range statements {
stmts[i] = spanner.Statement{SQL: s}
}
counts, err := tx.BatchUpdateWithOptions(ctx, stmts, spanner.QueryOptions{
Priority: p,
})
if err != nil {
return err
}
numAffectedRows += num
}
return nil
}, spanner.TransactionOptions{
CommitPriority: p,
})

for _, num := range counts {
numAffectedRows += num
}

return nil
},
spanner.TransactionOptions{
CommitPriority: p,
},
)
if err != nil {
return 0, &Error{
Code: ErrorCodeUpdateDML,
Expand Down

0 comments on commit 9f346f2

Please sign in to comment.