a
    !ff2                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ G d
d deZG dd deZdS )zDefine API Queries.    N)_TypedProperty)_rows_from_json)Dataset)QueryJob)UDFResourcesProperty)_build_udf_resources)_parse_schema_resourcec                   @   s,   e Zd ZdZdZdZdZdZdZdZ	dZ
dS )_SyncQueryConfigurationzwUser-settable configuration options for synchronous query jobs.

    Values which are ``None`` -> server defaults.
    N)__name__
__module____qualname____doc__Z_default_dataset_dry_runZ_max_resultsZ_timeout_msZ_preserve_nullsZ_use_query_cacheZ_use_legacy_sql r   r   V/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigquery/query.pyr	      s   r	   c                   @   s  e Zd ZdZdZd.ddZedd Zdd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZedeZedeZed ejZed!eZed"ejZe Zed#eZed$eZ d%d& Z!d'd( Z"d/d*d+Z#d0d,d-Z$d)S )1QueryResultsa  Synchronous job: query tables.

    :type query: string
    :param query: SQL query string

    :type client: :class:`gcloud.bigquery.client.Client`
    :param client: A client which holds credentials and project configuration
                   for the dataset (which requires a project).

    :type udf_resources: tuple
    :param udf_resources: An iterable of
                        :class:`gcloud.bigquery.job.UDFResource`
                        (empty by default)
    ZuserDefinedFunctionResourcesr   c                 C   s*   || _ i | _|| _t | _|| _d | _d S )N)_client_propertiesqueryr	   Z_configurationudf_resources_job)selfr   clientr   r   r   r   __init__<   s    zQueryResults.__init__c                 C   s   | j jS )zsProject bound to the job.

        :rtype: string
        :returns: the project (derived from the client).
        )r   projectr   r   r   r   r   D   s    zQueryResults.projectc                 C   s   |du r| j }|S )a  Check client or verify over-ride.

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: :class:`gcloud.bigquery.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r   )r   r   r   r   r   _require_clientM   s    
zQueryResults._require_clientc                 C   s   | j dS )a%  Query results served from cache.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#cacheHit

        :rtype: boolean or ``NoneType``
        :returns: True if the query results were served from cache (None
                  until set by the server).
        ZcacheHitr   getr   r   r   r   	cache_hit[   s    zQueryResults.cache_hitc                 C   s   | j dS )a  Server completed query.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#jobComplete

        :rtype: boolean or ``NoneType``
        :returns: True if the query completed on the server (None
                  until set by the server).
        ZjobCompleter   r   r   r   r   completeh   s    zQueryResults.completec                 C   s   | j dS )a,  Errors generated by the query.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#errors

        :rtype: list of mapping, or ``NoneType``
        :returns: Mappings describing errors generated on the server (None
                  until set by the server).
        errorsr   r   r   r   r   r!   u   s    zQueryResults.errorsc                 C   s   | j di dS )a8  Job name, generated by the back-end.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#jobReference

        :rtype: list of mapping, or ``NoneType``
        :returns: Mappings describing errors generated on the server (None
                  until set by the server).
        jobReferencejobIdr   r   r   r   r   name   s    zQueryResults.namec                 C   s:   | j du r4| jd}|dur4t|d | j| j| _ | j S )zJob instance used to run the query.

        :rtype: :class:`gcloud.bigquery.job.QueryJob`, or ``NoneType``
        :returns: Job instance used to run the query (None until
                  ``jobReference`` property is set by the server).
        Nr"   r#   )r   r   r   r   r   r   )r   Zjob_refr   r   r   job   s    
zQueryResults.jobc                 C   s   | j dS )a	  Token for fetching next bach of results.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#pageToken

        :rtype: string, or ``NoneType``
        :returns: Token generated on the server (None until set by the server).
        	pageTokenr   r   r   r   r   
page_token   s    
zQueryResults.page_tokenc                 C   s   | j dS )a  Total number of rows returned by the query

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#totalRows

        :rtype: integer, or ``NoneType``
        :returns: Count generated on the server (None until set by the server).
        	totalRowsr   r   r   r   r   
total_rows   s    
zQueryResults.total_rowsc                 C   s   | j dS )a  Total number of bytes processed by the query

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#totalBytesProcessed

        :rtype: integer, or ``NoneType``
        :returns: Count generated on the server (None until set by the server).
        ZtotalBytesProcessedr   r   r   r   r   total_bytes_processed   s    
z"QueryResults.total_bytes_processedc                 C   s   t | jdd| jS )zQuery results.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#rows

        :rtype: list of tuples of row values, or ``NoneType``
        :returns: fields describing the schema (None until set by the server).
        rowsr   )r   r   r   schemar   r   r   r   r+      s    
zQueryResults.rowsc                 C   s   t | jdi S )a  Schema for query results.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#schema

        :rtype: list of :class:`SchemaField`, or ``NoneType``
        :returns: fields describing the schema (None until set by the server).
        r,   )r   r   r   r   r   r   r   r,      s    
zQueryResults.schemadefault_datasetdry_runmax_resultspreserve_nulls
timeout_msuse_query_cacheuse_legacy_sqlc                 C   s   | j   | j | dS )zUpdate properties from resource in body of ``api_response``

        :type api_response: httplib2.Response
        :param api_response: response returned from an API call
        N)r   clearupdate)r   api_responser   r   r   _set_properties   s    
zQueryResults._set_propertiesc                 C   s   d| j i}| jdur(| j| jjd|d< | jdur<| j|d< | jdurP| j|d< | jdurd| j|d< | jdurx| j|d< | jdur| j|d	< | j	dur| j	|d
< t
| jdkrt| j|| j< |S )z&Generate a resource for :meth:`begin`.r   N)Z	projectIdZ	datasetIdZdefaultDataset
maxResultsZpreserveNulls	timeoutMsZuseQueryCacheZuseLegacySqlZdryRunr   )r   r-   r   r$   r/   r0   r1   r2   r3   r.   lenZ_udf_resourcesr   _UDF_KEY)r   resourcer   r   r   _build_resource	  s(    














zQueryResults._build_resourceNc                 C   s:   |  |}d| jf }|jjd||  d}| | dS )ad  API call:  run the query via a POST request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/query

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.
        z/projects/%s/queriesPOST)methodpathdataN)r   r   
connectionapi_requestr=   r7   )r   r   r@   r6   r   r   r   run*  s    


zQueryResults.runc                 C   s   | j du rtd| |}i }|dur0||d< |dur@||d< |durP||d< |dur`||d< d| j| j f }|jjd||d	}| | |d
}	|	durt|	}	|d}t	|dd| j
}
|
|	|fS )a  API call:  fetch a page of query result data via a GET request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults

        :type max_results: integer or ``NoneType``
        :param max_results: maximum number of rows to return.

        :type page_token: string or ``NoneType``
        :param page_token: token representing a cursor into the table's rows.

        :type start_index: integer or ``NoneType``
        :param start_index: zero-based index of starting row

        :type timeout_ms: integer or ``NoneType``
        :param timeout_ms: timeout, in milliseconds, to wait for query to
                           complete

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: tuple
        :returns: ``(row_data, total_rows, page_token)``, where ``row_data``
                  is a list of tuples, one per result row, containing only
                  the values;  ``total_rows`` is a count of the total number
                  of rows in the table;  and ``page_token`` is an opaque
                  string which can be used to fetch the next batch of rows
                  (``None`` if no further batches can be fetched).
        :raises: ValueError if the query has not yet been executed.
        Nz%Query not yet executed:  call 'run()'r8   r&   Z
startIndexr9   z/projects/%s/queries/%sGET)r?   r@   Zquery_paramsr(   r+   r   )r$   
ValueErrorr   r   rB   rC   r7   r   intr   r,   )r   r/   r'   start_indexr1   r   paramsr@   responser)   Z	rows_datar   r   r   
fetch_data:  s0    !




zQueryResults.fetch_data)r   )N)NNNNN)%r
   r   r   r   r;   r   propertyr   r   r   r    r!   r$   r%   r'   r)   r*   r+   r,   r   r   r-   boolr.   sixinteger_typesr/   r0   r1   r   r   r2   r3   r7   r=   rD   rK   r   r   r   r   r   *   sP   
















	!
  r   )r   rN   Zgcloud.bigquery._helpersr   r   Zgcloud.bigquery.datasetr   Zgcloud.bigquery.jobr   r   r   Zgcloud.bigquery.tabler   objectr	   r   r   r   r   r   <module>   s   