Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7bee802
feat(spanner): Added 8 samples for PG dialect
saranshdhingra Mar 4, 2022
eed2402
feat(spanner): Added 5 more samples for Postgres dialect
saranshdhingra Mar 4, 2022
d02d327
feat(spanner): Added tests for 13 samples
saranshdhingra Mar 4, 2022
83247ec
Spanner: Added sample to add a column on a table in a PG DB
saranshdhingra Mar 28, 2022
0fb4eea
Spanner: Added a test for pg_spanner_add_column
saranshdhingra Mar 28, 2022
49f23a7
Spanner: Added sample for creating an index
saranshdhingra Apr 4, 2022
53103de
Merge branch 'master' into spanner-pg-samples
saranshdhingra Apr 7, 2022
8680b02
Spanner: Changed the names of the PG sample files
saranshdhingra Apr 13, 2022
f629652
Merge branch 'master' into spanner-pg-samples
saranshdhingra May 3, 2022
549f188
Spanner: Update composer.json for PG samples
saranshdhingra May 4, 2022
b7dd564
Merge commit
saranshdhingra May 4, 2022
7ff207e
Spanner: Changed column names in PG samples for uniformity
saranshdhingra May 5, 2022
4de3144
Spanner: Lint fixes in PG samples
saranshdhingra May 5, 2022
06fb6fb
Spanner: Lint fixes in PG samples
saranshdhingra May 5, 2022
c04c5d5
Spanner: Added retry annotations in spanner tests
saranshdhingra May 6, 2022
f081e3c
Merge branch 'master' into spanner-pg-samples
saranshdhingra May 6, 2022
ced6ad0
Spanner: Removed some createInstance calls in setup methods
saranshdhingra May 8, 2022
874d67a
Merge commit
saranshdhingra May 8, 2022
e908241
Merge branch 'master' into spanner-pg-samples
saranshdhingra May 8, 2022
0f6c15c
Spanner: Changed retryAttempts back to 3 in tests and fixed minor ind…
saranshdhingra May 11, 2022
5cdc85d
Spanner: Removed unused classes from tests
saranshdhingra May 11, 2022
c386d58
Merge branch 'master' into spanner-pg-samples
saranshdhingra May 11, 2022
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
2 changes: 1 addition & 1 deletion spanner/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-spanner": "^1.48.0"
"google/cloud-spanner": "^1.49.0"
}
}
54 changes: 54 additions & 0 deletions spanner/src/pg_add_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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_postgresql_add_column]
use Google\Cloud\Spanner\SpannerClient;

/**
* Add a column to a table present in a PG Spanner database.
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function pg_add_column(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$operation = $database->updateDdl(
'ALTER TABLE Albums ADD COLUMN MarketingBudget bigint'
);

print('Waiting for operation to complete...' . PHP_EOL);
$operation->pollUntilComplete();

print('Added column MarketingBudget on table Albums' . PHP_EOL);
}
// [END spanner_postgresql_add_column]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
83 changes: 83 additions & 0 deletions spanner/src/pg_batch_dml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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_postgresql_batch_dml]
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Transaction;
use Google\Cloud\Spanner\Database;

/**
* Execute a batch of DML statements on a Spanner PostgreSQL database
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function pg_batch_dml(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$sql = 'INSERT INTO Singers (SingerId, FirstName, LastName) VALUES ($1, $2, $3)';

$database->runTransaction(function (Transaction $t) use ($sql) {
$result = $t->executeUpdateBatch([
[
'sql' => $sql,
'parameters' => [
'p1' => 1,
'p2' => 'Alice',
'p3' => 'Henderson',
],
'types' => [
'p1' => Database::TYPE_INT64,
'p2' => Database::TYPE_STRING,
'p3' => Database::TYPE_STRING,
]
],
[
'sql' => $sql,
'parameters' => [
'p1' => 2,
'p2' => 'Bruce',
'p3' => 'Allison',
],
// you can omit types(provided the value isn't null)
]
]);
$t->commit();

if ($result->error()) {
printf('An error occurred: %s' . PHP_EOL, $result->error()['status']['message']);
} else {
printf('Inserted %s singers using Batch DML.' . PHP_EOL, count($result->rowCounts()));
}
});
}
// [END spanner_postgresql_batch_dml]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
67 changes: 67 additions & 0 deletions spanner/src/pg_case_sensitivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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_postgresql_case_sensitivity]
use Google\Cloud\Spanner\SpannerClient;

/**
* Create a table with case-sensitive and case-folded columns for
* a Spanner PostgreSQL database
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
* @param string $tableName The name of the table to create, defaults to Singers.
*/
function pg_case_sensitivity(string $instanceId, string $databaseId, string $tableName = 'Singers'): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$operation = $database->updateDdl(
sprintf(
'
CREATE TABLE %s (
-- SingerId will be folded to "singerid"
SingerId bigint NOT NULL PRIMARY KEY,
-- FirstName and LastName are double-quoted and will therefore retain their
-- mixed case and are case-sensitive. This means that any statement that
-- references any of these columns must use double quotes.
"FirstName" varchar(1024) NOT NULL,
"LastName" varchar(1024) NOT NULL
)', $tableName)
);

print('Waiting for operation to complete...' . PHP_EOL);
$operation->pollUntilComplete();

printf('Created %s table in database %s on instance %s' . PHP_EOL,
$tableName, $databaseId, $instanceId);
}
// [END spanner_postgresql_case_sensitivity]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
61 changes: 61 additions & 0 deletions spanner/src/pg_cast_data_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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_postgresql_cast_data_type]
use Google\Cloud\Spanner\SpannerClient;

/**
* Cast values from one data type to another in a Spanner PostgreSQL SQL statement
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function pg_cast_data_type(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

$sql = "select 1::varchar as str, '2'::int as int, 3::decimal as dec,
'4'::bytea as bytes, 5::float as float, 'true'::bool as bool,
'2021-11-03T09:35:01UTC'::timestamptz as timestamp";

$results = $database->execute($sql);

foreach ($results as $row) {
printf('String: %s' . PHP_EOL, $row['str']);
printf('Int: %d' . PHP_EOL, $row['int']);
printf('Decimal: %s' . PHP_EOL, $row['dec']);
printf('Bytes: %s' . PHP_EOL, $row['bytes']);
printf('Float: %f' . PHP_EOL, $row['float']);
printf('Bool: %s' . PHP_EOL, $row['bool']);
printf('Timestamp: %s' . PHP_EOL, (string) $row['timestamp']);
}
}
// [END spanner_postgresql_cast_data_type]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
49 changes: 49 additions & 0 deletions spanner/src/pg_connect_to_db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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_postgresql_create_clients]
use Google\Cloud\Spanner\SpannerClient;

/**
* Create an instance client and a database client
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function pg_connect_to_db(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();

// Instance Admin Client
$instance = $spanner->instance($instanceId);

// Spanner Database Client
$database = $instance->database($databaseId);
}
// [END spanner_postgresql_create_clients]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Loading