google-cloud-spanner 1.6.4 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,7 +33,7 @@ module Google
33
33
  # db = spanner.client "my-instance", "my-database"
34
34
  #
35
35
  # db.snapshot do |snp|
36
- # results = snp.execute "SELECT * FROM users"
36
+ # results = snp.execute_query "SELECT * FROM users"
37
37
  #
38
38
  # results.rows.each do |row|
39
39
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -128,7 +128,7 @@ module Google
128
128
  # db = spanner.client "my-instance", "my-database"
129
129
  #
130
130
  # db.snapshot do |snp|
131
- # results = snp.execute "SELECT * FROM users"
131
+ # results = snp.execute_query "SELECT * FROM users"
132
132
  #
133
133
  # results.rows.each do |row|
134
134
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -142,7 +142,7 @@ module Google
142
142
  # db = spanner.client "my-instance", "my-database"
143
143
  #
144
144
  # db.snapshot do |snp|
145
- # results = snp.execute "SELECT * FROM users " \
145
+ # results = snp.execute_query "SELECT * FROM users " \
146
146
  # "WHERE active = @active",
147
147
  # params: { active: true }
148
148
  #
@@ -160,11 +160,13 @@ module Google
160
160
  # db.snapshot do |snp|
161
161
  # user_hash = { id: 1, name: "Charlie", active: false }
162
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 }
163
+ # results = snp.execute_query(
164
+ # "SELECT * FROM users WHERE " \
165
+ # "ID = @user_struct.id " \
166
+ # "AND name = @user_struct.name " \
167
+ # "AND active = @user_struct.active",
168
+ # params: { user_struct: user_hash }
169
+ # )
168
170
  #
169
171
  # results.rows.each do |row|
170
172
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -181,12 +183,14 @@ module Google
181
183
  # user_type = snp.fields id: :INT64, name: :STRING, active: :BOOL
182
184
  # user_hash = { id: 1, name: nil, active: false }
183
185
  #
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 }
186
+ # results = snp.execute_query(
187
+ # "SELECT * FROM users WHERE " \
188
+ # "ID = @user_struct.id " \
189
+ # "AND name = @user_struct.name " \
190
+ # "AND active = @user_struct.active",
191
+ # params: { user_struct: user_hash },
192
+ # types: { user_struct: user_type }
193
+ # )
190
194
  #
191
195
  # results.rows.each do |row|
192
196
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -203,30 +207,34 @@ module Google
203
207
  # user_type = snp.fields id: :INT64, name: :STRING, active: :BOOL
204
208
  # user_data = user_type.struct id: 1, name: nil, active: false
205
209
  #
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 }
210
+ # results = snp.execute_query(
211
+ # "SELECT * FROM users WHERE " \
212
+ # "ID = @user_struct.id " \
213
+ # "AND name = @user_struct.name " \
214
+ # "AND active = @user_struct.active",
215
+ # params: { user_struct: user_data }
216
+ # )
211
217
  #
212
218
  # results.rows.each do |row|
213
219
  # puts "User #{row[:id]} is #{row[:name]}"
214
220
  # end
215
221
  # end
216
222
  #
217
- def execute sql, params: nil, types: nil
223
+ def execute_query sql, params: nil, types: nil
218
224
  ensure_session!
219
225
 
220
226
  params, types = Convert.to_input_params_and_types params, types
221
227
 
222
- session.execute sql, params: params, types: types,
223
- transaction: tx_selector
228
+ session.execute_query sql, params: params, types: types,
229
+ transaction: tx_selector
224
230
  end
225
- alias query execute
231
+ alias execute execute_query
232
+ alias query execute_query
233
+ alias execute_sql execute_query
226
234
 
227
235
  ##
228
236
  # Read rows from a database table, as a simple alternative to
229
- # {#execute}.
237
+ # {#execute_query}.
230
238
  #
231
239
  # @param [String] table The name of the table in the database to be
232
240
  # read.
@@ -79,6 +79,7 @@ module Google
79
79
 
80
80
  def initialize
81
81
  @commit = Commit.new
82
+ @seqno = 0
82
83
  end
83
84
 
84
85
  ##
@@ -157,7 +158,7 @@ module Google
157
158
  # db = spanner.client "my-instance", "my-database"
158
159
  #
159
160
  # db.transaction do |tx|
160
- # results = tx.execute "SELECT * FROM users"
161
+ # results = tx.execute_query "SELECT * FROM users"
161
162
  #
162
163
  # results.rows.each do |row|
163
164
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -171,8 +172,10 @@ module Google
171
172
  # db = spanner.client "my-instance", "my-database"
172
173
  #
173
174
  # db.transaction do |tx|
174
- # results = tx.execute "SELECT * FROM users WHERE active = @active",
175
- # params: { active: true }
175
+ # results = tx.execute_query(
176
+ # "SELECT * FROM users WHERE active = @active",
177
+ # params: { active: true }
178
+ # )
176
179
  #
177
180
  # results.rows.each do |row|
178
181
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -188,11 +191,13 @@ module Google
188
191
  # db.transaction do |tx|
189
192
  # user_hash = { id: 1, name: "Charlie", active: false }
190
193
  #
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 }
194
+ # results = tx.execute_query(
195
+ # "SELECT * FROM users WHERE " \
196
+ # "ID = @user_struct.id " \
197
+ # "AND name = @user_struct.name " \
198
+ # "AND active = @user_struct.active",
199
+ # params: { user_struct: user_hash }
200
+ # )
196
201
  #
197
202
  # results.rows.each do |row|
198
203
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -209,12 +214,14 @@ module Google
209
214
  # user_type = tx.fields id: :INT64, name: :STRING, active: :BOOL
210
215
  # user_hash = { id: 1, name: nil, active: false }
211
216
  #
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 }
217
+ # results = tx.execute_query(
218
+ # "SELECT * FROM users WHERE " \
219
+ # "ID = @user_struct.id " \
220
+ # "AND name = @user_struct.name " \
221
+ # "AND active = @user_struct.active",
222
+ # params: { user_struct: user_hash },
223
+ # types: { user_struct: user_type }
224
+ # )
218
225
  #
219
226
  # results.rows.each do |row|
220
227
  # puts "User #{row[:id]} is #{row[:name]}"
@@ -231,30 +238,133 @@ module Google
231
238
  # user_type = tx.fields id: :INT64, name: :STRING, active: :BOOL
232
239
  # user_data = user_type.struct id: 1, name: nil, active: false
233
240
  #
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 }
241
+ # results = tx.execute_query(
242
+ # "SELECT * FROM users WHERE " \
243
+ # "ID = @user_struct.id " \
244
+ # "AND name = @user_struct.name " \
245
+ # "AND active = @user_struct.active",
246
+ # params: { user_struct: user_data }
247
+ # )
239
248
  #
240
249
  # results.rows.each do |row|
241
250
  # puts "User #{row[:id]} is #{row[:name]}"
242
251
  # end
243
252
  # end
244
253
  #
245
- def execute sql, params: nil, types: nil
254
+ def execute_query sql, params: nil, types: nil
246
255
  ensure_session!
247
256
 
257
+ @seqno += 1
258
+
248
259
  params, types = Convert.to_input_params_and_types params, types
249
260
 
250
- session.execute sql, params: params, types: types,
251
- transaction: tx_selector
261
+ session.execute_query sql, params: params, types: types,
262
+ transaction: tx_selector, seqno: @seqno
263
+ end
264
+ alias execute execute_query
265
+ alias query execute_query
266
+ alias execute_sql execute_query
267
+
268
+ ##
269
+ # Executes a DML statement.
270
+ #
271
+ # @param [String] sql The DML statement string. See [Query
272
+ # syntax](https://cloud.google.com/spanner/docs/query-syntax).
273
+ #
274
+ # The DML statement string can contain parameter placeholders. A
275
+ # parameter placeholder consists of "@" followed by the parameter
276
+ # name. Parameter names consist of any combination of letters,
277
+ # numbers, and underscores.
278
+ # @param [Hash] params Parameters for the DML statement string. The
279
+ # parameter placeholders, minus the "@", are the the hash keys, and
280
+ # the literal values are the hash values. If the query string contains
281
+ # something like "WHERE id > @msg_id", then the params must contain
282
+ # something like `:msg_id => 1`.
283
+ #
284
+ # Ruby types are mapped to Spanner types as follows:
285
+ #
286
+ # | Spanner | Ruby | Notes |
287
+ # |-------------|----------------|---|
288
+ # | `BOOL` | `true`/`false` | |
289
+ # | `INT64` | `Integer` | |
290
+ # | `FLOAT64` | `Float` | |
291
+ # | `STRING` | `String` | |
292
+ # | `DATE` | `Date` | |
293
+ # | `TIMESTAMP` | `Time`, `DateTime` | |
294
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
295
+ # | `ARRAY` | `Array` | Nested arrays are not supported. |
296
+ # | `STRUCT` | `Hash`, {Data} | |
297
+ #
298
+ # See [Data
299
+ # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
300
+ #
301
+ # See [Data Types - Constructing a
302
+ # STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
303
+ # @param [Hash] types Types of the SQL parameters in `params`. It is not
304
+ # always possible for Cloud Spanner to infer the right SQL type from a
305
+ # value in `params`. In these cases, the `types` hash can be used to
306
+ # specify the exact SQL type for some or all of the SQL query
307
+ # parameters.
308
+ #
309
+ # The keys of the hash should be query string parameter placeholders,
310
+ # minus the "@". The values of the hash should be Cloud Spanner type
311
+ # codes from the following list:
312
+ #
313
+ # * `:BOOL`
314
+ # * `:BYTES`
315
+ # * `:DATE`
316
+ # * `:FLOAT64`
317
+ # * `:INT64`
318
+ # * `:STRING`
319
+ # * `:TIMESTAMP`
320
+ # * `Array` - Lists are specified by providing the type code in an
321
+ # array. For example, an array of integers are specified as
322
+ # `[:INT64]`.
323
+ # * {Fields} - Nested Structs are specified by providing a Fields
324
+ # object.
325
+ #
326
+ # @return [Integer] The exact number of rows that were modified.
327
+ #
328
+ # @example
329
+ # require "google/cloud/spanner"
330
+ #
331
+ # spanner = Google::Cloud::Spanner.new
332
+ # db = spanner.client "my-instance", "my-database"
333
+ #
334
+ # db.transaction do |tx|
335
+ # row_count = tx.execute_update(
336
+ # "UPDATE users SET name = 'Charlie' WHERE id = 1",
337
+ # )
338
+ # end
339
+ #
340
+ # @example Update using SQL parameters:
341
+ # require "google/cloud/spanner"
342
+ #
343
+ # spanner = Google::Cloud::Spanner.new
344
+ # db = spanner.client "my-instance", "my-database"
345
+ #
346
+ # db.transaction do |tx|
347
+ # row_count = tx.execute_update(
348
+ # "UPDATE users SET name = @name WHERE id = @id",
349
+ # params: { id: 1, name: "Charlie" }
350
+ # )
351
+ # end
352
+ #
353
+ def execute_update sql, params: nil, types: nil
354
+ results = execute_query sql, params: params, types: types
355
+ # Stream all PartialResultSet to get ResultSetStats
356
+ results.rows.to_a
357
+ # Raise an error if there is not a row count returned
358
+ if results.row_count.nil?
359
+ raise Google::Cloud::InvalidArgumentError,
360
+ "DML statement is invalid."
361
+ end
362
+ results.row_count
252
363
  end
253
- alias query execute
254
364
 
255
365
  ##
256
366
  # Read rows from a database table, as a simple alternative to
257
- # {#execute}.
367
+ # {#execute_query}.
258
368
  #
259
369
  # @param [String] table The name of the table in the database to be
260
370
  # read.
@@ -530,7 +640,7 @@ module Google
530
640
  # end
531
641
  #
532
642
  def fields_for table
533
- execute("SELECT * FROM #{table} WHERE 1 = 0").fields
643
+ execute_query("SELECT * FROM #{table} WHERE 1 = 0").fields
534
644
  end
535
645
 
536
646
  ##
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Spanner
19
- VERSION = "1.6.4".freeze
19
+ VERSION = "1.7.1".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-spanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-09-21 00:00:00.000000000 Z
12
+ date: 2018-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core