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 support for query parameters #2776

Merged
merged 20 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions docs/bigquery-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ Run a query which can be expected to complete within bounded time:
:start-after: [START client_run_sync_query]
:end-before: [END client_run_sync_query]

Run a query using a named query parameter:

.. literalinclude:: bigquery_snippets.py
:start-after: [START client_run_sync_query_w_param]
:end-before: [END client_run_sync_query_w_param]

If the rows returned by the query do not fit into the initial response,
then we need to fetch the remaining rows via
:meth:`~google.cloud.bigquery.query.QueryResults.fetch_data`:
Expand Down
34 changes: 27 additions & 7 deletions docs/bigquery_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,9 @@ def do_something_with(_):
pass

# [START client_list_jobs]
jobs, token = client.list_jobs() # API request
while True:
for job in jobs:
do_something_with(job)
if token is None:
break
jobs, token = client.list_jobs(page_token=token) # API request
job_iterator = client.list_jobs() # API request

This comment was marked as spam.

for job in job_iterator:
do_something_with(job)
# [END client_list_jobs]


Expand All @@ -489,6 +485,30 @@ def client_run_sync_query(client, _):
# [END client_run_sync_query]


@snippet
def client_run_sync_query_w_param(client, _):
"""Run a synchronous query using a query parameter"""
QUERY_W_PARM = (

This comment was marked as spam.

'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
'WHERE state = @state')
LIMIT = 100
LIMITED = '%s LIMIT %d' % (QUERY_W_PARM, LIMIT)
TIMEOUT_MS = 1000

# [START client_run_sync_query_w_param]
from google.cloud.bigquery import ScalarQueryParameter
param = ScalarQueryParameter('state', 'STRING', 'TX')
query = client.run_sync_query(LIMITED, query_parameters=[param])
query.use_legacy_sql = False
query.timeout_ms = TIMEOUT_MS
query.run() # API request

assert query.complete
assert len(query.rows) == LIMIT
assert [field.name for field in query.schema] == ['name']
# [END client_run_sync_query_w_param]


@snippet
def client_run_sync_query_paged(client, _):
"""Run a synchronous query with paged results."""
Expand Down