-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PHP snippets for the Getting Started guide.
- Loading branch information
Showing
25 changed files
with
997 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"require": { | ||
"ext-pgsql": "*", | ||
"ext-pdo": "*", | ||
"testcontainers/testcontainers": "^0.2.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^11.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_add_column] | ||
function add_column(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
$connection->exec("ALTER TABLE albums ADD COLUMN marketing_budget bigint"); | ||
print("Added marketing_budget column\n"); | ||
|
||
$connection = null; | ||
} | ||
// [END spanner_add_column] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 1 Total Junk | ||
1 2 Go, Go, Go | ||
2 1 Green | ||
2 2 Forever Hold Your Peace | ||
2 3 Terrified |
35 changes: 35 additions & 0 deletions
35
samples/snippets/php-snippets/samples/create_connection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_create_connection] | ||
function create_connection(string $host, string $port, string $database): void | ||
{ | ||
// Connect to Spanner through PGAdapter using the PostgreSQL PDO driver. | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
// Execute a query on Spanner through PGAdapter. | ||
$statement = $connection->query("select 'Hello world!' as hello"); | ||
$rows = $statement->fetchAll(); | ||
|
||
printf("Greeting from Cloud Spanner PostgreSQL: %s\n", $rows[0][0]); | ||
|
||
// Cleanup resources. | ||
$rows = null; | ||
$statement = null; | ||
$connection = null; | ||
} | ||
// [END spanner_create_connection] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_create_database] | ||
function create_tables(string $host, string $port, string $database): void | ||
{ | ||
// Connect to Spanner through PGAdapter using the PostgreSQL PDO driver. | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
// Create two tables in one batch. | ||
$connection->exec("start batch ddl"); | ||
$connection->exec("create table singers (" | ||
." singer_id bigint primary key not null," | ||
." first_name character varying(1024)," | ||
." last_name character varying(1024)," | ||
." singer_info bytea," | ||
." full_name character varying(2048) generated " | ||
." always as (first_name || ' ' || last_name) stored" | ||
.")"); | ||
$connection->exec("create table albums (" | ||
." singer_id bigint not null," | ||
." album_id bigint not null," | ||
." album_title character varying(1024)," | ||
." primary key (singer_id, album_id)" | ||
.") interleave in parent singers on delete cascade"); | ||
$connection->exec("run batch"); | ||
print("Created Singers & Albums tables in database: [{$database}]\n"); | ||
|
||
$connection = null; | ||
} | ||
// [END spanner_create_database] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_data_boost] | ||
function data_boost(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
// This enables Data Boost for all partitioned queries on this | ||
// connection. | ||
$connection->exec("set spanner.data_boost_enabled=true"); | ||
|
||
// Run a partitioned query. This query will use Data Boost. | ||
$statement = $connection->query( | ||
"run partitioned query " | ||
."select singer_id, first_name, last_name " | ||
."from singers" | ||
); | ||
$rows = $statement->fetchAll(); | ||
foreach ($rows as $singer) { | ||
printf("%s\t%s\t%s\n", $singer["singer_id"], $singer["first_name"], $singer["last_name"]); | ||
} | ||
|
||
$rows = null; | ||
$statement = null; | ||
$connection = null; | ||
} | ||
// [END spanner_data_boost] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_ddl_batch] | ||
function ddl_batch(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
// Executing multiple DDL statements as one batch is | ||
// more efficient than executing each statement | ||
// individually. | ||
$connection->exec("start batch ddl"); | ||
$connection->exec("CREATE TABLE venues (" | ||
." venue_id bigint not null primary key," | ||
." name varchar(1024)," | ||
." description jsonb" | ||
.")"); | ||
$connection->exec("CREATE TABLE concerts (" | ||
." concert_id bigint not null primary key ," | ||
." venue_id bigint not null," | ||
." singer_id bigint not null," | ||
." start_time timestamptz," | ||
." end_time timestamptz," | ||
." constraint fk_concerts_venues foreign key" | ||
." (venue_id) references venues (venue_id)," | ||
." constraint fk_concerts_singers foreign key" | ||
." (singer_id) references singers (singer_id)" | ||
.")"); | ||
$connection->exec("run batch"); | ||
print("Added venues and concerts tables\n"); | ||
|
||
$connection = null; | ||
} | ||
// [END spanner_ddl_batch] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_partitioned_dml] | ||
function execute_partitioned_dml(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
// Change the DML mode that is used by this connection to Partitioned | ||
// DML. Partitioned DML is designed for bulk updates and deletes. | ||
// See https://cloud.google.com/spanner/docs/dml-partitioned for more | ||
// information. | ||
$connection->exec("set spanner.autocommit_dml_mode='partitioned_non_atomic'"); | ||
|
||
// The following statement will use Partitioned DML. | ||
$rowcount = $connection->exec( | ||
"update albums " | ||
."set marketing_budget=0 " | ||
."where marketing_budget is null" | ||
); | ||
printf("Updated at least %d albums\n", $rowcount); | ||
|
||
$statement = null; | ||
$connection = null; | ||
} | ||
// [END spanner_partitioned_dml] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_query_data] | ||
function query_data(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
$statement = $connection->query("SELECT singer_id, album_id, album_title " | ||
."FROM albums " | ||
."ORDER BY singer_id, album_id" | ||
); | ||
$rows = $statement->fetchAll(); | ||
foreach ($rows as $album) | ||
{ | ||
printf("%s\t%s\t%s\n", $album["singer_id"], $album["album_id"], $album["album_title"]); | ||
} | ||
|
||
$rows = null; | ||
$statement = null; | ||
$connection = null; | ||
} | ||
// [END spanner_query_data] |
38 changes: 38 additions & 0 deletions
38
samples/snippets/php-snippets/samples/query_data_with_new_column.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
// Copyright 2025 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START spanner_query_data_with_new_column] | ||
function query_data_with_new_column(string $host, string $port, string $database): void | ||
{ | ||
$dsn = sprintf("pgsql:host=%s;port=%s;dbname=%s", $host, $port, $database); | ||
$connection = new PDO($dsn); | ||
|
||
$statement = $connection->query( | ||
"SELECT singer_id, album_id, marketing_budget " | ||
."FROM albums " | ||
."ORDER BY singer_id, album_id" | ||
); | ||
$rows = $statement->fetchAll(); | ||
foreach ($rows as $album) | ||
{ | ||
printf("%s\t%s\t%s\n", $album["singer_id"], $album["album_id"], $album["marketing_budget"]); | ||
} | ||
|
||
$rows = null; | ||
$statement = null; | ||
$connection = null; | ||
} | ||
// [END spanner_query_data_with_new_column] |
Oops, something went wrong.