|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Spanner; |
| 25 | + |
| 26 | +// [START spanner_dml_batch_update_request_priority] |
| 27 | +use Google\Cloud\Spanner\SpannerClient; |
| 28 | +use Google\Cloud\Spanner\Transaction; |
| 29 | +use Google\Cloud\Spanner\V1\RequestOptions\Priority; |
| 30 | + |
| 31 | +/** |
| 32 | + * Updates sample data in the database with PRIORITY_LOW using Batch DML. |
| 33 | + * |
| 34 | + * This requires the `MarketingBudget` column which must be created before |
| 35 | + * running this sample. You can add the column by running the `add_column` |
| 36 | + * sample or by running this DDL statement against your database: |
| 37 | + * |
| 38 | + * ALTER TABLE Albums ADD COLUMN MarketingBudget INT64 |
| 39 | + * |
| 40 | + * Example: |
| 41 | + * ``` |
| 42 | + * dml_batch_update_request_priority($instanceId, $databaseId); |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * @param string $instanceId The Spanner instance ID. |
| 46 | + * @param string $databaseId The Spanner database ID. |
| 47 | + */ |
| 48 | +function dml_batch_update_request_priority(string $instanceId, string $databaseId): void |
| 49 | +{ |
| 50 | + $spanner = new SpannerClient(); |
| 51 | + $instance = $spanner->instance($instanceId); |
| 52 | + $database = $instance->database($databaseId); |
| 53 | + |
| 54 | + $batchDmlResult = $database->runTransaction(function (Transaction $t) { |
| 55 | + // Variable to define the Priority of this operation |
| 56 | + // For more information read [ |
| 57 | + // the upstream documentation](https://cloud.google.com/spanner/docs/reference/rest/v1/RequestOptions) |
| 58 | + $priority = Priority::PRIORITY_LOW; |
| 59 | + |
| 60 | + $result = $t->executeUpdateBatch([ |
| 61 | + [ |
| 62 | + 'sql' => 'UPDATE Albums ' |
| 63 | + . 'SET MarketingBudget = MarketingBudget * 2 ' |
| 64 | + . 'WHERE SingerId = 1 and AlbumId = 3' |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'sql' => 'UPDATE Albums ' |
| 68 | + . 'SET MarketingBudget = MarketingBudget * 2 ' |
| 69 | + . 'WHERE SingerId = 2 and AlbumId = 3' |
| 70 | + ], |
| 71 | + ], array('priority' => $priority)); |
| 72 | + $t->commit(); |
| 73 | + $rowCounts = count($result->rowCounts()); |
| 74 | + printf('Executed %s SQL statements using Batch DML with PRIORITY_LOW.' . PHP_EOL, |
| 75 | + $rowCounts); |
| 76 | + }); |
| 77 | +} |
| 78 | +// [END spanner_dml_batch_update_request_priority] |
| 79 | + |
| 80 | +// The following 2 lines are only needed to run the samples |
| 81 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 82 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments