google-cloud-spanner 2.22.0 → 2.24.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,288 @@
1
+ # Copyright 2024 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Google
16
+ module Cloud
17
+ module Spanner
18
+ ##
19
+ # Part of the BatchWrite DSL. This object defines a group of mutations
20
+ # to be included in a BatchWrite operation. All writes within a mutation
21
+ # group will execute atomically at a single logical point in time across
22
+ # columns, rows, and tables in a database.
23
+ #
24
+ # All changes are accumulated in memory until the block passed to
25
+ # {Client#batch_write} completes.
26
+ #
27
+ # @example
28
+ # require "google/cloud/spanner"
29
+ #
30
+ # spanner = Google::Cloud::Spanner.new
31
+ #
32
+ # db = spanner.client "my-instance", "my-database"
33
+ #
34
+ # results = db.batch_write do |b|
35
+ # # First mutation group
36
+ # b.mutation_group do |mg|
37
+ # mg.upsert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
38
+ # end
39
+ #
40
+ # # Second mutation group
41
+ # b.mutation_group do |mg|
42
+ # mg.upsert "Singers", [{ SingerId: 17, FirstName: "Catalina", LastName: "Smith" }]
43
+ # mg.update "Albums", [{ SingerId: 17, AlbumId: 1, AlbumTitle: "Go Go Go" }]
44
+ # end
45
+ # end
46
+ #
47
+ class MutationGroup
48
+ # @private
49
+ def initialize
50
+ @commit = Commit.new
51
+ end
52
+
53
+ ##
54
+ # Inserts or updates rows in a table. If any of the rows already exist,
55
+ # then its column values are overwritten with the ones provided. Any
56
+ # column values not explicitly written are preserved.
57
+ #
58
+ # All changes are accumulated in memory until the block passed to
59
+ # {Client#batch_write} completes.
60
+ #
61
+ # @param [String] table The name of the table in the database to be
62
+ # modified.
63
+ # @param [Array<Hash>] rows One or more hash objects with the hash keys
64
+ # matching the table's columns, and the hash values matching the
65
+ # table's values.
66
+ #
67
+ # Ruby types are mapped to Spanner types as follows:
68
+ #
69
+ # | Spanner | Ruby | Notes |
70
+ # |-------------|----------------|---|
71
+ # | `BOOL` | `true`/`false` | |
72
+ # | `INT64` | `Integer` | |
73
+ # | `FLOAT64` | `Float` | |
74
+ # | `FLOAT32` | `Float` | |
75
+ # | `NUMERIC` | `BigDecimal` | |
76
+ # | `STRING` | `String` | |
77
+ # | `DATE` | `Date` | |
78
+ # | `TIMESTAMP` | `Time`, `DateTime` | |
79
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
80
+ # | `ARRAY` | `Array` | Nested arrays are not supported. |
81
+ #
82
+ # See [Data
83
+ # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
84
+ #
85
+ # @example
86
+ # require "google/cloud/spanner"
87
+ #
88
+ # spanner = Google::Cloud::Spanner.new
89
+ #
90
+ # db = spanner.client "my-instance", "my-database"
91
+ #
92
+ # results = db.batch_write do |b|
93
+ # b.mutation_group do |mg|
94
+ # mg.upsert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
95
+ # end
96
+ # end
97
+ #
98
+ def upsert table, *rows
99
+ @commit.upsert table, rows
100
+ end
101
+ alias save upsert
102
+
103
+ ##
104
+ # Inserts new rows in a table. If any of the rows already exist, the
105
+ # write or request fails with error {Google::Cloud::AlreadyExistsError}.
106
+ #
107
+ # All changes are accumulated in memory until the block passed to
108
+ # {Client#batch_write} completes.
109
+ #
110
+ # @param [String] table The name of the table in the database to be
111
+ # modified.
112
+ # @param [Array<Hash>] rows One or more hash objects with the hash keys
113
+ # matching the table's columns, and the hash values matching the
114
+ # table's values.
115
+ #
116
+ # Ruby types are mapped to Spanner types as follows:
117
+ #
118
+ # | Spanner | Ruby | Notes |
119
+ # |-------------|----------------|---|
120
+ # | `BOOL` | `true`/`false` | |
121
+ # | `INT64` | `Integer` | |
122
+ # | `FLOAT64` | `Float` | |
123
+ # | `FLOAT32` | `Float` | |
124
+ # | `NUMERIC` | `BigDecimal` | |
125
+ # | `STRING` | `String` | |
126
+ # | `DATE` | `Date` | |
127
+ # | `TIMESTAMP` | `Time`, `DateTime` | |
128
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
129
+ # | `ARRAY` | `Array` | Nested arrays are not supported. |
130
+ #
131
+ # See [Data
132
+ # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
133
+ #
134
+ # @example
135
+ # require "google/cloud/spanner"
136
+ #
137
+ # spanner = Google::Cloud::Spanner.new
138
+ #
139
+ # db = spanner.client "my-instance", "my-database"
140
+ #
141
+ # results = db.batch_write do |b|
142
+ # b.mutation_group do |mg|
143
+ # mg.insert "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
144
+ # end
145
+ # end
146
+ #
147
+ def insert table, *rows
148
+ @commit.insert table, rows
149
+ end
150
+
151
+ ##
152
+ # Updates existing rows in a table. If any of the rows does not already
153
+ # exist, the request fails with error {Google::Cloud::NotFoundError}.
154
+ #
155
+ # All changes are accumulated in memory until the block passed to
156
+ # {Client#batch_write} completes.
157
+ #
158
+ # @param [String] table The name of the table in the database to be
159
+ # modified.
160
+ # @param [Array<Hash>] rows One or more hash objects with the hash keys
161
+ # matching the table's columns, and the hash values matching the
162
+ # table's values.
163
+ #
164
+ # Ruby types are mapped to Spanner types as follows:
165
+ #
166
+ # | Spanner | Ruby | Notes |
167
+ # |-------------|----------------|---|
168
+ # | `BOOL` | `true`/`false` | |
169
+ # | `INT64` | `Integer` | |
170
+ # | `FLOAT64` | `Float` | |
171
+ # | `FLOAT32` | `Float` | |
172
+ # | `NUMERIC` | `BigDecimal` | |
173
+ # | `STRING` | `String` | |
174
+ # | `DATE` | `Date` | |
175
+ # | `TIMESTAMP` | `Time`, `DateTime` | |
176
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
177
+ # | `ARRAY` | `Array` | Nested arrays are not supported. |
178
+ #
179
+ # See [Data
180
+ # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
181
+ #
182
+ # @example
183
+ # require "google/cloud/spanner"
184
+ #
185
+ # spanner = Google::Cloud::Spanner.new
186
+ #
187
+ # db = spanner.client "my-instance", "my-database"
188
+ #
189
+ # results = db.batch_write do |b|
190
+ # b.mutation_group do |mg|
191
+ # mg.update "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
192
+ # end
193
+ # end
194
+ #
195
+ def update table, *rows
196
+ @commit.update table, rows
197
+ end
198
+
199
+ ##
200
+ # Inserts or replaces rows in a table. If any of the rows already exist,
201
+ # it is deleted, and the column values provided are inserted instead.
202
+ # Unlike #upsert, this means any values not explicitly written become
203
+ # `NULL`.
204
+ #
205
+ # All changes are accumulated in memory until the block passed to
206
+ # {Client#batch_write} completes.
207
+ #
208
+ # @param [String] table The name of the table in the database to be
209
+ # modified.
210
+ # @param [Array<Hash>] rows One or more hash objects with the hash keys
211
+ # matching the table's columns, and the hash values matching the
212
+ # table's values.
213
+ #
214
+ # Ruby types are mapped to Spanner types as follows:
215
+ #
216
+ # | Spanner | Ruby | Notes |
217
+ # |-------------|----------------|---|
218
+ # | `BOOL` | `true`/`false` | |
219
+ # | `INT64` | `Integer` | |
220
+ # | `FLOAT64` | `Float` | |
221
+ # | `FLOAT32` | `Float` | |
222
+ # | `NUMERIC` | `BigDecimal` | |
223
+ # | `STRING` | `String` | |
224
+ # | `DATE` | `Date` | |
225
+ # | `TIMESTAMP` | `Time`, `DateTime` | |
226
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
227
+ # | `ARRAY` | `Array` | Nested arrays are not supported. |
228
+ #
229
+ # See [Data
230
+ # types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
231
+ #
232
+ # @example
233
+ # require "google/cloud/spanner"
234
+ #
235
+ # spanner = Google::Cloud::Spanner.new
236
+ #
237
+ # db = spanner.client "my-instance", "my-database"
238
+ #
239
+ # results = db.batch_write do |b|
240
+ # b.mutation_group do |mg|
241
+ # mg.replace "Singers", [{ SingerId: 16, FirstName: "Charlie", LastName: "Terry" }]
242
+ # end
243
+ # end
244
+ #
245
+ def replace table, *rows
246
+ @commit.replace table, rows
247
+ end
248
+
249
+ ##
250
+ # Deletes rows from a table. Succeeds whether or not the specified rows
251
+ # were present.
252
+ #
253
+ # All changes are accumulated in memory until the block passed to
254
+ # {Client#batch_write} completes.
255
+ #
256
+ # @param [String] table The name of the table in the database to be
257
+ # modified.
258
+ # @param [Object, Array<Object>] keys A single, or list of keys or key
259
+ # ranges to match returned data to. Values should have exactly as many
260
+ # elements as there are columns in the primary key.
261
+ #
262
+ # @example
263
+ # require "google/cloud/spanner"
264
+ #
265
+ # spanner = Google::Cloud::Spanner.new
266
+ #
267
+ # db = spanner.client "my-instance", "my-database"
268
+ #
269
+ # results = db.batch_write do |b|
270
+ # b.mutation_group do |mg|
271
+ # mg.delete "Singers", [1, 2, 3]
272
+ # end
273
+ # end
274
+ #
275
+ def delete table, keys = []
276
+ @commit.delete table, keys
277
+ end
278
+
279
+ ##
280
+ # @private
281
+ # All of the mutations created in the transaction block.
282
+ def mutations
283
+ @commit.mutations
284
+ end
285
+ end
286
+ end
287
+ end
288
+ end
@@ -106,9 +106,7 @@ module Google
106
106
  # @return [Array<Google::Cloud::Spanner::Instance>] The list of
107
107
  # instances. (See {Google::Cloud::Spanner::Instance::List})
108
108
  #
109
- # @deprecated Use
110
- # {Google::Cloud::Spanner::Admin::Instance#instance_admin Client#list_instances}
111
- # instead.
109
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Instance.instance_admin}.list_instances instead.
112
110
  #
113
111
  # @example
114
112
  # require "google/cloud/spanner"
@@ -144,9 +142,7 @@ module Google
144
142
  # @return [Google::Cloud::Spanner::Instance, nil] The instance, or `nil`
145
143
  # if the instance does not exist.
146
144
  #
147
- # @deprecated Use
148
- # {Google::Cloud::Spanner::Admin::Instance#instance_admin Client#get_instance}
149
- # instead.
145
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Instance.instance_admin}.get_instance instead.
150
146
  #
151
147
  # @example
152
148
  # require "google/cloud/spanner"
@@ -208,9 +204,7 @@ module Google
208
204
  # asynchronous processing of an instance create operation.
209
205
  # @raise [ArgumentError] if both processing_units or nodes are specified.
210
206
  #
211
- # @deprecated Use
212
- # {Google::Cloud::Spanner::Admin::Instance#instance_admin Client#create_instance}
213
- # instead.
207
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Instance.instance_admin}.create_instance instead.
214
208
  #
215
209
  # @example
216
210
  # require "google/cloud/spanner"
@@ -278,9 +272,7 @@ module Google
278
272
  # instance configurations. (See
279
273
  # {Google::Cloud::Spanner::Instance::Config::List})
280
274
  #
281
- # @deprecated Use
282
- # {Google::Cloud::Spanner::Admin::Instance#instance_admin Client#list_instance_configs}
283
- # instead.
275
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Instance.instance_admin}.list_instance_configs instead.
284
276
  #
285
277
  # @example
286
278
  # require "google/cloud/spanner"
@@ -319,9 +311,7 @@ module Google
319
311
  # configuration, or `nil` if the instance configuration does not
320
312
  # exist.
321
313
  #
322
- # @deprecated Use
323
- # {Google::Cloud::Spanner::Admin::Instance#instance_admin Client#get_instance_config}
324
- # instead.
314
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Instance.instance_admin}.get_instance_config instead.
325
315
  #
326
316
  # @example
327
317
  # require "google/cloud/spanner"
@@ -355,9 +345,7 @@ module Google
355
345
  # @return [Array<Google::Cloud::Spanner::Database>] The list of
356
346
  # databases. (See {Google::Cloud::Spanner::Database::List})
357
347
  #
358
- # @deprecated Use
359
- # {Google::Cloud::Spanner::Admin::Database#database_admin Client#list_databases}
360
- # instead.
348
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Database.database_admin}.list_databases instead.
361
349
  #
362
350
  # @example
363
351
  # require "google/cloud/spanner"
@@ -394,9 +382,7 @@ module Google
394
382
  # @return [Google::Cloud::Spanner::Database, nil] The database, or `nil`
395
383
  # if the database does not exist.
396
384
  #
397
- # @deprecated Use
398
- # {Google::Cloud::Spanner::Admin::Database#database_admin Client#get_database}
399
- # instead.
385
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Database.database_admin}.get_database instead.
400
386
  #
401
387
  # @example
402
388
  # require "google/cloud/spanner"
@@ -445,9 +431,7 @@ module Google
445
431
  # @return [Database::Job] The job representing the long-running,
446
432
  # asynchronous processing of a database create operation.
447
433
  #
448
- # @deprecated Use
449
- # {Google::Cloud::Spanner::Admin::Database#database_admin Client#create_database}
450
- # instead.
434
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Database.database_admin}.create_database instead.
451
435
  #
452
436
  # @example
453
437
  # require "google/cloud/spanner"
@@ -688,9 +672,7 @@ module Google
688
672
  raise "Must have active connection to service" unless service
689
673
  end
690
674
 
691
- # @deprecated Use
692
- # {Google::Cloud::Spanner::Admin::Database#database_admin Client#database_path}
693
- # instead.
675
+ # @deprecated Use {Google::Cloud::Spanner::Admin::Database.database_admin}.database_path instead.
694
676
  def database_path instance_id, database_id
695
677
  Admin::Database::V1::DatabaseAdminClient.database_path(
696
678
  project, instance_id, database_id
@@ -435,13 +435,15 @@ module Google
435
435
  service.partition_query request, opts
436
436
  end
437
437
 
438
- def commit session_name, mutations = [], transaction_id: nil,
438
+ def commit session_name, mutations = [],
439
+ transaction_id: nil, exclude_txn_from_change_streams: false,
439
440
  commit_options: nil, request_options: nil, call_options: nil
440
441
  route_to_leader = LARHeaders.commit
441
442
  tx_opts = nil
442
443
  if transaction_id.nil?
443
444
  tx_opts = V1::TransactionOptions.new(
444
- read_write: V1::TransactionOptions::ReadWrite.new
445
+ read_write: V1::TransactionOptions::ReadWrite.new,
446
+ exclude_txn_from_change_streams: exclude_txn_from_change_streams
445
447
  )
446
448
  end
447
449
  opts = default_options session_name: session_name,
@@ -482,11 +484,14 @@ module Google
482
484
  service.rollback request, opts
483
485
  end
484
486
 
485
- def begin_transaction session_name, request_options: nil,
487
+ def begin_transaction session_name,
488
+ exclude_txn_from_change_streams: false,
489
+ request_options: nil,
486
490
  call_options: nil,
487
491
  route_to_leader: nil
488
492
  tx_opts = V1::TransactionOptions.new(
489
- read_write: V1::TransactionOptions::ReadWrite.new
493
+ read_write: V1::TransactionOptions::ReadWrite.new,
494
+ exclude_txn_from_change_streams: exclude_txn_from_change_streams
490
495
  )
491
496
  opts = default_options session_name: session_name,
492
497
  call_options: call_options,
@@ -499,6 +504,24 @@ module Google
499
504
  service.begin_transaction request, opts
500
505
  end
501
506
 
507
+ def batch_write session_name,
508
+ mutation_groups,
509
+ exclude_txn_from_change_streams: false,
510
+ request_options: nil,
511
+ call_options: nil
512
+ route_to_leader = LARHeaders.batch_write
513
+ opts = default_options session_name: session_name,
514
+ call_options: call_options,
515
+ route_to_leader: route_to_leader
516
+ request = {
517
+ session: session_name,
518
+ request_options: request_options,
519
+ mutation_groups: mutation_groups,
520
+ exclude_txn_from_change_streams: exclude_txn_from_change_streams
521
+ }
522
+ service.batch_write request, opts
523
+ end
524
+
502
525
  def create_snapshot session_name, strong: nil, timestamp: nil,
503
526
  staleness: nil, call_options: nil
504
527
  tx_opts = V1::TransactionOptions.new(
@@ -517,9 +540,12 @@ module Google
517
540
  service.begin_transaction request, opts
518
541
  end
519
542
 
520
- def create_pdml session_name, call_options: nil
543
+ def create_pdml session_name,
544
+ exclude_txn_from_change_streams: false,
545
+ call_options: nil
521
546
  tx_opts = V1::TransactionOptions.new(
522
- partitioned_dml: V1::TransactionOptions::PartitionedDml.new
547
+ partitioned_dml: V1::TransactionOptions::PartitionedDml.new,
548
+ exclude_txn_from_change_streams: exclude_txn_from_change_streams
523
549
  )
524
550
  route_to_leader = LARHeaders.begin_transaction true
525
551
  opts = default_options session_name: session_name,