Class: Google::Cloud::Spanner::Commit
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Commit
- Defined in:
- lib/google/cloud/spanner/commit.rb
Overview
# Commit
Accepts mutations for execution within a transaction. All writes will 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 Google::Cloud::Spanner::Client#commit completes.
Instance Method Summary collapse
-
#delete(table, keys = []) ⇒ Object
Deletes rows from a table.
-
#initialize ⇒ Commit
constructor
A new instance of Commit.
-
#insert(table, *rows) ⇒ Object
Inserts new rows in a table.
- #mutations ⇒ Object
-
#replace(table, *rows) ⇒ Object
Inserts or replaces rows in a table.
-
#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 ⇒ Commit
Returns a new instance of Commit.
47 48 49 |
# File 'lib/google/cloud/spanner/commit.rb', line 47 def initialize @mutations = [] 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 Google::Cloud::Spanner::Client#commit completes.
307 308 309 310 311 312 313 314 315 |
# File 'lib/google/cloud/spanner/commit.rb', line 307 def delete table, keys = [] @mutations += [ Google::Spanner::V1::Mutation.new( delete: Google::Spanner::V1::Mutation::Delete.new( table: table, key_set: key_set(keys)) ) ] keys 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 Google::Cloud::Spanner::Client#commit completes.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/google/cloud/spanner/commit.rb', line 151 def insert table, *rows rows = Array(rows).flatten return rows if rows.empty? rows.delete_if(&:nil?) rows.delete_if(&:empty?) @mutations += rows.map do |row| Google::Spanner::V1::Mutation.new( insert: Google::Spanner::V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.raw_to_value(row.values).list_value] ) ) end rows end |
#mutations ⇒ Object
318 319 320 |
# File 'lib/google/cloud/spanner/commit.rb', line 318 def mutations @mutations 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 Google::Cloud::Spanner::Client#commit completes.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/google/cloud/spanner/commit.rb', line 267 def replace table, *rows rows = Array(rows).flatten return rows if rows.empty? rows.delete_if(&:nil?) rows.delete_if(&:empty?) @mutations += rows.map do |row| Google::Spanner::V1::Mutation.new( replace: Google::Spanner::V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.raw_to_value(row.values).list_value] ) ) end rows 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 Google::Cloud::Spanner::Client#commit completes.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/google/cloud/spanner/commit.rb', line 208 def update table, *rows rows = Array(rows).flatten return rows if rows.empty? rows.delete_if(&:nil?) rows.delete_if(&:empty?) @mutations += rows.map do |row| Google::Spanner::V1::Mutation.new( update: Google::Spanner::V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.raw_to_value(row.values).list_value] ) ) end 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 Google::Cloud::Spanner::Client#commit completes.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/google/cloud/spanner/commit.rb', line 93 def upsert table, *rows rows = Array(rows).flatten return rows if rows.empty? rows.delete_if(&:nil?) rows.delete_if(&:empty?) @mutations += rows.map do |row| Google::Spanner::V1::Mutation.new( insert_or_update: Google::Spanner::V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.raw_to_value(row.values).list_value] ) ) end rows end |