Skip to content

Commit ca02763

Browse files
authored
Merge pull request GoogleCloudPlatform#1041 from larkee/spanner-query-versioning-samples
feat: add query optimizer version samples
2 parents eb28409 + cf01ddb commit ca02763

File tree

5 files changed

+175
-2
lines changed

5 files changed

+175
-2
lines changed

spanner/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-spanner": "^1.11",
3+
"google/cloud-spanner": "^1.26",
44
"symfony/console": "^3.2"
55
},
66
"autoload": {
@@ -52,7 +52,9 @@
5252
"src/query_data_with_float_parameter.php",
5353
"src/query_data_with_int_parameter.php",
5454
"src/query_data_with_string_parameter.php",
55-
"src/query_data_with_timestamp_parameter.php"
55+
"src/query_data_with_timestamp_parameter.php",
56+
"src/create_client_with_query_options.php",
57+
"src/query_data_with_query_options.php"
5658
]
5759
}
5860
}

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_query_options(
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+
create_client_with_query_options(
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2020 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_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+
);
53+
54+
foreach ($results as $row) {
55+
printf('VenueId: %s, VenueName: %s, LastUpdateTime: %s' . PHP_EOL,
56+
$row['VenueId'], $row['VenueName'], $row['LastUpdateTime']);
57+
}
58+
}
59+
// [END spanner_create_client_with_query_options]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2020 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_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+
[
49+
'queryOptions' => [
50+
'optimizerVersion' => "1"
51+
]
52+
]
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 spanner_query_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)