3
Šãdec  ã               @   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deee edœdd„ƒZeeedœ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.
    Úutf8TFN)ÚcharsetÚuse_unicodeÚstr_fallbackÚreturnc             C   s8   d | _ d | _d | _d| _| j|ƒ || _|| _i | _d S )Nr   )Zpython_typesZmysql_typesr   Ú
charset_idÚset_charsetr   r   Ú_cache_field_types)Úselfr   r   r   © r!   úQ/var/www/agendate/envp3/lib/python3.6/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| j ƒd | _dS )zSet character setÚutf8mb4Úutf8mb3r   Nr   )r$   r%   )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   s@   |j jjƒ }yt| d|› d�ƒ|ƒ}|S  tk
r:   |S X dS )z!Convert Python data type to MySQLÚ_Ú	_to_mysqlN)Ú	__class__Ú__name__ÚlowerÚgetattrÚAttributeError)r    r'   Ú	type_nameÚ	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 | _xPt jjƒ D ]B\}}y$t| d|jƒ › d�ƒ| j|d < W q: tk
rz   Y q:X q:W |dkrŒdS y| j|d  ||ƒS  tk
r´   |S X dS )z!Convert MySQL data type to Pythonó    Nr   r)   Ú
_to_pythonr   )	r   ÚBITr   ÚdescÚitemsr.   r-   r/   ÚKeyError)r    r3   r'   ÚnameÚinfor!   r!   r"   Ú	to_pythonl   s     "
zMySQLConverterBase.to_python)r'   Úsql_moder   c             C   s   | S )z"Escape buffer for sending to MySQLr!   )r'   r=   r!   r!   r"   Úescape†   s    zMySQLConverterBase.escape)Úbufr   c             C   s   t | ƒS )z!Quote buffer for sending to MySQL)Ústr)r?   r!   r!   r"   ÚquoteŽ   s    zMySQLConverterBase.quote)r   TF)T)N)r,   Ú
__module__Ú__qualname__Ú__doc__r   r@   Úboolr#   r   r(   r   r
   r   r2   r   Úbytesr   r<   Ústaticmethodr   r>   r   rA   r!   r!   r!   r"   r   7   s      
r   c               @   s˜  e Zd ZdZdPee eeddœdd„ZedQe	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œd0d1„ƒZ,e-ed2f e.e e/d3œd4d5„Z0edReee ed6œd7d8„ƒZ1e1Z2edSeee ed6œd9d:„ƒZ3e3Z4e3Z5e3Z6e3Z7e3Z8dTeee ed6œd;d<„Z9e9Z:edUeee ed6œd=d>„ƒZ;edVeee ed?œd@dA„ƒZ<edWeee ee"j$ d?œdBdC„ƒZ=e=Z>edXeee e"j*d?œdDdE„ƒZ?edYeee ee"j" d?œdFdG„ƒZ@e@ZAedZeee ed?œdHdI„ƒZBd[eee eCe d?œdJdK„ZDd\eee eeEeCe f d?œdLdM„ZFeFZGeFZHd]eee eeEeCe f d?œdNdO„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).

    NTF)r   r   r   r   c             C   s   t j| |||ƒ i | _d S )N)r   r#   r   )r    r   r   r   r!   r!   r"   r#   ¢   s    zMySQLConverter.__init__)r'   r=   r   c             C   sâ   t | ttfƒrl|dkr"| jddƒS | jddƒ} | jddƒ} | jdd	ƒ} | jdd
ƒ} | jddƒ} | jddƒ} nrt | tƒrÞt | tƒ rÞ|dkr–| jddƒS | jddƒ} | jddƒ} | jddƒ} | jddƒ} | jddƒ} | j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.
        ZNO_BACKSLASH_ESCAPESó   's   ''ó   \s   \\ó   
s   \nó   s   \rs   \'ó   "s   \"ó   s   \ú'z''ú\z\\Ú
z\núz\rz\'ú"z\"úz\)Ú
isinstancerF   Ú	bytearrayÚreplacer@   r   )r'   r=   r!   r!   r"   r>   ®   s&    	zMySQLConverter.escape)r?   r   c             C   s>   t | tƒrt| ƒj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   NULLrI   )rU   r   r@   ÚencodeÚtyperV   )r?   r!   r!   r"   rA   Ë   s
    

zMySQLConverter.quote)r'   r   c             C   sd   |j jjƒ }yt| d|› d�ƒ|ƒ}|S  tk
r^   | jrHt|ƒjƒ S td|› d�ƒd‚Y nX dS )z!Convert Python data type to MySQLr)   r*   zPython 'z%' cannot be converted to a MySQL typeN)	r+   r,   r-   r.   r/   r   r@   rY   Ú	TypeError)r    r'   r0   r1   r!   r!   r"   r2   Û   s    zMySQLConverter.to_mysql)r3   r'   r   c             C   sJ  |dkr|d t jkrdS |dkr&dS | js„i | _xPt jjƒ D ]B\}}y$t| d|jƒ › d�ƒ| j|d < W q> tk
r~   Y q>X q>W y| j|d  ||ƒS  tk
rÌ   y
|j	dƒS  t
k
rÆ   |S X Y nz tk
�r } z t|› d|d › d�ƒ|‚W Y dd}~X n> tk
�rD } z t|› d|d › d�ƒ|‚W Y dd}~X nX dS )	z!Convert MySQL data type to Pythonr   r   Nr)   r5   zutf-8z (field ú))r   r6   r   r7   r8   r.   r-   r/   r9   ÚdecodeÚUnicodeDecodeErrorÚ
ValueErrorr[   )r    r3   r'   r:   r;   Úerrr!   r!   r"   r<   ê   s.    "


*zMySQLConverter.to_pythonc             C   s   t | ƒS )zConvert value to int)Úint)r'   r!   r!   r"   Ú_int_to_mysql  s    zMySQLConverter._int_to_mysqlc             C   s   t | ƒS )zÍConvert value to int

        Note: There is no type "long" in Python 3 since integers are of unlimited size.
              Since Python 2 is no longer supported, this method should be deprecated.
        )ra   )r'   r!   r!   r"   Ú_long_to_mysql  s    zMySQLConverter._long_to_mysqlc             C   s   t j| ƒrdS t| ƒS )zConvert value to floatN)ÚmathÚisnanÚfloat)r'   r!   r!   r"   Ú_float_to_mysql  s    
zMySQLConverter._float_to_mysqlc             C   s
   | j |ƒS )zConvert value to string)Ú_unicode_to_mysql)r    r'   r!   r!   r"   Ú_str_to_mysql$  s    zMySQLConverter._str_to_mysqlc             C   sP   | j }| j}|dkr&d}tj|ƒd }|j|ƒ}|tjkrLd|krLt||ƒS |S )zConvert unicodeÚbinaryr   r   rJ   )r   r   r   r&   rY   Zslash_charsetsr   )r    r'   r   r   Úencodedr!   r!   r"   rh   (  s    


z MySQLConverter._unicode_to_mysqlc             C   s   | S )zConvert value to bytesr!   )r'   r!   r!   r"   Ú_bytes_to_mysql5  s    zMySQLConverter._bytes_to_mysqlc             C   s   t | ƒS )zConvert value to bytes)rF   )r'   r!   r!   r"   Ú_bytearray_to_mysql:  s    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_mysqlD  s    	z!MySQLConverter._nonetype_to_mysqlc             C   s^   | j r4d}|j| j| j| j| j| j| j| j ƒjdƒS d}|j| j| 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}rX   z/{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d})	ÚmicrosecondÚformatÚyearÚmonthÚdayÚhourÚminuteÚsecondrY   )r'   Úfmtr!   r!   r"   Ú_datetime_to_mysqlO  s(    
z!MySQLConverter._datetime_to_mysqlc             C   s&   | j d›d| jd›d| jd›�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04dú-Z02drX   )rr   rs   rt   rY   )r'   r!   r!   r"   Ú_date_to_mysqlo  s    
zMySQLConverter._date_to_mysqlc             C   s&   | j r| jdƒjdƒS | jdƒj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.%frX   z%H:%M:%S)rp   ÚstrftimerY   )r'   r!   r!   r"   Ú_time_to_mysql{  s    
zMySQLConverter._time_to_mysqlc             C   s   t jd| ƒj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:%SrX   )Útimer|   rY   )r'   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Ž|j||||ƒ}n|j|||ƒ}|j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}rz   i  é<   rX   )ÚabsÚdaysÚsecondsÚmicrosecondsÚdivmodrq   rY   )	r'   rƒ   rx   ÚmcsÚhoursÚ	remainderÚminsÚsecsÚresultr!   r!   r"   Ú_timedelta_to_mysql”  s     



z"MySQLConverter._timedelta_to_mysqlc             C   s   t | tƒrt| ƒjdƒS dS )zŽ
        Converts a decimal.Decimal instance to a string suitable for
        MySQL.

        Returns a bytes or None when not valid.
        rX   N)rU   r   r@   rY   )r'   r!   r!   r"   Ú_decimal_to_mysqlµ  s    
z MySQLConverter._decimal_to_mysql.)ÚrowÚfieldsr   c       
      C   sn  d}dgt |ƒ }| jspi | _xPtjjƒ D ]B\}}y$t| d|jƒ › d�ƒ| j|d < W q* tk
rj   Y q*X q*W xô|D ]ì}|d }|| dkr˜|tjks¤|| dkr®|d7 }qvy| j| || |ƒ||< W nŽ t	k
�r   y|| j
dƒ||< W n" tk
�r   || ||< Y nX Y n@ ttfk
�rX }	 z|	› d|d › d�|	_‚ W Y dd}	~	X nX |d7 }qvW t|ƒ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)   r5   r   zutf-8z (field r\   )Úlenr   r   r7   r8   r.   r-   r/   r6   r9   r]   r^   r_   r[   ÚmessageÚtuple)
r    rŽ   r�   Úir‹   r:   r;   ÚfieldZ
field_typer`   r!   r!   r"   Úrow_to_pythonÂ  s6    "

"zMySQLConverter.row_to_python)r'   r7   r   c             C   s   t | ƒS )z.
        Returns value as float type.
        )rf   )r'   r7   r!   r!   r"   Ú_float_to_pythonõ  s    zMySQLConverter._float_to_pythonc             C   s   t | ƒS )z,
        Returns value as int type.
        )ra   )r'   r7   r!   r!   r"   Ú_int_to_pythonþ  s    zMySQLConverter._int_to_pythonc             C   s   |j | jƒ}t|ƒS )z5
        Returns value as a decimal.Decimal.
        )r]   r   r   )r    r'   r7   Úvalr!   r!   r"   Ú_decimal_to_python  s    z!MySQLConverter._decimal_to_pythonc             C   s   t | ƒS )z,
        Returns value as str type.
        )r@   )r'   r7   r!   r!   r"   Ú_str  s    zMySQLConverter._str)r'   Údscr   c             C   s8   | }t |ƒdk r$ddt |ƒ  | }ttjd|ƒd ƒS )z!Returns BIT columntype as integeré   r4   z>Qr   )r�   ra   Ú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 yn| jdƒ}t|ƒdkr@td|› dt|ƒ› �ƒ‚y&tjt|d ƒt|d ƒt|d ƒƒS  tk
rz   dS X W n. ttfk
r¬   td	t| ƒ› d
�ƒd‚Y nX dS )z¾Converts 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.
        ó   -é   zinvalid datetime format: z len: r   r   é   NzCould not convert z to python datetime.timedelta)	rU   ÚdatetimeÚdateÚsplitr�   r_   ra   Ú
IndexErrorÚrepr)r'   r›   Úpartsr!   r!   r"   Ú_date_to_python%  s    

&
zMySQLConverter._date_to_pythonc             C   sÖ   d}y"| j dƒ\}}t|jddƒƒ}W n  ttfk
rF   | }d}Y nX yZdd„ |j dƒD ƒ\}}}| d d	ks|| d d
kr’| | |   }}}tj||||d�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!   )ra   )Ú.0Údr!   r!   r"   ú
<listcomp>U  s    z2MySQLConverter._time_to_python.<locals>.<listcomp>ó   :é-   rz   )r‡   Úminutesrƒ   r„   zdatetime.timedelta)r'   Úpytype)
r¥   ra   Úljustr[   r_   r£   Ú	timedeltar¦   ÚCONVERT_ERRORrq   )r'   r›   r†   Úhmsr‡   r‰   rŠ   r!   r!   r"   Ú_time_to_python@  s&    
zMySQLConverter._time_to_pythonc             C   s  t | tjƒr| S d}d}y¼| jdƒ\}}t|ƒdkrT|jdƒ\}}t|jddƒƒ}n|}d}dd	„ |jd
ƒD ƒdd	„ |jdƒD ƒ |g }t|ƒdk r®td|› dt|ƒ› �ƒ‚ytj|Ž }W n tk
rÐ   dS X W n. ttfk
�r   tt	j
| dd�ƒd‚Y nX |S )zâConverts 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!   )ra   )r­   r“   r!   r!   r"   r¯   |  s    z6MySQLConverter._datetime_to_python.<locals>.<listcomp>r    c             S   s   g | ]}t |ƒ‘qS r!   )ra   )r­   r“   r!   r!   r"   r¯   }  s    r°   zinvalid datetime format: z len: zdatetime.timedelta)r'   r³   )rU   r£   r¥   r�   ra   r´   r_   r¦   r[   r¶   rq   )r'   r›   Zdatetime_valr†   Zdate_Ztime_r·   Zdtvalr!   r!   r"   Ú_datetime_to_pythond  s0    &

z"MySQLConverter._datetime_to_pythonc             C   sJ   yt | ƒ}W n8 tk
rD } ztdt| ƒ› d�ƒ|‚W Y dd}~X nX |S )z#Returns YEAR column type as integerzFailed converting YEAR to int (r\   N)ra   r_   r§   )r'   r›   rr   r`   r!   r!   r"   Ú_year_to_python•  s
    (zMySQLConverter._year_to_pythonc             C   sj   d}|j | jƒ}|stƒ S yt|jdƒƒ}W n8 tk
rd } ztdt|ƒ› d�ƒ|‚W Y dd}~X nX |S )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 z to a sequence)r]   r   Úsetr¥   r_   r§   )r    r'   r›   Úset_typer˜   r`   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| j ƒS |d tj@ rP| j||ƒS |d dkr`|S t|t	t
fƒr–| jr–y|j| j ƒS  tk
r”   |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.
        rj   Nr   é   rœ   é?   )r   r   ZJSONr   r]   r   ZSETr¿   rU   rF   rV   r^   )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 | j||ƒS )z!Convert BLOB data type to Python.NrÀ   rœ   rÁ   )r   ZBLOBÚBINARYrF   rÂ   )r    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)N)Mr,   rB   rC   rD   r   r@   rE   r#   rG   r   r>   r
   rf   ra   r   r   rF   rA   r   r   r2   r   r   r<   rb   rc   rg   ri   rh   rl   rV   rm   rn   ro   r£   ry   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"   rH   ”   s¨     $!

 1


!
,



rH   )"rD   r£   rd   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   rH   r!   r!   r!   r"   Ú<module>   s   ( ]