U
    ea                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZ ddlmZmZmZ ddlmZ ddlmZmZmZmZmZmZ dd	lmZ d
ZG dd dZ G dd de Z!dS )z"Converting MySQL and Python types
    N)Decimal)AnyCallableDictListOptionalSetTupleUnion   )CharacterSet	FieldFlag	FieldType)
HexLiteral)DescriptionTypeRowType
StrOrBytesToMysqlInputTypesToMysqlOutputTypesToPythonOutputTypes)NUMERIC_TYPESz.Could not convert '{value}' to python {pytype}c                   @   s   e Zd ZdZdee eeddddZee dd	d
dZdeddddZ	e
ee
ef dddZeee edddZeeedddZeeedddZdS )MySQLConverterBasezBase class for conversion classes

    All class dealing with converting to and from MySQL data types must
    be a subclass of this class.
    utf8TFNcharsetuse_unicodestr_fallbackreturnc                 C   s8   d | _ d | _d | _d| _| | || _|| _i | _d S )Nr   )Zpython_typesZmysql_typesr   
charset_idset_charsetr   r   _cache_field_typesselfr   r   r    r#   [/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/mysql/connector/conversion.py__init__>   s    
zMySQLConverterBase.__init__)r   r   c                 C   s8   |dkrd}|dk	r|| _ nd| _ t| j d | _dS )zSet character set)Zutf8mb4Zutf8mb3r   Nr   )r   r   get_charset_infor   )r"   r   r#   r#   r$   r   P   s    zMySQLConverterBase.set_charsetvaluer   c                 C   s
   || _ dS )zSet whether to use UnicodeN)r   r"   r(   r#   r#   r$   set_unicode[   s    zMySQLConverterBase.set_unicodec                 C   sF   |j j }zt| d| d|}|W S  tk
r@   | Y S X dS )!Convert Python data type to MySQL_	_to_mysqlN)	__class____name__lowergetattrAttributeErrorr"   r(   	type_nameZ	convertedr#   r#   r$   to_mysql_   s    zMySQLConverterBase.to_mysqlvtyper(   r   c              	   C   s   |dks|dkr"|d t jkr"dS | js|i | _t j D ]B\}}z$t| d|  d| j|d < W q8 tk
rx   Y q8X q8|dkrdS z| j|d  ||W S  tk
r   | Y S X dS )!Convert MySQL data type to Python    Nr   r,   
_to_pythonr   )	r   BITr    descitemsr1   r0   r2   KeyError)r"   r7   r(   nameinfor#   r#   r$   	to_pythonl   s$     zMySQLConverterBase.to_pythonc                 C   s   | S )z"Escape buffer for sending to MySQLr#   r(   r#   r#   r$   escape   s    zMySQLConverterBase.escapebufr   c                 C   s   t | S )z!Quote buffer for sending to MySQLstrrE   r#   r#   r$   quote   s    zMySQLConverterBase.quote)r   TF)T)r/   
__module____qualname____doc__r   rG   boolr%   r   r*   r   r
   r   r5   r   bytesr   rA   staticmethodr   rC   r   rI   r#   r#   r#   r$   r   7   s.      
 r   c                   @   s  e Zd ZdZdOee eeddddZee	e	dd	d
Z
eeeeeeeef  edddZeedddZeee edddZeeedddZeeedddZeeee dddZeeeef dddZeeeef dddZeeedddZeeeddd Zeeedd!d"Z edddd#d$Z!ee"j"edd%d&Z#ee"j$edd'd(Z%ee"j&edd)d*Z'ee&j(edd+d,Z)ee"j*edd-d.Z+eeee dd/d0Z,e-ed1f e.e e/d2d3d4Z0edPeee ed5d6d7Z1e1Z2edQeee ed5d8d9Z3e3Z4e3Z5e3Z6e3Z7e3Z8dReee ed5d:d;Z9e9Z:edSeee ed5d<d=Z;edTeee ed>d?d@Z<edUeee ee"j$ d>dAdBZ=e=Z>edVeee e"j*d>dCdDZ?edWeee ee"j" d>dEdFZ@e@ZAedXeee ed>dGdHZBdYeee eCe d>dIdJZDdZeee eeEeCe f d>dKdLZFeFZGeFZHd[eee eeEeCe f d>dMdNZIeIZJeIZKeIZLdS )\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).

    NTFr   c                 C   s   t | ||| i | _d S )N)r   r%   r    r!   r#   r#   r$   r%      s    zMySQLConverter.__init__r'   c                 C   s   t | ttfrX| dd} | dd} | dd} | dd} | d	d
} | dd} n\t | trt | t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.
           \s   \\   
s   \n   s   \r   's   \'   "s   \"   s   \\z\\
z\nz\r'z\'"z\"z\)
isinstancerN   	bytearrayreplacerG   r   rB   r#   r#   r$   rC      s    	zMySQLConverter.escaperD   c                 C   s>   t | tr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   NULLrT   )r]   r   rG   encodetyper^   rH   r#   r#   r$   rI      s
    

zMySQLConverter.quotec                 C   sj   |j j }zt| d| d|}|W S  tk
rd   | jrNt|  Y S td| ddY nX dS )r+   r,   r-   zPython 'z%' cannot be converted to a MySQL typeN)	r.   r/   r0   r1   r2   r   rG   ra   	TypeErrorr3   r#   r#   r$   r5      s    
zMySQLConverter.to_mysqlr6   c                 C   sV  |dkr|d t jkrdS |dkr&dS | jsi | _t j D ]B\}}z$t| d|  d| j|d < W q< tk
r|   Y q<X q<z| j|d  ||W S  tk
r   z|	dW  Y S  t
k
r   | Y  Y S X Y nz tk
r } zt| d|d  d|W 5 d}~X Y n> tk
rP } zt| d|d  d|W 5 d}~X Y nX dS )	r8   r   r   Nr,   r:   utf-8 (field ))r   r;   r    r<   r=   r1   r0   r2   r>   decodeUnicodeDecodeError
ValueErrorrc   )r"   r7   r(   r?   r@   errr#   r#   r$   rA      s2     *zMySQLConverter.to_pythonc                 C   s   t | S )zConvert value to intintrB   r#   r#   r$   _int_to_mysql  s    zMySQLConverter._int_to_mysqlc                 C   s   t | S )zConvert value to int

        Note: there is not type "long" in Python 3 since integers (int) are of unlimited size.
        Since Python 2 is no longer supported, this method should be deprecated.
        rk   rB   r#   r#   r$   _long_to_mysql  s    zMySQLConverter._long_to_mysqlc                 C   s   t | rdS t| S )zConvert value to floatN)mathisnanfloatrB   r#   r#   r$   _float_to_mysql  s    
zMySQLConverter._float_to_mysqlc                 C   s
   |  |S )zConvert value to string)_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krLd|krLt||S |S )zConvert unicodebinaryr   r   rQ   )r   r   r   r&   ra   Zslash_charsetsr   )r"   r(   r   r   encodedr#   r#   r$   rs   !  s    


z MySQLConverter._unicode_to_mysqlc                 C   s   | S zConvert value to bytesr#   rB   r#   r#   r$   _bytes_to_mysql.  s    zMySQLConverter._bytes_to_mysqlc                 C   s   t | S rw   )rN   rB   r#   r#   r$   _bytearray_to_mysql3  s    z"MySQLConverter._bytearray_to_mysqlc                 C   s   | rdS dS )zConvert value to booleanr   r   r#   rB   r#   r#   r$   _bool_to_mysql8  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#   rB   r#   r#   r$   _nonetype_to_mysql=  s    	z!MySQLConverter._nonetype_to_mysqlc              	   C   s^   | j r4d}|| 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.
        z7{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}.{6:06d}r`   z/{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d})	microsecondformatyearmonthdayhourminutesecondra   )r(   fmtr#   r#   r$   _datetime_to_mysqlH  s0    

z!MySQLConverter._datetime_to_mysqlc                 C   s&   | j dd| jdd| jd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04d-02dr`   )r~   r   r   ra   rB   r#   r#   r$   _date_to_mysqlh  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.%fr`   z%H:%M:%S)r|   strftimera   rB   r#   r#   r$   _time_to_mysqlt  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:%Sr`   )timer   ra   rB   r#   r#   r$   _struct_time_to_mysql  s    	z$MySQLConverter._struct_time_to_mysqlc           	      C   s   t | jd | j }| jrDd}| jdk r<d| j }|d8 }qH| j}nd}| jdk rZd| }t|d\}}t|d	\}}| jr|||||}n||||}|d
S )z
        Converts a timedelta instance to a string suitable for MySQL.
        The returned string has format: %H:%M:%S

        Returns a bytes.
        iQ z{0:02d}:{1:02d}:{2:02d}.{3:06d}r   i@B r   z{0:02d}:{1:02d}:{2:02d}r   i  <   r`   )absdayssecondsmicrosecondsdivmodr}   ra   )	r(   r   r   mcshours	remainderminssecsresultr#   r#   r$   _timedelta_to_mysql  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.
        r`   N)r]   r   rG   ra   rB   r#   r#   r$   _decimal_to_mysql  s    
z MySQLConverter._decimal_to_mysql.)rowfieldsr   c           
      C   sf  d}dgt | }| jsli | _tj D ]B\}}z$t| d|  d| j|d < W q( tk
rh   Y q(X q(|D ]}|d }|| dkr|tjks|| dkr|d7 }qpz| j| || |||< W n t	k
r   z|| 
d||< W n" tk
r   || ||< Y nX Y n@ ttfk
rR }	 z|	 d|d  d|	_ W 5 d}	~	X Y nX |d7 }qpt|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:   r   rd   re   rf   )lenr    r   r<   r=   r1   r0   r2   r;   r>   rg   rh   ri   rc   messagetuple)
r"   r   r   ir   r?   r@   fieldZ
field_typerj   r#   r#   r$   row_to_python  s:     "
zMySQLConverter.row_to_python)r(   r<   r   c                 C   s   t | S )z.
        Returns value as float type.
        )rq   r(   r<   r#   r#   r$   _float_to_python  s    zMySQLConverter._float_to_pythonc                 C   s   t | S )z,
        Returns value as int type.
        rk   r   r#   r#   r$   _int_to_python  s    zMySQLConverter._int_to_pythonc                 C   s   | | j}t|S )z5
        Returns value as a decimal.Decimal.
        )rg   r   r   )r"   r(   r<   valr#   r#   r$   _decimal_to_python  s    z!MySQLConverter._decimal_to_pythonc                 C   s   t | S )z,
        Returns value as str type.
        rF   r   r#   r#   r$   _str  s    zMySQLConverter._str)r(   dscr   c                 C   s8   | }t |dk r$ddt |  | }ttd|d S )z!Returns BIT columntype as integer   r9   z>Qr   )r   rl   structunpack)r(   r   Zint_valr#   r#   r$   _bit_to_python  s    zMySQLConverter._bit_to_pythonc              	   C   s   t | tjr| S zv| d}t|dkr@td| dt| z*tt|d t|d t|d W W S  tk
r   Y W dS X W n. ttfk
r   td	t|  d
dY nX dS )zConverts TIME column MySQL to a python datetime.datetime type.

        Raises ValueError if the value can not be converted.

        Returns DATE column type as datetime.date type.
           -   invalid datetime format:  len: r   r      NzCould not convert z to python datetime.timedelta)	r]   datetimedatesplitr   ri   rl   
IndexErrorrepr)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tfk
rF   | }d}Y nX z\dd | dD \}}}| d d	ks|| d d
kr| | |   }}}tj||||dW S  tttfk
r   ttj	| dddY nX dS )a  Converts TIME column value to python datetime.time value type.

        Converts the TIME column MySQL type passed as bytes to a python
        datetime.datetime type.

        Raises ValueError if the value can not be converted.

        Returns datetime.timedelta type.
        N   .      0r   c                 S   s   g | ]}t |qS r#   rk   ).0dr#   r#   r$   
<listcomp>N  s     z2MySQLConverter._time_to_python.<locals>.<listcomp>   :-   r   )r   minutesr   r   datetime.timedeltar(   Zpytype)
r   rl   ljustrc   ri   r   	timedeltar   CONVERT_ERRORr}   )r(   r   r   hmsr   r   r   r#   r#   r$   _time_to_python9  s4    

   zMySQLConverter._time_to_pythonc              	   C   s  t | tjr| S d}d}z| d\}}t|dkrT|d\}}t|dd}n|}d}dd	 |d
D dd	 |dD  |g }t|dk rtd| dt| ztj| }W n tk
r   Y W dS X W n. ttfk
r   tt	j
| dddY nX |S )zConverts DATETIME column value to python datetime.time value type.

        Converts the DATETIME column MySQL type passed as bytes to a python
        datetime.datetime type.

        Returns: datetime.datetime type.
        N    r   r   r   r   r   c                 S   s   g | ]}t |qS r#   rk   r   r   r#   r#   r$   r   u  s     z6MySQLConverter._datetime_to_python.<locals>.<listcomp>r   c                 S   s   g | ]}t |qS r#   rk   r   r#   r#   r$   r   v  s     r   r   r   r   r   )r]   r   r   r   rl   r   ri   r   rc   r   r}   )r(   r   Zdatetime_valr   Zdate_Ztime_r   Zdtvalr#   r#   r$   _datetime_to_python]  s>    z"MySQLConverter._datetime_to_pythonc              
   C   sJ   zt | }W n8 tk
rD } ztdt|  d|W 5 d}~X Y nX |S )z#Returns YEAR column type as integerzFailed converting YEAR to int (rf   N)rl   ri   r   )r(   r   r~   rj   r#   r#   r$   _year_to_python  s
    (zMySQLConverter._year_to_pythonc              
   C   sj   d}| | j}|st S zt|d}W n8 tk
rd } ztdt| d|W 5 d}~X Y nX |S )zReturns 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 z to a sequence)rg   r   setr   ri   r   )r"   r(   r   set_typer   rj   r#   r#   r$   _set_to_python  s    
zMySQLConverter._set_to_pythonc                 C   s   | j dkr|S |dk	r`|d tjkr6| jr6|| j S |d tj@ rP| ||S |d dkr`|S t|t	t
fr| jrz|| j W S  tk
r   | Y S X |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.
        ru   Nr      r   ?   )r   r   JSONr   rg   r   ZSETr   r]   rN   r^   rh   r"   r(   r   r#   r#   r$   _string_to_python  s    	

z MySQLConverter._string_to_pythonc                 C   sD   |dk	r8|d t j@ r8|d t j@ r8|d dkr8t|S | ||S )z!Convert BLOB data type to Python.Nr   r   r   )r   ZBLOBBINARYrN   r   r   r#   r#   r$   _blob_to_python  s    
zMySQLConverter._blob_to_python)NTF)N)N)N)N)N)N)N)N)N)N)N)N)Mr/   rJ   rK   rL   r   rG   rM   r%   rO   r   rC   r
   rq   rl   r   r   rN   rI   r   r   r5   r   r   rA   rm   rn   rr   rt   rs   rx   r^   ry   rz   r{   r   r   r   r   r   r   struct_timer   r   r   r   r	   r   r   r   r   Z_double_to_pythonr   Z_tiny_to_pythonZ_short_to_pythonZ_int24_to_pythonZ_long_to_pythonZ_longlong_to_pythonr   Z_newdecimal_to_pythonr   r   r   Z_NEWDATE_to_pythonr   r   Z_timestamp_to_pythonr   r   r   r   r   Z_var_string_to_pythonZ_json_to_pythonr   Z_long_blob_to_pythonZ_medium_blob_to_pythonZ_tiny_blob_to_pythonr#   r#   r#   r$   rP      s      $%

 
 3  	    #  .
      rP   )"rL   r   ro   r   r   decimalr   typingr   r   r   r   r   r   r	   r
   	constantsr   r   r   Zcustom_typesr   typesr   r   r   r   r   r   utilsr   r   r   rP   r#   r#   r#   r$   <module>   s   ( Z