Skip to content

Commit

Permalink
docs: add PHP snippets
Browse files Browse the repository at this point in the history
Adds PHP snippets for the Getting Started guide.
  • Loading branch information
olavloite committed Jan 10, 2025
1 parent 4d2510b commit 7030353
Show file tree
Hide file tree
Showing 25 changed files with 997 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/snippets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,18 @@ jobs:
- name: Run dotnet snippets
working-directory: ./samples/snippets/dotnet-snippets
run: dotnet test
php-snippets:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer
- run: php --version
- name: Install dependencies
working-directory: ./samples/snippets/php-snippets
run: composer install
- name: Run PHP snippets
working-directory: ./samples/snippets/php-snippets/test
run: ../vendor/bin/phpunit SamplesTest.php
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ samples/**/ruby/**/Gemfile.lock

samples/**/vendor
samples/**/composer.lock
samples/**/.phpunit.result.cache
src/test/**/vendor
src/test/**/composer.lock
10 changes: 10 additions & 0 deletions samples/snippets/php-snippets/composer.json
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"
}
}
28 changes: 28 additions & 0 deletions samples/snippets/php-snippets/samples/add_column.php
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]
5 changes: 5 additions & 0 deletions samples/snippets/php-snippets/samples/albums_data.txt
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 samples/snippets/php-snippets/samples/create_connection.php
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]
45 changes: 45 additions & 0 deletions samples/snippets/php-snippets/samples/create_tables.php
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]
42 changes: 42 additions & 0 deletions samples/snippets/php-snippets/samples/data_boost.php
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]
48 changes: 48 additions & 0 deletions samples/snippets/php-snippets/samples/ddl_batch.php
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]
40 changes: 40 additions & 0 deletions samples/snippets/php-snippets/samples/partitioned_dml.php
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]
37 changes: 37 additions & 0 deletions samples/snippets/php-snippets/samples/query_data.php
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]
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]
Loading

0 comments on commit 7030353

Please sign in to comment.