Skip to content

Commit b28a3aa

Browse files
authored
Merge pull request GoogleCloudPlatform#1286 from larkee/spanner-commit-stats-sample
feat(spanner): add sample for commit stats
2 parents 186b1eb + 3e9faad commit b28a3aa

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

spanner/src/get_commit_stats.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_get_commit_stats]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
30+
/**
31+
* Creates a database and tables for sample data.
32+
* Example:
33+
* ```
34+
* create_database($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function get_commit_stats($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$commitStats = $database->runTransaction(function (Transaction $t) use ($spanner) {
47+
$t->updateBatch('Albums', [
48+
[
49+
'SingerId' => 1,
50+
'AlbumId' => 1,
51+
'MarketingBudget' => 200000,
52+
],
53+
[
54+
'SingerId' => 2,
55+
'AlbumId' => 2,
56+
'MarketingBudget' => 400000,
57+
]
58+
]);
59+
$t->commit(['returnCommitStats' => true]);
60+
return $t->getCommitStats();
61+
});
62+
63+
print('Updated data with ' . $commitStats['mutationCount'] . ' mutations.' . PHP_EOL);
64+
}
65+
// [END spanner_get_commit_stats]
66+
67+
require_once __DIR__ . '/../../testing/sample_helpers.php';
68+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ public function testUpdateDataWithBatchDML()
511511
$this->assertStringContainsString('Executed 2 SQL statements using Batch DML', $output);
512512
}
513513

514+
/**
515+
* @depends testAddColumn
516+
*/
517+
public function testGetCommitStats()
518+
{
519+
$output = $this->runFunctionSnippet('get_commit_stats');
520+
$this->assertStringContainsString('Updated data with 10 mutations.', $output);
521+
}
522+
523+
514524
/**
515525
* @depends testCreateDatabase
516526
*/

0 commit comments

Comments
 (0)