o
    EDi8                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZm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mZmZ ddddddd ddddddee dee dedee dedee dedeeef dee deeee	f  deeee	f  defddZdedefddZdddddd ddddd eddee dee dededee dedeeef dee deeee	f  deeee	f  d ed!eedef defd"d#Z dS )$    N)ThreadPoolExecutor)	signature)OptionalUnionDictAny)urlparseparse_qs)Client)	dict_copy)ProgrammingError)
HttpClient)AsyncClientDefaultThreadPoolExecutorNEW_THREAD_POOL_EXECUTOR __default__F)hostusernamepasswordaccess_tokendatabase	interfaceportsecuredsnsettingsgeneric_argsr   r   r   r   r   r   r   r   r   r   r   returnc                 K   s   |rFt |}|p
|j}|p|j}| p|j} |p|j}|jr/|r#|dkr/|jdd dd }|p3|j}t|j	 D ]
\}}|d ||< q;t
| dkpZ|dkpZ| oZt
|dv }| s_d	} |sg|rednd
}|pmt||}|rz|sv|dkrztd|du rd|v r|d}|du rd|v r|d}|r|du rd}d|v rd|vr|d|d< |	pi }	|d
r|
rttj}|
	 D ])\}}||v r|||< q|dkrd|vr||d< q|dr|dd }||	|< qt|| |||||fd|	i|S td| )a  
    The preferred method to get a ClickHouse Connect Client instance

    :param host: The hostname or IP address of the ClickHouse server. If not set, localhost will be used.
    :param username: The ClickHouse username. If not set, the default ClickHouse user will be used.
      Should not be set if `access_token` is used.
    :param password: The password for username.
      Should not be set if `access_token` is used.
    :param access_token: JWT access token (ClickHouse Cloud feature).
      Should not be set if `username`/`password` are used.
    :param database:  The default database for the connection. If not set, ClickHouse Connect will use the
     default database for username.
    :param interface: Must be http or https.  Defaults to http, or to https if port is set to 8443 or 443
    :param port: The ClickHouse HTTP or HTTPS port. If not set will default to 8123, or to 8443 if secure=True
      or interface=https.
    :param secure: Use https/TLS. This overrides inferred values from the interface or port arguments.
    :param dsn: A string in standard DSN (Data Source Name) format. Other connection values (such as host or user)
      will be extracted from this string if not set otherwise.
    :param settings: ClickHouse server settings to be used with the session/every request
    :param generic_args: Used internally to parse DBAPI connection strings into keyword arguments and ClickHouse settings.
      It is not recommended to use this parameter externally.

    :param kwargs -- Recognized keyword arguments (used by the HTTP client), see below

    :param compress: Enable compression for ClickHouse HTTP inserts and query results.  True will select the preferred
      compression method (lz4).  A str of 'lz4', 'zstd', 'brotli', or 'gzip' can be used to use a specific compression type
    :param query_limit: Default LIMIT on returned rows.  0 means no limit
    :param connect_timeout:  Timeout in seconds for the http connection
    :param send_receive_timeout: Read timeout in seconds for http connection
    :param client_name: client_name prepended to the HTTP User Agent header. Set this to track client queries
      in the ClickHouse system.query_log.
    :param send_progress: Deprecated, has no effect.  Previous functionality is now automatically determined
    :param verify: Verify the server certificate in secure/https mode
    :param ca_cert: If verify is True, the file path to Certificate Authority root to validate ClickHouse server
     certificate, in .pem format.  Ignored if verify is False.  This is not necessary if the ClickHouse server
     certificate is trusted by the operating system.  To trust the maintained list of "global" public root
     certificates maintained by the Python 'certifi' package, set ca_cert to 'certifi'
    :param client_cert: File path to a TLS Client certificate in .pem format.  This file should contain any
      applicable intermediate certificates
    :param client_cert_key: File path to the private key for the Client Certificate.  Required if the private key
      is not included the Client Certificate key file
    :param session_id ClickHouse session id.  If not specified and the common setting 'autogenerate_session_id'
      is True, the client will generate a UUID1 session id
    :param pool_mgr Optional urllib3 PoolManager for this client.  Useful for creating separate connection
      pools for multiple client endpoints for applications with many clients
    :param http_proxy  http proxy address.  Equivalent to setting the HTTP_PROXY environment variable
    :param https_proxy https proxy address.  Equivalent to setting the HTTPS_PROXY environment variable
    :param server_host_name  This is the server host name that will be checked against a TLS certificate for
      validity.  This option can be used if using an ssh_tunnel or other indirect means to an ClickHouse server
      where the `host` argument refers to the tunnel or proxy and not the actual ClickHouse server
    :param utc_tz_aware When True, ClickHouse Connect will return timezone-aware UTC datetimes instead of the
      legacy naive UTC datetimes.
    :param autogenerate_session_id  If set, this will override the 'autogenerate_session_id' common setting.
    :param form_encode_query_params  If True, query parameters will be sent as form-encoded data in the request body
      instead of as URL parameters. This is useful for queries with large parameter sets that might exceed URL length
      limits. Only available for query operations (not inserts). Default: False
    :return: ClickHouse Connect Client instance
    r      N/r   truehttps)4438443	localhosthttpr   z2Cannot use both access_token and username/passworduser	user_namedefaultcompressioncompressch_   r   zUnrecognized client type )r   r   r   hostnamer   pathsplitr	   queryitemsstrlowerdefault_portr   pop
startswithr   r   
parameters)r   r   r   r   r   r   r   r   r   r   r   kwargsparsedkvuse_tlsclient_paramsnamevalue rA   [/var/www/Datamplify/venv/lib/python3.10/site-packages/clickhouse_connect/driver/__init__.pycreate_client   s^   G




*






rC   c                 C   s   |  dr|r	dS dS td)Nr&   i   i  z!Unrecognized ClickHouse interface)r7   
ValueError)r   r   rA   rA   rB   r5      s   
r5   )r   r   r   r   r   r   r   r   r   r   executor_threadsexecutorrE   rF   c                    sJ    	
fdd}t  }|d|I dH }t||
|dS )a`  
    The preferred method to get an async ClickHouse Connect Client instance.
    For sync version, see create_client.

    Unlike sync version, the 'autogenerate_session_id' setting by default is False.

    :param host: The hostname or IP address of the ClickHouse server. If not set, localhost will be used.
    :param username: The ClickHouse username. If not set, the default ClickHouse user will be used.
    :param password: The password for username.
    :param database:  The default database for the connection. If not set, ClickHouse Connect will use the
     default database for username.
    :param interface: Must be http or https.  Defaults to http, or to https if port is set to 8443 or 443
    :param port: The ClickHouse HTTP or HTTPS port. If not set will default to 8123, or to 8443 if secure=True
      or interface=https.
    :param secure: Use https/TLS. This overrides inferred values from the interface or port arguments.
    :param dsn: A string in standard DSN (Data Source Name) format. Other connection values (such as host or user)
      will be extracted from this string if not set otherwise.
    :param settings: ClickHouse server settings to be used with the session/every request
    :param generic_args: Used internally to parse DBAPI connection strings into keyword arguments and ClickHouse settings.
      It is not recommended to use this parameter externally
    :param executor_threads: 'max_worker' threads used by the client ThreadPoolExecutor.  If not set, the default
      of 4 + detected CPU cores will be used
    :param executor: Optional `ThreadPoolExecutor` to use for async operations.  If not set, a new `ThreadPoolExecutor`
      will be created with the number of threads specified by `executor_threads`.  If set to `None` it will use the
      default executor of the event loop.
    :param kwargs -- Recognized keyword arguments (used by the HTTP client), see below

    :param compress: Enable compression for ClickHouse HTTP inserts and query results.  True will select the preferred
      compression method (lz4).  A str of 'lz4', 'zstd', 'brotli', or 'gzip' can be used to use a specific compression type
    :param query_limit: Default LIMIT on returned rows.  0 means no limit
    :param connect_timeout:  Timeout in seconds for the http connection
    :param send_receive_timeout: Read timeout in seconds for http connection
    :param client_name: client_name prepended to the HTTP User Agent header. Set this to track client queries
      in the ClickHouse system.query_log.
    :param send_progress: Deprecated, has no effect.  Previous functionality is now automatically determined
    :param verify: Verify the server certificate in secure/https mode
    :param ca_cert: If verify is True, the file path to Certificate Authority root to validate ClickHouse server
     certificate, in .pem format.  Ignored if verify is False.  This is not necessary if the ClickHouse server
     certificate is trusted by the operating system.  To trust the maintained list of "global" public root
     certificates maintained by the Python 'certifi' package, set ca_cert to 'certifi'
    :param client_cert: File path to a TLS Client certificate in .pem format.  This file should contain any
      applicable intermediate certificates
    :param client_cert_key: File path to the private key for the Client Certificate.  Required if the private key
      is not included the Client Certificate key file
    :param session_id ClickHouse session id.  If not specified and the common setting 'autogenerate_session_id'
      is True, the client will generate a UUID1 session id
    :param pool_mgr Optional urllib3 PoolManager for this client.  Useful for creating separate connection
      pools for multiple client endpoints for applications with many clients
    :param http_proxy  http proxy address.  Equivalent to setting the HTTP_PROXY environment variable
    :param https_proxy https proxy address.  Equivalent to setting the HTTPS_PROXY environment variable
    :param server_host_name  This is the server host name that will be checked against a TLS certificate for
      validity.  This option can be used if using an ssh_tunnel or other indirect means to an ClickHouse server
      where the `host` argument refers to the tunnel or proxy and not the actual ClickHouse server
    :param utc_tz_aware When True, ClickHouse Connect will return timezone-aware UTC datetimes instead of the
      legacy naive UTC datetimes.
    :param autogenerate_session_id  If set, this will override the 'autogenerate_session_id' common setting.
    :param form_encode_query_params  If True, query parameters will be sent as form-encoded data in the request body
      instead of as URL parameters. This is useful for queries with large parameter sets that might exceed URL length
      limits. Only available for query operations (not inserts). Default: False
    :return: ClickHouse Connect Client instance
    c                      s4   dvrdd< t d
 	d
S )Nautogenerate_session_idF)
r   r   r   r   r   r   r   r   r   r   rA   )rC   rA   r   r   r   r   r   r9   r   r   r   r   r   rA   rB   _create_client   s   
z+create_async_client.<locals>._create_clientN)clientrE   rF   )asyncioget_running_looprun_in_executorr   )r   r   r   r   r   r   r   r   r   r   rE   rF   r9   rI   loop_clientrA   rH   rB   create_async_client   s
    LrP   )!rK   concurrent.futuresr   inspectr   typingr   r   r   r   urllib.parser   r	    clickhouse_connect.driver.ctypesclickhouse_connect clickhouse_connect.driver.clientr
    clickhouse_connect.driver.commonr   $clickhouse_connect.driver.exceptionsr   $clickhouse_connect.driver.httpclientr   %clickhouse_connect.driver.asyncclientr   r   r   r3   intboolrC   r5   rP   rA   rA   rA   rB   <module>   s    
	

u
	
