B
    Ñš[¥@  ã               @   s&  d Z yddlZdZW n   dZY nX ddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ dd	l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 dZdZG dd„ deƒZdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ de!ƒZ"G dd„ de!ƒZ#dS )z4Implementation of communication for MySQL X servers.é    NTF)Úwrapsé   )ÚMySQL41AuthPlugin)ÚInterfaceErrorÚOperationalErrorÚProgrammingError)ÚSchema)ÚProtocolÚMessageReaderWriter)ÚResultÚ	RowResultÚ	DocResult)ÚSqlStatementÚAddStatementzDROP DATABASE IF EXISTS `{0}`z#CREATE DATABASE IF NOT EXISTS `{0}`c               @   s@   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zi fdd„ZdS )ÚSocketStreamc             C   s   d | _ d| _d S )NF)Ú_socketÚ_is_ssl)Úself© r   ú•C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysqlx\connection.pyÚ__init__2   s    zSocketStream.__init__c             C   s6   t |tƒrtjntj}t |tj¡| _| j |¡ d S )N)Ú
isinstanceÚtupleÚsocketÚAF_INETÚAF_UNIXÚSOCK_STREAMr   Úconnect)r   ÚparamsZs_typer   r   r   r   6   s    zSocketStream.connectc             C   s`   | j d krtdƒ‚g }x>|dkrT| j  |¡}|dkr<tdƒ‚| |¡ |t|ƒ8 }qW d |¡S )NzMySQLx Connection not availabler   ó    zUnexpected connection close)r   r   ÚrecvÚRuntimeErrorÚappendÚlenÚjoin)r   ÚcountÚbufÚdatar   r   r   Úread;   s    


zSocketStream.readc             C   s"   | j d krtdƒ‚| j  |¡ d S )NzMySQLx Connection not available)r   r   Úsendall)r   r'   r   r   r   r)   G   s    
zSocketStream.sendallc             C   s   | j s
d S | j  ¡  d | _ d S )N)r   Úclose)r   r   r   r   r*   L   s    
zSocketStream.closec          	   C   sL  t s|  ¡  tdƒ‚t tj¡}| ¡  d|krvy| |d ¡ tj|_	W n* t
tjfk
rt   |  ¡  tdƒ‚Y nX d|krÄy| |d ¡ tj|_W n* t
tjfk
rÂ   |  ¡  tdƒ‚Y nX d|k�ry| |d | dd ¡¡ W n, t
tjfk
�r   |  ¡  tdƒ‚Y nX nd|k�r4|  ¡  td	ƒ‚| | j¡| _d
| _d S )Nz'Python installation has no SSL support.zssl-cazInvalid CA certificate.zssl-crlzInvalid CRL.zssl-certzssl-keyzInvalid Client Certificate/Key.z Client Certificate not provided.T)ÚSSL_AVAILABLEr*   r!   ÚsslÚ
SSLContextÚPROTOCOL_SSLv23Úload_default_certsÚload_verify_locationsÚCERT_REQUIREDÚverify_modeÚIOErrorÚSSLErrorr   ÚVERIFY_CRL_CHECK_CHAINÚverify_flagsÚload_cert_chainÚgetÚwrap_socketr   r   )r   Zssl_optsÚcontextr   r   r   Úset_sslS   s>    


zSocketStream.set_sslN)	Ú__name__Ú
__module__Ú__qualname__r   r   r(   r)   r*   r;   r   r   r   r   r   1   s   r   c                s   t ˆ ƒ‡ fdd„ƒ}|S )Nc          	      s@   yˆ | f|ž|ŽS  t jtfk
r:   |  ¡  tdƒ‚Y nX d S )NzCannot connect to host.)r   Úerrorr!   Ú
disconnectr   )r   ÚargsÚkwargs)Úfuncr   r   Úwrapperx   s
    z(catch_network_exception.<locals>.wrapper)r   )rC   rD   r   )rC   r   Úcatch_network_exceptionw   s    rE   c               @   sÚ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Z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d„ƒZe	dd„ ƒZe	dd„ ƒZe	dd„ ƒZe	d d!„ ƒZe	d"d#„ ƒZd$d%„ Zd&d'„ Zd(d)„ Zd*S ),Ú
Connectionc             C   sH   |  d¡| _|  d¡| _|  d¡| _d | _|| _tƒ | _d | _d | _	d S )NÚuserÚpasswordÚschema)
r8   Ú_userÚ	_passwordZ_schemaÚ_active_resultÚsettingsr   ÚstreamÚreader_writerÚprotocol)r   rM   r   r   r   r   ƒ   s    zConnection.__init__c             C   s   | j d k	r| j  ¡  d | _ d S )N)rL   Ú	fetch_all)r   r   r   r   Úfetch_active_result�   s    

zConnection.fetch_active_resultc             C   s:   d| j kr"| j d | j  dd¡fS d| j kr6| j d S dS )NÚhostÚporti$�  r   )Ú	localhosti$�  )rM   r8   )r   r   r   r   Ú_connection_params’   s
    


zConnection._connection_paramsc             C   s<   | j  |  ¡ ¡ t| j ƒ| _t| jƒ| _|  ¡  |  ¡  d S )N)	rN   r   rV   r
   rO   r	   rP   Ú_handle_capabilitiesÚ_authenticate)r   r   r   r   r   ™   s
    zConnection.connectc             C   s–   | j  ¡ j}|r$|d j ¡ dksJndsJ| j dd¡rF|  ¡  tdƒ‚d S t	j
dk rv| j dd¡rr|  ¡  tdƒ‚d S | j jdd	� | j | j¡ d S )
Nr   ÚtlsFz
ssl-enablezSSL not enabled at server.)é   é   é	   z=The support for SSL is not available for this Python version.T)rY   )rP   Zget_capabilitesZcapabilitiesÚnameÚlowerrM   r8   r*   r   ÚsysÚversion_infor!   Zset_capabilitiesrN   r;   )r   r'   r   r   r   rW       s    
zConnection._handle_capabilitiesc             C   sH   t | j| jƒ}| j | ¡ ¡ | j ¡ }| j | |¡¡ | j 	¡  d S )N)
r   rJ   rK   rP   Zsend_auth_startZ	auth_nameZread_auth_continueZsend_auth_continueZbuild_authentication_responseZread_auth_ok)r   ZpluginÚ
extra_datar   r   r   rX   ²   s    
zConnection._authenticatec             G   s   | j  d||¡ d S )NÚsql)rP   Úsend_execute_statement)r   rb   rA   r   r   r   Úsend_sqlº   s    zConnection.send_sqlc             C   s*   | j  |¡ d }t|tƒr |j}t| |ƒS )N)rP   Úsend_insertr   r   Z_idsr   )r   Ú	statementÚidsr   r   r   re   ¾   s
    
zConnection.send_insertc             C   s"   | j  |¡ |jrt| ƒS t| ƒS )N)rP   Z	send_findZ
_doc_basedr   r   )r   rf   r   r   r   ÚfindÆ   s    zConnection.findc             C   s   | j  |¡ t| ƒS )N)rP   Zsend_deleter   )r   rf   r   r   r   ÚdeleteË   s    zConnection.deletec             C   s   | j  |¡ t| ƒS )N)rP   Zsend_updater   )r   rf   r   r   r   ÚupdateÐ   s    zConnection.updateTc             G   s   | j  |||¡ t| ƒS )N)rP   rc   r   )r   Ú	namespaceÚcmdZraise_on_failrA   r   r   r   Úexecute_nonqueryÕ   s    zConnection.execute_nonqueryc             G   s>   | j  d||¡ t| ƒ}| ¡  |jdkr2tdƒ‚|d d S )Nrb   r   zNo data found)rP   rc   r   rQ   r%   r   )r   rb   rA   Úresultr   r   r   Úexecute_sql_scalarÚ   s    
zConnection.execute_sql_scalarc             G   s   | j  d||¡ t| ƒS )NZxplugin)rP   rc   r   )r   rl   rA   r   r   r   Úget_row_resultã   s    zConnection.get_row_resultc             C   s   | j  |¡S )N)rP   Úread_row)r   rn   r   r   r   rq   è   s    zConnection.read_rowc             C   s   | j  |¡ d S )N)rP   Úclose_result)r   rn   r   r   r   rr   ì   s    zConnection.close_resultc             C   s   | j  |¡S )N)rP   Úget_column_metadata)r   rn   r   r   r   rs   ð   s    zConnection.get_column_metadatac             C   s   | j jd k	S )N)rN   r   )r   r   r   r   Úis_openô   s    zConnection.is_openc             C   s   |   ¡ sd S | j ¡  d S )N)rt   rN   r*   )r   r   r   r   r@   ÷   s    zConnection.disconnectc             C   sB   |   ¡ sd S | jd k	r | j ¡  | j ¡  | j ¡  | j ¡  d S )N)rt   rL   rQ   rP   Z
send_closeÚread_okrN   r*   )r   r   r   r   r*   ü   s    



zConnection.closeN)T)r<   r=   r>   r   rR   rV   r   rW   rX   rE   rd   re   rh   ri   rj   rm   ro   rp   rq   rr   rs   rt   r@   r*   r   r   r   r   rF   ‚   s*   
	rF   c                   s\   e Zd Z‡ fdd„Zdd„ Z‡ fdd„Z‡ fdd„Zd	d
„ Z‡ fdd„Z‡ fdd„Z	‡  Z
S )ÚXConnectionc                s€   t t| ƒ |¡ g | _| dg ¡| _d|krT|d rT| j | d¡| dd ¡dœ¡ d| _d| _|  	¡  | jj
dd„ dd	� d S )
NZroutersrS   rT   )rS   rT   éÿÿÿÿTc             S   s   | d S )NÚpriorityr   )Úxr   r   r   Ú<lambda>  s    z&XConnection.__init__.<locals>.<lambda>)ÚkeyÚreverse)Úsuperrv   r   Údependent_connectionsÚpopÚ_routersr"   Ú_cur_routerÚ_can_failoverÚ_ensure_prioritiesÚsort)r   rM   )Ú	__class__r   r   r     s    zXConnection.__init__c             C   s„   d}d}xN| j D ]D}| dd ¡}|d kr:|d7 }||d< n|dkrLtddƒ‚|d8 }qW d|  k rrt| j ƒk r€n n
tddƒ‚d S )	Nr   éd   rx   r   z(The priorities must be between 0 and 100i§  z\You must either assign no priority to any of the routers or give a priority for every routeri   )r€   r8   r   r#   )r   Zpriority_countrx   ÚrouterÚprir   r   r   rƒ     s    
zXConnection._ensure_prioritiesc                s¼   | j sd| _tt| ƒ ¡ S | jr*| jdkrNd| _d| _x| j D ]}d|d< q>W |  jd7  _| j | j d }| j | j d }| jdkršd| j | jd  d< | jt| j ƒd kr´d| _||fS )	NFrw   TÚ	availabler   rS   rT   r   )r€   r‚   r}   rv   rV   r�   r#   )r   r‡   rS   rT   )r…   r   r   rV   )  s     
zXConnection._connection_paramsc          
      sr   d }xB| j rFytt| ƒ ¡ S  tjk
rB } z|}W d d }~X Y qX qW t| jƒdkrdtd 	|¡ƒ‚tddƒ‚d S )Nr   zCannot connect to host: {0}z(Failed to connect to any of the routers.i¡  )
r‚   r}   rv   r   r   r?   r#   r€   r   Úformat)r   r?   Úerr)r…   r   r   r   @  s    zXConnection.connectc             C   s   | j  |¡ d S )N)r~   r"   )r   Ú
connectionr   r   r   Úbind_connectionM  s    zXConnection.bind_connectionc                s,   x| j r| j  ¡  ¡  qW tt| ƒ ¡  d S )N)r~   r   r*   r}   rv   )r   )r…   r   r   r*   P  s    zXConnection.closec                s,   x| j r| j  ¡  ¡  qW tt| ƒ ¡  d S )N)r~   r   r@   r}   rv   )r   )r…   r   r   r@   U  s    zXConnection.disconnect)r<   r=   r>   r   rƒ   rV   r   r�   r*   r@   Ú__classcell__r   r   )r…   r   rv     s   rv   c                   s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚNodeConnectionc                s   t t| ƒ |¡ d S )N)r}   r�   r   )r   rM   )r…   r   r   r   \  s    zNodeConnection.__init__c          
      sJ   yt t| ƒ ¡  W n2 tjk
rD } ztd |¡ƒ‚W d d }~X Y nX d S )NzCannot connect to host: {0})r}   r�   r   r   r?   r   rŠ   )r   r‹   )r…   r   r   r   _  s    zNodeConnection.connect)r<   r=   r>   r   r   rŽ   r   r   )r…   r   r�   [  s   r�   c               @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚBaseSessionaÜ  Base functionality for Session classes through the X Protocol.

    This class encloses the core functionality to be made available on both
    the XSession and NodeSession classes, such functionality includes:

        - Accessing available schemas.
        - Schema management operations.
        - Enabling/disabling warning generation.
        - Retrieval of connection information.

    Args:
        settings (dict): Connection data used to connect to the database.
    c             C   s
   || _ d S )N)Ú	_settings)r   rM   r   r   r   r   t  s    zBaseSession.__init__c             C   s   | j jjd k	S )N)Ú_connectionrN   r   )r   r   r   r   rt   w  s    zBaseSession.is_openc             C   s
   t | |ƒS )zöRetrieves a Schema object from the current session by it's name.

        Args:
            name (string): The name of the Schema object to be retrieved.

        Returns:
            mysqlx.Schema: The Schema object with the given name.
        )r   )r   r]   r   r   r   Ú
get_schemaz  s    	zBaseSession.get_schemac             C   s,   | j j d¡r t| | j jd ƒS tdƒ‚dS )aK  Retrieves a Schema object from the current session by the schema
        name configured in the connection settings.

        Returns:
            mysqlx.Schema: The Schema object with the given name at connect
                           time.

        Raises:
            ProgrammingError: If default schema not provided.
        rI   zDefault schema not providedN)r’   rM   r8   r   r   )r   r   r   r   Úget_default_schema…  s    zBaseSession.get_default_schemac             C   s   | j  dt |¡d¡ dS )z‹Drops the schema with the specified name.

        Args:
            name (string): The name of the Schema object to be retrieved.
        rb   TN)r’   rm   Ú_DROP_DATABASE_QUERYrŠ   )r   r]   r   r   r   Údrop_schema”  s    zBaseSession.drop_schemac             C   s    | j  dt |¡d¡ t| |ƒS )z¬Creates a schema on the database and returns the corresponding
        object.

        Args:
            name (string): A string value indicating the schema name.
        rb   T)r’   rm   Ú_CREATE_DATABASE_QUERYrŠ   r   )r   r]   r   r   r   Úcreate_schema�  s    zBaseSession.create_schemac             C   s   | j  ddd¡ dS )z4Starts a transaction context on the server.
        rb   zSTART TRANSACTIONTN)r’   rm   )r   r   r   r   Ústart_transaction¨  s    zBaseSession.start_transactionc             C   s   | j  ddd¡ dS )zXCommits all the operations executed after a call to
        startTransaction().
        rb   ZCOMMITTN)r’   rm   )r   r   r   r   Úcommit­  s    zBaseSession.commitc             C   s   | j  ddd¡ dS )zYDiscards all the operations executed after a call to
        startTransaction().
        rb   ZROLLBACKTN)r’   rm   )r   r   r   r   Úrollback³  s    zBaseSession.rollbackc             C   s   | j  ¡  d S )N)r’   r*   )r   r   r   r   r*   ¹  s    zBaseSession.closeN)r<   r=   r>   Ú__doc__r   rt   r“   r”   r–   r˜   r™   rš   r›   r*   r   r   r   r   r�   f  s   	r�   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚXSessionaW  Enables interaction with a X Protocol enabled MySQL Product.

    The functionality includes:

    - Accessing available schemas.
    - Schema management operations.
    - Enabling/disabling warning generation.
    - Retrieval of connection information.

    Args:
        settings (dict): Connection data used to connect to the database.
    c                s*   t t| ƒ |¡ t| jƒ| _| j ¡  d S )N)r}   r�   r   rv   r‘   r’   r   )r   rM   )r…   r   r   r   Ê  s    zXSession.__init__c             C   s,   |   ¡ stdƒ‚t| jƒ}| j |j¡ |S )Nz$XSession is not connected to a farm.)rt   r   ÚNodeSessionr‘   r’   r�   )r   Znsessr   r   r   Úbind_to_default_shardÏ  s
    
zXSession.bind_to_default_shard)r<   r=   r>   rœ   r   rŸ   rŽ   r   r   )r…   r   r�   ½  s   r�   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rž   at  Enables interaction with a X Protocol enabled MySQL Server.

    The functionality includes:

    - Accessing available schemas.
    - Schema management operations.
    - Enabling/disabling warning generation.
    - Retrieval of connection information.
    - Includes SQL Execution.

    Args:
        settings (dict): Connection data used to connect to the database.
    c                s*   t t| ƒ |¡ t| jƒ| _| j ¡  d S )N)r}   rž   r   r�   r‘   r’   r   )r   rM   )r…   r   r   r   æ  s    zNodeSession.__init__c             C   s   t | j|ƒS )z}Creates a :class:`mysqlx.SqlStatement` object to allow running the
        SQL statement on the target MySQL Server.
        )r   r’   )r   rb   r   r   r   rb   ë  s    zNodeSession.sql)r<   r=   r>   rœ   r   rb   rŽ   r   r   )r…   r   rž   Ø  s   rž   )$rœ   r,   r+   r_   r   Ú	functoolsr   Zauthenticationr   Úerrorsr   r   r   Zcrudr   rP   r	   r
   rn   r   r   r   rf   r   r   r•   r—   Úobjectr   rE   rF   rv   r�   r�   r�   rž   r   r   r   r   Ú<module>   s2   
F UW