Skip to content

Commit

Permalink
Add queryWithParameter to Cloud Spanner sample. (#2153)
Browse files Browse the repository at this point in the history
* Add queryWithParameter to Cloud Spanner sample.

* Lint.

* Update to fix test.
  • Loading branch information
jsimonweb authored May 15, 2019
1 parent 1a2d2d7 commit c66eb83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,27 @@ 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]
Expand Down Expand Up @@ -1145,6 +1166,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(
Expand Down Expand Up @@ -1227,6 +1250,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':
Expand Down
8 changes: 7 additions & 1 deletion spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c66eb83

Please sign in to comment.