Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add queryWithParameter to Cloud Spanner sample. #2153

Merged
merged 3 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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