o
    –¨Êh£>  ã                   @   s¤  d 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.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z> e @d¡ZAe @dejB¡ZCg d¢ZDeDg d¢ ZEdd„ ZFdd„ ZGdd„ ZHdd„ ZIdd„ ZJdd„ ZKdd„ ZLg d ¢ZMd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+)\)$)ússl-certússl-caússl-keyússl-crl)ÚuserÚpasswordÚschemaÚhostÚportÚroutersÚsocketússl-modeÚauthzuse-pureúconnect-timeoutc                 C   sî   |   dd¡} d| vo|  d¡dko|  d¡dk o"|  d¡o"|  d¡}g }t |r/| dd… n| ¡}|D ]8}i }t |¡}|rM| d¡}t	| d	¡ƒ|d
< t
d |¡ƒ}|js^td |¡ƒ‚|j|j|jd� | |¡ q4|rsd|iS |d S )zÛParses 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@   Úaddress_listÚaddressÚrouterrU   © ra   úA/var/www/html/env/lib/python3.10/site-packages/mysqlx/__init__.pyÚ_parse_address_list?   s,   
ÿÿþ

rc   c                 C   s¶  ddi}d  |  d¡rdnd| ¡} |  dd¡\}}| d¡ddd	… \}}| d
¡ddd	… \}}| d¡}||d…  d¡dkrO|dkrO| dd¡\}|d< | d¡}|r\|r\d|vrctd  | ¡ƒ‚| dd¡\}}	t	|ƒt	|	ƒ|d< |d< | d¡r„t	|ƒ|d< n| d¡r�tdƒ‚| 
t|ƒ¡ t|dƒD ]?\}
}|
 dd¡ ¡ }||v r°td  |
¡ƒ‚|tv r¾t	| d¡ƒ||< q™| ¡ }|dv rËd||< q™|dv rÔd||< q™|||< q™|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.
    r=   rF   z{0}{1}z	mysqlx://z://r   ú@NrL   ú?ú/ú)rK   r   z()rH   zMalformed URI '{0}'r;   r<   )rf   z..Ú.rA   z\.zWindows Pipe is not supported.TÚ_ú-zDuplicate option '{0}'.)Ú1Útrue)Ú0ÚfalseF)rX   rP   rS   Ú	partitionÚrfindÚfindÚrsplitÚstripr   r   rZ   rc   r	   rN   ÚlowerÚ	_SSL_OPTS)ÚuriÚsettingsri   ÚtempÚuserinfor>   Ú	query_strÚposr;   r<   ÚkeyÚvalÚoptÚval_strra   ra   rb   Ú_parse_connection_urib   sB   ÿ






r€   c              	      s¨  t ˆ  ¡ ƒ t¡}|rtd d |¡¡ƒ‚dˆ v r%ˆ d D ]}t|ƒ qndˆ v r-tˆ ƒ dˆ v rkzˆ d  ¡ ˆ d< t	 
ˆ d ¡ W n ttfyT   td ˆ d ¡ƒ‚w ˆ d t	jkrkt‡ fdd„tD ƒƒrktd	ƒ‚d
ˆ v rwdˆ vrwtdƒ‚dˆ v rƒdˆ vrƒtdƒ‚dˆ vr–ˆ  d¡t	jt	jfv r–tdƒ‚dˆ v r©ˆ  d¡t	jt	jfvr©tdƒ‚dˆ v rÒzˆ d  ¡ ˆ d< t 
ˆ d ¡ W dS  ttfyÑ   td ˆ d ¡ƒ‚w 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>   rB   zInvalid SSL Mode '{0}'.c                 3   s   � | ]}|ˆ v V  qd S )Nra   )Ú.0r|   ©rw   ra   rb   Ú	<genexpr>²   s   € z%_validate_settings.<locals>.<genexpr>z*SSL options used with ssl-mode 'disabled'.r:   r8   zCA Certificate not provided.r9   r7   z Client Certificate not provided.z Cannot verify Server without CA.z%Must verify Server if CA is provided.rC   zInvalid Auth '{0}'N)ÚsetÚkeysÚ
differenceÚ
_SESS_OPTSr   rX   ÚjoinÚ_validate_hostsrt   r   ÚindexÚAttributeErrorÚ
ValueErrorr   ÚDISABLEDÚanyru   ÚgetÚVERIFY_IDENTITYÚ	VERIFY_CAr   )rw   Úinvalid_optsr`   ra   r‚   rb   Ú_validate_settings–   sX   	ÿ
ÿ
ÿÿÿ
ÿ
ÿÿür“   c                 C   s’   d| v r| d rz
t | d ƒ| d< W n ty   tdƒ‚w d| v r=| d r=zt | d ƒ| d< W dS  ty<   tdƒ‚w d| v rGd| d< dS dS )z£Validate hosts.

    Args:
        settings (dict): Settings dictionary.

    Raises:
        :class:`mysqlx.InterfaceError`: If priority or port are invalid.
    rM   zInvalid priorityr?   zInvalid portr>   i$�  N)rW   Ú	NameErrorr   r‚   ra   ra   rb   r‰   É   s   	ÿÿÿr‰   c                  O   s  i }| r-t | d tƒrt| d ƒ}n6t | d tƒr,| d  ¡ D ]\}}||| dd¡< qn|rH| |¡ |D ]}d|v rG| |¡|| dd¡< q6|sNtdƒ‚d|v r~z t |d tƒrbt	|d ƒ|d< t |d t
ƒro|d dk rqt‚W n ty}   tdƒ‚w 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   ri   rj   zSettings not providedrD   zEThe connection timeout value must be a positive integer (including 0))Ú
isinstancer   r€   ÚdictÚitemsrN   rZ   Úpopr   rW   r   rŒ   Ú	TypeErrorr“   )ÚargsÚkwargsrw   r|   r}   ra   ra   rb   Ú_get_connection_settingsá   s:   €
€ÿÿrœ   c                  O   s   t | i |¤Ž}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›   rw   ra   ra   rb   Úget_session  s   r�   c                 C   sF  t | ttfƒstdƒ‚t| ƒ}t |ttfƒstdƒ‚t |tƒr2zt |¡}W n ty1   tdƒ‚w i }| ¡ D ]\}}||| 	dd¡< q8t |tƒsNtdƒ‚i }d|v rž| 
d¡}t |tƒsbtdƒ‚| 
dd	¡|d< | 
d
d¡|d
< | 
dd¡|d< | 
dd¡|d< t|ƒdkr�td |¡ƒ‚t|ƒdkržtd | ¡ ¡ƒ‚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 dictrj   ri   Úpoolingz<'pooling' options must be given in the form document or dictÚenabledTÚmax_sizeé   Úmax_idle_timer   Úqueue_timeoutz Unrecognized pooling options: {}z#Unrecognized connection options: {})r•   r   r–   r   rœ   ÚjsonÚloadsr   r—   rN   r˜   ÚlenrX   r…   r
   )Úconnection_stringÚoptions_stringÚsettings_dictÚoptions_dictr|   ÚvalueÚpooling_options_dictÚpooling_optionsra   ra   rb   Ú
get_client"  sH   2
ÿ



ÿ
ÿÿ
ÿ
r®   )-r
   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   )NÚ__doc__Úrer¤   rF   r   Úcompatr   r   r   r   r   r	   Ú
connectionr
   r   r   r   r   Úcrudr   r   r   r   Údbdocr   Úerrorsr   r   r   r   r   r   r   r   r   r   r   Úresultr   r    r!   r"   r#   r$   r%   r&   Ú	statementr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r¯   r6   ÚcompilerR   ÚVERBOSErT   ru   r‡   rc   r€   r“   r‰   rœ   r�   r®   Ú__all__ra   ra   ra   rb   Ú<module>   s2    4(D
#430b