Skip to content

Commit bd5e175

Browse files
authored
feat(spanner): add standalone samples for NUMERIC type (GoogleCloudPlatform#1183)
1 parent 896c0c5 commit bd5e175

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed

spanner/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"src/delete_data.php",
2626
"src/create_table_with_timestamp_column.php",
2727
"src/insert_data_with_timestamp_column.php",
28+
"src/add_numeric_column.php",
2829
"src/add_timestamp_column.php",
30+
"src/update_data_with_numeric_column.php",
2931
"src/update_data_with_timestamp_column.php",
3032
"src/query_data_with_timestamp_column.php",
3133
"src/insert_struct_data.php",
@@ -53,6 +55,7 @@
5355
"src/query_data_with_date_parameter.php",
5456
"src/query_data_with_float_parameter.php",
5557
"src/query_data_with_int_parameter.php",
58+
"src/query_data_with_numeric_parameter.php",
5659
"src/query_data_with_string_parameter.php",
5760
"src/query_data_with_timestamp_parameter.php",
5861
"src/create_client_with_query_options.php",

spanner/spanner.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@
291291
})
292292
);
293293

294+
// Add numeric column command
295+
$application->add((new Command('add-numeric-column'))
296+
->setDefinition($inputDefinition)
297+
->setDescription('Adds a NUMERIC column to a table.')
298+
->setCode(function ($input, $output) {
299+
add_numeric_column(
300+
$input->getArgument('instance_id'),
301+
$input->getArgument('database_id')
302+
);
303+
})
304+
);
305+
294306
// Add timestamp column command
295307
$application->add((new Command('add-timestamp-column'))
296308
->setDefinition($inputDefinition)
@@ -303,6 +315,18 @@
303315
})
304316
);
305317

318+
// Update data with numeric column command
319+
$application->add((new Command('update-data-numeric'))
320+
->setDefinition($inputDefinition)
321+
->setDescription('Updates sample data in a table with a NUMERIC column.')
322+
->setCode(function ($input, $output) {
323+
update_data_with_numeric_column(
324+
$input->getArgument('instance_id'),
325+
$input->getArgument('database_id')
326+
);
327+
})
328+
);
329+
306330
// Update data with timestamp column command
307331
$application->add((new Command('update-data-timestamp'))
308332
->setDefinition($inputDefinition)
@@ -627,6 +651,18 @@
627651
})
628652
);
629653

654+
// Query Data with Numeric Parameter
655+
$application->add((new Command('query-data-with-numeric-parameter'))
656+
->setDefinition($inputDefinition)
657+
->setDescription('Queries sample data using SQL with a NUMERIC parameter.')
658+
->setCode(function ($input, $output) {
659+
query_data_with_numeric_parameter(
660+
$input->getArgument('instance_id'),
661+
$input->getArgument('database_id')
662+
);
663+
})
664+
);
665+
630666
// Query Data with String Parameter
631667
$application->add((new Command('query-data-with-string-parameter'))
632668
->setDefinition($inputDefinition)

spanner/src/add_numeric_column.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Copyright 2020 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_add_numeric_column]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Adds a NUMERIC column to a table.
31+
* Example:
32+
* ```
33+
* add_numeric_column($instanceId, $databaseId);
34+
* ```
35+
*
36+
* @param string $instanceId The Spanner instance ID.
37+
* @param string $databaseId The Spanner database ID.
38+
*/
39+
function add_numeric_column($instanceId, $databaseId)
40+
{
41+
$spanner = new SpannerClient();
42+
$instance = $spanner->instance($instanceId);
43+
$database = $instance->database($databaseId);
44+
45+
$operation = $database->updateDdl(
46+
"ALTER TABLE Venues ADD COLUMN Revenue NUMERIC"
47+
);
48+
49+
print('Waiting for operation to complete...' . PHP_EOL);
50+
$operation->pollUntilComplete();
51+
52+
printf('Added Revenue as a NUMERIC column in Venues table' . PHP_EOL);
53+
}
54+
// [END spanner_add_numeric_column]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
/**
3+
* Copyright 2020 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_query_with_numeric_parameter]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\Database;
29+
30+
/**
31+
* Queries sample data from the database using SQL with a NUMERIC parameter.
32+
* Example:
33+
* ```
34+
* query_data_with_numeric_parameter($instanceId, $databaseId);
35+
* ```
36+
*
37+
* @param string $instanceId The Spanner instance ID.
38+
* @param string $databaseId The Spanner database ID.
39+
*/
40+
function query_data_with_numeric_parameter($instanceId, $databaseId)
41+
{
42+
$spanner = new SpannerClient();
43+
$instance = $spanner->instance($instanceId);
44+
$database = $instance->database($databaseId);
45+
46+
$exampleNumeric = $spanner->numeric("100000");
47+
48+
$results = $database->execute(
49+
'SELECT VenueId, Revenue FROM Venues ' .
50+
'WHERE Revenue < @revenue',
51+
[
52+
'parameters' => [
53+
'revenue' => $exampleNumeric
54+
]
55+
]
56+
);
57+
58+
foreach ($results as $row) {
59+
printf('VenueId: %s, Revenue: %s' . PHP_EOL,
60+
$row['VenueId'], $row['Revenue']);
61+
}
62+
}
63+
// [END spanner_query_with_numeric_parameter]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2020 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_update_data_with_numeric_column]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
29+
/**
30+
* Updates sample data in a table with a NUMERIC column.
31+
*
32+
* Before executing this method, a new column Revenue has to be added to the Venues
33+
* table by applying the DDL statement "ALTER TABLE Venues ADD COLUMN Revenue NUMERIC".
34+
*
35+
* Example:
36+
* ```
37+
* update_data_with_numeric_column($instanceId, $databaseId);
38+
* ```
39+
*
40+
* @param string $instanceId The Spanner instance ID.
41+
* @param string $databaseId The Spanner database ID.
42+
*/
43+
function update_data_with_numeric_column($instanceId, $databaseId)
44+
{
45+
$spanner = new SpannerClient();
46+
$instance = $spanner->instance($instanceId);
47+
$database = $instance->database($databaseId);
48+
49+
$database->transaction(['singleUse' => true])
50+
->updateBatch('Venues', [
51+
['VenueId' => 4, 'Revenue' => $spanner->numeric("35000")],
52+
['VenueId' => 19, 'Revenue' => $spanner->numeric("104500")],
53+
['VenueId' => 42, 'Revenue' => $spanner->numeric("99999999999999999999999999999.99")],
54+
])
55+
->commit();
56+
57+
print('Updated data.' . PHP_EOL);
58+
}
59+
// [END spanner_update_data_with_numeric_column]

spanner/test/spannerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,34 @@ public function testQueryDataWithQueryOptions()
634634
});
635635
}
636636

637+
/**
638+
* @depends testInsertDataWithDatatypes
639+
*/
640+
public function testAddNumericColumn()
641+
{
642+
$output = $this->runCommand('add-numeric-column');
643+
$this->assertContains('Waiting for operation to complete...', $output);
644+
$this->assertContains('Added Revenue as a NUMERIC column in Venues table', $output);
645+
}
646+
647+
/**
648+
* @depends testAddNumericColumn
649+
*/
650+
public function testUpdateDataNumeric()
651+
{
652+
$output = $this->runCommand('update-data-numeric');
653+
$this->assertEquals('Updated data.' . PHP_EOL, $output);
654+
}
655+
656+
/**
657+
* @depends testUpdateDataTimestamp
658+
*/
659+
public function testQueryDataNumeric()
660+
{
661+
$output = $this->runCommand('query-data-with-numeric-parameter');
662+
$this->assertContains('VenueId: 4, Revenue: 35000', $output);
663+
}
664+
637665
/**
638666
* @depends testInsertDataWithDatatypes
639667
*/

0 commit comments

Comments
 (0)