|
23 | 23 | import argparse
|
24 | 24 | import base64
|
25 | 25 | import datetime
|
| 26 | +import decimal |
26 | 27 |
|
27 | 28 | from google.cloud import spanner
|
28 | 29 | from google.cloud.spanner_v1 import param_types
|
@@ -658,6 +659,57 @@ def query_data_with_timestamp(instance_id, database_id):
|
658 | 659 | # [END spanner_query_data_with_timestamp_column]
|
659 | 660 |
|
660 | 661 |
|
| 662 | +# [START spanner_add_numeric_column] |
| 663 | +def add_numeric_column(instance_id, database_id): |
| 664 | + """ Adds a new NUMERIC column to the Venues table in the example database. |
| 665 | + """ |
| 666 | + spanner_client = spanner.Client() |
| 667 | + instance = spanner_client.instance(instance_id) |
| 668 | + |
| 669 | + database = instance.database(database_id) |
| 670 | + |
| 671 | + operation = database.update_ddl([ |
| 672 | + 'ALTER TABLE Venues ADD COLUMN Revenue NUMERIC']) |
| 673 | + |
| 674 | + print('Waiting for operation to complete...') |
| 675 | + operation.result(120) |
| 676 | + |
| 677 | + print('Altered table "Venue" on database {} on instance {}.'.format( |
| 678 | + database_id, instance_id)) |
| 679 | +# [END spanner_add_numeric_column] |
| 680 | + |
| 681 | + |
| 682 | +# [START spanner_update_data_with_numeric_column] |
| 683 | +def update_data_with_numeric(instance_id, database_id): |
| 684 | + """Updates Venues tables in the database with the NUMERIC |
| 685 | + column. |
| 686 | +
|
| 687 | + This updates the `Revenue` column which must be created before |
| 688 | + running this sample. You can add the column by running the |
| 689 | + `add_numeric_column` sample or by running this DDL statement |
| 690 | + against your database: |
| 691 | +
|
| 692 | + ALTER TABLE Venues ADD COLUMN Revenue NUMERIC |
| 693 | + """ |
| 694 | + spanner_client = spanner.Client() |
| 695 | + instance = spanner_client.instance(instance_id) |
| 696 | + |
| 697 | + database = instance.database(database_id) |
| 698 | + |
| 699 | + with database.batch() as batch: |
| 700 | + batch.update( |
| 701 | + table='Venues', |
| 702 | + columns=('VenueId', 'Revenue'), |
| 703 | + values=[ |
| 704 | + (4, decimal.Decimal("35000")), |
| 705 | + (19, decimal.Decimal("104500")), |
| 706 | + (42, decimal.Decimal("99999999999999999999999999999.99")) |
| 707 | + ]) |
| 708 | + |
| 709 | + print('Updated data.') |
| 710 | +# [END spanner_update_data_with_numeric_column] |
| 711 | + |
| 712 | + |
661 | 713 | # [START spanner_write_data_for_struct_queries]
|
662 | 714 | def write_struct_data(instance_id, database_id):
|
663 | 715 | """Inserts sample data that can be used to test STRUCT parameters
|
@@ -1397,6 +1449,34 @@ def query_data_with_string(instance_id, database_id):
|
1397 | 1449 | # [END spanner_query_with_string_parameter]
|
1398 | 1450 |
|
1399 | 1451 |
|
| 1452 | +def query_data_with_numeric_parameter(instance_id, database_id): |
| 1453 | + """Queries sample data using SQL with a NUMERIC parameter. """ |
| 1454 | + # [START spanner_query_with_numeric_parameter] |
| 1455 | + # instance_id = "your-spanner-instance" |
| 1456 | + # database_id = "your-spanner-db-id" |
| 1457 | + spanner_client = spanner.Client() |
| 1458 | + instance = spanner_client.instance(instance_id) |
| 1459 | + database = instance.database(database_id) |
| 1460 | + |
| 1461 | + example_numeric = decimal.Decimal("100000") |
| 1462 | + param = { |
| 1463 | + 'revenue': example_numeric, |
| 1464 | + } |
| 1465 | + param_type = { |
| 1466 | + 'revenue': param_types.NUMERIC |
| 1467 | + } |
| 1468 | + |
| 1469 | + with database.snapshot() as snapshot: |
| 1470 | + results = snapshot.execute_sql( |
| 1471 | + 'SELECT VenueId, Revenue FROM Venues ' |
| 1472 | + 'WHERE Revenue < @revenue', |
| 1473 | + params=param, param_types=param_type) |
| 1474 | + |
| 1475 | + for row in results: |
| 1476 | + print(u"VenueId: {}, Revenue: {}".format(*row)) |
| 1477 | + # [END spanner_query_with_numeric_parameter] |
| 1478 | + |
| 1479 | + |
1400 | 1480 | def query_data_with_timestamp_parameter(instance_id, database_id):
|
1401 | 1481 | """Queries sample data using SQL with a TIMESTAMP parameter. """
|
1402 | 1482 | # [START spanner_query_with_timestamp_parameter]
|
|
0 commit comments