Skip to content

Commit

Permalink
Update spanner sample to use bind parameters [(googleapis#928)](Googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored May 3, 2017
1 parent 7391845 commit 3337ecb
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def add_index(instance_id, database_id):
print('Added the AlbumsByAlbumTitle index.')


def query_data_with_index(instance_id, database_id):
def query_data_with_index(
instance_id, database_id, start_title='Aardvark', end_title='Goo'):
"""Queries sample data from the database using SQL and an index.
The index must exist before running this sample. You can add the index
Expand All @@ -168,14 +169,25 @@ def query_data_with_index(instance_id, database_id):
ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
"""
from google.cloud.proto.spanner.v1 import type_pb2

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

params = {
'start_title': start_title,
'end_title': end_title
}
param_types = {
'start_title': type_pb2.Type(code=type_pb2.STRING),
'end_title': type_pb2.Type(code=type_pb2.STRING)
}
results = database.execute_sql(
"SELECT AlbumId, AlbumTitle, MarketingBudget "
"FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle} "
"WHERE AlbumTitle >= 'Ardvark' AND AlbumTitle < 'Goo'")
"WHERE AlbumTitle >= @start_title AND AlbumTitle < @end_title",
params=params, param_types=param_types)

for row in results:
print(
Expand Down Expand Up @@ -413,7 +425,12 @@ def read_only_transaction(instance_id, database_id):
subparsers.add_parser(
'read_only_transaction', help=read_only_transaction.__doc__)
subparsers.add_parser('add_index', help=add_index.__doc__)
subparsers.add_parser('query_data_with_index', help=insert_data.__doc__)
query_data_with_index_parser = subparsers.add_parser(
'query_data_with_index', help=query_data_with_index.__doc__)
query_data_with_index_parser.add_argument(
'start_title', default='Aardvark')
query_data_with_index_parser.add_argument(
'end_title', default='Goo')
subparsers.add_parser('read_data_with_index', help=insert_data.__doc__)
subparsers.add_parser('add_storing_index', help=add_storing_index.__doc__)
subparsers.add_parser(
Expand Down Expand Up @@ -442,7 +459,9 @@ def read_only_transaction(instance_id, database_id):
elif args.command == 'add_index':
add_index(args.instance_id, args.database_id)
elif args.command == 'query_data_with_index':
query_data_with_index(args.instance_id, args.database_id)
query_data_with_index(
args.instance_id, args.database_id,
args.start_title, args.end_title)
elif args.command == 'read_data_with_index':
read_data_with_index(args.instance_id, args.database_id)
elif args.command == 'add_storing_index':
Expand Down

0 comments on commit 3337ecb

Please sign in to comment.