class Google::Cloud::Spanner::BatchUpdate

# BatchUpdate

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

See {Google::Cloud::Spanner::Transaction#batch_update}.

Attributes

statements[R]

@private

Public Class Methods

new() click to toggle source

@private

# File lib/google/cloud/spanner/batch_update.rb, line 34
def initialize
  @statements = []
end

Public Instance Methods

batch_update(sql, params: nil, types: nil) click to toggle source

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

@param [String] sql The DML statement string. See [Query

syntax](https://cloud.google.com/spanner/docs/query-syntax).

The DML statement string can contain parameter placeholders. A
parameter placeholder consists of "@" followed by the parameter
name. Parameter names consist of any combination of letters,
numbers, and underscores.

@param [Hash] params Parameters for the DML statement string. The

parameter placeholders, minus the "@", are the the hash keys, and
the literal values are the hash values. If the query string contains
something like "WHERE id > @msg_id", then the params must contain
something like `:msg_id => 1`.

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. |
| `STRUCT`    | `Hash`, {Data} | |

See [Data
types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

See [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).

@param [Hash] types Types of the SQL parameters in `params`. It is not

always possible for Cloud Spanner to infer the right SQL type from a
value in `params`. In these cases, the `types` hash can be used to
specify the exact SQL type for some or all of the SQL query
parameters.

The keys of the hash should be query string parameter placeholders,
minus the "@". The values of the hash should be Cloud Spanner type
codes from the following list:

* `:BOOL`
* `:BYTES`
* `:DATE`
* `:FLOAT64`
* `:INT64`
* `:STRING`
* `:TIMESTAMP`
* `Array` - Lists are specified by providing the type code in an
  array. For example, an array of integers are specified as
  `[:INT64]`.
* {Fields} - Nested Structs are specified by providing a Fields
  object.

@example

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

@example 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
# File lib/google/cloud/spanner/batch_update.rb, line 138
def batch_update sql, params: nil, types: nil
  @statements << Statement.new(sql, params: params, types: types)
  true
end