3
(h{U                 @   s|   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 d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)
HexLiteralz.Could not convert '{value}' to python {pytype}c               @   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 )MySQLConverterBasezBase 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 | _| j| | j| i | _d S )Nr   )Zpython_typesZmysql_typescharset
charset_iduse_unicodeset_charsetset_unicode_cache_field_types)selfr   r    r   N/tmp/pip-install-q3hcpn_q/mysql-connector-python/mysql/connector/conversion.py__init__2   s    

zMySQLConverterBase.__init__c             C   s8   |dkrd}|dk	r|| _ nd| _ tj| j d | _dS )zSet character setZ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   G   s    zMySQLConverterBase.set_unicodec             C   s:   |j jj }yt| dj||S  tk
r4   |S X dS )z!Convert Python data type to MySQLz_{0}_to_mysqlN)	__class____name__lowergetattrformatAttributeError)r   r   	type_namer   r   r   to_mysqlK   s
    zMySQLConverterBase.to_mysqlc             C   s   |dks|dkr"|d t jkr"dS | jszi | _xJt jj D ]<\}}yt| dj|| j|d < W q: tk
rt   Y q:X q:W y| j|d  ||S  tk
r   |S X dS )z!Convert MySQL data type to Python    Nr   z_{0}_to_pythonr   )	r   BITr   descitemsr   r   r   KeyError)r   vtyper   nameinfor   r   r   	to_pythonS   s    
zMySQLConverterBase.to_pythonc             C   s   |S )z"Escape buffer for sending to MySQLr   )r   r   r   r   r   escapei   s    zMySQLConverterBase.escapec             C   s   t |S )z!Quote buffer for sending to MySQL)str)r   bufr   r   r   quotem   s    zMySQLConverterBase.quoteN)r   T)T)r   
__module____qualname____doc__r   r   r   r    r)   r*   r-   r   r   r   r   r   +   s   


r   c               @   sj  e Zd ZdZdFd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dGd.d/ZeZdHd0d1ZeZeZeZeZeZ dId2d3Z!e!Z"dJd4d5Z#dKd6d7Z$dLd8d9Z%e%Z&dMd:d;Z'dNd<d=Z(e(Z)dOd>d?Z*dPd@dAZ+dQdBdCZ,e,Z-e,Z.dRdDdEZ/e/Z0e/Z1e/Z2dS )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).

    NTc             C   s   t j| || i | _d S )N)r   r   r   )r   r   r   r   r   r   r      s    zMySQLConverter.__init__c             C   s   |dkr|S t |tr|S t |ttfrr|jdd}|jdd}|jdd}|jdd	}|jd
d}|jdd}nH|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.
        N   \s   \\   
s   \n   s   \r   's   \'   "s   \"   s   \\z\\
z\nz\r'z\'"z\"z\)
isinstancer   bytes	bytearrayreplace)r   r   r   r   r   r*      s$    
zMySQLConverter.escapec             C   s\   t |tr6tr(t |tr t|S t|S t|jdS t |tdrLt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   NULLr5   )	r>   r   r   floatreprr+   encodetyper@   )r   r,   r   r   r   r-      s    	

zMySQLConverter.quotec             C   sH   |j jj }yt| dj||S  tk
rB   tdj|Y nX dS )z!Convert Python data type to MySQLz_{0}_to_mysqlz0Python '{0}' cannot be converted to a MySQL typeN)r   r   r   r   r   r   	TypeError)r   r   r   r   r   r   r       s    zMySQLConverter.to_mysqlc             C   sD  |dkr|d t jkrdS |dkr&dS | js~i | _xJt jj D ]<\}}yt| dj|| j|d < W q> tk
rx   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 f W Y dd}~X nD tk
r2 } ztd||d f W Y dd}~X n    Y nX dS )z!Convert MySQL data type to Pythonr   r   Nz_{0}_to_pythonzutf-8z%s (field %s))r   r"   r   r#   r$   r   r   r   r%   decodeUnicodeDecodeError
ValueErrorrG   )r   r&   r   r'   r(   errr   r   r   r)      s2    


$$zMySQLConverter.to_pythonc             C   s   t |S )zConvert value to int)int)r   r   r   r   r   _int_to_mysql   s    zMySQLConverter._int_to_mysqlc             C   s   t |S )zConvert value to int)rL   )r   r   r   r   r   _long_to_mysql   s    zMySQLConverter._long_to_mysqlc             C   s   t |S )zConvert value to float)rC   )r   r   r   r   r   _float_to_mysql   s    zMySQLConverter._float_to_mysqlc             C   s   t rt|S | j|S )zConvert value to string)r   r+   _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   r2   )r   r   r   r   rE   Zslash_charsetsr
   )r   r   r   r   encodedr   r   r   rP      s    


z MySQLConverter._unicode_to_mysqlc             C   s   |S )zConvert value to bytesr   )r   r   r   r   r   _bytes_to_mysql   s    zMySQLConverter._bytes_to_mysqlc             C   s   t |S )zConvert value to bytes)r?   )r   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   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   r   _nonetype_to_mysql
  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}rB   z/{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d})	microsecondr   yearmonthdayhourminutesecondrE   )r   r   fmtr   r   r   _datetime_to_mysql  s    	z!MySQLConverter._datetime_to_mysqlc             C   s   dj |j|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:04d}-{1:02d}-{2:02d}rB   )r   rY   rZ   r[   rE   )r   r   r   r   r   _date_to_mysql)  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.%frB   z%H:%M:%S)rX   strftimerE   )r   r   r   r   r   _time_to_mysql5  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:%SrB   )timerb   rE   )r   r   r   r   r   _struct_time_to_mysqlB  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|||}	tr|	S |	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.
        iQ z{0:02d}:{1:02d}:{2:02d}.{3:06d}r   i@B r   z{0:02d}:{1:02d}:{2:02d}-i  <   rB   )absdayssecondsmicrosecondsdivmodr   r   rE   )
r   r   rj   r_   mcshours	remainderminssecsresultr   r   r   _timedelta_to_mysqlL  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.
        rB   N)r>   r   r+   rE   )r   r   r   r   r   _decimal_to_mysqln  s    
z MySQLConverter._decimal_to_mysqlc       
      C   sh  d}dgt | }| jsji | _xJtjj D ]<\}}yt| dj|| j|d < W q* tk
rd   Y q*X q*W x|D ]}|d }|| dkr|tjks|| dkr|d7 }qpy| j| || |||< W n t	k
r   y|| j
d||< W n" tk
r   || ||< Y nX Y n@ ttfk
rR }	 zdjt|	|d |	_ W Y dd}	~	X nX |d7 }qpW 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   Nz_{0}_to_pythonr   zutf-8z{0} (field {1}))lenr   r   r#   r$   r   r   r   r"   r%   rH   rI   rJ   rG   r+   messagetuple)
r   rowfieldsirr   r'   r(   fieldZ
field_typerK   r   r   r   row_to_pythonz  s6    	

"zMySQLConverter.row_to_pythonc             C   s   t |S )z.
        Returns value as float type.
        )rC   )r   r   r#   r   r   r   _FLOAT_to_python  s    zMySQLConverter._FLOAT_to_pythonc             C   s   t |S )z,
        Returns value as int type.
        )rL   )r   r   r#   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.
        )rH   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.
        )r+   )r   r   r#   r   r   r   _str  s    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   )ru   r	   )r   r   dscZint_valr   r   r   _BIT_to_python  s    zMySQLConverter._BIT_to_pythonc             C   s   t |tjr|S yj|jd}t|dkr<tdj|t|y&tjt|d t|d t|d S  tk
rv   dS X W n& ttfk
r   tdj|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.
           -   z#invalid datetime format: {} len: {}r   r      Nz2Could not convert {0} to python datetime.timedelta)	r>   datetimedatesplitru   rJ   r   rL   
IndexError)r   r   r   partsr   r   r   _DATE_to_python  s    
&
zMySQLConverter._DATE_to_pythonc             C   s   y"|j d\}}t|jdd}W n  ttfk
rB   |}d}Y nX yZdd |j dD \}}}|d dksx|d d	kr| | |   }}}tj||||d
S  tttfk
r   ttj	|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.time type.
           .      0r   c             S   s   g | ]}t |qS r   )rL   ).0dr   r   r   
<listcomp>  s    z2MySQLConverter._TIME_to_python.<locals>.<listcomp>   :-   rf   )rn   minutesrj   rk   zdatetime.timedelta)r   pytypeN)
r   rL   ljustrG   rJ   r   	timedeltar   CONVERT_ERRORr   )r   r   r   hmsrm   rn   rp   rq   r   r   r   _TIME_to_python  s    


zMySQLConverter._TIME_to_pythonc       	      C   s   t |tjr|S d}y|jd\}}t|dkrP|jd\}}t|jdd}n|}d}dd	 |jd
D dd	 |jdD  |g }t|dk rtdj|t|n$ytj| }W n tk
r   dS X W n* tt	fk
r   tt
j|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   )rL   )r   rz   r   r   r   r     s    z6MySQLConverter._DATETIME_to_python.<locals>.<listcomp>r   c             S   s   g | ]}t |qS r   )rL   )r   rz   r   r   r   r     s    r   z#invalid datetime format: {} len: {}zdatetime.timedelta)r   r   )r>   r   r   ru   rL   r   rJ   r   r   rG   r   )	r   r   r   Zdatetime_valZdate_Ztime_r   rm   Zdtvalr   r   r   _DATETIME_to_python  s,    .
z"MySQLConverter._DATETIME_to_pythonc             C   s2   yt |}W n  tk
r,   td| Y nX |S )z#Returns YEAR column type as integerz"Failed converting YEAR to int (%s))rL   rJ   )r   r   r#   rY   r   r   r   _YEAR_to_python.  s
    zMySQLConverter._YEAR_to_pythonc             C   sR   d}|j | j}|st S yt|jd}W n  tk
rL   td| 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 %s to a sequence.)rH   r   setr   rJ   )r   r   r   set_typer   r   r   r   _SET_to_python7  s    zMySQLConverter._SET_to_pythonc             C   s   |dk	rr|d t j@ r"| j||S |d t j@ rr| jdkrnt|t rny|j| jS  tt	fk
rj   |S X n|S | jdkr|S t|t
tfr| jr|j| 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.
        N   rR   )r   ZSETr   BINARYr   r>   r+   rH   LookupErrorrI   r?   r@   r   )r   r   r   r   r   r   _STRING_to_pythonI  s    
z MySQLConverter._STRING_to_pythonc             C   s8   |dk	r,|d t j@ r,|d t j@ r,t|S | j||S )z!Convert BLOB data type to Python.Nr   )r   ZBLOBr   r?   r   )r   r   r   r   r   r   _BLOB_to_pythong  s    zMySQLConverter._BLOB_to_python)NT)N)N)N)N)N)N)N)N)N)N)N)N)3r   r.   r/   r0   r   r*   r-   r    r)   rM   rN   rO   rQ   rP   rT   rU   rV   rW   r`   ra   rc   re   rs   rt   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   Z_VAR_STRING_to_pythonZ_JSON_to_pythonr   Z_LONG_BLOB_to_pythonZ_MEDIUM_BLOB_to_pythonZ_TINY_BLOB_to_pythonr   r   r   r   r1   r   s`   
	!

".







'
	


r1   )r0   r   rd   decimalr   	constantsr   r   r   Zcatch23r   r   r	   Zcustom_typesr
   r   objectr   r1   r   r   r   r   <module>   s   G