Skip to content

Commit 53d5893

Browse files
authored
samples(spanner): add JSON samples (GoogleCloudPlatform#1463)
1 parent 4b2c306 commit 53d5893

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

spanner/src/add_json_column.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2021 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_add_json_column]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Adds a JSON column to a table.
31+
* Example:
32+
* ```
33+
* add_json_column($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function add_json_column($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$operation = $database->updateDdl(
46+
'ALTER TABLE Venues ADD COLUMN VenueDetails JSON'
47+
);
48+
49+
print('Waiting for operation to complete...' . PHP_EOL);
50+
$operation->pollUntilComplete();
51+
52+
printf('Added VenueDetails as a JSON column in Venues table' . PHP_EOL);
53+
}
54+
// [END spanner_add_json_column]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
/**
3+
* Copyright 2021 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_json_parameter]
27+
use Google\Cloud\Spanner\Database;
28+
use Google\Cloud\Spanner\SpannerClient;
29+
30+
/**
31+
* Queries sample data from the database using SQL with a NUMERIC parameter.
32+
* Example:
33+
* ```
34+
* query_data_with_json_parameter($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_json_parameter($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$exampleJson = [
47+
'rating' => 9,
48+
'open' => true,
49+
];
50+
51+
$results = $database->execute(
52+
'SELECT VenueId, VenueDetails FROM Venues ' .
53+
'WHERE JSON_VALUE(VenueDetails, \'$.rating\') = JSON_VALUE(@venueDetails, \'$.rating\')',
54+
[
55+
'parameters' => [
56+
'venueDetails' => json_encode($exampleJson)
57+
],
58+
'types' => [
59+
'venueDetails' => Database::TYPE_JSON
60+
]
61+
]
62+
);
63+
64+
foreach ($results as $row) {
65+
printf(
66+
'VenueId: %s, VenueDetails: %s' . PHP_EOL,
67+
$row['VenueId'],
68+
$row['VenueDetails']
69+
);
70+
}
71+
}
72+
// [END spanner_query_with_json_parameter]
73+
74+
// The following 2 lines are only needed to run the samples
75+
require_once __DIR__ . '/../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
/**
3+
* Copyright 2021 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_update_data_with_json_column]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Updates sample data in a table with a JSON column.
31+
*
32+
* Before executing this method, a new column Revenue has to be added to the Venues
33+
* table by applying the DDL statement "ALTER TABLE Venues ADD COLUMN VenueDetails JSON".
34+
*
35+
* Example:
36+
* ```
37+
* update_data_with_json_column($instanceId, $databaseId);
38+
* ```
39+
*
40+
* @param string $instanceId The Spanner instance ID.
41+
* @param string $databaseId The Spanner database ID.
42+
*/
43+
function update_data_with_json_column($instanceId, $databaseId)
44+
{
45+
$spanner = new SpannerClient();
46+
$instance = $spanner->instance($instanceId);
47+
$database = $instance->database($databaseId);
48+
49+
$database->transaction(['singleUse' => true])
50+
->updateBatch('Venues', [
51+
[
52+
'VenueId' => 4,
53+
'VenueDetails' =>
54+
'[{"name":"room 1","open":true},{"name":"room 2","open":false}]'
55+
],
56+
[
57+
'VenueId' => 19,
58+
'VenueDetails' => '{"rating":9,"open":true}'
59+
],
60+
[
61+
'VenueId' => 42,
62+
'VenueDetails' =>
63+
'{"name":null,"open":{"Monday":true,"Tuesday":false},"tags":["large","airy"]}'
64+
],
65+
])
66+
->commit();
67+
68+
print('Updated data.' . PHP_EOL);
69+
}
70+
// [END spanner_update_data_with_json_column]
71+
72+
// The following 2 lines are only needed to run the samples
73+
require_once __DIR__ . '/../../testing/sample_helpers.php';
74+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/test/spannerTest.php

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

745+
/**
746+
* @depends testInsertDataWithDatatypes
747+
*/
748+
public function testAddJsonColumn()
749+
{
750+
$output = $this->runFunctionSnippet('add_json_column');
751+
$this->assertStringContainsString('Waiting for operation to complete...', $output);
752+
$this->assertStringContainsString('Added VenueDetails as a JSON column in Venues table', $output);
753+
}
754+
755+
/**
756+
* @depends testAddJsonColumn
757+
*/
758+
public function testUpdateDataJson()
759+
{
760+
$output = $this->runFunctionSnippet('update_data_with_json_column');
761+
$this->assertEquals('Updated data.' . PHP_EOL, $output);
762+
}
763+
764+
/**
765+
* @depends testUpdateDataJson
766+
*/
767+
public function testQueryDataJson()
768+
{
769+
$output = $this->runFunctionSnippet('query_data_with_json_parameter');
770+
$this->assertStringContainsString('VenueId: 19, VenueDetails: ', $output);
771+
}
772+
745773
/**
746774
* @depends testInsertDataWithDatatypes
747775
*/

0 commit comments

Comments
 (0)