Skip to content

Commit 0c58613

Browse files
feat(spanner): Added Samples for Postgres Dialect (GoogleCloudPlatform#1614)
* feat(spanner): Added samples and tests for Postgres dialect
1 parent 851454b commit 0c58613

20 files changed

+1491
-9
lines changed

spanner/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-spanner": "^1.48.0"
3+
"google/cloud-spanner": "^1.49.0"
44
}
55
}

spanner/src/pg_add_column.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_postgresql_add_column]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Add a column to a table present in a PG Spanner database.
31+
*
32+
* @param string $instanceId The Spanner instance ID.
33+
* @param string $databaseId The Spanner database ID.
34+
*/
35+
function pg_add_column(string $instanceId, string $databaseId): void
36+
{
37+
$spanner = new SpannerClient();
38+
$instance = $spanner->instance($instanceId);
39+
$database = $instance->database($databaseId);
40+
41+
$operation = $database->updateDdl(
42+
'ALTER TABLE Albums ADD COLUMN MarketingBudget bigint'
43+
);
44+
45+
print('Waiting for operation to complete...' . PHP_EOL);
46+
$operation->pollUntilComplete();
47+
48+
print('Added column MarketingBudget on table Albums' . PHP_EOL);
49+
}
50+
// [END spanner_postgresql_add_column]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/pg_batch_dml.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_postgresql_batch_dml]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Transaction;
29+
use Google\Cloud\Spanner\Database;
30+
31+
/**
32+
* Execute a batch of DML statements on a Spanner PostgreSQL database
33+
*
34+
* @param string $instanceId The Spanner instance ID.
35+
* @param string $databaseId The Spanner database ID.
36+
*/
37+
function pg_batch_dml(string $instanceId, string $databaseId): void
38+
{
39+
$spanner = new SpannerClient();
40+
$instance = $spanner->instance($instanceId);
41+
$database = $instance->database($databaseId);
42+
43+
$sql = 'INSERT INTO Singers (SingerId, FirstName, LastName) VALUES ($1, $2, $3)';
44+
45+
$database->runTransaction(function (Transaction $t) use ($sql) {
46+
$result = $t->executeUpdateBatch([
47+
[
48+
'sql' => $sql,
49+
'parameters' => [
50+
'p1' => 1,
51+
'p2' => 'Alice',
52+
'p3' => 'Henderson',
53+
],
54+
'types' => [
55+
'p1' => Database::TYPE_INT64,
56+
'p2' => Database::TYPE_STRING,
57+
'p3' => Database::TYPE_STRING,
58+
]
59+
],
60+
[
61+
'sql' => $sql,
62+
'parameters' => [
63+
'p1' => 2,
64+
'p2' => 'Bruce',
65+
'p3' => 'Allison',
66+
],
67+
// you can omit types(provided the value isn't null)
68+
]
69+
]);
70+
$t->commit();
71+
72+
if ($result->error()) {
73+
printf('An error occurred: %s' . PHP_EOL, $result->error()['status']['message']);
74+
} else {
75+
printf('Inserted %s singers using Batch DML.' . PHP_EOL, count($result->rowCounts()));
76+
}
77+
});
78+
}
79+
// [END spanner_postgresql_batch_dml]
80+
81+
// The following 2 lines are only needed to run the samples
82+
require_once __DIR__ . '/../../testing/sample_helpers.php';
83+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_postgresql_case_sensitivity]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Create a table with case-sensitive and case-folded columns for
31+
* a Spanner PostgreSQL database
32+
*
33+
* @param string $instanceId The Spanner instance ID.
34+
* @param string $databaseId The Spanner database ID.
35+
* @param string $tableName The name of the table to create, defaults to Singers.
36+
*/
37+
function pg_case_sensitivity(string $instanceId, string $databaseId, string $tableName = 'Singers'): void
38+
{
39+
$spanner = new SpannerClient();
40+
$instance = $spanner->instance($instanceId);
41+
$database = $instance->database($databaseId);
42+
43+
$operation = $database->updateDdl(
44+
sprintf(
45+
'
46+
CREATE TABLE %s (
47+
-- SingerId will be folded to "singerid"
48+
SingerId bigint NOT NULL PRIMARY KEY,
49+
-- FirstName and LastName are double-quoted and will therefore retain their
50+
-- mixed case and are case-sensitive. This means that any statement that
51+
-- references any of these columns must use double quotes.
52+
"FirstName" varchar(1024) NOT NULL,
53+
"LastName" varchar(1024) NOT NULL
54+
)', $tableName)
55+
);
56+
57+
print('Waiting for operation to complete...' . PHP_EOL);
58+
$operation->pollUntilComplete();
59+
60+
printf('Created %s table in database %s on instance %s' . PHP_EOL,
61+
$tableName, $databaseId, $instanceId);
62+
}
63+
// [END spanner_postgresql_case_sensitivity]
64+
65+
// The following 2 lines are only needed to run the samples
66+
require_once __DIR__ . '/../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/pg_cast_data_type.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_postgresql_cast_data_type]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Cast values from one data type to another in a Spanner PostgreSQL SQL statement
31+
*
32+
* @param string $instanceId The Spanner instance ID.
33+
* @param string $databaseId The Spanner database ID.
34+
*/
35+
function pg_cast_data_type(string $instanceId, string $databaseId): void
36+
{
37+
$spanner = new SpannerClient();
38+
$instance = $spanner->instance($instanceId);
39+
$database = $instance->database($databaseId);
40+
41+
$sql = "select 1::varchar as str, '2'::int as int, 3::decimal as dec,
42+
'4'::bytea as bytes, 5::float as float, 'true'::bool as bool,
43+
'2021-11-03T09:35:01UTC'::timestamptz as timestamp";
44+
45+
$results = $database->execute($sql);
46+
47+
foreach ($results as $row) {
48+
printf('String: %s' . PHP_EOL, $row['str']);
49+
printf('Int: %d' . PHP_EOL, $row['int']);
50+
printf('Decimal: %s' . PHP_EOL, $row['dec']);
51+
printf('Bytes: %s' . PHP_EOL, $row['bytes']);
52+
printf('Float: %f' . PHP_EOL, $row['float']);
53+
printf('Bool: %s' . PHP_EOL, $row['bool']);
54+
printf('Timestamp: %s' . PHP_EOL, (string) $row['timestamp']);
55+
}
56+
}
57+
// [END spanner_postgresql_cast_data_type]
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

spanner/src/pg_connect_to_db.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright 2022 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_postgresql_create_clients]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Create an instance client and a database client
31+
*
32+
* @param string $instanceId The Spanner instance ID.
33+
* @param string $databaseId The Spanner database ID.
34+
*/
35+
function pg_connect_to_db(string $instanceId, string $databaseId): void
36+
{
37+
$spanner = new SpannerClient();
38+
39+
// Instance Admin Client
40+
$instance = $spanner->instance($instanceId);
41+
42+
// Spanner Database Client
43+
$database = $instance->database($databaseId);
44+
}
45+
// [END spanner_postgresql_create_clients]
46+
47+
// The following 2 lines are only needed to run the samples
48+
require_once __DIR__ . '/../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)