Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions spanner/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-spanner": "^1.11",
"google/cloud-spanner": "^1.26",
"symfony/console": "^3.2"
},
"autoload": {
Expand Down Expand Up @@ -52,7 +52,9 @@
"src/query_data_with_float_parameter.php",
"src/query_data_with_int_parameter.php",
"src/query_data_with_string_parameter.php",
"src/query_data_with_timestamp_parameter.php"
"src/query_data_with_timestamp_parameter.php",
"src/create_client_with_query_options.php",
"src/query_data_with_query_options.php"
]
}
}
24 changes: 24 additions & 0 deletions spanner/spanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,30 @@
})
);

// Query Data with Query Options
$application->add((new Command('query-data-with-query-options'))
->setDefinition($inputDefinition)
->setDescription('Queries sample data using SQL with query options.')
->setCode(function ($input, $output) {
query_data_with_query_options(
$input->getArgument('instance_id'),
$input->getArgument('database_id')
);
})
);

// Create Client With Query Options
$application->add((new Command('create-client-with-query-options'))
->setDefinition($inputDefinition)
->setDescription('Create a client with query options.')
->setCode(function ($input, $output) {
create_client_with_query_options(
$input->getArgument('instance_id'),
$input->getArgument('database_id')
);
})
);

// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
return $application;
Expand Down
59 changes: 59 additions & 0 deletions spanner/src/create_client_with_query_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
*/

namespace Google\Cloud\Samples\Spanner;

// [START spanner_create_client_with_query_options]
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Database;

/**
* Create a client with query options.
* Example:
* ```
* create_client_with_query_options($instanceId, $databaseId);
* ```
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function create_client_with_query_options($instanceId, $databaseId)
{
$spanner = new SpannerClient([
'queryOptions' => [
'optimizerVersion' => "1"
]
]);
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$results = $database->execute(
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues'
);

foreach ($results as $row) {
printf('VenueId: %s, VenueName: %s, LastUpdateTime: %s' . PHP_EOL,
$row['VenueId'], $row['VenueName'], $row['LastUpdateTime']);
}
}
// [END spanner_create_client_with_query_options]
60 changes: 60 additions & 0 deletions spanner/src/query_data_with_query_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
*/

namespace Google\Cloud\Samples\Spanner;

// [START spanner_query_with_query_options]
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Database;

/**
* Queries sample data using SQL with query options.
* Example:
* ```
* query_data_with_query_options($instanceId, $databaseId);
* ```
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function query_data_with_query_options($instanceId, $databaseId)
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$results = $database->execute(
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues',
[
'queryOptions' => [
'optimizerVersion' => "1"
]
]
);

foreach ($results as $row) {
printf('VenueId: %s, VenueName: %s, LastUpdateTime: %s' . PHP_EOL,
$row['VenueId'], $row['VenueName'], $row['LastUpdateTime']);
}
}
// [END spanner_query_with_query_options]
28 changes: 28 additions & 0 deletions spanner/test/spannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,34 @@ public function testQueryDataWithTimestampParameter()
});
}

/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithQueryOptions()
{
$this->runEventuallyConsistentTest(function () {
$output = $this->runCommand('query-data-with-query-options');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastUpdateTime:', $output);
$this->assertContains('VenueId: 19, VenueName: Venue 19, LastUpdateTime:', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastUpdateTime:', $output);
});
}

/**
* @depends testInsertDataWithDatatypes
*/
public function testCreateClientWithQueryOptions()
{
$this->runEventuallyConsistentTest(function () {
$output = $this->runCommand('create-client-with-query-options');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastUpdateTime:', $output);
$this->assertContains('VenueId: 19, VenueName: Venue 19, LastUpdateTime:', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastUpdateTime:', $output);
});
}

private function runCommand($commandName)
{
return $this->traitRunCommand($commandName, [
Expand Down