Skip to content

Commit f0360bc

Browse files
jsimonwebbshaffer
authored andcommitted
Add Datatypes examples to Spanner sample. (GoogleCloudPlatform#928)
1 parent 1709669 commit f0360bc

14 files changed

+910
-1
lines changed

spanner/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,33 @@ To run the Spanner Samples:
102102
create-index Adds a simple index to the example database.
103103
create-storing-index Adds an storing index to the example database.
104104
create-table-timestamp Creates a table with a commit timestamp column.
105+
create-table-with-datatypes Creates a table with supported datatypes.
105106
delete-data-with-dml Remove sample data from the given database with a DML statement.
106107
deleted-data-with-partitioned-dml Deletes sample data in the database by partition with a DML statement.
107108
help Displays help for a command
108109
insert-data Inserts sample data into the given database.
109110
insert-data-timestamp Inserts data into a table with a commit timestamp column.
111+
insert-data-with-datatypes Inserts data with supported datatypes.
110112
insert-data-with-dml Inserts sample data into the given database with a DML statement.
111113
insert-struct-data Inserts sample data that can be used to test STRUCT parameters in queries.
112114
list Lists commands
113115
query-data Queries sample data from the database using SQL.
114116
query-data-timestamp Queries sample data from a database with a commit timestamp column.
117+
query-data-with-array-parameter Queries sample data using SQL with an ARRAY parameter.
115118
query-data-with-array-of-struct Queries sample data from the database with an array of struct.
119+
query-data-with-bool-parameter Queries sample data using SQL with a BOOL parameter.
120+
query-data-with-bytes-parameter Queries sample data using SQL with a BYTES parameter.
121+
query-data-with-date-parameter Queries sample data using SQL with a DATE parameter.
122+
query-data-with-float-parameter Queries sample data using SQL with a FLOAT64 parameter.
116123
query-data-with-index Queries sample data from the database using SQL and an index.
124+
query-data-with-int-parameter Queries sample data using SQL with a INT64 parameter.
117125
query-data-with-nested-struct-field Queries sample data from the database with a nested struct field value.
118126
query-data-with-new-column Queries sample data from the database using SQL.
119127
query-data-with-parameter Query DML inserted sample data using SQL with a parameter.
128+
query-data-with-string-parameter Queries sample data using SQL with a STRING parameter.
120129
query-data-with-struct Queries sample data from the database with a struct.
121130
query-data-with-struct-field Queries sample data from the database with a struct field value.
131+
query-data-with-timestamp-parameter Queries sample data using SQL with a TIMESTAMP parameter.
122132
read-data Reads sample data from the database.
123133
read-data-with-index Reads sample data from the database using an index.
124134
read-data-with-storing-index Reads sample data from the database using an index with a storing clause.

spanner/composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@
4242
"src/write_data_with_dml_transaction.php",
4343
"src/update_data_with_partitioned_dml.php",
4444
"src/delete_data_with_partitioned_dml.php",
45-
"src/update_data_with_batch_dml.php"
45+
"src/update_data_with_batch_dml.php",
46+
"src/create_table_with_datatypes.php",
47+
"src/insert_data_with_datatypes.php",
48+
"src/query_data_with_array_parameter.php",
49+
"src/query_data_with_bool_parameter.php",
50+
"src/query_data_with_bytes_parameter.php",
51+
"src/query_data_with_date_parameter.php",
52+
"src/query_data_with_float_parameter.php",
53+
"src/query_data_with_int_parameter.php",
54+
"src/query_data_with_string_parameter.php",
55+
"src/query_data_with_timestamp_parameter.php"
4656
]
4757
}
4858
}

spanner/spanner.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,126 @@
492492
})
493493
);
494494

495+
// Create table with supported datatypes
496+
$application->add((new Command('create-table-with-datatypes'))
497+
->setDefinition($inputDefinition)
498+
->setDescription('Creates a table with supported datatypes.')
499+
->setCode(function ($input, $output) {
500+
create_table_with_datatypes(
501+
$input->getArgument('instance_id'),
502+
$input->getArgument('database_id')
503+
);
504+
})
505+
);
506+
507+
// Insert data with supported datatypes
508+
$application->add((new Command('insert-data-with-datatypes'))
509+
->setDefinition($inputDefinition)
510+
->setDescription('Inserts data with supported datatypes.')
511+
->setCode(function ($input, $output) {
512+
insert_data_with_datatypes(
513+
$input->getArgument('instance_id'),
514+
$input->getArgument('database_id')
515+
);
516+
})
517+
);
518+
519+
// Query Data with Array Parameter
520+
$application->add((new Command('query-data-with-array-parameter'))
521+
->setDefinition($inputDefinition)
522+
->setDescription('Queries sample data using SQL with an ARRAY parameter.')
523+
->setCode(function ($input, $output) {
524+
query_data_with_array_parameter(
525+
$input->getArgument('instance_id'),
526+
$input->getArgument('database_id')
527+
);
528+
})
529+
);
530+
531+
// Query Data with Bool Parameter
532+
$application->add((new Command('query-data-with-bool-parameter'))
533+
->setDefinition($inputDefinition)
534+
->setDescription('Queries sample data using SQL with a BOOL parameter.')
535+
->setCode(function ($input, $output) {
536+
query_data_with_bool_parameter(
537+
$input->getArgument('instance_id'),
538+
$input->getArgument('database_id')
539+
);
540+
})
541+
);
542+
543+
// Query Data with Bytes Parameter
544+
$application->add((new Command('query-data-with-bytes-parameter'))
545+
->setDefinition($inputDefinition)
546+
->setDescription('Queries sample data using SQL with a BYTES parameter.')
547+
->setCode(function ($input, $output) {
548+
query_data_with_bytes_parameter(
549+
$input->getArgument('instance_id'),
550+
$input->getArgument('database_id')
551+
);
552+
})
553+
);
554+
555+
// Query Data with Date Parameter
556+
$application->add((new Command('query-data-with-date-parameter'))
557+
->setDefinition($inputDefinition)
558+
->setDescription('Queries sample data using SQL with a DATE parameter.')
559+
->setCode(function ($input, $output) {
560+
query_data_with_date_parameter(
561+
$input->getArgument('instance_id'),
562+
$input->getArgument('database_id')
563+
);
564+
})
565+
);
566+
567+
// Query Data with Float Parameter
568+
$application->add((new Command('query-data-with-float-parameter'))
569+
->setDefinition($inputDefinition)
570+
->setDescription('Queries sample data using SQL with a FLOAT64 parameter.')
571+
->setCode(function ($input, $output) {
572+
query_data_with_float_parameter(
573+
$input->getArgument('instance_id'),
574+
$input->getArgument('database_id')
575+
);
576+
})
577+
);
578+
579+
// Query Data with Int Parameter
580+
$application->add((new Command('query-data-with-int-parameter'))
581+
->setDefinition($inputDefinition)
582+
->setDescription('Queries sample data using SQL with a INT64 parameter.')
583+
->setCode(function ($input, $output) {
584+
query_data_with_int_parameter(
585+
$input->getArgument('instance_id'),
586+
$input->getArgument('database_id')
587+
);
588+
})
589+
);
590+
591+
// Query Data with String Parameter
592+
$application->add((new Command('query-data-with-string-parameter'))
593+
->setDefinition($inputDefinition)
594+
->setDescription('Queries sample data using SQL with a STRING parameter.')
595+
->setCode(function ($input, $output) {
596+
query_data_with_string_parameter(
597+
$input->getArgument('instance_id'),
598+
$input->getArgument('database_id')
599+
);
600+
})
601+
);
602+
603+
// Query Data with Timestamp Parameter
604+
$application->add((new Command('query-data-with-timestamp-parameter'))
605+
->setDefinition($inputDefinition)
606+
->setDescription('Queries sample data using SQL with a TIMESTAMP parameter.')
607+
->setCode(function ($input, $output) {
608+
query_data_with_timestamp_parameter(
609+
$input->getArgument('instance_id'),
610+
$input->getArgument('database_id')
611+
);
612+
})
613+
);
614+
495615
// for testing
496616
if (getenv('PHPUNIT_TESTS') === '1') {
497617
return $application;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
* Copyright 2019 Google LLC.
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/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_create_table_with_datatypes]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Creates a table with suported datatypes.
31+
* Example:
32+
* ```
33+
* create_table_with_datatypes($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function create_table_with_datatypes($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$operation = $database->updateDdl(
46+
"CREATE TABLE Venues (
47+
VenueId INT64 NOT NULL,
48+
VenueName STRING(100),
49+
VenueInfo BYTES(MAX),
50+
Capacity INT64,
51+
AvailableDates ARRAY,
52+
LastContactDate DATE,
53+
OutdoorVenue BOOL,
54+
PopularityScore FLOAT64,
55+
LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)
56+
) PRIMARY KEY (VenueId)"
57+
);
58+
59+
print('Waiting for operation to complete...' . PHP_EOL);
60+
$operation->pollUntilComplete();
61+
62+
printf('Created Venues table in database %s on instance %s' . PHP_EOL,
63+
$databaseId, $instanceId);
64+
}
65+
// [END spanner_create_table_with_datatypes]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
/**
3+
* Copyright 2019 Google LLC.
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/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_insert_datatypes_data]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Inserts sample data into a table with supported datatypes.
31+
*
32+
* The database and table must already exist and can be created using
33+
* `create_table_with_datatypes`.
34+
* Example:
35+
* ```
36+
* insert_data_with_datatypes($instanceId, $databaseId);
37+
* ```
38+
*
39+
* @param string $instanceId The Spanner instance ID.
40+
* @param string $databaseId The Spanner database ID.
41+
*/
42+
function insert_data_with_datatypes($instanceId, $databaseId)
43+
{
44+
$spanner = new SpannerClient();
45+
$instance = $spanner->instance($instanceId);
46+
$database = $instance->database($databaseId);
47+
48+
$operation = $database->transaction(['singleUse' => true])
49+
->insertBatch('Venues', [
50+
[
51+
'VenueId' => 4,
52+
'VenueName' => 'Venue 4',
53+
'VenueInfo' => base64_encode('Hello World 1'),
54+
'Capacity' => 1800,
55+
'AvailableDates' => ['2020-12-01', '2020-12-02', '2020-12-03'],
56+
'LastContactDate' => '2018-09-02',
57+
'OutdoorVenue' => false,
58+
'PopularityScore' => 0.85543,
59+
'LastUpdateTime' => $spanner->commitTimestamp()
60+
], [
61+
'VenueId' => 19,
62+
'VenueName' => 'Venue 19',
63+
'VenueInfo' => base64_encode('Hello World 2'),
64+
'Capacity' => 6300,
65+
'AvailableDates' => ['2020-11-01', '2020-11-05', '2020-11-15'],
66+
'LastContactDate' => '2019-01-15',
67+
'OutdoorVenue' => true,
68+
'PopularityScore' => 0.98716,
69+
'LastUpdateTime' => $spanner->commitTimestamp()
70+
], [
71+
'VenueId' => 42,
72+
'VenueName' => 'Venue 42',
73+
'VenueInfo' => base64_encode('Hello World 3'),
74+
'Capacity' => 3000,
75+
'AvailableDates' => ['2020-10-01', '2020-10-07'],
76+
'LastContactDate' => '2018-10-01',
77+
'OutdoorVenue' => false,
78+
'PopularityScore' => 0.72598,
79+
'LastUpdateTime' => $spanner->commitTimestamp()
80+
],
81+
])
82+
->commit();
83+
84+
print('Inserted data.' . PHP_EOL);
85+
}
86+
// [END spanner_insert_datatypes_data]

0 commit comments

Comments
 (0)