o
    –¨ÊhU  ã                
   @   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z"d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 n eyc Z z	ed e eƒ¡ƒ‚d	Z[ww 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'	I	Jd_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                    sP   t stdƒ‚d| _g | _d| _tt| ƒjdi |¤Ž |r&| jdi |¤Ž dS dS )ÚInitializationz0MySQL Connector/Python C Extension not availableN© )	ÚHAVE_CMYSQLÚRuntimeErrorÚ_cmysqlÚ_columnsÚ	converterÚsuperr   Ú__init__Úconnect)ÚselfÚkwargs©Ú	__class__r   úQ/var/www/html/env/lib/python3.10/site-packages/mysql/connector/connection_cext.pyr    C   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_originalÚserver_threadidÚcharsetÚserver_statusÚauth_pluginÚ	auth_dataÚcapabilitiesr(   )r   Úget_proto_infoÚget_server_infoÚ	thread_idÚst_server_capabilitiesÚ
_handshakeÚ_check_server_versionÚ_server_version©r"   r   r   r&   Ú_do_handshakeP   s   ø
ÿzCMySQLConnection._do_handshakec                 C   ó
   | j  ¡ S )z6Returns the server status attribute of MYSQL structure)r   Úst_server_statusr6   r   r   r&   Ú_server_statusa   ó   
zCMySQLConnection._server_statusTc                 C   s2   || _ | jr| j |¡ | jr| j |¡ dS 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&   r>   f   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_queryr?   r   r   r&   Ú
autocommitr   s   zCMySQLConnection.autocommitc              
   C   sH   z| j  |¡ || _W dS  ty# } ztj|j|j|jd�‚d}~ww )zToggle autocommit©ÚmsgÚerrnoÚsqlstateN)	r   rC   Ú_autocommitr   r   Úget_mysql_exceptionrE   rF   rG   )r"   r@   Úexcr   r   r&   rC   x   s   ÿ€ÿc                 C   s   |   d¡d S )zGet the current databasezSELECT DATABASE()r   rA   r6   r   r   r&   Údatabase‚   s   zCMySQLConnection.databasec                 C   ó   | j  |¡ dS )zSet the current databaseN)r   Ú	select_dbr?   r   r   r&   rK   ‡   ó   c                 C   s   | j tj@ S )z'MySQL session has started a transaction)r:   r   ÚSTATUS_IN_TRANSr6   r   r   r&   Úin_transactionŒ   s   zCMySQLConnection.in_transactionc              
   C   s  t  | j¡d }tj| j| j|| jpd| j| j	d�| _
| j| j| j| j| j| j| j|  tj¡ddœ	}| jsZ| | j d¡| j d¡| j d¡| j d¡pMd	| j d
¡pTd	| jdœ¡ z| j
jdi |¤Ž W n ty| } ztj|j|j|jd�‚d }~ww |   ¡  d S )Nr   )ÚbufferedÚrawÚcharset_nameÚconnection_timeoutr=   r,   T)	ÚhostÚuserÚpasswordrK   ÚportÚclient_flagsÚunix_socketÚcompressÚssl_disabledÚcaÚcertÚkeyÚverify_certFÚverify_identity)Ússl_caÚssl_certÚssl_keyÚssl_verify_certÚssl_verify_identityr\   rD   r   )!r   Úget_infoÚ_charset_idÚ_mysql_connectorÚMySQLÚ	_bufferedÚ_rawÚ_connection_timeoutr<   Ú_auth_pluginr   Ú_hostÚ_userÚ	_passwordÚ	_databaseÚ_portÚ_client_flagsÚ_unix_socketÚisset_client_flagr   ÚCOMPRESSÚ_ssl_disabledÚupdateÚ_sslÚgetr!   r   r   rI   rE   rF   rG   r7   )r"   rS   Ú
cnx_kwargsrJ   r   r   r&   Ú_open_connection‘   sJ   ú	
÷


ù
ÿ€ÿz!CMySQLConnection._open_connectionc              
   C   sZ   | j r+z|  ¡  | j  ¡  W n ty% } ztj|j|j|jd�‚d}~ww d| _ dS dS )z Disconnect from the MySQL serverrD   N)	r   Úfree_resultÚcloser   r   rI   rE   rF   rG   )r"   rJ   r   r   r&   r   ¹   s   ÿ€ÿ
ùzCMySQLConnection.closec                 C   s   | j r| j  ¡ S dS )z;Reports whether the connection to MySQL Server is availableF)r   Úpingr6   r   r   r&   Úis_connectedÅ   s   
zCMySQLConnection.is_connectedFr   r   c                 C   sN   d}z| j  ¡ }W n	 ty   Y nw |rdS |r"| j||d� dS t |¡‚)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   r€   ÚAttributeErrorÚ	reconnectr   ÚInterfaceError)r"   r…   r‚   rƒ   ÚerrmsgÚ	connectedr   r   r&   r€   Ì   s   ÿ
zCMySQLConnection.pingc                 C   rL   )zDSets the default character set name for current connection.
        N)r   Úset_character_set)r"   r*   r   r   r&   Úset_character_set_nameé   rN   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_rowr~   r   r†   )r"   r‹   Ú	first_rowr   r   r&   rB   î   s   


ÿ
zCMySQLConnection.info_queryc                 C   s"   z| j  ¡ W S  ty   Y dS w )zMySQL connection IDN)r   r1   r   r6   r   r   r&   Úconnection_idü   s   ýzCMySQLConnection.connection_idNc              
   C   s8  | j r| jst d¡‚|du r| j}g }|dur |dkr tdƒ‚d}z^| j  ¡ }|rh| jsQ| jrQt|ƒ}t	|ƒD ]\}}	|sL| j 
| j| || ¡||< q8t|ƒ}| |¡ |d7 }|ra||kran| j  ¡ }|s*|su|  ¡ d }
|  ¡  nd}
W ||
fS W ||
fS  ty› } z|  ¡  tj|j|j|jd�‚d}~ww )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   ÚeofrD   )r   Úunread_resultr   ÚInternalErrorrl   r„   r�   r   ÚlistÚ	enumerateÚ	to_pythonr   ÚtupleÚappendÚfetch_eof_columnsr~   r   rI   rE   rF   rG   )r"   ÚcountÚbinaryÚcolumnsrR   ÚrowsÚcounterÚrowÚiÚ_Ú_eofrJ   r   r   r&   Úget_rows  sP   

ÿ€

ô
øûÿ€þzCMySQLConnection.get_rowsc                 C   sH   z| j d|||d�\}}|r|d |fW S d|fW S  ty#   Y dS w )z.Get the next rows returned by the MySQL serverr   )r™   rš   r›   rR   r   N)NN)r¢   Ú
IndexError)r"   rš   r›   rR   rœ   r�   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_resultr6   r   r   r&   r¦   :  s   

zCMySQLConnection.next_resultc                 C   ó   | j r
| j  ¡  dS dS )zFrees the resultN)r   r~   r6   r   r   r&   r~   A  ó   ÿzCMySQLConnection.free_resultc                 C   r§   )zCommit current transactionN)r   Úcommitr6   r   r   r&   r©   F  r¨   zCMySQLConnection.commitc                 C   s"   | j r| j  ¡  | j  ¡  dS dS )zRollback current transactionN)r   r¥   Úrollbackr6   r   r   r&   rª   K  s   
þzCMySQLConnection.rollbackc              
   C   sB   z	| j  |¡ W dS  ty  } ztj|j|j|jd�‚d}~ww )zChange the current databaserD   N)r   rM   r   r   rI   rE   rF   rG   )r"   rK   rJ   r   r   r&   Úcmd_init_dbQ  s   ÿ€ÿzCMySQLConnection.cmd_init_dbc                 C   s†   | j js	t d¡‚| j  ¡ }g | _|D ]"}| j |d t|d ƒddddt|d ƒ tj	@ t|d ƒf¡ q| j
| j  ¡ dœ| jdœS )z Fetch EOF and column informationúNo result seté   é   Né	   )Ústatus_flagÚwarning_count)r�   r›   )r   rŒ   r   r†   Úfetch_fieldsr   r—   Úintr   ÚNOT_NULLr:   Úst_warning_count)r"   ÚfieldsÚcolr   r   r&   r˜   Y  s(   



øþûz"CMySQLConnection.fetch_eof_columnsc                 C   s4   | j r| j  ¡ | j  ¡ | j  ¡ | j  ¡ | jdœS dS )z Fetch EOF and status information)r±   Úfield_countÚ	insert_idÚaffected_rowsr+   N)r   rµ   Úst_field_countr¹   rº   r:   r6   r   r   r&   Úfetch_eof_statust  s   ûz!CMySQLConnection.fetch_eof_statusc              
   C   sÐ   |   ¡  |du r| j}zt|tƒs| d¡}| jj||||d� W n7 ty8 } ztj	|j
|j|jd�‚d}~w tyX   | jrE| j}n
| jd t| jƒ }tjd|dfd�‚w g | _| jjsd|  ¡ S |  ¡ S )	z Send a query to the MySQL serverNzutf-8)rR   rQ   Úraw_as_string)rE   rG   ú:i  zConnection not available.)rF   Úvalues)Úhandle_unread_resultrl   Ú
isinstanceÚbytesÚencoder   r‹   r   r   rI   rF   rE   rG   r„   ru   ro   Ústrrs   ÚOperationalErrorr   rŒ   r¼   r˜   )r"   r‹   rR   rQ   r½   rJ   Úaddrr   r   r&   Ú	cmd_query�  s6   


þÿ€ÿûzCMySQLConnection.cmd_queryc              
      s  |   ¡  |  ¡ st d¡‚|durt|tƒst d¡‚|| ƒS |p#| j}|p(| j}d‰|du r3ˆdO ‰|du r;ˆdO ‰|du rCˆdO ‰|du rKˆd	O ‰|du rSˆd
O ‰t	t
tttttttdœ	}z|ˆ | ƒW S  ty‚   d‰ tdd ‡ ‡fdd„tdƒD ƒ¡ ƒ‚w )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É   )rQ   rR   Ú
dictionaryÚnamed_tupleÚpreparedz*Cursor not available with given criteria: z, c                    s$   g | ]}ˆd |> @ dkrˆ | ‘qS )r   r   r   )Ú.0rŸ   ©ÚargsÚcursor_typer   r&   Ú
<listcomp>æ  s    ÿz+CMySQLConnection.cursor.<locals>.<listcomp>rË   )rÀ   r�   r   rÅ   Ú
issubclassr
   ÚProgrammingErrorrk   rl   r   r   r   r   r   r   r   r   r   ÚKeyErrorÚ
ValueErrorÚjoinÚrange)r"   rQ   rR   rÎ   Úcursor_classrÌ   rÍ   Útypesr   rÐ   r&   ÚcursorŸ  sP   

ÿ

÷ÿþzCMySQLConnection.cursorc                 C   s   | j js	t d¡‚| j  ¡ S )z,Returns number of rows of current result setr¬   )r   rŒ   r   r†   Únum_rowsr6   r   r   r&   rÝ   é  s   

zCMySQLConnection.num_rowsc                 C   s   | j sdS | j  ¡ S )zReturns number of warningsr   )r   r±   r6   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Œ   r6   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Þ   r6   r   r   r&   r‘     s   zCMySQLConnection.unread_resultc                 C   r8   )zCheck if there are more results)r   Úmore_resultsr6   r   r   r&   rß     r;   zCMySQLConnection.more_resultsc                 C   s^   t |ttfƒr| jj|Ž }|S t |tƒr+i }| ¡ D ]\}}| j |¡d ||< q|S tdƒ‚)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Á   r“   r–   r   Úconvert_to_mysqlÚdictÚitemsr×   )r"   ÚparamsÚresultr_   r@   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   r¥   r6   r   r   r&   Úconsume_results  s   z CMySQLConnection.consume_resultsÚ é-   c              
   C   sV   z
| j  |||¡ W n ty! } ztj|j|j|jd�‚d}~ww || _|  	¡  dS )z!Change the current logged in userrD   N)
r   Úchange_userr   r   rI   rE   rF   rG   rh   Ú_post_connection)r"   ÚusernamerW   rK   r*   rJ   r   r   r&   Úcmd_change_user%  s   ÿ€ÿz CMySQLConnection.cmd_change_userc              
   C   sF   z| j  |¡ W |  ¡ S  ty" } ztj|j|j|jd�‚d}~ww )z,Send the Refresh command to the MySQL serverrD   N)	r   Úrefreshr   r   rI   rE   rF   rG   r¼   )r"   ÚoptionsrJ   r   r   r&   Úcmd_refresh1  s   üÿ€ÿzCMySQLConnection.cmd_refreshc                 C   s   |   ¡  dS )z,Close the current connection with the serverN)r   r6   r   r   r&   Úcmd_quit;  s   zCMySQLConnection.cmd_quitc              
   C   s€   | j st d¡‚|rt |¡st d¡‚|}ntj}z| j  |¡ W n ty9 } ztj	|j
|j|jd�‚d}~ww |  ¡  dS )zShut down the MySQL ServerzMySQL Connection not availablezInvalid shutdown typerD   N)r   r   rÅ   r   rg   r†   ÚSHUTDOWN_DEFAULTÚshutdownr   rI   rE   rF   rG   r   )r"   Úshutdown_typeÚlevelrJ   r   r   r&   Úcmd_shutdown?  s    


ÿ€ÿzCMySQLConnection.cmd_shutdownc              
   C   sZ   |   ¡  z| j ¡ }tƒ j|dd�W S  ttjfy, } ztj|j	|j
|jd�‚d}~ww )z'Return statistics from the MySQL serverF)Úwith_headerrD   N)rÀ   r   Ústatr   Úparse_statisticsr   r   r†   rI   rE   rF   rG   )r"   r÷   rJ   r   r   r&   Úcmd_statisticsS  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×   rB   Úformat)r"   Ú	mysql_pidr   r   r&   Úcmd_process_kill^  s   
z!CMySQLConnection.cmd_process_killc                 C   s&   | j r	|  ¡  dS | jrt d¡‚dS )z'Check whether there is an unread resultzUnread result foundN)Úcan_consume_resultsræ   r‘   r   r’   r6   r   r   r&   rÀ   d  s
   
ÿz%CMySQLConnection.handle_unread_result)T)Fr   r   )NFNN)FNN)NFF)NNNNNN)rç   rç   rç   rè   )N)0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r7   Úpropertyr:   r>   rC   ÚsetterrK   rP   r}   r   Ú
disconnectr�   r€   rŠ   rB   r�   r¢   r¤   r¦   r~   r©   rª   r«   r˜   r¼   rÇ   Ú_execute_queryrÜ   rÝ   r±   rÞ   r‘   rß   rå   ræ   rì   rï   rð   rõ   rù   rü   rÀ   Ú__classcell__r   r   r$   r&   r   ?   sz    



	


(



	
(

ÿJ





ÿ

r   )"r  rç   r   Úcatch23r   Ú	constantsr   r   r   r   r   Ú	abstractsr	   r
   r'   r   r   ri   Úcursor_cextr   r   r   r   r   r   r   r   r   r   ÚImportErrorrJ   rú   rÄ   r   r   r   r   r&   Ú<module>   s*   ,ÿÿ€ÿ