Skip to content

Commit c1b12f3

Browse files
authored
samples(spanner): add tagging samples (GoogleCloudPlatform#1539)
1 parent 8d25b48 commit c1b12f3

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

spanner/src/set_request_tag.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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_set_request_tag]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Executes a read with a request tag.
31+
* Example:
32+
* ```
33+
* spanner_set_request_tag($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function set_request_tag(string $instanceId, string $databaseId): void
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$snapshot = $database->snapshot();
46+
$results = $snapshot->execute(
47+
'SELECT SingerId, AlbumId, AlbumTitle FROM Albums',
48+
[
49+
'requestOptions' => [
50+
'requestTag' => 'app=concert,env=dev,action=select'
51+
]
52+
]
53+
);
54+
foreach ($results as $row) {
55+
printf('SingerId: %s, AlbumId: %s, AlbumTitle: %s' . PHP_EOL,
56+
$row['SingerId'], $row['AlbumId'], $row['AlbumTitle']);
57+
}
58+
}
59+
// [END spanner_set_request_tag]
60+
61+
// The following 2 lines are only needed to run the samples
62+
require_once __DIR__ . '/../../testing/sample_helpers.php';
63+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/set_transaction_tag.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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_set_transaction_tag]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Executes a transaction with a transaction tag.
32+
* Example:
33+
* ```
34+
* spanner_set_transaction_tag($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function set_transaction_tag(string $instanceId, string $databaseId): void
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$database->runTransaction(function (Transaction $t) use ($spanner) {
47+
$t->executeUpdate(
48+
'UPDATE Venues SET Capacity = CAST(Capacity/4 AS INT64) WHERE OutdoorVenue = false',
49+
[
50+
'requestOptions' => ['requestTag' => 'app=concert,env=dev,action=update']
51+
]
52+
);
53+
print('Venue capacities updated.' . PHP_EOL);
54+
$t->executeUpdate(
55+
'INSERT INTO Venues (VenueId, VenueName, Capacity, OutdoorVenue, LastUpdateTime) '
56+
. 'VALUES (@venueId, @venueName, @capacity, @outdoorVenue, PENDING_COMMIT_TIMESTAMP())',
57+
[
58+
'parameters' => [
59+
'venueId' => 81,
60+
'venueName' => 'Venue 81',
61+
'capacity' => 1440,
62+
'outdoorVenue' => true,
63+
],
64+
'requestOptions' => ['requestTag' => 'app=concert,env=dev,action=insert']
65+
]
66+
);
67+
print('New venue inserted.' . PHP_EOL);
68+
$t->commit();
69+
}, [
70+
'requestOptions' => ['transactionTag' => 'app=concert,env=dev']
71+
]);
72+
}
73+
// [END spanner_set_transaction_tag]
74+
75+
// The following 2 lines are only needed to run the samples
76+
require_once __DIR__ . '/../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,25 @@ public function testQueryDataNumeric()
742742
$this->assertStringContainsString('VenueId: 4, Revenue: 35000', $output);
743743
}
744744

745+
/**
746+
* @depends testInsertDataWithDatatypes
747+
*/
748+
public function testSetTransactionTag()
749+
{
750+
$output = $this->runFunctionSnippet('set_transaction_tag');
751+
$this->assertStringContainsString('Venue capacities updated.', $output);
752+
$this->assertStringContainsString('New venue inserted.', $output);
753+
}
754+
755+
/**
756+
* @depends testInsertData
757+
*/
758+
public function testSetRequestTag()
759+
{
760+
$output = $this->runFunctionSnippet('set_request_tag');
761+
$this->assertStringContainsString('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk', $output);
762+
}
763+
745764
/**
746765
* @depends testInsertDataWithDatatypes
747766
*/

0 commit comments

Comments
 (0)