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.
-
#insert(table, *rows) ⇒ Object
Inserts new rows in a table.
-
#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.
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.
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/google/cloud/spanner/commit.rb', line 311 def delete table, keys = [] @mutations += [ V1::Mutation.new( delete: 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.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/google/cloud/spanner/commit.rb', line 153 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| V1::Mutation.new( insert: V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.object_to_grpc_value(row.values).list_value] ) ) end rows 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.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/google/cloud/spanner/commit.rb', line 271 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| V1::Mutation.new( replace: V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.object_to_grpc_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.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/google/cloud/spanner/commit.rb', line 211 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| V1::Mutation.new( update: V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.object_to_grpc_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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/google/cloud/spanner/commit.rb', line 94 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| V1::Mutation.new( insert_or_update: V1::Mutation::Write.new( table: table, columns: row.keys.map(&:to_s), values: [Convert.object_to_grpc_value(row.values).list_value] ) ) end rows end |