B
    Ñš['Q  ã            
   @   sê   d Z ddl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 e eƒ¡ƒ‚W d	d	Z[X Y nX dZG dd„ deƒZ!d	S )z'Connection class using the C Extension
é   )Úerrors)Ú	INT_TYPES)ÚCharacterSetÚ	FieldFlagÚ
ServerFlagÚShutdownTypeÚ
ClientFlag)ÚMySQLConnectionAbstractÚMySQLCursorAbstract)ÚMySQLProtocolFé    N)	Ú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edd„ ƒZdYd	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dZdd„Zdd„ Zd d!„ Zed"d#„ ƒZd[d%d&„Zd\d'd(„Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd]d7d8„ZeZd^d9d:„Z ed;d<„ ƒZ!ed=d>„ ƒZ"ed?d@„ ƒZ#edAdB„ ƒZ$edCdD„ ƒZ%dEdF„ Z&dGdH„ Z'd_dKdL„Z(dMdN„ Z)dOdP„ Z*d`dQdR„Z+dSdT„ Z,dUdV„ Z-dWdX„ Z.‡  Z/S )aÚCMySQLConnectionz5Class initiating a MySQL Connection using Connector/Cc                sR   t stdƒ‚d| _d| _g | _d| _tt| ƒjf |Ž t	|ƒdkrN| j
f |Ž dS )ZInitializationz0MySQL Connector/Python C Extension not availableNé   r   )ÚHAVE_CMYSQLÚRuntimeErrorÚ_cmysqlÚ_connection_timeoutÚ_columnsÚ	converterÚsuperr   Ú__init__ÚlenÚconnect)ÚselfÚkwargs)Ú	__class__© ú£C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysql\connector\connection_cext.pyr    =   s    zCMySQLConnection.__init__c          	   C   sF   | j  ¡ | j  ¡ | j  ¡ dddd| j  ¡ dœ| _|  | 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_versionZ_server_version)r#   r&   r&   r'   Ú_do_handshakeK   s    zCMySQLConnection._do_handshakec             C   s
   | j  ¡ S )z6Returns the server status attribute of MYSQL structure)r   Zst_server_status)r#   r&   r&   r'   Ú_server_status\   s    zCMySQLConnection._server_statusTc             C   s.   || _ | jr| j |¡ | jr*| 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'   r2   a   s
    zCMySQLConnection.set_unicodec             C   s   |   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#   r3   r&   r&   r'   Ú
autocommitm   s    zCMySQLConnection.autocommitc          
   C   sT   y| j  |¡ || _W n8 tk
rN } ztj|j|j|jd�‚W dd}~X Y nX dS )zToggle autocommit)ÚmsgÚerrnoÚsqlstateN)	r   r5   Z_autocommitr   r   Úget_mysql_exceptionr6   r7   r8   )r#   r3   Úexcr&   r&   r'   r5   s   s    
c             C   s   |   d¡d S )zGet the current databasezSELECT DATABASE()r   )r4   )r#   r&   r&   r'   Údatabase}   s    zCMySQLConnection.databasec             C   s   | j  |¡ dS )zSet the current databaseN)r   Ú	select_db)r#   r3   r&   r&   r'   r;   ‚   s    c             C   s   | j tj@ S )z'MySQL session has started a transaction)r/   r   ZSTATUS_IN_TRANS)r#   r&   r&   r'   Úin_transaction‡   s    zCMySQLConnection.in_transactionc          
   C   sö   t  | j¡d }tj| j| j|t| jp(dƒ| j	| j
d�| _| j| j| j| j| j| j| j|  tj¡dœ}|  tj¡rž| | jd | jd | jd | jd d	œ¡ y| jjf |Ž W n8 tk
rè } ztj|j|j|jd
�‚W d d }~X Y nX |   ¡  d S )Nr   é
   )ÚbufferedÚrawÚcharset_nameZconnection_timeoutr1   r,   )ÚhostÚuserÚpasswordr;   ÚportZclient_flagsZunix_socketÚcompressÚcaÚcertÚkeyZverify_cert)Zssl_caZssl_certZssl_keyZssl_verify_cert)r6   r7   r8   )!r   Úget_infoÚ_charset_idÚ_mysql_connectorZMySQLÚ	_bufferedÚ_rawÚintr   r0   Z_auth_pluginr   Ú_hostÚ_userZ	_passwordZ	_databaseÚ_portZ_client_flagsÚ_unix_socketZisset_client_flagr   ZCOMPRESSZSSLÚupdateÚ_sslr"   r   r   r9   r6   r7   r8   r.   )r#   rA   Z
cnx_kwargsr:   r&   r&   r'   Ú_open_connectionŒ   s8    z!CMySQLConnection._open_connectionc          
   C   sX   | j rTy| j  ¡  W n8 tk
rL } ztj|j|j|jd�‚W dd}~X Y nX d| _ dS )z Disconnect from the MySQL server)r6   r7   r8   N)r   Úcloser   r   r9   r6   r7   r8   )r#   r:   r&   r&   r'   rW   ±   s    zCMySQLConnection.closec             C   s   | j r| 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  ¡ }W n tk
r&   Y n
X |r0dS |rD| j||d� n
t |¡‚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   rX   ÚAttributeErrorÚ	reconnectr   ÚInterfaceError)r#   r]   rZ   r[   ÚerrmsgÚ	connectedr&   r&   r'   rX   Ã   s    zCMySQLConnection.pingc             C   s   | 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  |¡ d}| j jr@| j  ¡ }| j  ¡ r@| j  ¡  t d¡‚| j  ¡  |S )z%Send a query which only returns 1 rowr&   z'Query should not return more than 1 row)r   ÚqueryÚhave_result_setÚ	fetch_rowÚfree_resultr   r^   )r#   rb   Z	first_rowr&   r&   r'   r4   å   s    



zCMySQLConnection.info_queryc             C   s$   y
| j  ¡ S  tk
r   Y nX dS )zMySQL connection IDN)r   r-   r   )r#   r&   r&   r'   Úconnection_idó   s
    
zCMySQLConnection.connection_idNc       
   
   C   s  | j r| jst d¡‚g }|dk	r2|dkr2tdƒ‚d}yŠ| j  ¡ }xz|r¼| jr�t|ƒ}x0t|ƒD ]$\}}| j 	| j
| || ¡||< q`W t|ƒ}| |¡ |d7 }|r°||kr°P | j  ¡ }qDW W nB tk
�r }	 z"|  ¡  tj|	j|	j|	jd�‚W dd}	~	X Y nX |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   )r6   r7   r8   )r   Úunread_resultr   ÚInternalErrorr\   rd   r   ÚlistÚ	enumerateZ	to_pythonr   ÚtupleÚappendr   re   r9   r6   r7   r8   )
r#   ÚcountÚbinaryÚcolumnsZrowsÚcounterÚrowÚiÚ_r:   r&   r&   r'   Úget_rowsý   s2    


zCMySQLConnection.get_rowsc             C   s.   y| j d||d�d S  tk
r(   dS X dS )z.Get the next rows returned by the MySQL serverr   )rm   rn   ro   r   N)rt   Ú
IndexError)r#   rn   ro   r&   r&   r'   Úget_row  s    zCMySQLConnection.get_rowc             C   s   | j r| j  ¡  | j  ¡ S dS )zReads the next resultN)r   Úconsume_resultÚnext_result)r#   r&   r&   r'   rx   $  s    

zCMySQLConnection.next_resultc             C   s   | j r| j  ¡  dS )zFrees the resultN)r   re   )r#   r&   r&   r'   re   +  s    zCMySQLConnection.free_resultc             C   s   | j r| j  ¡  dS )zCommit current transactionN)r   Úcommit)r#   r&   r&   r'   ry   0  s    zCMySQLConnection.commitc             C   s   | j r| j  ¡  | j  ¡  dS )zRollback current transactionN)r   rw   Úrollback)r#   r&   r&   r'   rz   5  s    
zCMySQLConnection.rollbackc          
   C   sN   y| j  |¡ W n8 tk
rH } ztj|j|j|jd�‚W dd}~X Y nX dS )zChange the current database)r6   r7   r8   N)r   r<   r   r   r9   r6   r7   r8   )r#   r;   r:   r&   r&   r'   Úcmd_init_db;  s
    zCMySQLConnection.cmd_init_dbc             C   sŠ   | j jst d¡‚| j  ¡ }g | _xL|D ]D}| j |d t|d ƒddddt|d ƒ tj	@ t|d ƒf¡ q(W | j
| j  ¡ dœ| jdœS )z Fetch EOF and column informationzNo result seté   é   Né	   )Zstatus_flagÚwarning_count)Úeofro   )r   rc   r   r^   Zfetch_fieldsr   rl   rO   r   ZNOT_NULLr/   Úst_warning_count)r#   ÚfieldsÚcolr&   r&   r'   Úfetch_eof_columnsC  s"    



z"CMySQLConnection.fetch_eof_columnsc             C   s4   | j r0| j  ¡ | j  ¡ | j  ¡ | j  ¡ | jdœS dS )z Fetch EOF and status information)r   Zfield_countÚ	insert_idÚaffected_rowsr+   N)r   r�   Zst_field_countr…   r†   r/   )r#   r&   r&   r'   Úfetch_eof_status^  s    
z!CMySQLConnection.fetch_eof_statusc          
   C   sÒ   |   ¡  y,t|tƒs| d¡}| jj||||d� W n~ tk
rl } ztj|j	|j
|jd�‚W dd}~X Y 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Ê|  ¡ S |  ¡ S )	z Send a query to the MySQL serverzutf-8)r@   r?   Úraw_as_string)r6   r8   Nú:i  zConnection not available.)r7   Úvalues)Úhandle_unread_resultÚ
isinstanceÚbytesÚencoder   rb   r   r   r9   r7   r6   r8   r\   rS   rP   ÚstrrR   ÚOperationalErrorr   rc   r‡   r„   )r#   rb   r@   r?   rˆ   r:   Úaddrr&   r&   r'   Ú	cmd_queryk  s(    

zCMySQLConnection.cmd_queryc          
      s  |   ¡  |  ¡ st d¡‚|dk	r>t|tƒs6t d¡‚|| ƒS |pF| j}|pP| j}d‰|dkrfˆdO ‰|dkrvˆ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 ‡ ‡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   r   é   r|   é   r}   r~   r“   )r?   r@   Ú
dictionaryÚnamed_tupleÚpreparedz*Cursor not available with given criteria: z, c                s$   g | ]}ˆd |> @ dkrˆ | ‘qS )r   r   r&   )Ú.0rr   )ÚargsÚcursor_typer&   r'   ú
<listcomp>Ï  s    z+CMySQLConnection.cursor.<locals>.<listcomp>r•   )r‹   rY   r   r�   Ú
issubclassr
   ZProgrammingErrorrM   rN   r   r   r   r   r   r   r   r   r   ÚKeyErrorÚ
ValueErrorÚjoinÚrange)r#   r?   r@   r˜   Zcursor_classr–   r—   Útypesr&   )rš   r›   r'   Úcursorˆ  sH    



zCMySQLConnection.cursorc             C   s   | j jst d¡‚| j  ¡ S )z,Returns number of rows of current result setzNo result set)r   rc   r   r^   Únum_rows)r#   r&   r&   r'   r¤   Ò  s    
zCMySQLConnection.num_rowsc             C   s   | j s
dS | 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   rc   )r#   r&   r&   r'   Úresult_set_availableâ  s    z%CMySQLConnection.result_set_availablec             C   s   | j S )z)Check if there are unread results or rows)r¥   )r#   r&   r&   r'   rg   ê  s    zCMySQLConnection.unread_resultc             C   s
   | j  ¡ S )zCheck if there are more results)r   Úmore_results)r#   r&   r&   r'   r¦   ï  s    zCMySQLConnection.more_resultsc             C   sb   t |ttfƒr| jj|Ž }nBt |tƒrVi }x2| ¡ D ]\}}| j |¡d ||< q4W ntdƒ‚|S )z¬Prepare 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Œ   ri   rk   r   Zconvert_to_mysqlÚdictÚitemsrŸ   )r#   ÚparamsÚresultrI   r3   r&   r&   r'   Úprepare_for_mysqlô  s    
z"CMySQLConnection.prepare_for_mysqlc             C   s   | j  ¡  dS )zlConsume the current result

        This method consume the result by reading (consuming) all rows.
        N)r   rw   )r#   r&   r&   r'   Úconsume_results  s    z CMySQLConnection.consume_resultsÚ é!   c          
   C   s`   y| j  |||¡ W n8 tk
rL } ztj|j|j|jd�‚W dd}~X Y nX || _|  	¡  dS )z!Change the current logged in user)r6   r7   r8   N)
r   Zchange_userr   r   r9   r6   r7   r8   rK   Z_post_connection)r#   ÚusernamerD   r;   r*   r:   r&   r&   r'   Úcmd_change_user  s    z CMySQLConnection.cmd_change_userc          
   C   sR   y| j  |¡ W n8 tk
rH } ztj|j|j|jd�‚W dd}~X Y nX |  ¡ S )z,Send the Refresh command to the MySQL server)r6   r7   r8   N)	r   Zrefreshr   r   r9   r6   r7   r8   r‡   )r#   Úoptionsr:   r&   r&   r'   Úcmd_refresh  s    zCMySQLConnection.cmd_refreshc             C   s   |   ¡  dS )z,Close the current connection with the serverN)rW   )r#   r&   r&   r'   Úcmd_quit$  s    zCMySQLConnection.cmd_quitc          
   C   sŠ   | j st d¡‚|r.t |¡s(t d¡‚|}ntj}y| j  |¡ W n8 tk
r| } ztj	|j
|j|jd�‚W dd}~X Y nX |  ¡  dS )zShut down the MySQL ServerzMySQL Connection not availablezInvalid shutdown type)r6   r7   r8   N)r   r   r�   r   rJ   r^   ZSHUTDOWN_DEFAULTÚshutdownr   r9   r6   r7   r8   rW   )r#   Zshutdown_typeÚlevelr:   r&   r&   r'   Úcmd_shutdown(  s    


zCMySQLConnection.cmd_shutdownc          
   C   sf   |   ¡  y| j ¡ }tƒ j|dd�S  ttjfk
r` } ztj|j	|j
|jd�‚W dd}~X Y nX dS )z'Return statistics from the MySQL serverF)Zwith_header)r6   r7   r8   N)r‹   r   Ústatr   Zparse_statisticsr   r   r^   r9   r6   r7   r8   )r#   r·   r:   r&   r&   r'   Úcmd_statistics<  s    
zCMySQLConnection.cmd_statisticsc             C   s&   t |tƒstdƒ‚|  d |¡¡ dS )zKill a MySQL processzMySQL PID must be intzKILL {0}N)rŒ   r   rŸ   r4   Úformat)r#   Z	mysql_pidr&   r&   r'   Úcmd_process_killG  s    
z!CMySQLConnection.cmd_process_killc             C   s$   | j r|  ¡  n| jr t d¡‚dS )z'Check whether there is an unread resultzUnread result foundN)Zcan_consume_resultsr¬   rg   r   rh   )r#   r&   r&   r'   r‹   M  s    
z%CMySQLConnection.handle_unread_result)T)Fr   r   )NFN)FN)FFF)NNNNNN)r­   r­   r­   r®   )N)0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r.   Úpropertyr/   r2   r5   Úsetterr;   r=   rV   rW   Z
disconnectrY   rX   ra   r4   rf   rt   rv   rx   re   ry   rz   r{   r„   r‡   r’   Z_execute_queryr£   r¤   r   r¥   rg   r¦   r«   r¬   r°   r²   r³   r¶   r¸   rº   r‹   Ú__classcell__r&   r&   )r%   r'   r   9   s\   

%	




 
I 


r   )"r¾   r­   r   Zcatch23r   Ú	constantsr   r   r   r   r   Z	abstractsr	   r
   r(   r   r   rL   Zcursor_cextr   r   r   r   r   r   r   r   r   r   ÚImportErrorr:   r¹   r�   r   r&   r&   r&   r'   Ú<module>   s    ,