3
S^Q              -   @   s6  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZmZ ddlmZmZmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> ddl?m@Z? ejAdZBejAdejCZDddddgZEeEddddddddddddg ZFejGeHjIejJ  d d! ZKd"d# ZLd$d% ZMd&d' ZNd(d) ZOd*d+ ZPd,d- ZQd.d/ ZRd0d1d/d-d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZg-ZSdS )[z$MySQL X DevAPI Python implementation    N   )	constants)	INT_TYPESSTRING_TYPESJSONDecodeErrorurlparseunquote	parse_qsl)ClientSession)AuthLockContentionSSLMode)Schema
CollectionTableView)DbDoc)ErrorInterfaceErrorDatabaseErrorNotSupportedError	DataErrorIntegrityErrorProgrammingErrorOperationalErrorInternalError	PoolErrorTimeoutError)ColumnRowResultBufferingResult	RowResult	SqlResult	DocResult
ColumnType)	StatementFilterableStatementSqlStatementFindStatementAddStatementRemoveStatementModifyStatementSelectStatementInsertStatementDeleteStatementUpdateStatementCreateCollectionIndexStatementExprReadStatementWriteStatement)
ExprParserz,(?![^\(\)]*\))z!^\(address=(.+),priority=(\d+)\)$zssl-certzssl-cazssl-keyzssl-crluserpasswordschemahostportrouterssocketzssl-modeauthzuse-purezconnect-timeoutzconnection-attributesc             C   s   | j dd} d| ko.| jddko.| jddk oD| jdoD| jd}g }tj|r^| dd n| }xx|D ]p}i }tj|}|r|jd}t	|jd|d	< t
d
j|}|jstdj||j|j|jd |j| qjW |rd|iS |d S )zParses a list of host, port pairs

    Args:
        path: String containing a list of routers or just router

    Returns:
        Returns a dict with parsed values of host, port and priority if
        specified.
      ,:r   []   priorityz//{0}zInvalid address: {0})r:   r;   r<   r   )replacecount
startswithendswith_SPLITsplit	_PRIORITYmatchgroupintr   formathostnamer   updater;   append)patharrayr<   Zaddress_listaddressrouterrO    rZ   A/tmp/pip-build-8app2_gc/mysql-connector-python/mysqlx/__init__.py_parse_address_listC   s$    




r\   c             C   s  ddi}dj | jdrdnd| } | jdd\}}|jdddd	 \}}|jd
ddd	 \}}|jd}||d jdd!kr|dkr|jdd\}|d< |jd}| s| sd|krtdj | |jdd\}}	t	|t	|	 |d< |d< |jd"rt	||d< n$|jdr$tdn|j
t| xt|dD ]\}
}|
jddj }||krntdj |
|tkrt	|jd||< n8|j }|d#krd||< n|d$krd ||< n|||< q>W |S )%a  Parses the connection string and returns a dictionary with the
    connection settings.

    Args:
        uri: mysqlx URI scheme to connect to a MySQL server/farm.

    Returns:
        Returns a dict with parsed values of credentials and address of the
        MySQL server/farm.
    r9   r@   z{0}{1}z	mysqlx://z://r   @NrE   ?/)r   z()rB   zMalformed URI '{0}'r7   r8   ...r=   z\.zWindows Pipe is not supported.T_-zDuplicate option '{0}'.1true0falseFrG   )r_   ra   rb   )re   rf   )rg   rh   )rR   rJ   rM   	partitionrfindfindrsplitstripr   r   rT   r\   r	   rH   lower	_SSL_OPTS)urisettingsrc   tempuserinfor:   Z	query_strposr7   r8   keyvaloptZval_strrZ   rZ   r[   _parse_connection_urif   s@    








rx   c                s  t  j jt}|r*tdjdj|d krNx* d D ]}t| q<W nd kr^t  d kry" d j  d< t	j
 d  W n* ttfk
r   tdj d Y nX  d t	jkrt fddtD rtd	d
 krd krtdd krd krtdd kr> jdt	jt	jgkr>tdd krh jdt	jt	jgkrhtdd kry" d j  d< tj
 d  W n, ttfk
r   tdj d Y nX d krt  dS )a  Validates the settings to be passed to a Session object
    the port values are converted to int if specified or set to 33060
    otherwise. The priority values for each router is converted to int
    if specified.

    Args:
        settings: dict containing connection settings.
    zInvalid options: {0}.z, r<   r:   zssl-modezInvalid SSL Mode '{0}'.c             3   s   | ]}| kV  qd S )NrZ   ).0ru   )rq   rZ   r[   	<genexpr>   s    z%_validate_settings.<locals>.<genexpr>z*SSL options used with ssl-mode 'disabled'.zssl-crlzssl-cazCA Certificate not provided.zssl-keyzssl-certz Client Certificate not provided.z Cannot verify Server without CA.z%Must verify Server if CA is provided.r>   zInvalid Auth '{0}'zconnection-attributesN)setkeys
difference
_SESS_OPTSr   rR   join_validate_hostsrn   r   indexAttributeError
ValueErrorr   ZDISABLEDanyro   getZVERIFY_IDENTITYZ	VERIFY_CAr   validate_connection_attributes)rq   Zinvalid_optsrY   rZ   )rq   r[   _validate_settings   sJ    	

r   c             C   s   d| krB| d rByt | d | d< W n tk
r@   tdY nX d| kr| d ryt | d | d< W q tk
r   tdY qX nd| krd| d< dS )zValidate hosts.

    Args:
        settings (dict): Settings dictionary.

    Raises:
        :class:`mysqlx.InterfaceError`: If priority or port are invalid.
    rF   zInvalid priorityr;   zInvalid portr:   i$  N)rQ   	NameErrorr   )rq   rZ   rZ   r[   r      s    	r   c       	      C   s  i }d| krdS | d }t |tr|dkr8i | d< dS |jdoJ|jd rf|dkrftd
j|n|dkr|dkrd| d< ni | d< dS |dd jd}xb|D ]Z}|dkrq|jd}|d }t|dkr|d nd}||k rtdj|q|||< qW n.t |trHx2|D ]*}|| }t |ts8t	|}|||< qW nt |t
s^|dkrz|rni | d< nd| d< dS t |trx|D ]}d||< qW nt |trx|D ]b}|dkrĐq|jd}|d }t|dkr|d nd}||kr
tdj|n|||< qW nt |t
s4td
j||rx|D ]}|| }t |tsftdj|t|dkrtdj||jdrtdj|t |tstdj||t|dkr@tdj||q@W || d< dS )zValidate connection-attributes.

    Args:
        settings (dict): Settings dictionary.

    Raises:
        :class:`mysqlx.InterfaceError`: If attribute name or value exceeds size.
    zconnection-attributesNr@   rC   rD   Falserh   Truerf   zOconnection-attributes must be Boolean or a list of key-value pairs, found: '{}'Fr   rA   =r   z0Duplicate key '{}' used in connection-attributesz(Attribute name '{}' must be a stringtype    z5Attribute name '{}' exceeds 32 characters limit size.rc   zEKey names in connection-attributes cannot start with '_', found: '{}'z1Attribute '{}' value: '{}' must be a string type.i   z=Attribute '{}' value: '{}' exceeds 1024 characters limit size)r   rh   r   rf   )r   rh   r   rf   )r   rh   rG   )r   r   )
isinstancer   rJ   rK   r   rR   rM   lendictreprboolr{   list)	rq   
attributesZ
conn_attrsZconn_attributesattrZattr_name_val	attr_nameattr_val
attr_valuerZ   rZ   r[   r      s    	















r   c              O   s  i }| r^t | d tr$t| d }qt | d trxT| d j D ]\}}|||jdd< q@W n*|rx$|j D ]\}}|||jdd< qlW |stdd|kryBt |d trt|d |d< t |d t s|d dk rt	W n t	k
r   t
dY nX t| |S )a  Parses the connection string and returns a dictionary with the
    connection settings.

    Args:
        *args: Variable length argument list with the connection data used
               to connect to the database. It can be a dictionary or a
               connection string.
        **kwargs: Arbitrary keyword arguments with connection data used to
                  connect to the database.

    Returns:
        mysqlx.Session: Session object.

    Raises:
        TypeError: If connection timeout is not a positive integer.
    r   rc   rd   zSettings not providedzconnect-timeoutzEThe connection timeout value must be a positive integer (including 0))r   r   rx   r   itemsrH   r   rQ   r   r   	TypeErrorr   )argskwargsrq   ru   rv   rZ   rZ   r[   _get_connection_settingsU  s.    r   c              O   s   t | |}t|S )a  Creates a Session instance using the provided connection data.

    Args:
        *args: Variable length argument list with the connection data used
               to connect to a MySQL server. It can be a dictionary or a
               connection string.
        **kwargs: Arbitrary keyword arguments with connection data used to
                  connect to the database.

    Returns:
        mysqlx.Session: Session object.
    )r   r   )r   r   rq   rZ   rZ   r[   get_session  s    
r   c             C   sX  t | ttfstdt| }t |ttfs4tdt |trlytj|}W q tk
rh   tdY qX n*i }x$|j D ]\}}|||j	dd< qzW t |tstdi }d|krN|j
d}t |tstd|j
dd	|d< |j
d
d|d
< |j
dd|d< |j
dd|d< t|dkr.tdj|t|dkrNtdj|j t||S )a8  Creates a Client instance with the provided connection data and settings.

    Args:
        connection_string: A string or a dict type object to indicate the             connection data used to connect to a MySQL server.

            The string must have the following uri format::

                cnx_str = 'mysqlx://{user}:{pwd}@{host}:{port}'
                cnx_str = ('mysqlx://{user}:{pwd}@['
                           '    (address={host}:{port}, priority=n),'
                           '    (address={host}:{port}, priority=n), ...]'
                           '       ?[option=value]')

            And the dictionary::

                cnx_dict = {
                    'host': 'The host where the MySQL product is running',
                    'port': '(int) the port number configured for X protocol',
                    'user': 'The user name account',
                    'password': 'The password for the given user account',
                    'ssl-mode': 'The flags for ssl mode in mysqlx.SSLMode.FLAG',
                    'ssl-ca': 'The path to the ca.cert'
                    "connect-timeout": '(int) milliseconds to wait on timeout'
                }

        options_string: A string in the form of a document or a dictionary             type with configuration for the client.

            Current options include::

                options = {
                    'pooling': {
                        'enabled': (bool), # [True | False], True by default
                        'max_size': (int), # Maximum connections per pool
                        "max_idle_time": (int), # milliseconds that a
                            # connection will remain active while not in use.
                            # By default 0, means infinite.
                        "queue_timeout": (int), # milliseconds a request will
                            # wait for a connection to become available.
                            # By default 0, means infinite.
                    }
                }

    Returns:
        mysqlx.Client: Client object.

    .. versionadded:: 8.0.13
    z(connection_data must be a string or dictz+connection_options must be a string or dictzA'pooling' options must be given in the form of a document or dictrd   rc   Zpoolingz<'pooling' options must be given in the form document or dictenabledTmax_size   Zmax_idle_timer   Zqueue_timeoutz Unrecognized pooling options: {}z#Unrecognized connection options: {})r   r   r   r   r   jsonloadsr   r   rH   popr   rR   r|   r
   )Zconnection_stringZoptions_stringZsettings_dictZoptions_dictru   valueZpooling_options_dictZpooling_optionsrZ   rZ   r[   
get_client  s>    2




r   r
   r   exprr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   )T__doc__rer   loggingr@   r   compatr   r   r   r   r   r	   
connectionr
   r   r   r   r   Zcrudr   r   r   r   Zdbdocr   errorsr   r   r   r   r   r   r   r   r   r   r   resultr   r    r!   r"   r#   r$   r%   r&   Z	statementr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r   r6   compilerL   VERBOSErN   ro   r~   	getLogger__name__
addHandlerNullHandlerr\   rx   r   r   r   r   r   r   __all__rZ   rZ   rZ   r[   <module>   sP    4(D


#46m.d

