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 Attribute Summary collapse
Class Method Summary collapse
-
.from_grpc(grpc, session) ⇒ Object
Google::Spanner::V1::Transaction.
Instance Method Summary collapse
-
#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_for(table) ⇒ Hash, Array
Returns the field names and types for a table.
-
#idle_since?(duration) ⇒ Boolean
Determines if the transaction has been idle longer than the given duration.
-
#initialize ⇒ Transaction
constructor
A new instance of Transaction.
-
#insert(table, *rows) ⇒ Object
Inserts new rows in a table.
-
#keepalive! ⇒ Object
Keeps the transaction current by creating a new transaction.
-
#mutations ⇒ Object
All of the mutations created in the transaction block.
-
#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.
-
#release! ⇒ Object
Permanently deletes the transaction and session.
-
#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.
79 80 81 |
# File 'lib/google/cloud/spanner/transaction.rb', line 79 def initialize @commit = Commit.new end |
Instance Attribute Details
#session ⇒ Object
77 78 79 |
# File 'lib/google/cloud/spanner/transaction.rb', line 77 def session @session end |
Class Method Details
.from_grpc(grpc, session) ⇒ Object
Google::Spanner::V1::Transaction.
532 533 534 535 536 537 |
# File 'lib/google/cloud/spanner/transaction.rb', line 532 def self.from_grpc grpc, session new.tap do |s| s.instance_variable_set :@grpc, grpc s.instance_variable_set :@session, session end end |
Instance Method Details
#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.
431 432 433 434 |
# File 'lib/google/cloud/spanner/transaction.rb', line 431 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.
Arguments can be passed using ‘params`, Ruby types are mapped to Spanner types as follows:
| Spanner | Ruby | Notes | |————-|—————-|—| | ‘BOOL` | `true`/`false` | | | `INT64` | `Integer` | | | `FLOAT64` | `Float` | | | `STRING` | `String` | | | `DATE` | `Date` | | | `TIMESTAMP` | `Time`, `DateTime` | | | `BYTES` | `File`, `IO`, `StringIO`, or similar | | | `ARRAY` | `Array` | Nested arrays are not supported. |
See [Data types](cloud.google.com/spanner/docs/data-definition-language#data_types).
179 180 181 182 183 |
# File 'lib/google/cloud/spanner/transaction.rb', line 179 def execute sql, params: nil, types: nil ensure_session! session.execute sql, params: params, types: types, transaction: tx_selector end |
#fields_for(table) ⇒ Hash, Array
Returns the field names and types for a table.
459 460 461 |
# File 'lib/google/cloud/spanner/transaction.rb', line 459 def fields_for table execute("SELECT * FROM #{table} WHERE 1 = 0").fields end |
#idle_since?(duration) ⇒ Boolean
Determines if the transaction has been idle longer than the given duration.
518 519 520 |
# File 'lib/google/cloud/spanner/transaction.rb', line 518 def idle_since? duration session.idle_since? duration 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.
313 314 315 316 |
# File 'lib/google/cloud/spanner/transaction.rb', line 313 def insert table, *rows ensure_session! @commit.insert table, rows end |
#keepalive! ⇒ Object
Keeps the transaction current by creating a new transaction.
501 502 503 504 |
# File 'lib/google/cloud/spanner/transaction.rb', line 501 def keepalive! ensure_session! @grpc = session.create_transaction.instance_variable_get :@grpc end |
#mutations ⇒ Object
All of the mutations created in the transaction block.
525 526 527 |
# File 'lib/google/cloud/spanner/transaction.rb', line 525 def mutations @commit.mutations 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.
492 493 494 495 496 |
# File 'lib/google/cloud/spanner/transaction.rb', line 492 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.
220 221 222 223 224 |
# File 'lib/google/cloud/spanner/transaction.rb', line 220 def read table, columns, keys: nil, index: nil, limit: nil ensure_session! session.read table, columns, keys: keys, index: index, limit: limit, transaction: tx_selector end |
#release! ⇒ Object
Permanently deletes the transaction and session.
509 510 511 512 |
# File 'lib/google/cloud/spanner/transaction.rb', line 509 def release! ensure_session! session.release! 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.
405 406 407 408 |
# File 'lib/google/cloud/spanner/transaction.rb', line 405 def replace table, *rows ensure_session! @commit.replace table, rows end |
#transaction_id ⇒ String
Identifier of the transaction results were run in.
86 87 88 89 |
# File 'lib/google/cloud/spanner/transaction.rb', line 86 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.
358 359 360 361 |
# File 'lib/google/cloud/spanner/transaction.rb', line 358 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.
267 268 269 270 |
# File 'lib/google/cloud/spanner/transaction.rb', line 267 def upsert table, *rows ensure_session! @commit.upsert table, rows end |