google-cloud-spanner 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -37,7 +37,7 @@ module Google
|
|
37
37
|
# results = db.execute "SELECT * FROM users"
|
38
38
|
#
|
39
39
|
# results.fields.pairs.each do |name, type|
|
40
|
-
# puts "Column #{name} is type {type}"
|
40
|
+
# puts "Column #{name} is type #{type}"
|
41
41
|
# end
|
42
42
|
#
|
43
43
|
class Results
|
@@ -51,7 +51,8 @@ module Google
|
|
51
51
|
end
|
52
52
|
|
53
53
|
##
|
54
|
-
# Returns the
|
54
|
+
# Returns the configuration object ({Fields}) of the names and types of
|
55
|
+
# the rows in the returned data.
|
55
56
|
#
|
56
57
|
# @return [Fields] The fields of the returned data.
|
57
58
|
#
|
@@ -65,7 +66,7 @@ module Google
|
|
65
66
|
# results = db.execute "SELECT * FROM users"
|
66
67
|
#
|
67
68
|
# results.fields.pairs.each do |name, type|
|
68
|
-
# puts "Column #{name} is type {type}"
|
69
|
+
# puts "Column #{name} is type #{type}"
|
69
70
|
# end
|
70
71
|
#
|
71
72
|
def fields
|
@@ -93,23 +93,6 @@ module Google
|
|
93
93
|
##
|
94
94
|
# Executes a SQL query.
|
95
95
|
#
|
96
|
-
# Arguments can be passed using `params`, Ruby types are mapped to
|
97
|
-
# Spanner types as follows:
|
98
|
-
#
|
99
|
-
# | Spanner | Ruby | Notes |
|
100
|
-
# |-------------|----------------|---|
|
101
|
-
# | `BOOL` | `true`/`false` | |
|
102
|
-
# | `INT64` | `Integer` | |
|
103
|
-
# | `FLOAT64` | `Float` | |
|
104
|
-
# | `STRING` | `String` | |
|
105
|
-
# | `DATE` | `Date` | |
|
106
|
-
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
107
|
-
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
108
|
-
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
109
|
-
#
|
110
|
-
# See [Data
|
111
|
-
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
112
|
-
#
|
113
96
|
# @param [String] sql The SQL query string. See [Query
|
114
97
|
# syntax](https://cloud.google.com/spanner/docs/query-syntax).
|
115
98
|
#
|
@@ -122,11 +105,30 @@ module Google
|
|
122
105
|
# the literal values are the hash values. If the query string contains
|
123
106
|
# something like "WHERE id > @msg_id", then the params must contain
|
124
107
|
# something like `:msg_id => 1`.
|
108
|
+
#
|
109
|
+
# Ruby types are mapped to Spanner types as follows:
|
110
|
+
#
|
111
|
+
# | Spanner | Ruby | Notes |
|
112
|
+
# |-------------|----------------|---|
|
113
|
+
# | `BOOL` | `true`/`false` | |
|
114
|
+
# | `INT64` | `Integer` | |
|
115
|
+
# | `FLOAT64` | `Float` | |
|
116
|
+
# | `STRING` | `String` | |
|
117
|
+
# | `DATE` | `Date` | |
|
118
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
119
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
120
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
121
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
122
|
+
#
|
123
|
+
# See [Data
|
124
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
125
|
+
#
|
126
|
+
# See [Data Types - Constructing a
|
127
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
125
128
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
126
129
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
127
|
-
# value in `params`. In these cases, the `types` hash
|
128
|
-
# specify the
|
129
|
-
# parameters.
|
130
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
131
|
+
# specify the SQL type for these values.
|
130
132
|
#
|
131
133
|
# The keys of the hash should be query string parameter placeholders,
|
132
134
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -139,11 +141,11 @@ module Google
|
|
139
141
|
# * `:INT64`
|
140
142
|
# * `:STRING`
|
141
143
|
# * `:TIMESTAMP`
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
144
|
+
# * `Array` - Lists are specified by providing the type code in an
|
145
|
+
# array. For example, an array of integers are specified as
|
146
|
+
# `[:INT64]`.
|
147
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
148
|
+
# specified using a {Fields} object.
|
147
149
|
#
|
148
150
|
# Types are optional.
|
149
151
|
# @param [Google::Spanner::V1::TransactionSelector] transaction The
|
@@ -180,6 +182,66 @@ module Google
|
|
180
182
|
# puts "User #{row[:id]} is #{row[:name]}"
|
181
183
|
# end
|
182
184
|
#
|
185
|
+
# @example Query with a SQL STRUCT query parameter as a Hash:
|
186
|
+
# require "google/cloud/spanner"
|
187
|
+
#
|
188
|
+
# spanner = Google::Cloud::Spanner.new
|
189
|
+
#
|
190
|
+
# db = spanner.client "my-instance", "my-database"
|
191
|
+
#
|
192
|
+
# user_hash = { id: 1, name: "Charlie", active: false }
|
193
|
+
#
|
194
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
195
|
+
# "ID = @user_struct.id " \
|
196
|
+
# "AND name = @user_struct.name " \
|
197
|
+
# "AND active = @user_struct.active",
|
198
|
+
# params: { user_struct: user_hash }
|
199
|
+
#
|
200
|
+
# results.rows.each do |row|
|
201
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
202
|
+
# end
|
203
|
+
#
|
204
|
+
# @example Specify the SQL STRUCT type using Fields object:
|
205
|
+
# require "google/cloud/spanner"
|
206
|
+
#
|
207
|
+
# spanner = Google::Cloud::Spanner.new
|
208
|
+
#
|
209
|
+
# db = spanner.client "my-instance", "my-database"
|
210
|
+
#
|
211
|
+
# user_type = db.fields id: :INT64, name: :STRING, active: :BOOL
|
212
|
+
# user_hash = { id: 1, name: nil, active: false }
|
213
|
+
#
|
214
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
215
|
+
# "ID = @user_struct.id " \
|
216
|
+
# "AND name = @user_struct.name " \
|
217
|
+
# "AND active = @user_struct.active",
|
218
|
+
# params: { user_struct: user_hash },
|
219
|
+
# types: { user_struct: user_type }
|
220
|
+
#
|
221
|
+
# results.rows.each do |row|
|
222
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
223
|
+
# end
|
224
|
+
#
|
225
|
+
# @example Or, query with a SQL STRUCT as a typed Data object:
|
226
|
+
# require "google/cloud/spanner"
|
227
|
+
#
|
228
|
+
# spanner = Google::Cloud::Spanner.new
|
229
|
+
#
|
230
|
+
# db = spanner.client "my-instance", "my-database"
|
231
|
+
#
|
232
|
+
# user_type = db.fields id: :INT64, name: :STRING, active: :BOOL
|
233
|
+
# user_data = user_type.struct id: 1, name: nil, active: false
|
234
|
+
#
|
235
|
+
# results = db.execute "SELECT * FROM users WHERE " \
|
236
|
+
# "ID = @user_struct.id " \
|
237
|
+
# "AND name = @user_struct.name " \
|
238
|
+
# "AND active = @user_struct.active",
|
239
|
+
# params: { user_struct: user_struct }
|
240
|
+
#
|
241
|
+
# results.rows.each do |row|
|
242
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
243
|
+
# end
|
244
|
+
#
|
183
245
|
def execute sql, params: nil, types: nil, transaction: nil,
|
184
246
|
partition_token: nil
|
185
247
|
ensure_service!
|
@@ -63,23 +63,6 @@ module Google
|
|
63
63
|
##
|
64
64
|
# Executes a SQL query.
|
65
65
|
#
|
66
|
-
# Arguments can be passed using `params`, Ruby types are mapped to
|
67
|
-
# Spanner types as follows:
|
68
|
-
#
|
69
|
-
# | Spanner | Ruby | Notes |
|
70
|
-
# |-------------|----------------|---|
|
71
|
-
# | `BOOL` | `true`/`false` | |
|
72
|
-
# | `INT64` | `Integer` | |
|
73
|
-
# | `FLOAT64` | `Float` | |
|
74
|
-
# | `STRING` | `String` | |
|
75
|
-
# | `DATE` | `Date` | |
|
76
|
-
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
77
|
-
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
78
|
-
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
79
|
-
#
|
80
|
-
# See [Data
|
81
|
-
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
82
|
-
#
|
83
66
|
# @param [String] sql The SQL query string. See [Query
|
84
67
|
# syntax](https://cloud.google.com/spanner/docs/query-syntax).
|
85
68
|
#
|
@@ -92,11 +75,30 @@ module Google
|
|
92
75
|
# the literal values are the hash values. If the query string contains
|
93
76
|
# something like "WHERE id > @msg_id", then the params must contain
|
94
77
|
# something like `:msg_id => 1`.
|
78
|
+
#
|
79
|
+
# Ruby types are mapped to Spanner types as follows:
|
80
|
+
#
|
81
|
+
# | Spanner | Ruby | Notes |
|
82
|
+
# |-------------|----------------|---|
|
83
|
+
# | `BOOL` | `true`/`false` | |
|
84
|
+
# | `INT64` | `Integer` | |
|
85
|
+
# | `FLOAT64` | `Float` | |
|
86
|
+
# | `STRING` | `String` | |
|
87
|
+
# | `DATE` | `Date` | |
|
88
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
89
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
90
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
91
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
92
|
+
#
|
93
|
+
# See [Data
|
94
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
95
|
+
#
|
96
|
+
# See [Data Types - Constructing a
|
97
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
95
98
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
96
99
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
97
|
-
# value in `params`. In these cases, the `types` hash
|
98
|
-
# specify the
|
99
|
-
# parameters.
|
100
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
101
|
+
# specify the SQL type for these values.
|
100
102
|
#
|
101
103
|
# The keys of the hash should be query string parameter placeholders,
|
102
104
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -109,11 +111,11 @@ module Google
|
|
109
111
|
# * `:INT64`
|
110
112
|
# * `:STRING`
|
111
113
|
# * `:TIMESTAMP`
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
114
|
+
# * `Array` - Lists are specified by providing the type code in an
|
115
|
+
# array. For example, an array of integers are specified as
|
116
|
+
# `[:INT64]`.
|
117
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
118
|
+
# specified using a {Fields} object.
|
117
119
|
#
|
118
120
|
# Types are optional.
|
119
121
|
# @return [Google::Cloud::Spanner::Results] The results of the query
|
@@ -149,6 +151,69 @@ module Google
|
|
149
151
|
# end
|
150
152
|
# end
|
151
153
|
#
|
154
|
+
# @example Query with a SQL STRUCT query parameter as a Hash:
|
155
|
+
# require "google/cloud/spanner"
|
156
|
+
#
|
157
|
+
# spanner = Google::Cloud::Spanner.new
|
158
|
+
# db = spanner.client "my-instance", "my-database"
|
159
|
+
#
|
160
|
+
# db.snapshot do |snp|
|
161
|
+
# user_hash = { id: 1, name: "Charlie", active: false }
|
162
|
+
#
|
163
|
+
# results = snp.execute "SELECT * FROM users WHERE " \
|
164
|
+
# "ID = @user_struct.id " \
|
165
|
+
# "AND name = @user_struct.name " \
|
166
|
+
# "AND active = @user_struct.active",
|
167
|
+
# params: { user_struct: user_hash }
|
168
|
+
#
|
169
|
+
# results.rows.each do |row|
|
170
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
171
|
+
# end
|
172
|
+
# end
|
173
|
+
#
|
174
|
+
# @example Specify the SQL STRUCT type using Fields object:
|
175
|
+
# require "google/cloud/spanner"
|
176
|
+
#
|
177
|
+
# spanner = Google::Cloud::Spanner.new
|
178
|
+
# db = spanner.client "my-instance", "my-database"
|
179
|
+
#
|
180
|
+
# db.snapshot do |snp|
|
181
|
+
# user_type = snp.fields id: :INT64, name: :STRING, active: :BOOL
|
182
|
+
# user_hash = { id: 1, name: nil, active: false }
|
183
|
+
#
|
184
|
+
# results = snp.execute "SELECT * FROM users WHERE " \
|
185
|
+
# "ID = @user_struct.id " \
|
186
|
+
# "AND name = @user_struct.name " \
|
187
|
+
# "AND active = @user_struct.active",
|
188
|
+
# params: { user_struct: user_hash },
|
189
|
+
# types: { user_struct: user_type }
|
190
|
+
#
|
191
|
+
# results.rows.each do |row|
|
192
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
193
|
+
# end
|
194
|
+
# end
|
195
|
+
#
|
196
|
+
# @example Or, query with a SQL STRUCT as a typed Data object:
|
197
|
+
# require "google/cloud/spanner"
|
198
|
+
#
|
199
|
+
# spanner = Google::Cloud::Spanner.new
|
200
|
+
# db = spanner.client "my-instance", "my-database"
|
201
|
+
#
|
202
|
+
# db.snapshot do |snp|
|
203
|
+
# user_type = snp.fields id: :INT64, name: :STRING, active: :BOOL
|
204
|
+
# user_data = user_type.struct id: 1, name: nil, active: false
|
205
|
+
#
|
206
|
+
# results = snp.execute "SELECT * FROM users WHERE " \
|
207
|
+
# "ID = @user_struct.id " \
|
208
|
+
# "AND name = @user_struct.name " \
|
209
|
+
# "AND active = @user_struct.active",
|
210
|
+
# params: { user_struct: user_data }
|
211
|
+
#
|
212
|
+
# results.rows.each do |row|
|
213
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
214
|
+
# end
|
215
|
+
# end
|
216
|
+
#
|
152
217
|
def execute sql, params: nil, types: nil
|
153
218
|
ensure_session!
|
154
219
|
|
@@ -203,6 +268,84 @@ module Google
|
|
203
268
|
transaction: tx_selector
|
204
269
|
end
|
205
270
|
|
271
|
+
##
|
272
|
+
# Creates a configuration object ({Fields}) that may be provided to
|
273
|
+
# queries or used to create STRUCT objects. (The STRUCT will be
|
274
|
+
# represented by the {Data} class.) See {Client#execute} and/or
|
275
|
+
# {Fields#struct}.
|
276
|
+
#
|
277
|
+
# For more information, see [Data Types - Constructing a
|
278
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
279
|
+
#
|
280
|
+
# See [Data Types - Constructing a
|
281
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
282
|
+
#
|
283
|
+
# @param [Array, Hash] types Accepts an array or hash types.
|
284
|
+
#
|
285
|
+
# Arrays can contain just the type value, or a sub-array of the
|
286
|
+
# field's name and type value. Hash keys must contain the field name
|
287
|
+
# as a `Symbol` or `String`, or the field position as an `Integer`.
|
288
|
+
# Hash values must contain the type value. If a Hash is used the
|
289
|
+
# fields will be created using the same order as the Hash keys.
|
290
|
+
#
|
291
|
+
# Supported type values incude:
|
292
|
+
#
|
293
|
+
# * `:BOOL`
|
294
|
+
# * `:BYTES`
|
295
|
+
# * `:DATE`
|
296
|
+
# * `:FLOAT64`
|
297
|
+
# * `:INT64`
|
298
|
+
# * `:STRING`
|
299
|
+
# * `:TIMESTAMP`
|
300
|
+
# * `Array` - Lists are specified by providing the type code in an
|
301
|
+
# array. For example, an array of integers are specified as
|
302
|
+
# `[:INT64]`.
|
303
|
+
# * {Fields} - Nested Structs are specified by providing a Fields
|
304
|
+
# object.
|
305
|
+
#
|
306
|
+
# @return [Fields] The fields of the given types.
|
307
|
+
#
|
308
|
+
# @example Create a STRUCT value with named fields using Fields object:
|
309
|
+
# require "google/cloud/spanner"
|
310
|
+
#
|
311
|
+
# spanner = Google::Cloud::Spanner.new
|
312
|
+
# db = spanner.client "my-instance", "my-database"
|
313
|
+
#
|
314
|
+
# db.snapshot do |snp|
|
315
|
+
# named_type = snp.fields(
|
316
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
317
|
+
# )
|
318
|
+
# named_data = named_type.struct(
|
319
|
+
# { id: 42, name: nil, active: false }
|
320
|
+
# )
|
321
|
+
# end
|
322
|
+
#
|
323
|
+
# @example Create a STRUCT value with anonymous field names:
|
324
|
+
# require "google/cloud/spanner"
|
325
|
+
#
|
326
|
+
# spanner = Google::Cloud::Spanner.new
|
327
|
+
# db = spanner.client "my-instance", "my-database"
|
328
|
+
#
|
329
|
+
# db.snapshot do |snp|
|
330
|
+
# anon_type = snp.fields [:INT64, :STRING, :BOOL]
|
331
|
+
# anon_data = anon_type.struct [42, nil, false]
|
332
|
+
# end
|
333
|
+
#
|
334
|
+
# @example Create a STRUCT value with duplicate field names:
|
335
|
+
# require "google/cloud/spanner"
|
336
|
+
#
|
337
|
+
# spanner = Google::Cloud::Spanner.new
|
338
|
+
# db = spanner.client "my-instance", "my-database"
|
339
|
+
#
|
340
|
+
# db.snapshot do |snp|
|
341
|
+
# dup_type = snp.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
|
342
|
+
# dup_data = dup_type.struct [42, nil, false]
|
343
|
+
# end
|
344
|
+
#
|
345
|
+
def fields types
|
346
|
+
Fields.new types
|
347
|
+
end
|
348
|
+
|
206
349
|
##
|
207
350
|
# Creates a Cloud Spanner Range. This can be used in place of a Ruby
|
208
351
|
# Range when needing to exclude the beginning value.
|
@@ -92,23 +92,6 @@ module Google
|
|
92
92
|
##
|
93
93
|
# Executes a SQL query.
|
94
94
|
#
|
95
|
-
# Arguments can be passed using `params`, Ruby types are mapped to
|
96
|
-
# Spanner types as follows:
|
97
|
-
#
|
98
|
-
# | Spanner | Ruby | Notes |
|
99
|
-
# |-------------|----------------|---|
|
100
|
-
# | `BOOL` | `true`/`false` | |
|
101
|
-
# | `INT64` | `Integer` | |
|
102
|
-
# | `FLOAT64` | `Float` | |
|
103
|
-
# | `STRING` | `String` | |
|
104
|
-
# | `DATE` | `Date` | |
|
105
|
-
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
106
|
-
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
107
|
-
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
108
|
-
#
|
109
|
-
# See [Data
|
110
|
-
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
111
|
-
#
|
112
95
|
# @param [String] sql The SQL query string. See [Query
|
113
96
|
# syntax](https://cloud.google.com/spanner/docs/query-syntax).
|
114
97
|
#
|
@@ -121,11 +104,30 @@ module Google
|
|
121
104
|
# the literal values are the hash values. If the query string contains
|
122
105
|
# something like "WHERE id > @msg_id", then the params must contain
|
123
106
|
# something like `:msg_id => 1`.
|
107
|
+
#
|
108
|
+
# Ruby types are mapped to Spanner types as follows:
|
109
|
+
#
|
110
|
+
# | Spanner | Ruby | Notes |
|
111
|
+
# |-------------|----------------|---|
|
112
|
+
# | `BOOL` | `true`/`false` | |
|
113
|
+
# | `INT64` | `Integer` | |
|
114
|
+
# | `FLOAT64` | `Float` | |
|
115
|
+
# | `STRING` | `String` | |
|
116
|
+
# | `DATE` | `Date` | |
|
117
|
+
# | `TIMESTAMP` | `Time`, `DateTime` | |
|
118
|
+
# | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
|
119
|
+
# | `ARRAY` | `Array` | Nested arrays are not supported. |
|
120
|
+
# | `STRUCT` | `Hash`, {Data} | |
|
121
|
+
#
|
122
|
+
# See [Data
|
123
|
+
# types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
|
124
|
+
#
|
125
|
+
# See [Data Types - Constructing a
|
126
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
124
127
|
# @param [Hash] types Types of the SQL parameters in `params`. It is not
|
125
128
|
# always possible for Cloud Spanner to infer the right SQL type from a
|
126
|
-
# value in `params`. In these cases, the `types` hash
|
127
|
-
# specify the
|
128
|
-
# parameters.
|
129
|
+
# value in `params`. In these cases, the `types` hash must be used to
|
130
|
+
# specify the SQL type for these values.
|
129
131
|
#
|
130
132
|
# The keys of the hash should be query string parameter placeholders,
|
131
133
|
# minus the "@". The values of the hash should be Cloud Spanner type
|
@@ -138,11 +140,11 @@ module Google
|
|
138
140
|
# * `:INT64`
|
139
141
|
# * `:STRING`
|
140
142
|
# * `:TIMESTAMP`
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
143
|
+
# * `Array` - Lists are specified by providing the type code in an
|
144
|
+
# array. For example, an array of integers are specified as
|
145
|
+
# `[:INT64]`.
|
146
|
+
# * {Fields} - Types for STRUCT values (`Hash`/{Data} objects) are
|
147
|
+
# specified using a {Fields} object.
|
146
148
|
#
|
147
149
|
# Types are optional.
|
148
150
|
# @return [Google::Cloud::Spanner::Results] The results of the query
|
@@ -177,6 +179,69 @@ module Google
|
|
177
179
|
# end
|
178
180
|
# end
|
179
181
|
#
|
182
|
+
# @example Query with a SQL STRUCT query parameter as a Hash:
|
183
|
+
# require "google/cloud/spanner"
|
184
|
+
#
|
185
|
+
# spanner = Google::Cloud::Spanner.new
|
186
|
+
# db = spanner.client "my-instance", "my-database"
|
187
|
+
#
|
188
|
+
# db.transaction do |tx|
|
189
|
+
# user_hash = { id: 1, name: "Charlie", active: false }
|
190
|
+
#
|
191
|
+
# results = tx.execute "SELECT * FROM users WHERE " \
|
192
|
+
# "ID = @user_struct.id " \
|
193
|
+
# "AND name = @user_struct.name " \
|
194
|
+
# "AND active = @user_struct.active",
|
195
|
+
# params: { user_struct: user_hash }
|
196
|
+
#
|
197
|
+
# results.rows.each do |row|
|
198
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
199
|
+
# end
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
# @example Specify the SQL STRUCT type using Fields object:
|
203
|
+
# require "google/cloud/spanner"
|
204
|
+
#
|
205
|
+
# spanner = Google::Cloud::Spanner.new
|
206
|
+
# db = spanner.client "my-instance", "my-database"
|
207
|
+
#
|
208
|
+
# db.transaction do |tx|
|
209
|
+
# user_type = tx.fields id: :INT64, name: :STRING, active: :BOOL
|
210
|
+
# user_hash = { id: 1, name: nil, active: false }
|
211
|
+
#
|
212
|
+
# results = tx.execute "SELECT * FROM users WHERE " \
|
213
|
+
# "ID = @user_struct.id " \
|
214
|
+
# "AND name = @user_struct.name " \
|
215
|
+
# "AND active = @user_struct.active",
|
216
|
+
# params: { user_struct: user_hash },
|
217
|
+
# types: { user_struct: user_type }
|
218
|
+
#
|
219
|
+
# results.rows.each do |row|
|
220
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
221
|
+
# end
|
222
|
+
# end
|
223
|
+
#
|
224
|
+
# @example Or, query with a SQL STRUCT as a typed Data object:
|
225
|
+
# require "google/cloud/spanner"
|
226
|
+
#
|
227
|
+
# spanner = Google::Cloud::Spanner.new
|
228
|
+
# db = spanner.client "my-instance", "my-database"
|
229
|
+
#
|
230
|
+
# db.transaction do |tx|
|
231
|
+
# user_type = tx.fields id: :INT64, name: :STRING, active: :BOOL
|
232
|
+
# user_data = user_type.struct id: 1, name: nil, active: false
|
233
|
+
#
|
234
|
+
# results = tx.execute "SELECT * FROM users WHERE " \
|
235
|
+
# "ID = @user_struct.id " \
|
236
|
+
# "AND name = @user_struct.name " \
|
237
|
+
# "AND active = @user_struct.active",
|
238
|
+
# params: { user_struct: user_data }
|
239
|
+
#
|
240
|
+
# results.rows.each do |row|
|
241
|
+
# puts "User #{row[:id]} is #{row[:name]}"
|
242
|
+
# end
|
243
|
+
# end
|
244
|
+
#
|
180
245
|
def execute sql, params: nil, types: nil
|
181
246
|
ensure_session!
|
182
247
|
|
@@ -468,6 +533,81 @@ module Google
|
|
468
533
|
execute("SELECT * FROM #{table} WHERE 1 = 0").fields
|
469
534
|
end
|
470
535
|
|
536
|
+
##
|
537
|
+
# Creates a configuration object ({Fields}) that may be provided to
|
538
|
+
# queries or used to create STRUCT objects. (The STRUCT will be
|
539
|
+
# represented by the {Data} class.) See {Client#execute} and/or
|
540
|
+
# {Fields#struct}.
|
541
|
+
#
|
542
|
+
# For more information, see [Data Types - Constructing a
|
543
|
+
# STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
|
544
|
+
#
|
545
|
+
# @param [Array, Hash] types Accepts an array or hash types.
|
546
|
+
#
|
547
|
+
# Arrays can contain just the type value, or a sub-array of the
|
548
|
+
# field's name and type value. Hash keys must contain the field name
|
549
|
+
# as a `Symbol` or `String`, or the field position as an `Integer`.
|
550
|
+
# Hash values must contain the type value. If a Hash is used the
|
551
|
+
# fields will be created using the same order as the Hash keys.
|
552
|
+
#
|
553
|
+
# Supported type values incude:
|
554
|
+
#
|
555
|
+
# * `:BOOL`
|
556
|
+
# * `:BYTES`
|
557
|
+
# * `:DATE`
|
558
|
+
# * `:FLOAT64`
|
559
|
+
# * `:INT64`
|
560
|
+
# * `:STRING`
|
561
|
+
# * `:TIMESTAMP`
|
562
|
+
# * `Array` - Lists are specified by providing the type code in an
|
563
|
+
# array. For example, an array of integers are specified as
|
564
|
+
# `[:INT64]`.
|
565
|
+
# * {Fields} - Nested Structs are specified by providing a Fields
|
566
|
+
# object.
|
567
|
+
#
|
568
|
+
# @return [Fields] The fields of the given types.
|
569
|
+
#
|
570
|
+
# @example Create a STRUCT value with named fields using Fields object:
|
571
|
+
# require "google/cloud/spanner"
|
572
|
+
#
|
573
|
+
# spanner = Google::Cloud::Spanner.new
|
574
|
+
# db = spanner.client "my-instance", "my-database"
|
575
|
+
#
|
576
|
+
# db.transaction do |tx|
|
577
|
+
# named_type = tx.fields(
|
578
|
+
# { id: :INT64, name: :STRING, active: :BOOL }
|
579
|
+
# )
|
580
|
+
# named_data = named_type.struct(
|
581
|
+
# { id: 42, name: nil, active: false }
|
582
|
+
# )
|
583
|
+
# end
|
584
|
+
#
|
585
|
+
# @example Create a STRUCT value with anonymous field names:
|
586
|
+
# require "google/cloud/spanner"
|
587
|
+
#
|
588
|
+
# spanner = Google::Cloud::Spanner.new
|
589
|
+
# db = spanner.client "my-instance", "my-database"
|
590
|
+
#
|
591
|
+
# db.transaction do |tx|
|
592
|
+
# anon_type = tx.fields [:INT64, :STRING, :BOOL]
|
593
|
+
# anon_data = anon_type.struct [42, nil, false]
|
594
|
+
# end
|
595
|
+
#
|
596
|
+
# @example Create a STRUCT value with duplicate field names:
|
597
|
+
# require "google/cloud/spanner"
|
598
|
+
#
|
599
|
+
# spanner = Google::Cloud::Spanner.new
|
600
|
+
# db = spanner.client "my-instance", "my-database"
|
601
|
+
#
|
602
|
+
# db.transaction do |tx|
|
603
|
+
# dup_type = tx.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
|
604
|
+
# dup_data = dup_type.struct [42, nil, false]
|
605
|
+
# end
|
606
|
+
#
|
607
|
+
def fields types
|
608
|
+
Fields.new types
|
609
|
+
end
|
610
|
+
|
471
611
|
##
|
472
612
|
# Creates a Cloud Spanner Range. This can be used in place of a Ruby
|
473
613
|
# Range when needing to exclude the beginning value.
|