Class: Google::Cloud::Spanner::Transaction
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Transaction
- Defined in:
- lib/google/cloud/spanner/transaction.rb
Overview
Transaction
A transaction in Cloud Spanner is a set of reads and writes that execute atomically at a single logical point in time across columns, rows, and tables in a database.
All changes are accumulated in memory until the block passed to Client#transaction completes. Transactions will be automatically retried when possible. See Client#transaction.
Instance Method Summary collapse
-
#batch_update {|batch_update| ... } ⇒ Array<Integer>
Executes DML statements in a batch.
-
#commit_timestamp ⇒ ColumnValue
Creates a column value object representing setting a field's value to the timestamp of the commit.
-
#delete(table, keys = []) ⇒ Object
Deletes rows from a table.
-
#execute_query(sql, params: nil, types: nil, query_options: nil) ⇒ Google::Cloud::Spanner::Results
(also: #execute, #query, #execute_sql)
Executes a SQL query.
-
#execute_update(sql, params: nil, types: nil, query_options: nil) ⇒ Integer
Executes a DML statement.
-
#fields(types) ⇒ Fields
Creates a configuration object (Fields) that may be provided to queries or used to create STRUCT objects.
-
#initialize ⇒ Transaction
constructor
A new instance of Transaction.
-
#insert(table, *rows) ⇒ Object
Inserts new rows in a table.
-
#range(beginning, ending, exclude_begin: false, exclude_end: false) ⇒ Google::Cloud::Spanner::Range
Creates a Cloud Spanner Range.
-
#read(table, columns, keys: nil, index: nil, limit: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
-
#replace(table, *rows) ⇒ Object
Inserts or replaces rows in a table.
-
#transaction_id ⇒ String
Identifier of the transaction results were run in.
-
#update(table, *rows) ⇒ Object
Updates existing rows in a table.
-
#upsert(table, *rows) ⇒ Object
(also: #save)
Inserts or updates rows in a table.
Constructor Details
#initialize ⇒ Transaction
Returns a new instance of Transaction.
80 81 82 83 |
# File 'lib/google/cloud/spanner/transaction.rb', line 80 def initialize @commit = Commit.new @seqno = 0 end |
Instance Method Details
#batch_update {|batch_update| ... } ⇒ Array<Integer>
Executes DML statements in a batch.
467 468 469 470 471 |
# File 'lib/google/cloud/spanner/transaction.rb', line 467 def batch_update &block ensure_session! @seqno += 1 session.batch_update tx_selector, @seqno, &block end |
#commit_timestamp ⇒ ColumnValue
Creates a column value object representing setting a field's value to the timestamp of the commit. (See Client#commit_timestamp)
This placeholder value can only be used for timestamp columns that have set the option "(allow_commit_timestamp=true)" in the schema.
886 887 888 |
# File 'lib/google/cloud/spanner/transaction.rb', line 886 def ColumnValue. end |
#delete(table, keys = []) ⇒ Object
Deletes rows from a table. Succeeds whether or not the specified rows were present.
All changes are accumulated in memory until the block passed to Client#transaction completes.
722 723 724 725 |
# File 'lib/google/cloud/spanner/transaction.rb', line 722 def delete table, keys = [] ensure_session! @commit.delete table, keys end |
#execute_query(sql, params: nil, types: nil, query_options: nil) ⇒ Google::Cloud::Spanner::Results Also known as: execute, query, execute_sql
Executes a SQL query.
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/google/cloud/spanner/transaction.rb', line 277 def execute_query sql, params: nil, types: nil, query_options: nil ensure_session! @seqno += 1 params, types = Convert.to_input_params_and_types params, types session.execute_query sql, params: params, types: types, transaction: tx_selector, seqno: @seqno, query_options: end |
#execute_update(sql, params: nil, types: nil, query_options: nil) ⇒ Integer
Executes a DML statement.
396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/google/cloud/spanner/transaction.rb', line 396 def execute_update sql, params: nil, types: nil, query_options: nil results = execute_query sql, params: params, types: types, query_options: # Stream all PartialResultSet to get ResultSetStats results.rows.to_a # Raise an error if there is not a row count returned if results.row_count.nil? raise Google::Cloud::InvalidArgumentError, "DML statement is invalid." end results.row_count end |
#fields(types) ⇒ Fields
Creates a configuration object (Fields) that may be provided to queries or used to create STRUCT objects. (The STRUCT will be represented by the Data class.) See Client#execute and/or Fields#struct.
For more information, see Data Types - Constructing a STRUCT.
825 826 827 |
# File 'lib/google/cloud/spanner/transaction.rb', line 825 def fields types Fields.new types end |
#insert(table, *rows) ⇒ Object
Inserts new rows in a table. If any of the rows already exist, the write or request fails with error AlreadyExistsError.
All changes are accumulated in memory until the block passed to Client#transaction completes.
604 605 606 607 |
# File 'lib/google/cloud/spanner/transaction.rb', line 604 def insert table, *rows ensure_session! @commit.insert table, rows end |
#range(beginning, ending, exclude_begin: false, exclude_end: false) ⇒ Google::Cloud::Spanner::Range
Creates a Cloud Spanner Range. This can be used in place of a Ruby Range when needing to exclude the beginning value.
858 859 860 861 862 |
# File 'lib/google/cloud/spanner/transaction.rb', line 858 def range beginning, ending, exclude_begin: false, exclude_end: false Range.new beginning, ending, exclude_begin: exclude_begin, exclude_end: exclude_end end |
#read(table, columns, keys: nil, index: nil, limit: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
507 508 509 510 511 512 513 514 515 |
# File 'lib/google/cloud/spanner/transaction.rb', line 507 def read table, columns, keys: nil, index: nil, limit: nil ensure_session! columns = Array(columns).map(&:to_s) keys = Convert.to_key_set keys session.read table, columns, keys: keys, index: index, limit: limit, transaction: tx_selector end |
#replace(table, *rows) ⇒ Object
Inserts or replaces rows in a table. If any of the rows already exist,
it is deleted, and the column values provided are inserted instead.
Unlike #upsert, this means any values not explicitly written become
NULL.
All changes are accumulated in memory until the block passed to Client#transaction completes.
696 697 698 699 |
# File 'lib/google/cloud/spanner/transaction.rb', line 696 def replace table, *rows ensure_session! @commit.replace table, rows end |
#transaction_id ⇒ String
Identifier of the transaction results were run in.
88 89 90 91 |
# File 'lib/google/cloud/spanner/transaction.rb', line 88 def transaction_id return nil if @grpc.nil? @grpc.id end |
#update(table, *rows) ⇒ Object
Updates existing rows in a table. If any of the rows does not already exist, the request fails with error NotFoundError.
All changes are accumulated in memory until the block passed to Client#transaction completes.
649 650 651 652 |
# File 'lib/google/cloud/spanner/transaction.rb', line 649 def update table, *rows ensure_session! @commit.update table, rows end |
#upsert(table, *rows) ⇒ Object Also known as: save
Inserts or updates rows in a table. If any of the rows already exist, then its column values are overwritten with the ones provided. Any column values not explicitly written are preserved.
All changes are accumulated in memory until the block passed to Client#transaction completes.
558 559 560 561 |
# File 'lib/google/cloud/spanner/transaction.rb', line 558 def upsert table, *rows ensure_session! @commit.upsert table, rows end |