Skip to content

Commit defb7da

Browse files
committed
refactor
1 parent 4847326 commit defb7da

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

spanner/src/create_database_with_proto_columns.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
* @param string $instanceId The Spanner instance ID.
3939
* @param string $databaseId The Spanner database ID.
4040
*/
41-
function create_database_with_proto_columns(string $projectId, string $instanceId, string $databaseId): void
42-
{
41+
function create_database_with_proto_columns(
42+
string $projectId,
43+
string $instanceId,
44+
string $databaseId
45+
): void {
4346
$databaseAdminClient = new DatabaseAdminClient();
4447
$instance = $databaseAdminClient->instanceName($projectId, $instanceId);
4548

spanner/src/insert_data_with_proto_columns.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
// [START spanner_insert_data_with_proto_columns]
2727
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Proto;
2829
use Testing\Data\User;
2930
use Testing\Data\Book;
3031

@@ -45,7 +46,7 @@
4546
function insert_data_with_proto_columns(
4647
string $instanceId,
4748
string $databaseId,
48-
int $userId,
49+
int $userId = 1,
4950
): void {
5051
$spanner = new SpannerClient();
5152
$instance = $spanner->instance($instanceId);
@@ -57,9 +58,17 @@ function insert_data_with_proto_columns(
5758
$address->setState('CA');
5859
$user->setAddress($address);
5960

61+
$book1 = new Book(['title' => 'Book 1', 'author' => 'Author 1']);
62+
$book2 = new Book(['title' => 'Book 2', 'author' => 'Author 2']);
63+
6064
$books = [
61-
new Book(['title' => 'Book 1', 'author' => 'Author 1']),
62-
new Book(['title' => 'Book 2', 'author' => 'Author 2']),
65+
// insert using the proto message
66+
$book1,
67+
// insert using the Proto wrapper class
68+
new Proto(
69+
base64_encode($book2->serializeToString()),
70+
'testing.data.Book'
71+
),
6372
];
6473

6574
$transaction = $database->transaction(['singleUse' => true])

spanner/src/query_data_with_proto_columns.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,54 @@
2424
namespace Google\Cloud\Samples\Spanner;
2525

2626
use Google\Cloud\Spanner\SpannerClient;
27+
use Google\Cloud\Spanner\Proto;
2728
use Testing\Data\User;
2829
use Testing\Data\Book;
2930

3031
/**
3132
* Queries sample data from the database using proto columns.
3233
* Example:
3334
* ```
34-
* query_data_with_proto_columns($instanceId, $databaseId);
35+
* query_data_with_proto_columns($instanceId, $databaseId, $userId);
3536
* ```
3637
*
3738
* @param string $instanceId The Spanner instance ID.
3839
* @param string $databaseId The Spanner database ID.
40+
* @param int $userId The ID of the user to query.
3941
*/
40-
function query_data_with_proto_columns(string $instanceId, string $databaseId): void
41-
{
42+
function query_data_with_proto_columns(
43+
string $instanceId,
44+
string $databaseId,
45+
int $userId = 1
46+
): void {
4247
$spanner = new SpannerClient();
4348
$instance = $spanner->instance($instanceId);
4449
$database = $instance->database($databaseId);
4550

46-
// this ensures that the User class has been loaded into the descriptor pool
47-
\GPBMetadata\Data\User::initOnce();
48-
49-
$nameValue = 'TestUser 3';
5051
// [START spanner_query_data_with_proto_columns]
52+
$userProto = (new User())
53+
->setName('Test User ' . $userId);
54+
5155
$results = $database->execute(
52-
'SELECT * FROM Users '
53-
. 'WHERE User.name = @name',
56+
'SELECT * FROM Users, UNNEST(Books) as Book '
57+
. 'WHERE User.name = @user.name '
58+
. 'AND Book.title = @bookTitle',
5459
[
5560
'parameters' => [
56-
'name' => $nameValue
61+
'user' => $userProto,
62+
'bookTitle' => 'Book 1',
5763
],
5864
]
5965
);
6066
foreach ($results as $row) {
6167
/** @var User $user */
62-
$user = $row['User'];
68+
$user = $row['User']->get();
69+
// Print the decoded Protobuf message as JSON
6370
printf('User: %s' . PHP_EOL, $user->serializeToJsonString());
64-
/** @var Book $book */
71+
/** @var Proto<Book> $book */
6572
foreach ($row['Books'] ?? [] as $book) {
66-
printf('Book: %s' . PHP_EOL, $book->serializeToJsonString());
73+
// Print the raw row value
74+
printf('Book: %s' . PHP_EOL, $book->getValue());
6775
}
6876
}
6977
// [END spanner_query_data_with_proto_columns]

0 commit comments

Comments
 (0)