U Yzf_U@s|dZddlZddlZddlmZddlmZmZmZddl m Z m Z m Z ddl mZdZGd d d eZGd d d eZdS) 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@sLeZdZdZdddZddZddd Zd d Zd d ZddZ ddZ dS)MySQLConverterBasezBase class for conversion classes All class dealing with converting to and from MySQL data types must be a subclass of this class. utf8TcCs<d|_d|_d|_d|_d|_||||i|_dS)Nr)Z python_typesZ mysql_typescharset charset_id use_unicode set_charset set_unicode_cache_field_typesselfr rrr/private/var/folders/n9/53xbvtmd7sjg1q1l55xmpg58n4mgng/T/pip-unpacked-wheel-47tvuv7y/mysql/connector/conversion.py__init__2s  zMySQLConverterBase.__init__cCs8|dkr d}|dk r||_nd|_t|jd|_dS)zSet character setZutf8mb4r Nr)r rget_charset_infor)rr rrrr<s zMySQLConverterBase.set_charsetcCs ||_dS)zSet whether to use UnicodeN)rrvaluerrrrGszMySQLConverterBase.set_unicodecCs@|jj}zt|d||WStk r:|YSXdS)!Convert Python data type to MySQL _{0}_to_mysqlN) __class____name__lowergetattrformatAttributeErrorrrZ type_namerrrto_mysqlKs  zMySQLConverterBase.to_mysqlc Cs|dks|dkr"|dtjkr"dS|jsvi|_tjD]<\}}zt|d||j|d<Wq8tk rrYq8Xq8z|j|d||WStk r|YSXdS)!Convert MySQL data type to PythonNr_{0}_to_pythonr) rBITrdescitemsr r!r"KeyError)rvtypernameinforrr to_pythonSs zMySQLConverterBase.to_pythoncCs|S)z"Escape buffer for sending to MySQLrrrrrescapeiszMySQLConverterBase.escapecCst|S)z!Quote buffer for sending to MySQLstrrbufrrrquotemszMySQLConverterBase.quoteN)r T)T) r __module__ __qualname____doc__rrrr$r/r0r5rrrrr +s  r c@sjeZdZdZdFddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-ZdGd.d/ZeZdHd0d1ZeZeZeZeZeZ dId2d3Z!e!Z"dJd4d5Z#dKd6d7Z$dLd8d9Z%e%Z&dMd:d;Z'dNdd?Z*dPd@dAZ+dQdBdCZ,e,Z-e,Z.dRdDdEZ/e/Z0e/Z1e/Z2dS)SMySQLConverteraDefault 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). NTcCst|||i|_dS)N)r rrrrrrrszMySQLConverter.__init__cCs|dkr |St|tr|St|ttfrr|dd}|dd}|dd}|dd }|d d }|d d }nH|dd}|dd}|dd}|dd}|dd}|dd}|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\) isinstancerbytes bytearrayreplacerrrrr0s$            zMySQLConverter.escapecCs\t|tr6tr(t|tr t|St|St|dSt|tdrLtdStd|dS)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 '' Returns a bytearray object. asciiNsNULLr=) rFrrfloatreprr2encodetyperHr3rrrr5s  zMySQLConverter.quotecCsJ|jj}zt|d||WStk rDtd|YnXdS)rrz0Python '{0}' cannot be converted to a MySQL typeN)rrrr r!r" TypeErrorr#rrrr$s zMySQLConverter.to_mysqlc CsP|dkr|dtjkrdS|dkr&dS|jszi|_tjD]<\}}zt|d||j|d<Wq<tk rvYq}zt d||dfW5d}~XYnYnXdS)r%rrNr'utf-8z %s (field %s)) rr(rr)r*r r!r"r+decodeUnicodeDecodeError ValueErrorrO)rr,rr-r.errrrrr/s6$$zMySQLConverter.to_pythoncCst|SzConvert value to intintrrrr _int_to_mysqlszMySQLConverter._int_to_mysqlcCst|SrUrVrrrr_long_to_mysqlszMySQLConverter._long_to_mysqlcCst|S)zConvert value to floatrKrrrr_float_to_mysqlszMySQLConverter._float_to_mysqlcCstr t|S||S)zConvert value to string)rr2_unicode_to_mysqlrrrr _str_to_mysqlszMySQLConverter._str_to_mysqlcCsP|j}|j}|dkr&d}t|d}||}|tjkrLd|krLt||S|S)zConvert unicodebinaryr rr:)r rrrrMZslash_charsetsr )rrr rencodedrrrr\s   z MySQLConverter._unicode_to_mysqlcCs|SzConvert value to bytesrrrrr_bytes_to_mysqlszMySQLConverter._bytes_to_mysqlcCst|Sr`)rGrrrr_bytearray_to_mysqlsz"MySQLConverter._bytearray_to_mysqlcCs |rdSdS)zConvert value to booleanrrrrrrr_bool_to_mysqlszMySQLConverter._bool_to_mysqlcCsdS)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. Nrrrrr_nonetype_to_mysql sz!MySQLConverter._nonetype_to_mysqlc Cs^|jr4d}||j|j|j|j|j|j|jdSd}||j|j|j|j|j|jdS)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}rJz-{0:d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}) microsecondr!yearmonthdayhourminutesecondrM)rrfmtrrr_datetime_to_mysqls0 z!MySQLConverter._datetime_to_mysqlcCsd|j|j|jdS)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}rJ)r!rfrgrhrMrrrr_date_to_mysql)s zMySQLConverter._date_to_mysqlcCs&|jr|ddS|ddS)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.%frJz%H:%M:%S)restrftimerMrrrr_time_to_mysql5s zMySQLConverter._time_to_mysqlcCstd|dS)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:%SrJ)timerorMrrrr_struct_time_to_mysqlBsz$MySQLConverter._struct_time_to_mysqlc Cst|jd|j}|jrDd}|jdkrQr)rr )rrdscZint_valrrr_BIT_to_pythons zMySQLConverter._BIT_to_pythonc Cst|tjr|Szr|d}t|dkrsz2MySQLConverter._TIME_to_python..:-rs)r{minutesrwrxdatetime.timedeltarZpytypeN) rrWljustrOrSr timedeltar CONVERT_ERRORr!)rrrhmsrzr{r}r~rrr_TIME_to_pythons$  zMySQLConverter._TIME_to_pythonc Cst|tjr|Sd}z|d\}}t|dkrP|d\}}t|dd}n|}d}dd |d Dd d |d D|g}t|dkrtd |t|n(ztj|}Wntk rYWdSXWn,tt fk rtt j|ddYnX|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 rrrrrcSsg|] }t|qSrrVrrrrrrsz6MySQLConverter._DATETIME_to_python..rcSsg|] }t|qSrrVrrrrrsrrrr) rFrrrrWrrSr!rrOr) rrrZ datetime_valZdate_Ztime_rrzZdtvalrrr_DATETIME_to_pythons:   z"MySQLConverter._DATETIME_to_pythoncCs2z t|}Wn tk r,td|YnX|S)z#Returns YEAR column type as integerz"Failed converting YEAR to int (%s))rWrS)rrr)rfrrr_YEAR_to_python.s  zMySQLConverter._YEAR_to_pythoncCsRd}||j}|stSzt|d}Wn tk rLtd|YnX|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.)rQr setrrS)rrrset_typerrrr_SET_to_python7s zMySQLConverter._SET_to_pythonc Cs|dk rv|dtj@r"|||S|dtj@rv|jdkrrt|tsrz||jWStt fk rn|YSXn|S|jdkr|St|t t fr|j r||jS|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^) rZSETrBINARYr rFr2rQ LookupErrorrRrGrHrrrrrrr_STRING_to_pythonIs    z MySQLConverter._STRING_to_pythoncCs8|dk r,|dtj@r,|dtj@r,t|S|||S)z!Convert BLOB data type to Python.Nr)rZBLOBrrGrrrrr_BLOB_to_pythongszMySQLConverter._BLOB_to_python)NT)N)N)N)N)N)N)N)N)N)N)N)N)3rr6r7r8rr0r5r$r/rXrYr[r]r\rarbrcrdrmrnrprrrrrrZ_DOUBLE_to_pythonrZ_TINY_to_pythonZ_SHORT_to_pythonZ_INT24_to_pythonZ_LONG_to_pythonZ_LONGLONG_to_pythonrZ_NEWDECIMAL_to_pythonrrrZ_NEWDATE_to_pythonrrZ_TIMESTAMP_to_pythonrrrZ_VAR_STRING_to_pythonZ_JSON_to_pythonrZ_LONG_BLOB_to_pythonZ_MEDIUM_BLOB_to_pythonZ_TINY_BLOB_to_pythonrrrrr9rs`  !     " .        '   r9)r8rrqdecimalr constantsrrrZcatch23rrr Z custom_typesr robjectr r9rrrrs  G