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
-
#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(sql, params: nil, types: nil) ⇒ Google::Cloud::Spanner::Results
(also: #query)
Executes a SQL query.
-
#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.
-
#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 |
# File 'lib/google/cloud/spanner/transaction.rb', line 80 def initialize @commit = Commit.new end |
Instance Method Details
#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.
668 669 670 |
# File 'lib/google/cloud/spanner/transaction.rb', line 668 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.
504 505 506 507 |
# File 'lib/google/cloud/spanner/transaction.rb', line 504 def delete table, keys = [] ensure_session! @commit.delete table, keys end |
#execute(sql, params: nil, types: nil) ⇒ Google::Cloud::Spanner::Results Also known as: query
Executes a SQL query.
245 246 247 248 249 250 251 252 |
# File 'lib/google/cloud/spanner/transaction.rb', line 245 def execute sql, params: nil, types: nil ensure_session! params, types = Convert.to_input_params_and_types params, types session.execute sql, params: params, types: types, transaction: tx_selector 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.
607 608 609 |
# File 'lib/google/cloud/spanner/transaction.rb', line 607 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.
386 387 388 389 |
# File 'lib/google/cloud/spanner/transaction.rb', line 386 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.
640 641 642 643 644 |
# File 'lib/google/cloud/spanner/transaction.rb', line 640 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.
289 290 291 292 293 294 295 296 297 |
# File 'lib/google/cloud/spanner/transaction.rb', line 289 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.
478 479 480 481 |
# File 'lib/google/cloud/spanner/transaction.rb', line 478 def replace table, *rows ensure_session! @commit.replace table, rows end |
#transaction_id ⇒ String
Identifier of the transaction results were run in.
87 88 89 90 |
# File 'lib/google/cloud/spanner/transaction.rb', line 87 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.
431 432 433 434 |
# File 'lib/google/cloud/spanner/transaction.rb', line 431 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.
340 341 342 343 |
# File 'lib/google/cloud/spanner/transaction.rb', line 340 def upsert table, *rows ensure_session! @commit.upsert table, rows end |