<s> """<STR_LIT>""" <EOL> import six <EOL> from gcloud . bigquery . _helpers import _TypedProperty <EOL> from gcloud . bigquery . _helpers import _rows_from_json <EOL> from gcloud . bigquery . dataset import Dataset <EOL> from gcloud . bigquery . job import QueryJob <EOL> from gcloud . bigquery . table import _parse_schema_resource <EOL> class _SyncQueryConfiguration ( object ) : <EOL> """<STR_LIT>""" <EOL> _default_dataset = None <EOL> _max_results = None <EOL> _timeout_ms = None <EOL> _preserve_nulls = None <EOL> _use_query_cache = None <EOL> class QueryResults ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query , client ) : <EOL> self . _client = client <EOL> self . _properties = { } <EOL> self . query = query <EOL> self . _configuration = _SyncQueryConfiguration ( ) <EOL> self . _job = None <EOL> @ property <EOL> def project ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . project <EOL> def _require_client ( self , client ) : <EOL> """<STR_LIT>""" <EOL> if client is None : <EOL> client = self . _client <EOL> return client <EOL> @ property <EOL> def cache_hit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def complete ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def errors ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def job ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _job is None : <EOL> job_ref = self . _properties . get ( '<STR_LIT>' ) <EOL> if job_ref is not None : <EOL> self . _job = QueryJob ( job_ref [ '<STR_LIT>' ] , self . query , <EOL> self . _client ) <EOL> return self . _job <EOL> @ property <EOL> def page_token ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def total_rows ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def total_bytes_processed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def rows ( self ) : <EOL> """<STR_LIT>""" <EOL> return _rows_from_json ( self . _properties . get ( '<STR_LIT>' , ( ) ) , self . schema ) <EOL> @ property <EOL> def schema ( self ) : <EOL> """<STR_LIT>""" <EOL> return _parse_schema_resource ( self . _properties . get ( '<STR_LIT>' , { } ) ) <EOL> default_dataset = _TypedProperty ( '<STR_LIT>' , Dataset ) <EOL> """<STR_LIT>""" <EOL> max_results = _TypedProperty ( '<STR_LIT>' , six . integer_types ) <EOL> """<STR_LIT>""" <EOL> preserve_nulls = _TypedProperty ( '<STR_LIT>' , bool ) <EOL> """<STR_LIT>""" <EOL> timeout_ms = _TypedProperty ( '<STR_LIT>' , six . integer_types ) <EOL> """<STR_LIT>""" <EOL> use_query_cache = _TypedProperty ( '<STR_LIT>' , bool ) <EOL> """<STR_LIT>""" <EOL> def _set_properties ( self , api_response ) : <EOL> """<STR_LIT>""" <EOL> self . _properties . clear ( ) <EOL> self . _properties . update ( api_response ) <EOL> def _build_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = { '<STR_LIT>' : self . query } <EOL> if self . default_dataset is not None : <EOL> resource [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . default_dataset . name , <EOL> } <EOL> if self . max_results is not None : <EOL> resource [ '<STR_LIT>' ] = self . max_results <EOL> if self . preserve_nulls is not None : <EOL> resource [ '<STR_LIT>' ] = self . preserve_nulls <EOL> if self . timeout_ms is not None : <EOL> resource [ '<STR_LIT>' ] = self . timeout_ms <EOL> if self . use_query_cache is not None : <EOL> resource [ '<STR_LIT>' ] = self . use_query_cache <EOL> return resource <EOL> def run ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> path = '<STR_LIT>' % ( self . project , ) <EOL> api_response = client . connection . api_request ( <EOL> method = '<STR_LIT:POST>' , path = path , data = self . _build_resource ( ) ) <EOL> self . _set_properties ( api_response ) <EOL> def fetch_data ( self , max_results = None , page_token = None , start_index = None , <EOL> timeout_ms = None , client = None ) : <EOL> """<STR_LIT>""" <EOL> if self . name is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> client = self . _require_client ( client ) <EOL> params = { } <EOL> if max_results is not None : <EOL> params [ '<STR_LIT>' ] = max_results <EOL> if page_token is not None : <EOL> params [ '<STR_LIT>' ] = page_token <EOL> if start_index is not None : <EOL> params [ '<STR_LIT>' ] = start_index <EOL> if timeout_ms is not None : <EOL> params [ '<STR_LIT>' ] = timeout_ms <EOL> path = '<STR_LIT>' % ( self . project , self . name ) <EOL> response = client . connection . api_request ( method = '<STR_LIT:GET>' , <EOL> path = path , <EOL> query_params = params ) <EOL> self . _set_properties ( response ) <EOL> total_rows = response . get ( '<STR_LIT>' ) <EOL> page_token = response . get ( '<STR_LIT>' ) <EOL> rows_data = _rows_from_json ( response . get ( '<STR_LIT>' , ( ) ) , self . schema ) <EOL> return rows_data , total_rows , page_token </s>
