From e29bace364d6f46a3a67a4c33c15b463b70653b2 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Tue, 14 May 2019 12:48:34 -0700 Subject: [PATCH 1/3] Add queryWithParameter to Cloud Spanner sample. --- spanner/cloud-client/snippets.py | 26 ++++++++++++++++++++++++++ spanner/cloud-client/snippets_test.py | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 9bbe0fbd8c21..d67829a531dd 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -943,6 +943,28 @@ def insert_singers(transaction): # [END spanner_dml_getting_started_insert] + +def query_data_with_parameter(instance_id, database_id): + """Queries sample data from the database using SQL with a parameter.""" + # [START spanner_query_with_parameter] + # instance_id = "your-spanner-instance" + # database_id = "your-spanner-db-id" + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + database = instance.database(database_id) + + with database.snapshot() as snapshot: + results = snapshot.execute_sql( + "SELECT SingerId, FirstName, LastName FROM Singers " + "WHERE LastName = @lastName", + params={"lastName": "Garcia"}, + param_types={"lastName": spanner.param_types.STRING}) + + for row in results: + print(u"SingerId: {}, FirstName: {}, LastName: {}".format(*row)) +# [END spanner_query_with_parameter] + + def write_with_dml_transaction(instance_id, database_id): """ Transfers a marketing budget from one album to another. """ # [START spanner_dml_getting_started_update] @@ -1145,6 +1167,8 @@ def update_albums(transaction): 'update_data_with_dml_struct', help=update_data_with_dml_struct.__doc__) subparsers.add_parser('insert_with_dml', help=insert_with_dml.__doc__) + subparsers.add_parser( + 'query_data_with_parameter', help=query_data_with_parameter.__doc__) subparsers.add_parser( 'write_with_dml_transaction', help=write_with_dml_transaction.__doc__) subparsers.add_parser( @@ -1227,6 +1251,8 @@ def update_albums(transaction): update_data_with_dml_struct(args.instance_id, args.database_id) elif args.command == 'insert_with_dml': insert_with_dml(args.instance_id, args.database_id) + elif args.command == 'query_data_with_parameter': + query_data_with_parameter(args.instance_id, args.database_id) elif args.command == 'write_with_dml_transaction': write_with_dml_transaction(args.instance_id, args.database_id) elif args.command == 'update_data_with_partitioned_dml': diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 9bd39d27a571..257b46b1839a 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -261,6 +261,12 @@ def test_insert_with_dml(capsys): assert '4 record(s) inserted' in out +def test_query_data_with_parameter(capsys): + snippets.query_data_with_parameter(INSTANCE_ID, DATABASE_ID) + out, _ = capsys.readouterr() + assert 'SingerId: 12, FirstName: Melissa, LastName: Garcia' in out + + def test_write_with_dml_transaction(capsys): snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() From a6f393480d5cedf4cb49a77558597a110304353f Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Tue, 14 May 2019 16:51:49 -0700 Subject: [PATCH 2/3] Lint. --- spanner/cloud-client/snippets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index d67829a531dd..8d4019416055 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -943,7 +943,6 @@ def insert_singers(transaction): # [END spanner_dml_getting_started_insert] - def query_data_with_parameter(instance_id, database_id): """Queries sample data from the database using SQL with a parameter.""" # [START spanner_query_with_parameter] From 8a5ac918a10bf2506e132feb814c4ef4b86967ba Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 15 May 2019 11:47:22 -0700 Subject: [PATCH 3/3] Update to fix test. --- spanner/cloud-client/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 257b46b1839a..199d69f16dbf 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -202,7 +202,7 @@ def test_query_with_struct(capsys): def test_query_with_array_of_struct(capsys): snippets.query_with_array_of_struct(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr() - assert 'SingerId: 6\nSingerId: 7' in out + assert 'SingerId: 8\nSingerId: 7\nSingerId: 6' in out def test_query_struct_field(capsys):