Skip to content

Commit e696e1f

Browse files
committed
add tests for struct querying
1 parent 26e28ec commit e696e1f

File tree

6 files changed

+116
-16
lines changed

6 files changed

+116
-16
lines changed

spanner/data/user.pb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
�
2+
�
33
data/user.proto testing.data"�
44
User
55
id (Rid
@@ -8,7 +8,7 @@
88
address ( 2.testing.data.User.AddressRaddress3
99
Address
1010
city ( Rcity
11-
state ( Rstate"4
11+
state ( Rstate"H
1212
Book
13-
title ( Rtitle
14-
author ( Rauthorbproto3
13+
title ( Rtitle*
14+
author ( 2.testing.data.UserRauthorbproto3

spanner/data/user.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ message User {
3838
message Book {
3939
string title = 1;
4040

41-
string author = 2;
41+
User author = 2;
4242
}

spanner/generated/GPBMetadata/Data/User.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spanner/generated/Testing/Data/Book.php

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spanner/src/insert_data_with_proto_columns.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ function insert_data_with_proto_columns(
5858
->setName('Test User ' . $userId)
5959
->setAddress($address);
6060

61-
$book1 = new Book(['title' => 'Book 1', 'author' => 'Author 1']);
62-
$book2 = new Book(['title' => 'Book 2', 'author' => 'Author 2']);
61+
$book1 = new Book([
62+
'title' => 'Book 1',
63+
'author' => new User(['name' => 'Author of Book 1']),
64+
]);
65+
$book2 = new Book([
66+
'title' => 'Book 2',
67+
'author' => new User(['name' => 'Author of Book 2']),
68+
]);
6369

6470
$books = [
6571
// insert using the proto message
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright 2025 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/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
use Google\Cloud\Spanner\SpannerClient;
27+
use Google\Cloud\Spanner\Database;
28+
use Google\Cloud\Spanner\Proto;
29+
use Google\Cloud\Spanner\StructType;
30+
use Google\Cloud\Spanner\StructValue;
31+
use Testing\Data\User;
32+
use Testing\Data\Book;
33+
34+
/**
35+
* Queries sample data from the database using proto columns.
36+
* Example:
37+
* ```
38+
* query_data_with_struct_proto_columns($instanceId, $databaseId, $userId);
39+
* ```
40+
*
41+
* @param string $instanceId The Spanner instance ID.
42+
* @param string $databaseId The Spanner database ID.
43+
* @param int $userId The ID of the user to query.
44+
*/
45+
function query_data_with_struct_proto_columns(
46+
string $instanceId,
47+
string $databaseId,
48+
int $userId = 1
49+
): void {
50+
// [START spanner_query_data_with_struct_proto_columns]
51+
$spanner = new SpannerClient();
52+
$database = $spanner->instance($instanceId)->database($databaseId);
53+
54+
$structType = (new StructType)
55+
->add('title', Database::TYPE_STRING)
56+
->add('author', Database::TYPE_PROTO);
57+
58+
$results = $database->execute(
59+
'SELECT u.Id, u.User.name, ' .
60+
'ARRAY(SELECT AS STRUCT b.title, b.author ' .
61+
'FROM u.Books AS b) as book_struct '.
62+
'FROM Users AS u'
63+
);
64+
foreach ($results as $row) {
65+
// Print the decoded Protobuf message as JSON
66+
printf('User name: %s' . PHP_EOL, $row['name']);
67+
foreach ($row['book_struct'] as $bookStruct) {
68+
if (!class_exists(Book::class, false)) {
69+
// If you receive an error such as
70+
// "Unable to decode proto value. Descriptor not found for testing.data.User"
71+
// you may need to initialize the generated classes. This also happens when creating
72+
// an instance of the message class (e.g. `new User()`).
73+
\GPBMetadata\Data\User::initOnce();
74+
}
75+
printf('Book struct title: %s' . PHP_EOL, $bookStruct['title']);
76+
printf('Book struct author: %s' . PHP_EOL, $bookStruct['author']->get()->serializeToJsonString());
77+
}
78+
}
79+
// [END spanner_query_data_with_struct_proto_columns]
80+
}
81+
82+
// The following 2 lines are only needed to run the samples
83+
require_once __DIR__ . '/../../testing/sample_helpers.php';
84+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)