a
    !f3*                     @   sx   d 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 dd	lmZ G d
d deZdS )z4Client for interacting with the Google BigQuery API.    )
JSONClient)
ConnectionDatasetCopyJobExtractTableToStorageJobLoadTableFromStorageJobQueryJobQueryResultsc                   @   s`   e Zd ZdZeZdddZdd Zdd	 Zdd
dZ	dd Z
dd Zdd Zdd Zdd ZdS )Clienta  Client to bundle configuration needed for API requests.

    :type project: str
    :param project: the project which the client acts on behalf of. Will be
                    passed when creating a dataset / job.  If not passed,
                    falls back to the default inferred from the environment.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for the connection
                        owned by this client. If not passed (and if no ``http``
                        object is passed), falls back to the default inferred
                        from the environment.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests. If not passed, an
                 ``http`` object is created that is bound to the
                 ``credentials`` for the current object.
    FNc                    sv   i }|rd|d< |dur ||d< |dur0||d< d j f } jjd||d} fd	d
|ddD }||dfS )a  List datasets for the project associated with this client.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/datasets/list

        :type include_all: boolean
        :param include_all: True if results include hidden datasets.

        :type max_results: int
        :param max_results: maximum number of datasets to return, If not
                            passed, defaults to a value set by the API.

        :type page_token: str
        :param page_token: opaque marker for the next "page" of datasets. If
                           not passed, the API will return the first page of
                           datasets.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.bigquery.dataset.Dataset`, plus a
                  "next page token" string:  if the token is not None,
                  indicates that more datasets can be retrieved with another
                  call (pass that value as ``page_token``).
        TallN
maxResults	pageTokenz/projects/%s/datasetsGETmethodpathZquery_paramsc                    s   g | ]}t | qS  )r   from_api_repr.0resourceselfr   W/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigquery/client.py
<listcomp>Z   s   z(Client.list_datasets.<locals>.<listcomp>datasetsr   nextPageTokenproject
connectionZapi_requestget)r   Zinclude_allmax_results
page_tokenparamsr   respr!   r   r   r   list_datasets3   s    


zClient.list_datasetsc                 C   s   t || dS )zConstruct a dataset bound to this client.

        :type dataset_name: str
        :param dataset_name: Name of the dataset.

        :rtype: :class:`gcloud.bigquery.dataset.Dataset`
        :returns: a new ``Dataset`` instance
        clientr   )r   Zdataset_namer   r   r   dataset^   s    	zClient.datasetc                 C   sd   |d }d|v rt || S d|v r0t|| S d|v rDt|| S d|v rXt|| S tddS )a*  Detect correct job type from resource and instantiate.

        :type resource: dict
        :param resource: one job resource from API response

        :rtype: One of:
                :class:`gcloud.bigquery.job.LoadTableFromStorageJob`,
                :class:`gcloud.bigquery.job.CopyJob`,
                :class:`gcloud.bigquery.job.ExtractTableToStorageJob`,
                :class:`gcloud.bigquery.job.QueryJob`,
                :class:`gcloud.bigquery.job.RunSyncQueryJob`
        :returns: the job instance, constructed via the resource
        configurationloadcopyextractqueryzCannot parse job resourceN)r   r   r   r	   r   
ValueError)r   r   configr   r   r   job_from_resourcei   s    zClient.job_from_resourcec           	         s   ddi}|dur||d< |dur(||d< |dur8||d< |durH||d< d j f } jjd	||d
} fdd|ddD }||dfS )a  List jobs for the project associated with this client.

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

        :type max_results: int
        :param max_results: maximum number of jobs to return, If not
                            passed, defaults to a value set by the API.

        :type page_token: str
        :param page_token: opaque marker for the next "page" of jobs. If
                           not passed, the API will return the first page of
                           jobs.

        :type all_users: boolean
        :param all_users: if true, include jobs owned by all users in the
                          project.

        :type state_filter: str
        :param state_filter: if passed, include only jobs matching the given
                             state.  One of

                             * ``"done"``
                             * ``"pending"``
                             * ``"running"``

        :rtype: tuple, (list, str)
        :returns: list of job instances, plus a "next page token" string:
                  if the token is not ``None``, indicates that more jobs can be
                  retrieved with another call, passing that value as
                  ``page_token``).
        Z
projectionfullNr   r   ZallUsersZstateFilterz/projects/%s/jobsr   r   c                    s   g | ]}  |qS r   )r6   r   r   r   r   r       s   z$Client.list_jobs.<locals>.<listcomp>jobsr   r"   r#   )	r   r'   r(   Z	all_usersZstate_filterr)   r   r*   r8   r   r   r   	list_jobs   s"    "


zClient.list_jobsc                 G   s   t |||| dS )a  Construct a job for loading data into a table from CloudStorage.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.load

        :type job_name: str
        :param job_name: Name of the job.

        :type destination: :class:`gcloud.bigquery.table.Table`
        :param destination: Table into which data is to be loaded.

        :type source_uris: sequence of string
        :param source_uris: URIs of data files to be loaded; in format
                            ``gs://<bucket_name>/<object_name_or_glob>``.

        :rtype: :class:`gcloud.bigquery.job.LoadTableFromStorageJob`
        :returns: a new ``LoadTableFromStorageJob`` instance
        r,   r
   )r   job_namedestinationZsource_urisr   r   r   load_table_from_storage   s    zClient.load_table_from_storagec                 G   s   t |||| dS )aQ  Construct a job for copying one or more tables into another table.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy

        :type job_name: str
        :param job_name: Name of the job.

        :type destination: :class:`gcloud.bigquery.table.Table`
        :param destination: Table into which data is to be copied.

        :type sources: sequence of :class:`gcloud.bigquery.table.Table`
        :param sources: tables to be copied.

        :rtype: :class:`gcloud.bigquery.job.CopyJob`
        :returns: a new ``CopyJob`` instance
        r,   r   )r   r:   r;   sourcesr   r   r   
copy_table   s    zClient.copy_tablec                 G   s   t |||| dS )a  Construct a job for extracting a table into Cloud Storage files.

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.extract

        :type job_name: str
        :param job_name: Name of the job.

        :type source: :class:`gcloud.bigquery.table.Table`
        :param source: table to be extracted.

        :type destination_uris: sequence of string
        :param destination_uris: URIs of CloudStorage file(s) into which
                                 table data is to be extracted; in format
                                 ``gs://<bucket_name>/<object_name_or_glob>``.

        :rtype: :class:`gcloud.bigquery.job.ExtractTableToStorageJob`
        :returns: a new ``ExtractTableToStorageJob`` instance
        r,   r   )r   r:   sourceZdestination_urisr   r   r   extract_table_to_storage   s    zClient.extract_table_to_storagec                 C   s   t ||| dS )a  Construct a job for running a SQL query asynchronously.

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

        :type job_name: str
        :param job_name: Name of the job.

        :type query: str
        :param query: SQL query to be executed

        :rtype: :class:`gcloud.bigquery.job.QueryJob`
        :returns: a new ``QueryJob`` instance
        r,   r   )r   r:   r3   r   r   r   run_async_query   s    zClient.run_async_queryc                 C   s   t || dS )zRun a SQL query synchronously.

        :type query: str
        :param query: SQL query to be executed

        :rtype: :class:`gcloud.bigquery.query.QueryResults`
        :returns: a new ``QueryResults`` instance
        r,   r   )r   r3   r   r   r   run_sync_query  s    	zClient.run_sync_query)FNN)NNNN)__name__
__module____qualname____doc__r   Z_connection_classr+   r.   r6   r9   r<   r>   r@   rA   rB   r   r   r   r   r      s     
+  
7r   N)rF   Zgcloud.clientr   Zgcloud.bigquery.connectionr   Zgcloud.bigquery.datasetr   Zgcloud.bigquery.jobr   r	   r   r   Zgcloud.bigquery.queryr   r   r   r   r   r   <module>   s   