3
(hp                 @   s  d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZmZmZ ddlmZmZ ddlmZ d	ZyDddlZdd
lmZmZmZmZmZmZmZmZmZ ddlm Z  W n4 e!k
r Z" ze!dj#e$e"W Y ddZ"["X nX dZG dd deZ%dS )z'Connection class using the C Extension
    N   )errorsversion)	INT_TYPES)CharacterSet	FieldFlag
ServerFlagShutdownType
ClientFlag)MySQLConnectionAbstractMySQLCursorAbstract)MySQLProtocolF)	CMySQLCursorCMySQLCursorRawCMySQLCursorBufferedCMySQLCursorBufferedRawCMySQLCursorPreparedCMySQLCursorDictCMySQLCursorBufferedDictCMySQLCursorNamedTupleCMySQLCursorBufferedNamedTuple)MySQLInterfaceErrorz6MySQL Connector/Python C Extension not available ({0})Tc                   s  e Zd ZdZ fddZdd Zdd Zedd	 Zd
d Z	diddZ
edd Zejdd Zedd Zejdd Zedd Zdd Zdd ZeZdd Zdjd d!Zd"d# Zd$d% Zed&d' Zdkd)d*Zdld+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zdmd7d8Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dndCdDZ$e$Z%dodEdFZ&edGdH Z'edIdJ Z(edKdL Z)edMdN Z*edOdP Z+dQdR Z,dSdT Z-dpdWdXZ.dYdZ Z/d[d\ Z0d]d^ Z1dqd_d`Z2dadb Z3dcdd Z4drdedfZ5dsdgdhZ6  Z7S )tCMySQLConnectionz5Class initiating a MySQL Connection using Connector/Cc                sh   t stdd| _g | _tjjtjjtjjt	j
dd| _d| _tt| jf | |rd| jf | dS )ZInitializationz0MySQL Connector/Python C Extension not availableNZmysqlvendor)HAVE_CMYSQLRuntimeError_cmysql_columnsospathjoindirnameabspath_mysql_connector__file___plugin_dir	convertersuperr   __init__connect)selfkwargs)	__class__ S/tmp/pip-install-q3hcpn_q/mysql-connector-python/mysql/connector/connection_cext.pyr(   G   s    
zCMySQLConnection.__init__c             C   sZ   t jjd}|d dkrd}nd}| jjd|djdd	 t jdd
 D tj d dS )z!Add default connection attributes r   ZGPLv2zGPL-2.0Z
Commercialzmysql-connector-python.c             S   s   g | ]}t |qS r-   )str).0xr-   r-   r.   
<listcomp>d   s    z<CMySQLConnection._add_default_conn_attrs.<locals>.<listcomp>   )Z_connector_nameZ_connector_licenseZ_connector_versionZ_source_hostN)	r   LICENSEsplit_conn_attrsupdater    VERSIONsocketgethostname)r*   Zlicense_chunksZclient_licenser-   r-   r.   _add_default_conn_attrsX   s    z(CMySQLConnection._add_default_conn_attrsc          	   C   sF   | j j | j j | j j dddd| j j d| _| j| jd | _dS )z<Gather information of the MySQL server before authenticationN)protocolserver_version_originalZserver_threadidcharsetserver_statusauth_pluginZ	auth_dataZcapabilitiesr?   )r   Zget_proto_infoZget_server_info	thread_idZst_server_capabilitiesZ
_handshakeZ_check_server_version_server_version)r*   r-   r-   r.   _do_handshakeh   s    zCMySQLConnection._do_handshakec             C   s
   | j j S )z6Returns the server status attribute of MYSQL structure)r   Zst_server_status)r*   r-   r-   r.   _server_statusy   s    zCMySQLConnection._server_statusc             C   s   | j r| j j| dS )zJset local_infile_in_path

        Set allow_local_infile_in_path.
        N)r   Z!set_load_data_local_infile_option)r*   r   r-   r-   r.   set_allow_local_infile_in_path~   s    z/CMySQLConnection.set_allow_local_infile_in_pathTc             C   s.   || _ | jr| jj| | jr*| jj| dS )zuToggle unicode mode

        Set whether we return string fields as unicode or not.
        Default is True.
        N)_use_unicoder   use_unicoder&   set_unicode)r*   valuer-   r-   r.   rJ      s
    zCMySQLConnection.set_unicodec             C   s   | j dd }|dkrdS dS )z#Get whether autocommit is on or offzSELECT @@session.autocommitr   r   TF)
info_query)r*   rK   r-   r-   r.   
autocommit   s    zCMySQLConnection.autocommitc             C   sT   y| j j| || _W n8 tk
rN } ztj|j|j|jdW Y dd}~X nX dS )zToggle autocommit)msgerrnosqlstateN)	r   rM   Z_autocommitr   r   get_mysql_exceptionrN   rO   rP   )r*   rK   excr-   r-   r.   rM      s    
c             C   s   | j dd S )zGet the current databasezSELECT DATABASE()r   )rL   )r*   r-   r-   r.   database   s    zCMySQLConnection.databasec             C   s   | j j| dS )zSet the current databaseN)r   	select_db)r*   rK   r-   r-   r.   rS      s    c             C   s   | j tj@ S )z'MySQL session has started a transaction)rF   r   ZSTATUS_IN_TRANS)r*   r-   r-   r.   in_transaction   s    zCMySQLConnection.in_transactionc             C   s  t j| jd }tj| j| j|| jp&d| j| j	| j
d| _| jtjsLi | _| j| j| j| j| j| j| j| jtjd| j| j| jd}| jjd}|d k	r|jdd dj|}| jjdd k	r| jjdd }| jjdd	 }nd }d }|d k	r
d
|kr
| r
d}| jsf|j| jjd| jjd| jjd|||| jjdpJd| jjdpZd| jd	 y| jj f | W n: t!k
r } zt"j#|j$|j%|j&dW Y d d }~X nX | j'  d S )Nr   )bufferedrawcharset_nameZconnection_timeoutrI   rB   Z
plugin_dirT)hostuserpasswordrS   portZclient_flagsZunix_socketcompressssl_disabledZ
conn_attrsZlocal_infileZload_data_local_dirtls_versions)reverse,tls_ciphersuitesr   zTLSv1.3ZTLS_AES_256_GCM_SHA384cacertkeyZverify_certFZverify_identity)	Zssl_caZssl_certZssl_keyZssl_cipher_suitesr_   Ztls_cipher_suitesZssl_verify_certZssl_verify_identityr^   )rN   rO   rP   )(r   get_info_charset_idr#   ZMySQL	_buffered_rawZ_connection_timeoutrH   Z_auth_pluginr%   r   Zisset_client_flagr
   ZCONNECT_ARGSr8   _host_user	_password	_database_portZ_client_flags_unix_socketZCOMPRESSZ_allow_local_infileZ_allow_local_infile_in_path_sslgetsortr    Z_ssl_disabledr9   r)   r   r   rQ   rN   rO   rP   rE   )r*   rX   Z
cnx_kwargsr_   Zssl_ciphersuitesrb   rR   r-   r-   r.   _open_connection   sh    





z!CMySQLConnection._open_connectionc             C   s`   | j r\y| j  | j j  W n8 tk
rT } ztj|j|j|jdW Y dd}~X nX d| _ dS )z Disconnect from the MySQL server)rN   rO   rP   N)	r   free_resultcloser   r   rQ   rN   rO   rP   )r*   rR   r-   r-   r.   ru      s    zCMySQLConnection.closec             C   s   | j r| j j S dS )z;Reports whether the connection to MySQL Server is availableF)r   ping)r*   r-   r-   r.   is_connected   s    
zCMySQLConnection.is_connectedFr   r   c             C   sR   d}y| j j }W n tk
r&   Y n
X |r0dS |rD| j||d n
tj|dS )a  Check availability of the MySQL server

        When reconnect is set to True, one or more attempts are made to try
        to reconnect to the MySQL server using the reconnect()-method.

        delay is the number of seconds to wait between each retry.

        When the connection is not available, an InterfaceError is raised. Use
        the is_connected()-method if you just want to check the connection
        without raising an error.

        Raises InterfaceError on errors.
        z$Connection to MySQL is not availableN)attemptsdelay)r   rv   AttributeError	reconnectr   InterfaceError)r*   r{   rx   ry   errmsg	connectedr-   r-   r.   rv     s    zCMySQLConnection.pingc             C   s   | j j| dS )zDSets the default character set name for current connection.
        N)r   Zset_character_set)r*   r@   r-   r-   r.   set_character_set_name   s    z'CMySQLConnection.set_character_set_namec             C   sN   | j j| f }| j jr@| j j }| j j r@| j j  tjd| j j  |S )z%Send a query which only returns 1 rowz'Query should not return more than 1 row)r   queryhave_result_set	fetch_rowrt   r   r|   )r*   r   Z	first_rowr-   r-   r.   rL   %  s    



zCMySQLConnection.info_queryc             C   s$   y
| j j S  tk
r   Y nX dS )zMySQL connection IDN)r   rC   r   )r*   r-   r-   r.   connection_id3  s
    
zCMySQLConnection.connection_idNc             C   s  |r
|j n| j}| jo|s$tjd|dkr2| j}g }|dk	rN|dkrNtdd}y|r`|j n| jj }	x|	 r| j r| jrt	|	}	x4t
|	D ](\}
}|s| jj| j|
 |	|
 |	|
< qW t|	}	|j|	 |d7 }|r||krP |r|j n| jj }	qlW |	s4| j|d }|r*|j  d| _n| j  nd}W n` tk
r } zB|rl|j  tjt|n| j  tj|j|j|jdW Y dd}~X nX ||fS )	z8Get all or a subset of rows returned by the MySQL serverzNo result set availableNr   z$count should be 1 or higher, or Noner   eofF)rN   rO   rP   )r   unread_resultr   r   InternalErrorri   rz   r   r&   list	enumerateZ	to_pythonr   tupleappendfetch_eof_columnsrt   _unread_resultr   r|   r1   rQ   rN   rO   rP   )r*   countbinarycolumnsrW   	prep_stmtr   rowscounterrowi__eofrR   r-   r-   r.   get_rows=  sT    





zCMySQLConnection.get_rowsc             C   sJ   y0| j d||||d\}}|r*|d |fS d|fS  tk
rD   dS X dS )z.Get the next rows returned by the MySQL serverr   )r   r   r   rW   r   r   N)NN)r   
IndexError)r*   r   r   rW   r   r   r   r-   r-   r.   get_rowr  s    
zCMySQLConnection.get_rowc             C   s   | j r| j j  | j j S dS )zReads the next resultN)r   consume_resultnext_result)r*   r-   r-   r.   r   ~  s    

zCMySQLConnection.next_resultc             C   s   | j r| j j  dS )zFrees the resultN)r   rt   )r*   r-   r-   r.   rt     s    zCMySQLConnection.free_resultc             C   s   | j r| j j  dS )zCommit current transactionN)r   commit)r*   r-   r-   r.   r     s    zCMySQLConnection.commitc             C   s   | j r| j j  | j j  dS )zRollback current transactionN)r   r   rollback)r*   r-   r-   r.   r     s    
zCMySQLConnection.rollbackc             C   sN   y| j j| W n8 tk
rH } ztj|j|j|jdW Y dd}~X nX dS )zChange the current database)rN   rO   rP   N)r   rT   r   r   rQ   rN   rO   rP   )r*   rS   rR   r-   r-   r.   cmd_init_db  s
    zCMySQLConnection.cmd_init_dbc             C   s   |r
|j n| jj }|s tjd|r,|j n| jj }g | _xL|D ]D}| jj|d t|d ddddt|d  tj	@ t|d f qBW | j
| jj d| jdS )z Fetch EOF and column informationzNo result set      N	   )Zstatus_flagwarning_count)r   r   )r   r   r   r|   Zfetch_fieldsr   r   intr   ZNOT_NULLrF   st_warning_count)r*   r   r   fieldscolr-   r-   r.   r     s(    




z"CMySQLConnection.fetch_eof_columnsc             C   s4   | j r0| j j | j j | j j | j j | jdS dS )z Fetch EOF and status information)r   Zfield_count	insert_idaffected_rowsrA   N)r   r   Zst_field_countr   r   rF   )r*   r-   r-   r.   fetch_eof_status  s    
z!CMySQLConnection.fetch_eof_statusc             C   sR   | j stjdy| j j|S  tk
rL } ztjt|W Y dd}~X nX dS )zPrepares the SQL statementzMySQL Connection not availableN)r   r   OperationalErrorZstmt_preparer   r|   r1   )r*   Z	statementerrr-   r-   r.   cmd_stmt_prepare  s    
z!CMySQLConnection.cmd_stmt_preparec             G   sj   y|j |  W n0 tk
r> } ztjt|W Y dd}~X nX g | _|jsZd| _| j S d| _| j	|S )zExecutes the prepared statementNFT)
Zstmt_executer   r   r|   r1   r   r   r   r   r   )r*   r   argsr   r-   r-   r.   cmd_stmt_execute  s     z!CMySQLConnection.cmd_stmt_executec             C   s   | j rtjd|j  dS )zCloses the prepared statementzUnread result foundN)r   r   r   Z
stmt_close)r*   r   r-   r-   r.   cmd_stmt_close  s    
zCMySQLConnection.cmd_stmt_closec             C   s   | j rtjd|j  dS )zResets the prepared statementzUnread result foundN)r   r   r   Z
stmt_reset)r*   r   r-   r-   r.   cmd_stmt_reset  s    
zCMySQLConnection.cmd_stmt_resetc             C   s   | j   |dkr| j}y,t|ts,|jd}| jj||||d W n~ tk
rz } ztj	|j
|j|jdW Y dd}~X nH tk
r   | jr| j}n| jd t| j }tjd|dfdY nX g | _| jjs| j S | j S )	z Send a query to the MySQL serverNzutf-8)rW   rV   raw_as_string)rN   rP   :i  zConnection not available.)rO   values)handle_unread_resultri   
isinstancebytesencoder   r   r   r   rQ   rO   rN   rP   rz   ro   rj   r1   rn   r   r   r   r   r   )r*   r   rW   rV   r   rR   addrr-   r-   r.   	cmd_query  s,    

zCMySQLConnection.cmd_queryc                s  | j | | j stjd|dk	r@t|ts8tjd|| S |pH| j}|pR| j}d|dkrhdO |dkrxdO |dkrdO |dkrd	O |dkrd
O t	t
tttttttd	}y| | S  tk
r   d tddj fddtdD  Y nX dS )a  Instantiates and returns a cursor using C Extension

        By default, CMySQLCursor is returned. Depending on the options
        while connecting, a buffered and/or raw cursor is instantiated
        instead. Also depending upon the cursor options, rows can be
        returned as dictionary or named tuple.

        Dictionary and namedtuple based cursors are available with buffered
        output but not raw.

        It is possible to also give a custom cursor through the
        cursor_class parameter, but it needs to be a subclass of
        mysql.connector.cursor_cext.CMySQLCursor.

        Raises ProgrammingError when cursor_class is not a subclass of
        CursorBase. Raises ValueError when cursor is not available.

        Returns instance of CMySQLCursor or subclass.

        :param buffered: Return a buffering cursor
        :param raw: Return a raw cursor
        :param prepared: Return a cursor which uses prepared statements
        :param cursor_class: Use a custom cursor class
        :param dictionary: Rows are returned as dictionary
        :param named_tuple: Rows are returned as named tuple
        :return: Subclass of CMySQLCursor
        :rtype: CMySQLCursor or subclass
        zMySQL Connection not available.Nz=Cursor class needs be to subclass of cursor_cext.CMySQLCursorr   Tr      r   r      )	r   r   r   r5   r      r   r   r   rV   rW   
dictionarynamed_tuplepreparedz*Cursor not available with given criteria: z, c                s$   g | ]}d |> @ dkr | qS )r   r   r-   )r2   r   )r   cursor_typer-   r.   r4   U  s    z+CMySQLConnection.cursor.<locals>.<listcomp>r   )rV   rW   r   r   r   )r   rw   r   r   
issubclassr   ZProgrammingErrorrh   ri   r   r   r   r   r   r   r   r   r   KeyError
ValueErrorr    range)r*   rV   rW   r   Zcursor_classr   r   typesr-   )r   r   r.   cursor  sH    




zCMySQLConnection.cursorc             C   s   | j jstjd| j j S )z,Returns number of rows of current result setzNo result set)r   r   r   r|   num_rows)r*   r-   r-   r.   r   X  s    
zCMySQLConnection.num_rowsc             C   s   | j s
dS | j j S )zReturns number of warningsr   )r   r   )r*   r-   r-   r.   r   `  s    zCMySQLConnection.warning_countc             C   s   | j s
dS | j jS )z"Check if a result set is availableF)r   r   )r*   r-   r-   r.   result_set_availableh  s    z%CMySQLConnection.result_set_availablec             C   s   | j S )z)Check if there are unread results or rows)r   )r*   r-   r-   r.   r   p  s    zCMySQLConnection.unread_resultc             C   s
   | j j S )zCheck if there are more results)r   more_results)r*   r-   r-   r.   r   u  s    zCMySQLConnection.more_resultsc             C   sb   t |ttfr| jj| }nBt |trVi }x2|j D ]\}}| jj|d ||< q4W ntd|S )zPrepare parameters for statements

        This method is use by cursors to prepared parameters found in the
        list (or tuple) params.

        Returns dict.
        r   zCould not process parameters)r   r   r   r   Zconvert_to_mysqldictitemsr   )r*   paramsresultre   rK   r-   r-   r.   prepare_for_mysqlz  s    
z"CMySQLConnection.prepare_for_mysqlc             C   s   | j j  dS )zlConsume the current result

        This method consume the result by reading (consuming) all rows.
        N)r   r   )r*   r-   r-   r.   consume_results  s    z CMySQLConnection.consume_results -   c             C   s`   y| j j||| W n8 tk
rL } ztj|j|j|jdW Y dd}~X nX || _| j	  dS )z!Change the current logged in user)rN   rO   rP   N)
r   Zchange_userr   r   rQ   rN   rO   rP   rg   _post_connection)r*   usernamer[   rS   r@   rR   r-   r-   r.   cmd_change_user  s    z CMySQLConnection.cmd_change_userc             C   sh   | j dk rtjdy| jj  W n8 tk
rZ } ztj|j|j|j	dW Y dd}~X nX | j
  dS )zpResets the session state without re-authenticating

        Works only for MySQL server 5.7.3 or later.
        r      r5   zFMySQL version 5.7.2 and earlier does not support COM_RESET_CONNECTION.)rN   rO   rP   N)r   r   r5   )rD   r   NotSupportedErrorr   Zreset_connectionr   rQ   rN   rO   rP   r   )r*   rR   r-   r-   r.   cmd_reset_connection  s    

z%CMySQLConnection.cmd_reset_connectionc             C   sR   y| j j| W n8 tk
rH } ztj|j|j|jdW Y dd}~X nX | j S )z,Send the Refresh command to the MySQL server)rN   rO   rP   N)	r   Zrefreshr   r   rQ   rN   rO   rP   r   )r*   optionsrR   r-   r-   r.   cmd_refresh  s    zCMySQLConnection.cmd_refreshc             C   s   | j   dS )z,Close the current connection with the serverN)ru   )r*   r-   r-   r.   cmd_quit  s    zCMySQLConnection.cmd_quitc             C   s   | j stjd|r.tj|s(tjd|}ntj}y| j j| W n8 tk
r| } ztj	|j
|j|jdW Y dd}~X nX | j  dS )zShut down the MySQL ServerzMySQL Connection not availablezInvalid shutdown type)rN   rO   rP   N)r   r   r   r	   rf   r|   ZSHUTDOWN_DEFAULTshutdownr   rQ   rN   rO   rP   ru   )r*   Zshutdown_typelevelrR   r-   r-   r.   cmd_shutdown  s    


zCMySQLConnection.cmd_shutdownc             C   sf   | j   y| jj }t j|ddS  ttjfk
r` } ztj|j	|j
|jdW Y dd}~X nX dS )z'Return statistics from the MySQL serverF)Zwith_header)rN   rO   rP   N)r   r   statr   Zparse_statisticsr   r   r|   rQ   rN   rO   rP   )r*   r   rR   r-   r-   r.   cmd_statistics  s    
zCMySQLConnection.cmd_statisticsc             C   s&   t |tstd| jdj| dS )zKill a MySQL processzMySQL PID must be intzKILL {0}N)r   r   r   rL   format)r*   Z	mysql_pidr-   r-   r.   cmd_process_kill  s    
z!CMySQLConnection.cmd_process_killc             C   s6   |dkr| j n| j}| jr$| j  n|r2tjddS )z'Check whether there is an unread resultTzUnread result foundN)r   r   Zcan_consume_resultsr   r   r   )r*   r   r   r-   r-   r.   r     s    
z%CMySQLConnection.handle_unread_resultc             C   s   | j  stjdy| j  W nZ tjtfk
rx   | jrFtjdn.| jd
k r\tjdn| j| j	| j
| j| j Y nX |s|r| j }|rx(|j D ]\}}|jdj||f qW |rx(|j D ]\}}|jdj||f qW |j  d	S )a  Clears the current active session

        This method resets the session state, if the MySQL server is 5.7.3
        or later active session will be reset without re-authenticating.
        For other server versions session will be reset by re-authenticating.

        It is possible to provide a sequence of variables and their values to
        be set after clearing the session. This is possible for both user
        defined variables and session variables.
        This method takes two arguments user_variables and session_variables
        which are dictionaries.

        Raises OperationalError if not connected, InternalError if there are
        unread results and InterfaceError on errors.
        zMySQL Connection not available.zZReset session is not supported with compression for MySQL server version 5.7.2 or earlier.r   r   r5   zJReset session is not supported with MySQL server version 5.7.2 or earlier.zSET @`{0}` = %szSET SESSION `{0}` = %sN)r   r   r5   )rw   r   r   r   r   NotImplementedErrorZ	_compressrD   r   rk   rl   rm   rg   r   r   executer   ru   )r*   Zuser_variablesZsession_variablescurre   rK   r-   r-   r.   reset_session  s,    

zCMySQLConnection.reset_session)T)Fr   r   )NFNNN)FNNN)N)NFF)NNNNNN)r   r   r   r   )N)F)NN)8__name__
__module____qualname____doc__r(   r=   rE   propertyrF   rG   rJ   rM   setterrS   rU   rs   ru   Z
disconnectrw   rv   r   rL   r   r   r   r   rt   r   r   r   r   r   r   r   r   r   r   Z_execute_queryr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r-   r-   )r,   r.   r   C   sn   	

>


 
4


 
I 



	r   )&r   r   r;   	sysconfigr   r   r   Zcatch23r   	constantsr   r   r   r	   r
   	abstractsr   r   r>   r   r   r#   Zcursor_cextr   r   r   r   r   r   r   r   r   r   ImportErrorrR   r   r1   r   r-   r-   r-   r.   <module>   s&   ,