Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from darrenmcc/tx
Browse files Browse the repository at this point in the history
Transaction support
  • Loading branch information
jprobinson committed Mar 29, 2018
2 parents c81d64c + db1a62f commit fd1d1d5
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions spannerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,28 @@ func (c *Client) Close(ctx context.Context) error {
return nil
}

// BeginTransaction starts a new transaction.
func (s *Session) BeginTransaction(ctx context.Context, opts *spanner.BeginTransactionRequest) (*spanner.Transaction, error) {
return s.sess.BeginTransaction(s.name, opts).Context(ctx).Do()
}

// Commit commits a transaction. The request includes the mutations to be applied to
// rows in the database.
// rows in the database. Including opts signals a one-off query, whereas including txID
// signals this commit is part of a larger transaction.
// This function wraps https://godoc.org/google.golang.org/api/spanner/v1#ProjectsInstancesDatabasesSessionsService.Commit
func (s *Session) Commit(ctx context.Context, mutations []*spanner.Mutation, opts *spanner.TransactionOptions) (*spanner.CommitResponse, error) {
func (s *Session) Commit(ctx context.Context, mutations []*spanner.Mutation, opts *spanner.TransactionOptions, txID string) (*spanner.CommitResponse, error) {
return s.sess.Commit(s.name, &spanner.CommitRequest{
Mutations: mutations, SingleUseTransaction: opts,
Mutations: mutations,
SingleUseTransaction: opts,
TransactionId: txID,
}).Context(ctx).Do()
}

// ExecuteSQL executes an SQL query, returning all rows in a single reply.
// It can be called within a transaction by including a TransactionSelector
// with its Id field set.
// This function wraps https://godoc.org/google.golang.org/api/spanner/v1#ProjectsInstancesDatabasesSessionsExecuteSqlCall
func (s *Session) ExecuteSQL(ctx context.Context, params []*Param, sql, queryMode string) (*spanner.ResultSet, error) {
func (s *Session) ExecuteSQL(ctx context.Context, params []*Param, sql, queryMode string, tx *spanner.TransactionSelector) (*spanner.ResultSet, error) {
var (
pTypes = map[string]spanner.Type{}
pVals = map[string]interface{}{}
Expand All @@ -184,10 +194,11 @@ func (s *Session) ExecuteSQL(ctx context.Context, params []*Param, sql, queryMod
return nil, errors.Wrap(err, "unable to encode query params")
}
res, err := s.sess.ExecuteSql(s.name, &spanner.ExecuteSqlRequest{
ParamTypes: pTypes,
Params: pJSON,
QueryMode: queryMode,
Sql: sql,
ParamTypes: pTypes,
Params: pJSON,
QueryMode: queryMode,
Sql: sql,
Transaction: tx,
}).Context(ctx).Do()
return res, errors.Wrap(err, "unable to execute query")
}
Expand Down

0 comments on commit fd1d1d5

Please sign in to comment.