Skip to content

Commit 85e020c

Browse files
committed
spanner: add query versioning samples
1 parent 1b4dc4a commit 85e020c

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

spanner/spanner.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,30 @@
612612
})
613613
);
614614

615+
// Query Data with Query Options
616+
$application->add((new Command('query-data-with-query-options'))
617+
->setDefinition($inputDefinition)
618+
->setDescription('Queries sample data using SQL with query options.')
619+
->setCode(function ($input, $output) {
620+
query_data_with_timestamp_parameter(
621+
$input->getArgument('instance_id'),
622+
$input->getArgument('database_id')
623+
);
624+
})
625+
);
626+
627+
// Create Client With Query Options
628+
$application->add((new Command('create-client-with-query-options'))
629+
->setDefinition($inputDefinition)
630+
->setDescription('Create a client with query options.')
631+
->setCode(function ($input, $output) {
632+
query_data_with_timestamp_parameter(
633+
$input->getArgument('instance_id'),
634+
$input->getArgument('database_id')
635+
);
636+
})
637+
);
638+
615639
// for testing
616640
if (getenv('PHPUNIT_TESTS') === '1') {
617641
return $application;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 create_client_with_query_options]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Database;
29+
30+
/**
31+
* Create a client with query options.
32+
* Example:
33+
* ```
34+
* create_client_with_query_options($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function create_client_with_query_options($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient([
43+
'queryOptions' => [
44+
'optimizerVersion' => "1"
45+
]
46+
]);
47+
$instance = $spanner->instance($instanceId);
48+
$database = $instance->database($databaseId);
49+
50+
$results = $database->execute(
51+
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues ' .
52+
'WHERE LastUpdateTime < @lastUpdateTime'
53+
);
54+
55+
foreach ($results as $row) {
56+
printf('VenueId: %s, VenueName: %s, LastUpdateTime: %s' . PHP_EOL,
57+
$row['VenueId'], $row['VenueName'], $row['LastUpdateTime']);
58+
}
59+
}
60+
// [END create_client_with_query_options]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 query_data_with_query_options]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Database;
29+
30+
/**
31+
* Queries sample data using SQL with query options.
32+
* Example:
33+
* ```
34+
* query_data_with_query_options($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function query_data_with_query_options($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$results = $database->execute(
47+
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues ' .
48+
'WHERE LastUpdateTime < @lastUpdateTime',
49+
[
50+
'queryOptions' => [
51+
'optimizerVersion' => "1"
52+
]
53+
]
54+
);
55+
56+
foreach ($results as $row) {
57+
printf('VenueId: %s, VenueName: %s, LastUpdateTime: %s' . PHP_EOL,
58+
$row['VenueId'], $row['VenueName'], $row['LastUpdateTime']);
59+
}
60+
}
61+
// [END query_data_with_query_options]

spanner/test/spannerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,34 @@ public function testQueryDataWithTimestampParameter()
578578
});
579579
}
580580

581+
/**
582+
* @depends testInsertDataWithDatatypes
583+
*/
584+
public function testQueryDataWithQueryOptions()
585+
{
586+
$this->runEventuallyConsistentTest(function () {
587+
$output = $this->runCommand('query-data-with-query-options');
588+
self::$lastUpdateDataTimestamp = time();
589+
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastUpdateTime:', $output);
590+
$this->assertContains('VenueId: 19, VenueName: Venue 19, LastUpdateTime:', $output);
591+
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastUpdateTime:', $output);
592+
});
593+
}
594+
595+
/**
596+
* @depends testInsertDataWithDatatypes
597+
*/
598+
public function testCreateClientWithQueryOptions()
599+
{
600+
$this->runEventuallyConsistentTest(function () {
601+
$output = $this->runCommand('create-client-with-query-options');
602+
self::$lastUpdateDataTimestamp = time();
603+
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastUpdateTime:', $output);
604+
$this->assertContains('VenueId: 19, VenueName: Venue 19, LastUpdateTime:', $output);
605+
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastUpdateTime:', $output);
606+
});
607+
}
608+
581609
private function runCommand($commandName)
582610
{
583611
return $this->traitRunCommand($commandName, [

0 commit comments

Comments
 (0)