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(request_options: nil, call_options: nil) {|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, request_options: nil, call_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, request_options: nil, call_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, request_options: nil, call_options: 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/google/cloud/spanner/transaction.rb', line 88 def initialize @commit = Commit.new @seqno = 0 # Mutex to enfore thread safety for transaction creation and query executions. # # This mutex protects two things: # (1) the generation of sequence numbers # (2) the creation of transactions. # # Specifically, @seqno is protected so it always reflects the last sequence number # generated and provided to an operation in any thread. Any acquisition of a # sequence number must be synchronized. # # Furthermore, @grpc is protected such that it either is nil if the # transaction has not yet been created, or references the transaction # resource if the transaction has been created. Any operation that could # create a transaction must be synchronized, and any logic that depends on # the state of transaction creation must also be synchronized. @mutex = Mutex.new end |
Instance Method Details
#batch_update(request_options: nil, call_options: nil) {|batch_update| ... } ⇒ Array<Integer>
Executes DML statements in a batch.
645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/google/cloud/spanner/transaction.rb', line 645 def batch_update request_options: nil, call_options: nil, &block ensure_session! = safe_execute do |seqno| response = session.batch_update tx_selector, seqno, request_options: , call_options: , &block batch_update_results = BatchUpdateResults.new response @grpc ||= batch_update_results.transaction batch_update_results.row_counts end 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.
1117 1118 1119 |
# File 'lib/google/cloud/spanner/transaction.rb', line 1117 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.
951 952 953 954 |
# File 'lib/google/cloud/spanner/transaction.rb', line 951 def delete table, keys = [] ensure_session! @commit.delete table, keys end |
#execute_query(sql, params: nil, types: nil, query_options: nil, request_options: nil, call_options: nil) ⇒ Google::Cloud::Spanner::Results Also known as: execute, query, execute_sql
Executes a SQL query.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/google/cloud/spanner/transaction.rb', line 361 def execute_query sql, params: nil, types: nil, query_options: nil, request_options: nil, call_options: nil ensure_session! params, types = Convert.to_input_params_and_types params, types = route_to_leader = LARHeaders.execute_query true safe_execute do |seqno| results = session.execute_query sql, params: params, types: types, transaction: tx_selector, seqno: seqno, query_options: , request_options: , call_options: , route_to_leader: route_to_leader @grpc ||= results.transaction results end end |
#execute_update(sql, params: nil, types: nil, query_options: nil, request_options: nil, call_options: nil) ⇒ Integer
Executes a DML statement.
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/google/cloud/spanner/transaction.rb', line 545 def execute_update sql, params: nil, types: nil, query_options: nil, request_options: nil, call_options: nil results = execute_query sql, params: params, types: types, query_options: , request_options: , call_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.
1056 1057 1058 |
# File 'lib/google/cloud/spanner/transaction.rb', line 1056 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.
829 830 831 832 |
# File 'lib/google/cloud/spanner/transaction.rb', line 829 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.
1089 1090 1091 1092 1093 |
# File 'lib/google/cloud/spanner/transaction.rb', line 1089 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, request_options: nil, call_options: nil) ⇒ Google::Cloud::Spanner::Results
Read rows from a database table, as a simple alternative to #execute_query.
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/google/cloud/spanner/transaction.rb', line 718 def read table, columns, keys: nil, index: nil, limit: nil, request_options: nil, call_options: nil ensure_session! columns = Array(columns).map(&:to_s) keys = Convert.to_key_set keys = route_to_leader = LARHeaders.read true safe_execute do results = session.read table, columns, keys: keys, index: index, limit: limit, transaction: tx_selector, request_options: , call_options: , route_to_leader: route_to_leader @grpc ||= results.transaction results end 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.
925 926 927 928 |
# File 'lib/google/cloud/spanner/transaction.rb', line 925 def replace table, *rows ensure_session! @commit.replace table, rows end |
#transaction_id ⇒ String
Identifier of the transaction results were run in.
113 114 115 116 117 |
# File 'lib/google/cloud/spanner/transaction.rb', line 113 def transaction_id return @grpc.id if existing_transaction? safe_begin_transaction @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.
876 877 878 879 |
# File 'lib/google/cloud/spanner/transaction.rb', line 876 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.
781 782 783 784 |
# File 'lib/google/cloud/spanner/transaction.rb', line 781 def upsert table, *rows ensure_session! @commit.upsert table, rows end |