Skip to content

Commit 8814af2

Browse files
authored
Added Spanner DML/PDML samples. (GoogleCloudPlatform#733)
* Added Spanner DML/PDML samples. * Bump versions. Change-Id: I1de56960f6f5c194afa0247e821353b8c5687cec * Fix testUpdateDataWithDmlStructs. Change-Id: I381dd370e3bd4970ff295bc7017c093051fd8ceb * Update result->rows API. Change-Id: I54d4b77168fa7991f33a750c9bb966814150094b * Explictly declare generator usage.
1 parent 0a11ea0 commit 8814af2

13 files changed

+883
-2
lines changed

spanner/composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-spanner": "^1.6",
3+
"google/cloud-spanner": "^1.11",
44
"symfony/console": "^3.2"
55
},
66
"require-dev": {
@@ -33,7 +33,17 @@
3333
"src/query_data_with_struct.php",
3434
"src/query_data_with_array_of_struct.php",
3535
"src/query_data_with_struct_field.php",
36-
"src/query_data_with_nested_struct_field.php"
36+
"src/query_data_with_nested_struct_field.php",
37+
"src/insert_data_with_dml.php",
38+
"src/update_data_with_dml.php",
39+
"src/delete_data_with_dml.php",
40+
"src/update_data_with_dml_timestamp.php",
41+
"src/write_read_with_dml.php",
42+
"src/update_data_with_dml_structs.php",
43+
"src/write_data_with_dml.php",
44+
"src/write_data_with_dml_transaction.php",
45+
"src/update_data_with_partitioned_dml.php",
46+
"src/delete_data_with_partitioned_dml.php"
3747
]
3848
}
3949
}

spanner/spanner.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,127 @@
348348
})
349349
);
350350

351+
// Insert data with DML
352+
$application->add((new Command('insert-data-with-dml'))
353+
->setDefinition($inputDefinition)
354+
->setDescription('Inserts sample data into the given database with a DML statement.')
355+
->setCode(function ($input, $output) {
356+
insert_data_with_dml(
357+
$input->getArgument('instance_id'),
358+
$input->getArgument('database_id')
359+
);
360+
})
361+
);
362+
363+
// Update data with DML
364+
$application->add((new Command('update-data-with-dml'))
365+
->setDefinition($inputDefinition)
366+
->setDescription('Updates sample data into the given database with a DML statement.')
367+
->setCode(function ($input, $output) {
368+
update_data_with_dml(
369+
$input->getArgument('instance_id'),
370+
$input->getArgument('database_id')
371+
);
372+
})
373+
);
374+
375+
// Delete data with DML
376+
$application->add((new Command('delete-data-with-dml'))
377+
->setDefinition($inputDefinition)
378+
->setDescription('Remove sample data from the given database with a DML statement.')
379+
->setCode(function ($input, $output) {
380+
delete_data_with_dml(
381+
$input->getArgument('instance_id'),
382+
$input->getArgument('database_id')
383+
);
384+
})
385+
);
386+
387+
// Update data with DML Timestamp
388+
$application->add((new Command('update-data-with-dml-timestamp'))
389+
->setDefinition($inputDefinition)
390+
->setDescription('Update sample data from the given database with a DML statement and timestamp.')
391+
->setCode(function ($input, $output) {
392+
update_data_with_dml_timestamp(
393+
$input->getArgument('instance_id'),
394+
$input->getArgument('database_id')
395+
);
396+
})
397+
);
398+
399+
// Write Read with DML
400+
$application->add((new Command('write-read-with-dml'))
401+
->setDefinition($inputDefinition)
402+
->setDescription('Writes then reads data inside a Transaction with a DML statement.')
403+
->setCode(function ($input, $output) {
404+
write_read_with_dml(
405+
$input->getArgument('instance_id'),
406+
$input->getArgument('database_id')
407+
);
408+
})
409+
);
410+
411+
// Update Data with DML Structs
412+
$application->add((new Command('update-data-with-dml-structs'))
413+
->setDefinition($inputDefinition)
414+
->setDescription('Updates data using DML statement with structs.')
415+
->setCode(function ($input, $output) {
416+
update_data_with_dml_structs(
417+
$input->getArgument('instance_id'),
418+
$input->getArgument('database_id')
419+
);
420+
})
421+
);
422+
423+
// Write Data with DML
424+
$application->add((new Command('write-data-with-dml'))
425+
->setDefinition($inputDefinition)
426+
->setDescription('Writes sample data into the given database with a DML statement.')
427+
->setCode(function ($input, $output) {
428+
write_data_with_dml(
429+
$input->getArgument('instance_id'),
430+
$input->getArgument('database_id')
431+
);
432+
})
433+
);
434+
435+
// Write Data with DML Transaction
436+
$application->add((new Command('write-data-with-dml-transaction'))
437+
->setDefinition($inputDefinition)
438+
->setDescription('Performs a read-write transaction to update two sample records in the database.')
439+
->setCode(function ($input, $output) {
440+
write_data_with_dml_transaction(
441+
$input->getArgument('instance_id'),
442+
$input->getArgument('database_id')
443+
);
444+
})
445+
);
446+
447+
// Update Data with Partitioned DML
448+
$application->add((new Command('update-data-with-partitioned-dml'))
449+
->setDefinition($inputDefinition)
450+
->setDescription('Updates sample data in the database by partition with a DML statement.')
451+
->setCode(function ($input, $output) {
452+
update_data_with_partitioned_dml(
453+
$input->getArgument('instance_id'),
454+
$input->getArgument('database_id')
455+
);
456+
})
457+
);
458+
459+
// Delete Data with Partitioned DML
460+
$application->add((new Command('deleted-data-with-partitioned-dml'))
461+
->setDefinition($inputDefinition)
462+
->setDescription('Deletes sample data in the database by partition with a DML statement.')
463+
->setCode(function ($input, $output) {
464+
delete_data_with_partitioned_dml(
465+
$input->getArgument('instance_id'),
466+
$input->getArgument('database_id')
467+
);
468+
})
469+
);
470+
471+
351472
// for testing
352473
if (getenv('PHPUNIT_TESTS') === '1') {
353474
return $application;

spanner/src/delete_data_with_dml.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Copyright 2018 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_standard_delete]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Deletes sample data in the database with a DML statement.
32+
*
33+
* @param string $instanceId The Spanner instance ID.
34+
* @param string $databaseId The Spanner database ID.
35+
*/
36+
function delete_data_with_dml($instanceId, $databaseId)
37+
{
38+
$spanner = new SpannerClient();
39+
$instance = $spanner->instance($instanceId);
40+
$database = $instance->database($databaseId);
41+
42+
$database->runTransaction(function (Transaction $t) use ($spanner) {
43+
$rowCount = $t->executeUpdate(
44+
"DELETE Singers WHERE FirstName = 'Alice'");
45+
$t->commit();
46+
printf('Deleted %d row(s).' . PHP_EOL, $rowCount);
47+
});
48+
49+
}
50+
// [END spanner_dml_standard_delete]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2018 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_partitioned_delete]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Delete sample data in the database by partition with a DML statement.
31+
*
32+
* This updates the `MarketingBudget` column which must be created before
33+
* running this sample. You can add the column by running the `add_column`
34+
* sample or by running this DDL statement against your database:
35+
*
36+
* ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
37+
*
38+
* Example:
39+
* ```
40+
* update_data($instanceId, $databaseId);
41+
* ```
42+
*
43+
* @param string $instanceId The Spanner instance ID.
44+
* @param string $databaseId The Spanner database ID.
45+
*/
46+
function delete_data_with_partitioned_dml($instanceId, $databaseId)
47+
{
48+
$spanner = new SpannerClient();
49+
$instance = $spanner->instance($instanceId);
50+
$database = $instance->database($databaseId);
51+
52+
$rowCount = $database->executePartitionedUpdate(
53+
"DELETE Singers WHERE SingerId > 10"
54+
);
55+
56+
printf('Deleted %d row(s).' . PHP_EOL, $rowCount);
57+
}
58+
// [END spanner_dml_partitioned_delete]

spanner/src/insert_data_with_dml.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2018 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_standard_insert]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Inserts sample data into the given database with a DML statement.
32+
*
33+
* The database and table must already exist and can be created using
34+
* `create_database`.
35+
* Example:
36+
* ```
37+
* insert_data($instanceId, $databaseId);
38+
* ```
39+
*
40+
* @param string $instanceId The Spanner instance ID.
41+
* @param string $databaseId The Spanner database ID.
42+
*/
43+
function insert_data_with_dml($instanceId, $databaseId)
44+
{
45+
$spanner = new SpannerClient();
46+
$instance = $spanner->instance($instanceId);
47+
$database = $instance->database($databaseId);
48+
49+
$database->runTransaction(function (Transaction $t) use ($spanner) {
50+
$rowCount = $t->executeUpdate(
51+
"INSERT Singers (SingerId, FirstName, LastName) "
52+
. " VALUES (10, 'Virginia', 'Watson')");
53+
$t->commit();
54+
printf('Inserted %d row(s).' . PHP_EOL, $rowCount);
55+
});
56+
}
57+
// [END spanner_dml_standard_insert]

spanner/src/update_data_with_dml.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
/**
3+
* Copyright 2018 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_standard_update]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Updates sample data in the database with a DML statement.
32+
*
33+
* This requires the `MarketingBudget` column which must be created before
34+
* running this sample. You can add the column by running the `add_column`
35+
* sample or by running this DDL statement against your database:
36+
*
37+
* ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
38+
*
39+
* Example:
40+
* ```
41+
* update_data($instanceId, $databaseId);
42+
* ```
43+
*
44+
* @param string $instanceId The Spanner instance ID.
45+
* @param string $databaseId The Spanner database ID.
46+
*/
47+
function update_data_with_dml($instanceId, $databaseId)
48+
{
49+
$spanner = new SpannerClient();
50+
$instance = $spanner->instance($instanceId);
51+
$database = $instance->database($databaseId);
52+
53+
$database->runTransaction(function (Transaction $t) use ($spanner) {
54+
$rowCount = $t->executeUpdate(
55+
"UPDATE Albums "
56+
. "SET MarketingBudget = MarketingBudget * 2 "
57+
. "WHERE SingerId = 1 and AlbumId = 1");
58+
$t->commit();
59+
printf('Updated %d row(s).' . PHP_EOL, $rowCount);
60+
});
61+
62+
}
63+
// [END spanner_dml_standard_update]

0 commit comments

Comments
 (0)