Skip to content

Commit b3fc37c

Browse files
committed
Add queryWithParameter to Cloud Spanner sample.
1 parent 882adac commit b3fc37c

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

spanner/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,19 @@ To run the Spanner Samples:
8484
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
8585

8686
Available commands:
87-
add-column Adds a new column to the Albums table in the example database.
8887
add-column Adds a new column to the Albums table in the example database.
8988
add-timestamp-column Adds a commit timestamp column to a table.
9089
batch-query-data Batch queries sample data from the database using SQL.
9190
create-database Creates a database and tables for sample data.
9291
create-index Adds a simple index to the example database.
9392
create-storing-index Adds an storing index to the example database.
9493
create-table-timestamp Creates a table with a commit timestamp column.
94+
delete-data-with-dml Remove sample data from the given database with a DML statement.
95+
deleted-data-with-partitioned-dml Deletes sample data in the database by partition with a DML statement.
9596
help Displays help for a command
9697
insert-data Inserts sample data into the given database.
9798
insert-data-timestamp Inserts data into a table with a commit timestamp column.
99+
insert-data-with-dml Inserts sample data into the given database with a DML statement.
98100
insert-struct-data Inserts sample data that can be used to test STRUCT parameters in queries.
99101
list Lists commands
100102
query-data Queries sample data from the database using SQL.
@@ -103,6 +105,7 @@ To run the Spanner Samples:
103105
query-data-with-index Queries sample data from the database using SQL and an index.
104106
query-data-with-nested-struct-field Queries sample data from the database with a nested struct field value.
105107
query-data-with-new-column Queries sample data from the database using SQL.
108+
query-data-with-parameter Query DML inserted sample data using SQL with a parameter.
106109
query-data-with-struct Queries sample data from the database with a struct.
107110
query-data-with-struct-field Queries sample data from the database with a struct field value.
108111
read-data Reads sample data from the database.
@@ -113,6 +116,14 @@ To run the Spanner Samples:
113116
read-write-transaction Performs a read-write transaction to update two sample records in the database.
114117
update-data Updates sample data in the database.
115118
update-data-timestamp Updates sample data in a table with a commit timestamp column.
119+
update-data-with-batch-dml Updates sample data in the given database using Batch DML.
120+
update-data-with-dml Updates sample data into the given database with a DML statement.
121+
update-data-with-dml-structs Updates data using DML statement with structs.
122+
update-data-with-dml-timestamp Update sample data from the given database with a DML statement and timestamp.
123+
update-data-with-partitioned-dml Updates sample data in the database by partition with a DML statement.
124+
write-data-with-dml Writes sample data into the given database with a DML statement.
125+
write-data-with-dml-transaction Performs a read-write transaction to update two sample records in the database.
126+
write-read-with-dml Writes then reads data inside a Transaction with a DML statement.
116127

117128

118129
## Troubleshooting

spanner/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"src/write_read_with_dml.php",
4242
"src/update_data_with_dml_structs.php",
4343
"src/write_data_with_dml.php",
44+
"src/query_data_with_parameter.php",
4445
"src/write_data_with_dml_transaction.php",
4546
"src/update_data_with_partitioned_dml.php",
4647
"src/delete_data_with_partitioned_dml.php",

spanner/spanner.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@
432432
})
433433
);
434434

435+
// Query Data with Parameter
436+
$application->add((new Command('query-data-with-parameter'))
437+
->setDefinition($inputDefinition)
438+
->setDescription('Query DML inserted sample data using SQL with a parameter.')
439+
->setCode(function ($input, $output) {
440+
query_data_with_parameter(
441+
$input->getArgument('instance_id'),
442+
$input->getArgument('database_id')
443+
);
444+
})
445+
);
446+
435447
// Write Data with DML Transaction
436448
$application->add((new Command('write-data-with-dml-transaction'))
437449
->setDefinition($inputDefinition)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2019 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_query_with_parameter]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Queries sample data from the database using SQL with a parameter.
31+
* Example:
32+
* ```
33+
* query_data_with_parameter($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function query_data_with_parameter($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$results = $database->execute(
46+
'SELECT SingerId, FirstName, LastName FROM Singers '.
47+
'WHERE LastName = @lastName',
48+
['parameters' => ['lastName' => 'Garcia']]
49+
);
50+
51+
foreach ($results as $row) {
52+
printf('SingerId: %s, FirstName: %s, LastName: %s' . PHP_EOL,
53+
$row['SingerId'], $row['FirstName'], $row['LastName']);
54+
}
55+
}
56+
// [END spanner_query_with_parameter]

spanner/test/spannerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ public function testWriteDataWithDML()
418418
$this->assertContains('Inserted 4 row(s)', $output);
419419
}
420420

421+
/**
422+
* @depends testWriteDataWithDML
423+
*/
424+
public function testQueryDataWithParameter()
425+
{
426+
$output = $this->runCommand('query-data-with-parameter');
427+
self::$lastUpdateDataTimestamp = time();
428+
$this->assertContains('SingerId: 12, FirstName: Melissa, LastName: Garcia', $output);
429+
}
430+
421431
/**
422432
* @depends testAddColumn
423433
*/

0 commit comments

Comments
 (0)