google-cloud-spanner 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/google/cloud/spanner.rb +33 -0
- data/lib/google/cloud/spanner/batch_client.rb +74 -0
- data/lib/google/cloud/spanner/batch_snapshot.rb +127 -42
- data/lib/google/cloud/spanner/client.rb +143 -33
- data/lib/google/cloud/spanner/commit.rb +5 -5
- data/lib/google/cloud/spanner/convert.rb +125 -110
- data/lib/google/cloud/spanner/data.rb +39 -6
- data/lib/google/cloud/spanner/fields.rb +252 -60
- data/lib/google/cloud/spanner/results.rb +4 -3
- data/lib/google/cloud/spanner/session.rb +87 -25
- data/lib/google/cloud/spanner/snapshot.rb +168 -25
- data/lib/google/cloud/spanner/transaction.rb +165 -25
- data/lib/google/cloud/spanner/v1/doc/overview.rb +2 -2
- data/lib/google/cloud/spanner/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab2f6de02b2dc38f872be9ecf0b9a538353c23457d7d82277bab5e636420b2a4
|
4
|
+
data.tar.gz: 31e72548e11120e24de11bf932fc5c575478ba5732e21b4610ac42fa3af695ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef87329e6ccc9269a894aafaa179016269ed61a9ebcf338a51faa9a43027fdda825d05b534bda2946a101f9896dc5d98b861e714fc24aa64e5585423c0489548
|
7
|
+
data.tar.gz: 5d7e360cf9678935eb7d295bca0f808c942a217310d5bfb81dbc0eb3357020f2f32e8d40ef2ffa838c8faf56b882592b5d373e4261bb6ec67ec5acdb867920d4
|
data/lib/google/cloud/spanner.rb
CHANGED
@@ -40,6 +40,39 @@ module Google
|
|
40
40
|
# [Authentication
|
41
41
|
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
42
42
|
#
|
43
|
+
# ## Enabling Logging
|
44
|
+
#
|
45
|
+
# To enable logging for this library, set the logger for the underlying
|
46
|
+
# [gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library. The
|
47
|
+
# logger that you set may be a Ruby stdlib
|
48
|
+
# [`Logger`](https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html)
|
49
|
+
# as shown below, or a
|
50
|
+
# [`Google::Cloud::Logging::Logger`](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-logging/latest/google/cloud/logging/logger)
|
51
|
+
# that will write logs to [Stackdriver
|
52
|
+
# Logging](https://cloud.google.com/logging/). See
|
53
|
+
# [grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb)
|
54
|
+
# and the gRPC
|
55
|
+
# [spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb)
|
56
|
+
# for additional information.
|
57
|
+
#
|
58
|
+
# Configuring a Ruby stdlib logger:
|
59
|
+
#
|
60
|
+
# ```ruby
|
61
|
+
# require "logger"
|
62
|
+
#
|
63
|
+
# module MyLogger
|
64
|
+
# LOGGER = Logger.new $stderr, level: Logger::WARN
|
65
|
+
# def logger
|
66
|
+
# LOGGER
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# # Define a gRPC module-level logger method before grpc/logconfig.rb loads.
|
71
|
+
# module GRPC
|
72
|
+
# extend MyLogger
|
73
|
+
# end
|
74
|
+
# ```
|
75
|
+
#
|
43
76
|
# ## Creating instances
|
44
77
|
#
|
45
78
|
# When you first use Cloud Spanner, you must create an instance, which is an
|
@@ -272,6 +272,80 @@ module Google
|
|
272
272
|
Partition.load serialized_partition
|
273
273
|
end
|
274
274
|
|
275
|
+
##
|
276
|
+
# Creates a configuration object ({Fields}) that may be provided to
|
277
|
+
# queries or used to create STRUCT objects. (The STRUCT will be
|
278
|
+
# represented by the {Data} class.) See {Client#execute} and/or
|
279
|
+
# {Fields#struct}.
|
280
|
+
#
|
281
|
+
# For more information, see [Data Types - Constructing a
|
282
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
283
|
+
#
|
284
|
+
# @param [Array, Hash] types Accepts an array or hash types.
|
285
|
+
#
|
286
|
+
# Arrays can contain just the type value, or a sub-array of the
|
287
|
+
# field's name and type value. Hash keys must contain the field name
|
288
|
+
# as a `Symbol` or `String`, or the field position as an `Integer`.
|
289
|
+
# Hash values must contain the type value. If a Hash is used the
|
290
|
+
# fields will be created using the same order as the Hash keys.
|
291
|
+
#
|
292
|
+
# Supported type values incude:
|
293
|
+
#
|
294
|
+
# * `:BOOL`
|
295
|
+
# * `:BYTES`
|
296
|
+
# * `:DATE`
|
297
|
+
# * `:FLOAT64`
|
298
|
+
# * `:INT64`
|
299
|
+
# * `:STRING`
|
300
|
+
# * `:TIMESTAMP`
|
301
|
+
# * `Array` - Lists are specified by providing the type code in an
|
302
|
+
# array. For example, an array of integers are specified as
|
303
|
+
# `[:INT64]`.
|
304
|
+
# * {Fields} - Nested Structs are specified by providing a Fields
|
305
|
+
# object.
|
306
|
+
#
|
307
|
+
# @return [Fields] The fields of the given types.
|
308
|
+
#
|
309
|
+
# @example Create a STRUCT value with named fields using Fields object:
|
310
|
+
# require "google/cloud/spanner"
|
311
|
+
#
|
312
|
+
# spanner = Google::Cloud::Spanner.new
|
313
|
+
#
|
314
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
315
|
+
#
|
316
|
+
# named_type = batch_client.fields(
|
317
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
318
|
+
# )
|
319
|
+
# named_data = named_type.struct(
|
320
|
+
# { id: 42, name: nil, active: false }
|
321
|
+
# )
|
322
|
+
#
|
323
|
+
# @example Create a STRUCT value with anonymous field names:
|
324
|
+
# require "google/cloud/spanner"
|
325
|
+
#
|
326
|
+
# spanner = Google::Cloud::Spanner.new
|
327
|
+
#
|
328
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
329
|
+
#
|
330
|
+
# anon_type = batch_client.fields [:INT64, :STRING, :BOOL]
|
331
|
+
# anon_data = anon_type.struct [42, nil, false]
|
332
|
+
#
|
333
|
+
# @example Create a STRUCT value with duplicate field names:
|
334
|
+
# require "google/cloud/spanner"
|
335
|
+
#
|
336
|
+
# spanner = Google::Cloud::Spanner.new
|
337
|
+
#
|
338
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
339
|
+
#
|
340
|
+
# dup_type = batch_client.fields(
|
341
|
+
# [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
|
342
|
+
# )
|
343
|
+
# dup_data = dup_type.struct [42, nil, false]
|
344
|
+
#
|
345
|
+
def fields types
|
346
|
+
Fields.new types
|
347
|
+
end
|
348
|
+
|
275
349
|
##
|
276
350
|
# Creates a Spanner Range. This can be used in place of a Ruby Range
|
277
351
|
# when needing to exclude the beginning value.
|
@@ -112,23 +112,35 @@ module Google
|
|
112
112
|
# placeholder consists of "@" followed by the parameter name.
|
113
113
|
# Parameter names consist of any combination of letters, numbers, and
|
114
114
|
# underscores.
|
115
|
-
# @param [Integer] partition_size_bytes The desired data size for each
|
116
|
-
# partition generated. This is only a hint. The actual size of each
|
117
|
-
# partition may be smaller or larger than this size request.
|
118
|
-
# @param [Integer] max_partitions The desired maximum number of
|
119
|
-
# partitions to return. For example, this may be set to the number of
|
120
|
-
# workers available. This is only a hint and may provide different
|
121
|
-
# results based on the request.
|
122
115
|
# @param [Hash] params SQL parameters for the query string. The
|
123
116
|
# parameter placeholders, minus the "@", are the the hash keys, and
|
124
117
|
# the literal values are the hash values. If the query string contains
|
125
118
|
# something like "WHERE id > @msg_id", then the params must contain
|
126
119
|
# something like `:msg_id => 1`.
|
120
|
+
#
|
121
|
+
# Ruby types are mapped to Spanner types as follows:
|
122
|
+
#
|
123
|
+
# | Spanner | Ruby | Notes |
|
124
|
+
# |-------------|----------------|---|
|
125
|
+
# | `BOOL` | `true`/`false` | |
|
126
|
+
# | `INT64` | `Integer` | |
|
127
|
+
# | `FLOAT64` | `Float` | |
|
128
|
+
# | `STRING` | `String` | |
|
129
|
+
# | `DATE` | `Date` | |
|
130
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
131
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
132
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
133
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
134
|
+
#
|
135
|
+
# See [Data
|
136
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
137
|
+
#
|
138
|
+
# See [Data Types - Constructing a
|
139
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
127
140
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
128
141
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
129
|
-
# value in `params`. In these cases, the `types` hash
|
130
|
-
# specify the
|
131
|
-
# parameters.
|
142
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
143
|
+
# specify the SQL type for these values.
|
132
144
|
#
|
133
145
|
# The keys of the hash should be query string parameter placeholders,
|
134
146
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -141,13 +153,20 @@ module Google
|
|
141
153
|
# * `:INT64`
|
142
154
|
# * `:STRING`
|
143
155
|
# * `:TIMESTAMP`
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
156
|
+
# * `Array` - Lists are specified by providing the type code in an
|
157
|
+
# array. For example, an array of integers are specified as
|
158
|
+
# `[:INT64]`.
|
159
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
160
|
+
# specified using a {Fields} object.
|
149
161
|
#
|
150
162
|
# Types are optional.
|
163
|
+
# @param [Integer] partition_size_bytes The desired data size for each
|
164
|
+
# partition generated. This is only a hint. The actual size of each
|
165
|
+
# partition may be smaller or larger than this size request.
|
166
|
+
# @param [Integer] max_partitions The desired maximum number of
|
167
|
+
# partitions to return. For example, this may be set to the number of
|
168
|
+
# workers available. This is only a hint and may provide different
|
169
|
+
# results based on the request.
|
151
170
|
#
|
152
171
|
# @return [Array<Google::Cloud::Spanner::Partition>] The partitions
|
153
172
|
# created by the query partition.
|
@@ -342,23 +361,6 @@ module Google
|
|
342
361
|
##
|
343
362
|
# Executes a SQL query.
|
344
363
|
#
|
345
|
-
# Arguments can be passed using `params`, Ruby types are mapped to
|
346
|
-
# Spanner types as follows:
|
347
|
-
#
|
348
|
-
# | Spanner | Ruby | Notes |
|
349
|
-
# |-------------|----------------|---|
|
350
|
-
# | `BOOL` | `true`/`false` | |
|
351
|
-
# | `INT64` | `Integer` | |
|
352
|
-
# | `FLOAT64` | `Float` | |
|
353
|
-
# | `STRING` | `String` | |
|
354
|
-
# | `DATE` | `Date` | |
|
355
|
-
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
356
|
-
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
357
|
-
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
358
|
-
#
|
359
|
-
# See [Data
|
360
|
-
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
361
|
-
#
|
362
364
|
# @param [String] sql The SQL query string. See [Query
|
363
365
|
# syntax](https://cloud.google.com/spanner/docs/query-syntax).
|
364
366
|
#
|
@@ -371,11 +373,30 @@ module Google
|
|
371
373
|
# the literal values are the hash values. If the query string contains
|
372
374
|
# something like "WHERE id > @msg_id", then the params must contain
|
373
375
|
# something like `:msg_id => 1`.
|
376
|
+
#
|
377
|
+
# Ruby types are mapped to Spanner types as follows:
|
378
|
+
#
|
379
|
+
# | Spanner | Ruby | Notes |
|
380
|
+
# |-------------|----------------|---|
|
381
|
+
# | `BOOL` | `true`/`false` | |
|
382
|
+
# | `INT64` | `Integer` | |
|
383
|
+
# | `FLOAT64` | `Float` | |
|
384
|
+
# | `STRING` | `String` | |
|
385
|
+
# | `DATE` | `Date` | |
|
386
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
387
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
388
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
389
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
390
|
+
#
|
391
|
+
# See [Data
|
392
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
393
|
+
#
|
394
|
+
# See [Data Types - Constructing a
|
395
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
374
396
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
375
397
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
376
|
-
# value in `params`. In these cases, the `types` hash
|
377
|
-
# specify the
|
378
|
-
# parameters.
|
398
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
399
|
+
# specify the SQL type for these values.
|
379
400
|
#
|
380
401
|
# The keys of the hash should be query string parameter placeholders,
|
381
402
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -388,11 +409,11 @@ module Google
|
|
388
409
|
# * `:INT64`
|
389
410
|
# * `:STRING`
|
390
411
|
# * `:TIMESTAMP`
|
391
|
-
#
|
392
|
-
#
|
393
|
-
#
|
394
|
-
#
|
395
|
-
#
|
412
|
+
# * `Array` - Lists are specified by providing the type code in an
|
413
|
+
# array. For example, an array of integers are specified as
|
414
|
+
# `[:INT64]`.
|
415
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
416
|
+
# specified using a {Fields} object.
|
396
417
|
#
|
397
418
|
# Types are optional.
|
398
419
|
# @return [Google::Cloud::Spanner::Results] The results of the query
|
@@ -419,8 +440,72 @@ module Google
|
|
419
440
|
# batch_snapshot = batch_client.batch_snapshot
|
420
441
|
#
|
421
442
|
# results = batch_snapshot.execute "SELECT * FROM users " \
|
422
|
-
#
|
423
|
-
#
|
443
|
+
# "WHERE active = @active",
|
444
|
+
# params: { active: true }
|
445
|
+
#
|
446
|
+
# results.rows.each do |row|
|
447
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
448
|
+
# end
|
449
|
+
#
|
450
|
+
# @example Query with a SQL STRUCT query parameter as a Hash:
|
451
|
+
# require "google/cloud/spanner"
|
452
|
+
#
|
453
|
+
# spanner = Google::Cloud::Spanner.new
|
454
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
455
|
+
# batch_snapshot = batch_client.batch_snapshot
|
456
|
+
#
|
457
|
+
# user_hash = { id: 1, name: "Charlie", active: false }
|
458
|
+
#
|
459
|
+
# results = batch_snapshot.execute "SELECT * FROM users WHERE " \
|
460
|
+
# "ID = @user_struct.id " \
|
461
|
+
# "AND name = @user_struct.name " \
|
462
|
+
# "AND active = @user_struct.active",
|
463
|
+
# params: { user_struct: user_hash }
|
464
|
+
#
|
465
|
+
# results.rows.each do |row|
|
466
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
467
|
+
# end
|
468
|
+
#
|
469
|
+
# @example Specify the SQL STRUCT type using Fields object:
|
470
|
+
# require "google/cloud/spanner"
|
471
|
+
#
|
472
|
+
# spanner = Google::Cloud::Spanner.new
|
473
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
474
|
+
# batch_snapshot = batch_client.batch_snapshot
|
475
|
+
#
|
476
|
+
# user_type = batch_client.fields(
|
477
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
478
|
+
# )
|
479
|
+
# user_hash = { id: 1, name: nil, active: false }
|
480
|
+
#
|
481
|
+
# results = batch_snapshot.execute "SELECT * FROM users WHERE " \
|
482
|
+
# "ID = @user_struct.id " \
|
483
|
+
# "AND name = @user_struct.name " \
|
484
|
+
# "AND active = @user_struct.active",
|
485
|
+
# params: { user_struct: user_hash },
|
486
|
+
# types: { user_struct: user_type }
|
487
|
+
#
|
488
|
+
# results.rows.each do |row|
|
489
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
490
|
+
# end
|
491
|
+
#
|
492
|
+
# @example Or, query with a SQL STRUCT as a typed Data object:
|
493
|
+
# require "google/cloud/spanner"
|
494
|
+
#
|
495
|
+
# spanner = Google::Cloud::Spanner.new
|
496
|
+
# batch_client = spanner.batch_client "my-instance", "my-database"
|
497
|
+
# batch_snapshot = batch_client.batch_snapshot
|
498
|
+
#
|
499
|
+
# user_type = batch_client.fields(
|
500
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
501
|
+
# )
|
502
|
+
# user_data = user_type.struct id: 1, name: nil, active: false
|
503
|
+
#
|
504
|
+
# results = batch_snapshot.execute "SELECT * FROM users WHERE " \
|
505
|
+
# "ID = @user_struct.id " \
|
506
|
+
# "AND name = @user_struct.name " \
|
507
|
+
# "AND active = @user_struct.active",
|
508
|
+
# params: { user_struct: user_data }
|
424
509
|
#
|
425
510
|
# results.rows.each do |row|
|
426
511
|
# puts "User #{row[:id]} is #{row[:name]}"
|
@@ -98,23 +98,6 @@ module Google
|
|
98
98
|
##
|
99
99
|
# Executes a SQL query.
|
100
100
|
#
|
101
|
-
# Arguments can be passed using `params`, Ruby types are mapped to
|
102
|
-
# Spanner types as follows:
|
103
|
-
#
|
104
|
-
# | Spanner | Ruby | Notes |
|
105
|
-
# |-------------|----------------|---|
|
106
|
-
# | `BOOL` | `true`/`false` | |
|
107
|
-
# | `INT64` | `Integer` | |
|
108
|
-
# | `FLOAT64` | `Float` | |
|
109
|
-
# | `STRING` | `String` | |
|
110
|
-
# | `DATE` | `Date` | |
|
111
|
-
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
112
|
-
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
113
|
-
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
114
|
-
#
|
115
|
-
# See [Data
|
116
|
-
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
117
|
-
#
|
118
101
|
# @param [String] sql The SQL query string. See [Query
|
119
102
|
# syntax](https://cloud.google.com/spanner/docs/query-syntax).
|
120
103
|
#
|
@@ -127,11 +110,30 @@ module Google
|
|
127
110
|
# the literal values are the hash values. If the query string contains
|
128
111
|
# something like "WHERE id > @msg_id", then the params must contain
|
129
112
|
# something like `:msg_id => 1`.
|
113
|
+
#
|
114
|
+
# Ruby types are mapped to Spanner types as follows:
|
115
|
+
#
|
116
|
+
# | Spanner | Ruby | Notes |
|
117
|
+
# |-------------|----------------|---|
|
118
|
+
# | `BOOL` | `true`/`false` | |
|
119
|
+
# | `INT64` | `Integer` | |
|
120
|
+
# | `FLOAT64` | `Float` | |
|
121
|
+
# | `STRING` | `String` | |
|
122
|
+
# | `DATE` | `Date` | |
|
123
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
124
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
125
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
126
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
127
|
+
#
|
128
|
+
# See [Data
|
129
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
130
|
+
#
|
131
|
+
# See [Data Types - Constructing a
|
132
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
130
133
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
131
134
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
132
|
-
# value in `params`. In these cases, the `types` hash
|
133
|
-
# specify the
|
134
|
-
# parameters.
|
135
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
136
|
+
# specify the SQL type for these values.
|
135
137
|
#
|
136
138
|
# The keys of the hash should be query string parameter placeholders,
|
137
139
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -144,11 +146,11 @@ module Google
|
|
144
146
|
# * `:INT64`
|
145
147
|
# * `:STRING`
|
146
148
|
# * `:TIMESTAMP`
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
149
|
+
# * `Array` - Lists are specified by providing the type code in an
|
150
|
+
# array. For example, an array of integers are specified as
|
151
|
+
# `[:INT64]`.
|
152
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
153
|
+
# specified using a {Fields} object.
|
152
154
|
#
|
153
155
|
# Types are optional.
|
154
156
|
# @param [Hash] single_use Perform the read with a single-use snapshot
|
@@ -234,6 +236,66 @@ module Google
|
|
234
236
|
# puts "User #{row[:id]} is #{row[:name]}"
|
235
237
|
# end
|
236
238
|
#
|
239
|
+
# @example Query with a SQL STRUCT query parameter as a Hash:
|
240
|
+
# require "google/cloud/spanner"
|
241
|
+
#
|
242
|
+
# spanner = Google::Cloud::Spanner.new
|
243
|
+
#
|
244
|
+
# db = spanner.client "my-instance", "my-database"
|
245
|
+
#
|
246
|
+
# user_hash = { id: 1, name: "Charlie", active: false }
|
247
|
+
#
|
248
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
249
|
+
# "ID = @user_struct.id " \
|
250
|
+
# "AND name = @user_struct.name " \
|
251
|
+
# "AND active = @user_struct.active",
|
252
|
+
# params: { user_struct: user_hash }
|
253
|
+
#
|
254
|
+
# results.rows.each do |row|
|
255
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
256
|
+
# end
|
257
|
+
#
|
258
|
+
# @example Specify the SQL STRUCT type using Fields object:
|
259
|
+
# require "google/cloud/spanner"
|
260
|
+
#
|
261
|
+
# spanner = Google::Cloud::Spanner.new
|
262
|
+
#
|
263
|
+
# db = spanner.client "my-instance", "my-database"
|
264
|
+
#
|
265
|
+
# user_type = db.fields id: :INT64, name: :STRING, active: :BOOL
|
266
|
+
# user_hash = { id: 1, name: nil, active: false }
|
267
|
+
#
|
268
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
269
|
+
# "ID = @user_struct.id " \
|
270
|
+
# "AND name = @user_struct.name " \
|
271
|
+
# "AND active = @user_struct.active",
|
272
|
+
# params: { user_struct: user_hash },
|
273
|
+
# types: { user_struct: user_type }
|
274
|
+
#
|
275
|
+
# results.rows.each do |row|
|
276
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
277
|
+
# end
|
278
|
+
#
|
279
|
+
# @example Or, query with a SQL STRUCT as a typed Data object:
|
280
|
+
# require "google/cloud/spanner"
|
281
|
+
#
|
282
|
+
# spanner = Google::Cloud::Spanner.new
|
283
|
+
#
|
284
|
+
# db = spanner.client "my-instance", "my-database"
|
285
|
+
#
|
286
|
+
# user_type = db.fields id: :INT64, name: :STRING, active: :BOOL
|
287
|
+
# user_data = user_type.struct id: 1, name: nil, active: false
|
288
|
+
#
|
289
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
290
|
+
# "ID = @user_struct.id " \
|
291
|
+
# "AND name = @user_struct.name " \
|
292
|
+
# "AND active = @user_struct.active",
|
293
|
+
# params: { user_struct: user_data }
|
294
|
+
#
|
295
|
+
# results.rows.each do |row|
|
296
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
297
|
+
# end
|
298
|
+
#
|
237
299
|
def execute sql, params: nil, types: nil, single_use: nil
|
238
300
|
validate_single_use_args! single_use
|
239
301
|
ensure_service!
|
@@ -855,24 +917,72 @@ module Google
|
|
855
917
|
end
|
856
918
|
|
857
919
|
##
|
858
|
-
#
|
859
|
-
#
|
920
|
+
# Creates a configuration object ({Fields}) that may be provided to
|
921
|
+
# queries or used to create STRUCT objects. (The STRUCT will be
|
922
|
+
# represented by the {Data} class.) See {Client#execute} and/or
|
923
|
+
# {Fields#struct}.
|
924
|
+
#
|
925
|
+
# For more information, see [Data Types - Constructing a
|
926
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
860
927
|
#
|
861
|
-
# @param [Array, Hash] types Accepts an array
|
862
|
-
#
|
928
|
+
# @param [Array, Hash] types Accepts an array or hash types.
|
929
|
+
#
|
930
|
+
# Arrays can contain just the type value, or a sub-array of the
|
931
|
+
# field's name and type value. Hash keys must contain the field name
|
932
|
+
# as a `Symbol` or `String`, or the field position as an `Integer`.
|
933
|
+
# Hash values must contain the type value. If a Hash is used the
|
934
|
+
# fields will be created using the same order as the Hash keys.
|
935
|
+
#
|
936
|
+
# Supported type values incude:
|
937
|
+
#
|
938
|
+
# * `:BOOL`
|
939
|
+
# * `:BYTES`
|
940
|
+
# * `:DATE`
|
941
|
+
# * `:FLOAT64`
|
942
|
+
# * `:INT64`
|
943
|
+
# * `:STRING`
|
944
|
+
# * `:TIMESTAMP`
|
945
|
+
# * `Array` - Lists are specified by providing the type code in an
|
946
|
+
# array. For example, an array of integers are specified as
|
947
|
+
# `[:INT64]`.
|
948
|
+
# * {Fields} - Nested Structs are specified by providing a Fields
|
949
|
+
# object.
|
863
950
|
#
|
864
951
|
# @return [Fields] The fields of the given types.
|
865
952
|
#
|
866
|
-
# @example
|
953
|
+
# @example Create a STRUCT value with named fields using Fields object:
|
954
|
+
# require "google/cloud/spanner"
|
955
|
+
#
|
956
|
+
# spanner = Google::Cloud::Spanner.new
|
957
|
+
#
|
958
|
+
# db = spanner.client "my-instance", "my-database"
|
959
|
+
#
|
960
|
+
# named_type = db.fields(
|
961
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
962
|
+
# )
|
963
|
+
# named_data = named_type.struct(
|
964
|
+
# { id: 42, name: nil, active: false }
|
965
|
+
# )
|
966
|
+
#
|
967
|
+
# @example Create a STRUCT value with anonymous field names:
|
968
|
+
# require "google/cloud/spanner"
|
969
|
+
#
|
970
|
+
# spanner = Google::Cloud::Spanner.new
|
971
|
+
#
|
972
|
+
# db = spanner.client "my-instance", "my-database"
|
973
|
+
#
|
974
|
+
# anon_type = db.fields [:INT64, :STRING, :BOOL]
|
975
|
+
# anon_data = anon_type.struct [42, nil, false]
|
976
|
+
#
|
977
|
+
# @example Create a STRUCT value with duplicate field names:
|
867
978
|
# require "google/cloud/spanner"
|
868
979
|
#
|
869
980
|
# spanner = Google::Cloud::Spanner.new
|
870
981
|
#
|
871
982
|
# db = spanner.client "my-instance", "my-database"
|
872
|
-
# user_fields = db.fields id: :INT64, name: :STRING, active: :BOOL
|
873
983
|
#
|
874
|
-
# db.
|
875
|
-
#
|
984
|
+
# dup_type = db.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
|
985
|
+
# dup_data = dup_type.struct [42, nil, false]
|
876
986
|
#
|
877
987
|
def fields types
|
878
988
|
Fields.new types
|