o
    –¨ÊhéV  ã                   @   sx   d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
mZmZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZdS )z"Converting MySQL and Python types
é    N)ÚDecimalé   )Ú	FieldTypeÚ	FieldFlagÚCharacterSet)ÚPY2ÚNUMERIC_TYPESÚstruct_unpack)Ú
HexLiteralc                   @   sL   e Zd ZdZddd„Zdd„ Zddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚMySQLConverterBasez�Base class for conversion classes

    All class dealing with converting to and from MySQL data types must
    be a subclass of this class.
    Úutf8Tc                 C   s<   d | _ d | _d | _d| _d | _|  |¡ |  |¡ i | _d S )Nr   )Úpython_typesÚmysql_typesÚcharsetÚ
charset_idÚuse_unicodeÚset_charsetÚset_unicodeÚ_cache_field_types©Úselfr   r   © r   úL/var/www/html/env/lib/python3.10/site-packages/mysql/connector/conversion.pyÚ__init__0   s   


zMySQLConverterBase.__init__c                 C   s8   |dkrd}|dur|| _ nd| _ t | j ¡d | _dS )zSet character setÚutf8mb4r   Nr   )r   r   Úget_charset_infor   )r   r   r   r   r   r   :   s   zMySQLConverterBase.set_charsetc                 C   s
   || _ dS )zSet whether to use UnicodeN)r   ©r   Úvaluer   r   r   r   E   s   
zMySQLConverterBase.set_unicodec                 C   s:   |j j ¡ }zt| d |¡ƒ|ƒW S  ty   | Y S w )ú!Convert Python data type to MySQLú_{0}_to_mysql)Ú	__class__Ú__name__ÚlowerÚgetattrÚformatÚAttributeError©r   r   Ú	type_namer   r   r   Úto_mysqlI   s   ÿzMySQLConverterBase.to_mysqlc              	   C   s    |dks|du r|d t jkrdS | js9i | _t j ¡ D ]\}}zt| d |¡ƒ| j|d < W q ty8   Y qw z| j|d  ||ƒW S  tyO   | Y S w )ú!Convert MySQL data type to Pythonó    Nr   ú_{0}_to_pythonr   )	r   ÚBITr   ÚdescÚitemsr#   r$   r%   ÚKeyError)r   Úvtyper   ÚnameÚinfor   r   r   Ú	to_pythonQ   s"   
ÿþÿzMySQLConverterBase.to_pythonc                 C   ó   |S )z"Escape buffer for sending to MySQLr   r   r   r   r   Úescapeg   ó   zMySQLConverterBase.escapec                 C   ó   t |ƒS )z!Quote buffer for sending to MySQL©Ústr©r   Úbufr   r   r   Úquotek   ó   zMySQLConverterBase.quoteN)r   T)T)r!   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r(   r3   r5   r<   r   r   r   r   r   )   s    


r   c                   @   sp  e Zd ZdZdHd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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dId.d/„ZeZdId0d1„ZeZeZeZeZeZ dId2d3„Z!e!Z"dId4d5„Z#dId6d7„Z$dId8d9„Z%e%Z&dId:d;„Z'dId<d=„Z(e(Z)dId>d?„Z*dId@dA„Z+dIdBdC„Z,dIdDdE„Z-e-Z.dIdFdG„Z/e,Z0e/Z1e/Z2dS )JÚMySQLConverteraæ  Default conversion class for MySQL Connector/Python.

     o escape method: for escaping values send to MySQL
     o quoting method: for quoting values send to MySQL in statements
     o conversion mapping: maps Python and MySQL data types to
       function for converting them.

    Whenever one needs to convert values differently, a converter_class
    argument can be given while instantiating a new connection like
    cnx.connect(converter_class=CustomMySQLConverterClass).

    NTc                 C   s   t  | ||¡ i | _d S ©N)r   r   r   r   r   r   r   r   ~   s   
zMySQLConverter.__init__c                 C   sÀ   |du r|S t |tƒr|S t |ttfƒr:| dd¡}| dd¡}| dd¡}| dd	¡}| d
d¡}| dd¡}|S | dd¡}| dd¡}| dd¡}| dd¡}| dd¡}| dd¡}|S )zÝ
        Escapes special characters as they are expected to by when MySQL
        receives them.
        As found in MySQL source mysys/charset.c

        Returns the value if not a string, or the escaped string.
        Nó   \s   \\ó   
s   \nó   s   \ró   's   \'ó   "s   \"ó   s   \ú\z\\Ú
z\núz\rú'z\'ú"z\"úz\)Ú
isinstancer   ÚbytesÚ	bytearrayÚreplacer   r   r   r   r5   ‚   s&   
úzMySQLConverter.escapec                 C   s\   t |tƒrtrt |tƒrt|ƒS t|ƒS t|ƒ d¡S t |tdƒƒr&tdƒS td| d ƒS )a  
        Quote the parameters for commands. General rules:
          o numbers are returns as bytes using ascii codec
          o None is returned as bytearray(b'NULL')
          o Everything else is single quoted '<buf>'

        Returns a bytearray object.
        ÚasciiNs   NULLrF   )	rO   r   r   ÚfloatÚreprr9   ÚencodeÚtyperQ   r:   r   r   r   r<   ž   s   
	
zMySQLConverter.quotec                 C   s@   |j j ¡ }zt| d |¡ƒ|ƒW S  ty   td |¡ƒ‚w )r   r   z0Python '{0}' cannot be converted to a MySQL type)r    r!   r"   r#   r$   r%   Ú	TypeErrorr&   r   r   r   r(   ±   s   ÿÿzMySQLConverter.to_mysqlc                 C   s"  |dkr|d t jkrdS |du rdS | js;i | _t j ¡ D ]\}}zt| d |¡ƒ| j|d < W q ty:   Y qw z| j|d  ||ƒW S  tyc   z| 	d¡W  Y S  t
yb   | Y  Y S w  tyx } z
td||d f ƒ‚d}~w ty� } z
td||d f ƒ‚d}~w   ‚ )r)   r   r   Nr+   úutf-8z%s (field %s))r   r,   r   r-   r.   r#   r$   r%   r/   ÚdecodeÚUnicodeDecodeErrorÚ
ValueErrorrX   )r   r0   r   r1   r2   Úerrr   r   r   r3   º   s<   
ÿþÿ€€zMySQLConverter.to_pythonc                 C   r7   ©zConvert value to int©Úintr   r   r   r   Ú_int_to_mysqlÛ   r=   zMySQLConverter._int_to_mysqlc                 C   r7   r^   r_   r   r   r   r   Ú_long_to_mysqlß   r=   zMySQLConverter._long_to_mysqlc                 C   r7   )zConvert value to float©rT   r   r   r   r   Ú_float_to_mysqlã   r=   zMySQLConverter._float_to_mysqlc                 C   s   t rt|ƒS |  |¡S )zConvert value to string)r   r9   Ú_unicode_to_mysqlr   r   r   r   Ú_str_to_mysqlç   s   
zMySQLConverter._str_to_mysqlc                 C   sP   | j }| j}|dkrd}t |¡d }| |¡}|tjv r&d|v r&t||ƒS |S )zConvert unicodeÚbinaryr   r   rC   )r   r   r   r   rV   Úslash_charsetsr
   )r   r   r   r   Úencodedr   r   r   re   í   s   


z MySQLConverter._unicode_to_mysqlc                 C   r4   ©zConvert value to bytesr   r   r   r   r   Ú_bytes_to_mysqlú   r6   zMySQLConverter._bytes_to_mysqlc                 C   r7   rj   )rP   r   r   r   r   Ú_bytearray_to_mysqlþ   r=   z"MySQLConverter._bytearray_to_mysqlc                 C   s   |rdS dS )zConvert value to booleanr   r   r   r   r   r   r   Ú_bool_to_mysql  s   zMySQLConverter._bool_to_mysqlc                 C   s   dS )zë
        This would return what None would be in MySQL, but instead we
        leave it None and return it right away. The actual conversion
        from None to NULL happens in the quoting functionality.

        Return None.
        Nr   r   r   r   r   Ú_nonetype_to_mysql  s   z!MySQLConverter._nonetype_to_mysqlc              	   C   s^   |j rd}| |j|j|j|j|j|j|j ¡ d¡S d}| |j|j|j|j|j|j¡ d¡S )zð
        Converts a datetime instance to a string suitable for MySQL.
        The returned string has format: %Y-%m-%d %H:%M:%S[.%f]

        If the instance isn't a datetime.datetime type, it return None.

        Returns a bytes.
        z5{0:d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}.{6:06d}rS   z-{0:d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d})	Úmicrosecondr$   ÚyearÚmonthÚdayÚhourÚminuteÚsecondrV   )r   r   Úfmtr   r   r   Ú_datetime_to_mysql  s    	ýýþþz!MySQLConverter._datetime_to_mysqlc                 C   s   d  |j|j|j¡ d¡S )zÚ
        Converts a date instance to a string suitable for MySQL.
        The returned string has format: %Y-%m-%d

        If the instance isn't a datetime.date type, it return None.

        Returns a bytes.
        z{0:d}-{1:02d}-{2:02d}rS   )r$   rp   rq   rr   rV   r   r   r   r   Ú_date_to_mysql'  s
   	ÿÿzMySQLConverter._date_to_mysqlc                 C   s&   |j r| d¡ d¡S | d¡ d¡S )zß
        Converts a time instance to a string suitable for MySQL.
        The returned string has format: %H:%M:%S[.%f]

        If the instance isn't a datetime.time type, it return None.

        Returns a bytes.
        z%H:%M:%S.%frS   z%H:%M:%S)ro   ÚstrftimerV   r   r   r   r   Ú_time_to_mysql3  s   	zMySQLConverter._time_to_mysqlc                 C   s   t  d|¡ d¡S )zÉ
        Converts a time.struct_time sequence to a string suitable
        for MySQL.
        The returned string has format: %Y-%m-%d %H:%M:%S

        Returns a bytes or None when not valid.
        z%Y-%m-%d %H:%M:%SrS   )Útimery   rV   r   r   r   r   Ú_struct_time_to_mysql@  s   z$MySQLConverter._struct_time_to_mysqlc           
      C   s®   t |jd |j ƒ}|jr"d}|jdk rd|j }|d8 }n|j}nd}|jdk r-d| }t|dƒ\}}t|d	ƒ\}}|jrG| ||||¡}	n| |||¡}	trR|	S |	 d
¡S )zš
        Converts a timedelta instance to a string suitable for MySQL.
        The returned string has format: %H:%M:%S

        Returns a bytes.
        i€Q z{0:02d}:{1:02d}:{2:02d}.{3:06d}r   i@B r   z{0:02d}:{1:02d}:{2:02d}ú-i  é<   rS   )ÚabsÚdaysÚsecondsÚmicrosecondsÚdivmodr$   r   rV   )
r   r   r�   rv   ÚmcsÚhoursÚ	remainderÚminsÚsecsÚresultr   r   r   Ú_timedelta_to_mysqlJ  s$   




z"MySQLConverter._timedelta_to_mysqlc                 C   s   t |tƒrt|ƒ d¡S dS )zŽ
        Converts a decimal.Decimal instance to a string suitable for
        MySQL.

        Returns a bytes or None when not valid.
        rS   N)rO   r   r9   rV   r   r   r   r   Ú_decimal_to_mysqll  s   
z MySQLConverter._decimal_to_mysqlc           
      C   sH  d}dgt |ƒ }| js1i | _tj ¡ D ]\}}zt| d |¡ƒ| j|d < W q ty0   Y qw |D ]l}|d }|| dkrD|tjksJ|| du rO|d7 }q3z| j| || |ƒ||< W n= t	y�   z||  
d¡||< W n ty~   || ||< Y nw Y n ttfyš }	 zd t|	ƒ|d ¡|	_‚ d}	~	ww |d7 }q3t|ƒS )a   Convert a MySQL text result row to Python types

        The row argument is a sequence containing text result returned
        by a MySQL server. Each value of the row is converted to the
        using the field type information in the fields argument.

        Returns a tuple.
        r   Nr+   r   rY   z{0} (field {1}))Úlenr   r   r-   r.   r#   r$   r%   r,   r/   rZ   r[   r\   rX   r9   ÚmessageÚtuple)
r   ÚrowÚfieldsÚir‰   r1   r2   ÚfieldÚ
field_typer]   r   r   r   Úrow_to_pythonx  sB   	
ÿþ"ÿ€€þ
zMySQLConverter.row_to_pythonc                 C   r7   )z.
        Returns value as float type.
        rc   ©r   r   r-   r   r   r   Ú_FLOAT_to_python¦  ó   zMySQLConverter._FLOAT_to_pythonc                 C   r7   )z,
        Returns value as int type.
        r_   r•   r   r   r   Ú_INT_to_python®  r—   zMySQLConverter._INT_to_pythonc                 C   s   |  | j¡}t|ƒS )z5
        Returns value as a decimal.Decimal.
        )rZ   r   r   )r   r   r-   Úvalr   r   r   Ú_DECIMAL_to_pythonº  s   z!MySQLConverter._DECIMAL_to_pythonc                 C   r7   )z,
        Returns value as str type.
        r8   r•   r   r   r   Ú_strÃ  r—   zMySQLConverter._strc                 C   s2   |}t |ƒdk rddt |ƒ  | }td|ƒd S )z!Returns BIT columntype as integeré   r*   z>Qr   )rŒ   r	   )r   r   ÚdscÚint_valr   r   r   Ú_BIT_to_pythonÉ  s   zMySQLConverter._BIT_to_pythonc                 C   sX   t |tjƒr|S z| d¡}t t|d ƒt|d ƒt|d ƒ¡W S  ty+   Y dS w )zA
        Returns DATE column type as datetime.date type.
        ó   -r   r   é   N)rO   ÚdatetimeÚdateÚsplitr`   r\   )r   r   r�   Úpartsr   r   r   Ú_DATE_to_pythonÐ  s   
(ÿzMySQLConverter._DATE_to_pythonc           	      C   sÀ   d}z|  d¡\}}t| dd¡ƒ}W n ty    |}d}Y nw z0dd„ |  d¡D ƒ\}}}|d d	ks;|d d
krF| | | }}}tj||||d�}W |S  ty_   td |¡ƒ‚w )zA
        Returns TIME column type as datetime.time type.
        Nó   .é   ó   0r   c                 S   ó   g | ]}t |ƒ‘qS r   r_   )Ú.0Údr   r   r   Ú
<listcomp>ê  ó    z2MySQLConverter._TIME_to_python.<locals>.<listcomp>ó   :é-   r}   )r…   Úminutesr�   r‚   z2Could not convert {0} to python datetime.timedelta)r¤   r`   Úljustr\   r¢   Ú	timedeltar$   )	r   r   r�   Útime_valÚhmsr„   r…   r‡   rˆ   r   r   r   Ú_TIME_to_pythonÞ  s.   þÿûÿÿÿzMySQLConverter._TIME_to_pythonc           	      C   s®   t |tjƒr|S d}z@| d¡\}}t|ƒdkr(| d¡\}}t| dd¡ƒ}n|}d}dd	„ | d
¡D ƒdd	„ | d¡D ƒ |g }tj|Ž }W |S  tyV   d}Y |S w )zI
        Returns DATETIME column type as datetime.datetime type.
        Nó    rœ   r§   r¨   r©   r   c                 S   rª   r   r_   ©r«   r‘   r   r   r   r­     r®   z6MySQLConverter._DATETIME_to_python.<locals>.<listcomp>r    c                 S   rª   r   r_   r¸   r   r   r   r­     r®   r¯   )rO   r¢   r¤   rŒ   r`   r²   r\   )	r   r   r�   Údatetime_valÚdate_Útime_rµ   r„   Údtvalr   r   r   Ú_DATETIME_to_pythonö  s*   ÿÿýýz"MySQLConverter._DATETIME_to_pythonc                 C   s*   zt |ƒ}W |S  ty   td| ƒ‚w )z#Returns YEAR column type as integerz"Failed converting YEAR to int (%s))r`   r\   )r   r   r-   rp   r   r   r   Ú_YEAR_to_python  s   
ýÿzMySQLConverter._YEAR_to_pythonc                 C   sJ   d}|  | j¡}|stƒ S z
t| d¡ƒ}W |S  ty$   td| ƒ‚w )zêReturns SET column type as set

        Actually, MySQL protocol sees a SET as a string type field. So this
        code isn't called directly, but used by STRING_to_python() method.

        Returns SET column type as a set.
        Nú,z'Could not convert set %s to a sequence.)rZ   r   Úsetr¤   r\   )r   r   r�   Úset_typer™   r   r   r   Ú_SET_to_python  s   þÿzMySQLConverter._SET_to_pythonc              	   C   sd  zt |ƒ}| ¡ rt|ƒW S |W S  ty   Y nw |dkr dS |dkr&dS |d dkrs|d dkrs|dd… }|  |¡}|d	urC|S |  |¡}|d	urN|S z|  |¡}|d	ur[|W S W n	 tye   Y nw t|tt	fƒrs| 
| j¡S |d	ur¬|d
 tj@ r„|  ||¡S |d
 tj@ r¬| jdkrªt|tƒsªz| 
| j¡W S  ttfy©   | Y S w |S |  ||¡S )zbReturns JSON column type as python type

        Returns JSON column type as python type.
        s   trueTs   falseFr   é"   éÿÿÿÿr   Né   rg   )rT   Ú
is_integerr`   r\   r½   r¦   r¶   rO   rP   rQ   rZ   r   r   ÚSETrÂ   ÚBINARYr9   ÚLookupErrorr[   Ú_STRING_to_python)r   r   r�   ÚnumÚvalue_nqÚvalue_datetimeÚ
value_dateÚ
value_timer   r   r   Ú_JSON_to_python*  sT   
ÿ


ÿÿÿzMySQLConverter._JSON_to_pythonc              	   C   s¤   |dur9|d t j@ r|  ||¡S |d t j@ r9| jdkr7t|tƒs7z| | j¡W S  tt	fy6   | Y S w |S | jdkr@|S t|t
tfƒrP| jrP| | j¡S |S )z°
        Note that a SET is a string too, but using the FieldFlag we can see
        whether we have to split it.

        Returns string typed columns as string type.
        NrÅ   rg   )r   rÇ   rÂ   rÈ   r   rO   r9   rZ   rÉ   r[   rP   rQ   r   ©r   r   r�   r   r   r   rÊ   a  s    ÿ
z MySQLConverter._STRING_to_pythonc                 C   sH   |dur|d t j@ rtr|S t|tƒrt|| jƒS t|ƒS |  ||¡S )z Convert BLOB data type to PythonNrÅ   )r   rÈ   r   rO   r9   rP   r   rÊ   rÑ   r   r   r   Ú_BLOB_to_python~  s   
zMySQLConverter._BLOB_to_python)NTrB   )3r!   r>   r?   r@   r   r5   r<   r(   r3   ra   rb   rd   rf   re   rk   rl   rm   rn   rw   rx   rz   r|   rŠ   r‹   r”   r–   Ú_DOUBLE_to_pythonr˜   Ú_TINY_to_pythonÚ_SHORT_to_pythonÚ_INT24_to_pythonÚ_LONG_to_pythonÚ_LONGLONG_to_pythonrš   Ú_NEWDECIMAL_to_pythonr›   rŸ   r¦   Ú_NEWDATE_to_pythonr¶   r½   Ú_TIMESTAMP_to_pythonr¾   rÂ   rÐ   rÊ   Ú_VAR_STRING_to_pythonrÒ   Ú_LONG_BLOB_to_pythonÚ_MEDIUM_BLOB_to_pythonÚ_TINY_BLOB_to_pythonr   r   r   r   rA   p   sb    
	!

"
.








	

7
rA   )r@   r¢   r{   Údecimalr   Ú	constantsr   r   r   Úcatch23r   r   r	   Úcustom_typesr
   Úobjectr   rA   r   r   r   r   Ú<module>   s   G