Class: Google::Cloud::Spanner::BatchUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/batch_update.rb

Overview

BatchUpdate

Accepts DML statements and optional parameters and types of the parameters for a batch update.

See Transaction#batch_update.

Instance Method Summary collapse

Instance Method Details

#batch_update(sql, params: nil, types: nil) ⇒ Object

Adds a DML statement to a batch update. See Transaction#batch_update.

Examples:

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
db = spanner.client "my-instance", "my-database"

db.transaction do |tx|
  begin
    row_counts = tx.batch_update do |b|
      statement_count = b.batch_update(
        "UPDATE users SET name = 'Charlie' WHERE id = 1"
      )
    end
    puts row_counts.inspect
  rescue Google::Cloud::Spanner::BatchUpdateError => err
    puts err.cause.message
    puts err.row_counts.inspect
  end
end

Update using SQL parameters:

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
db = spanner.client "my-instance", "my-database"

db.transaction do |tx|
  begin
    row_counts = tx.batch_update do |b|
      statement_count = b.batch_update(
        "UPDATE users SET name = 'Charlie' WHERE id = 1",
        params: { id: 1, name: "Charlie" }
      )
    end
    puts row_counts.inspect
  rescue Google::Cloud::Spanner::BatchUpdateError => err
    puts err.cause.message
    puts err.row_counts.inspect
  end
end


140
141
142
143
# File 'lib/google/cloud/spanner/batch_update.rb', line 140

def batch_update sql, params: nil, types: nil
  @statements << Statement.new(sql, params: params, types: types)
  true
end