
    "iT                         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
 ddlmZ ddlmZ dZ G d	 d
e          Z G d de          ZdS )z"Converting MySQL and Python types
    N)Decimal   )	FieldType	FieldFlagCharacterSet)NUMERIC_TYPES)
HexLiteralz.Could not convert '{value}' to python {pytype}c                   @    e Zd ZdZddZd ZddZd Zd	 Zd
 Z	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.
    utf8TFc                     d | _         d | _        d | _        d| _        d | _        |                     |           || _        || _        i | _        d S )Nr   )python_typesmysql_typescharset
charset_iduse_unicodeset_charsetstr_fallback_cache_field_typesselfr   r   r   s       R/srv/django_bis/venv311/lib/python3.11/site-packages/mysql/connector/conversion.py__init__zMySQLConverterBase.__init__4   sZ     !!!&("$    c                     |dk    rd}||| _         nd| _         t          j        | j                   d         | _        dS )zSet character setutf8mb4r   Nr   )r   r   get_charset_infor   )r   r   s     r   r   zMySQLConverterBase.set_charset?   sG    iG"DLL "DL&7EEaHr   c                     || _         dS )zSet whether to use UnicodeN)r   r   values     r   set_unicodezMySQLConverterBase.set_unicodeJ   s     r   c                     |j         j                                        }	  t          | d                    |                    |          S # t
          $ r |cY S w xY w)!Convert Python data type to MySQL_{0}_to_mysql)	__class____name__lowergetattrformatAttributeErrorr   r    	type_names      r   to_mysqlzMySQLConverterBase.to_mysqlN   si    O,2244		C74!7!7	!B!BCCEJJJ 	 	 	LLL	s   +A AAc                    |dk    s||d         t           j        k    rdS | j        smi | _        t           j                                        D ]G\  }}	 t          | d                    |                    | j        |d         <   8# t          $ r Y Dw xY w	  | j        |d                  ||          S # t          $ r |cY S w xY w)!Convert MySQL data type to Python    Nr   _{0}_to_pythonr   )	r   BITr   descitemsr(   r)   r*   KeyError)r   vtyper    nameinfos        r   	to_pythonzMySQLConverterBase.to_pythonV   s     W58y}3L3L4& 	&(D#'n2244  
d7>.55d;;8= 8=D+DG44%   D	44*584UEBBB 	 	 	LLL	s$   1B
BBB3 3CCc                     |S )z"Escape buffer for sending to MySQL r   s     r   escapezMySQLConverterBase.escapel       r   c                      t          |          S )z!Quote buffer for sending to MySQLstrr   bufs     r   quotezMySQLConverterBase.quotep   s    3xxr   N)r   TF)T)r&   
__module____qualname____doc__r   r   r!   r-   r9   r<   rC   r;   r   r   r   r   -   s         	% 	% 	% 	%	I 	I 	I! ! ! !    ,      r   r   c                   *   e Zd ZdZd&dZd Zd Zd Zd	 Zd
 Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd'dZeZd'dZeZeZeZeZeZ d'dZ!e!Z"d'dZ#d'dZ$d'dZ%e%Z&d'd Z'd'd!Z(e(Z)d'd"Z*d'd#Z+d'd$Z,e,Z-e,Z.d'd%Z/e/Z0e/Z1e/Z2d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).

    NTFc                 N    t                               | |||           i | _        d S N)r   r   r   r   s       r   r   zMySQLConverter.__init__   s*    ##D';MMM"$r   c                    ||S t          |t                    r|S t          |t          t          f          r|                    dd          }|                    dd          }|                    dd          }|                    dd	          }|                    d
d          }|                    dd          }n|                    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   s     r   r<   zMySQLConverter.escape   s'    =L}-- 	LeeY/00 	6MM%11EMM%00EMM%00EMM';77EMM';77EMM';77EEMM$//EMM$..EMM$..EMM&*55EMM&*55EMM&*55Er   c                     t          |t                    r"t          |                              d          S t          |t	          d                    rt          d          S t          d|z   dz             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   NULLrO   )rX   r   r@   encodetyperZ   rA   s     r   rC   zMySQLConverter.quote   sk     c=)) 	&s88??7+++T$ZZ(( 	&W%%%d*+++r   c                 L   |j         j                                        }	  t          | d                    |                    |          S # t
          $ rM | j        r#t          |                                          cY S t          d                    |                    w xY w)r#   r$   z0Python '{0}' cannot be converted to a MySQL type)
r%   r&   r'   r(   r)   r*   r   r@   r^   	TypeErrorr+   s      r   r-   zMySQLConverter.to_mysql   s    O,2244		<C74!7!7	!B!BCCEJJJ 	< 	< 	<  +5zz((***** ))/	):):< < <	<s   +A 2B# #B#c                    |dk    r|d         t           j        k    rdS |dS | j        smi | _        t           j                                        D ]G\  }}	 t          | d                    |                    | j        |d         <   8# t          $ r Y Dw xY w	  | j        |d                  ||          S # t          $ r- 	 |	                    d          cY S # t          $ r |cY cY S w xY wt          $ r }t          |d|d         d          d}~wt          $ r }t          |d|d         d          d}~w  xY w)r/   r   r   Nr1   utf-8z (field ))r   r2   r   r3   r4   r(   r)   r*   r5   decodeUnicodeDecodeError
ValueErrorra   )r   r6   r    r7   r8   errs         r   r9   zMySQLConverter.to_python   s   A::%(im334=4& 	&(D#'n2244  
d7>.55d;;8= 8=D+DG44%   D	44*584UEBBB 	 	 	||G,,,,,%    	@ 	@ 	@U1XXX>??? 	? 	? 	?sssE!HHH=>>>	sZ   1B
BBB5 5
D= CD=C(#D='C((D=4DD=D77D=c                      t          |          S zConvert value to intintr   s     r   _int_to_mysqlzMySQLConverter._int_to_mysql       5zzr   c                      t          |          S rj   rk   r   s     r   _long_to_mysqlzMySQLConverter._long_to_mysql   rn   r   c                 L    t          j        |          rdS t          |          S )zConvert value to floatN)mathisnanfloatr   s     r   _float_to_mysqlzMySQLConverter._float_to_mysql   s%    :e 	4U||r   c                 ,    |                      |          S )zConvert value to string)_unicode_to_mysqlr   s     r   _str_to_mysqlzMySQLConverter._str_to_mysql   s    %%e,,,r   c                     | j         }| j        }|dk    rd}t          j        |          d         }|                    |          }|t          j        v rd|v rt          ||          S |S )zConvert unicodebinaryr   r   rL   )r   r   r   r   r^   slash_charsetsr	   )r   r    r   r   encodeds        r   rw   z MySQLConverter._unicode_to_mysql   st    ,_
hG%6w??BJ,,w''444'!!!%111r   c                     |S zConvert value to bytesr;   r   s     r   _bytes_to_mysqlzMySQLConverter._bytes_to_mysql   r=   r   c                      t          |          S r~   )rY   r   s     r   _bytearray_to_mysqlz"MySQLConverter._bytearray_to_mysql  s    U||r   c                     |rdS dS )zConvert value to booleanr   r   r;   r   s     r   _bool_to_mysqlzMySQLConverter._bool_to_mysql  s     	1qr   c                     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   s     r   _nonetype_to_mysqlz!MySQLConverter._nonetype_to_mysql  s	     tr   c           	      P   |j         rSd}|                    |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})	microsecondr)   yearmonthdayhourminutesecondr^   )r   r    fmts      r   _datetime_to_mysqlz!MySQLConverter._datetime_to_mysql  s      	3KC::
EK
EL%,!# # $*6'??3
 @zzJUYJel4 44:F7OO	Dr   c                 t    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:04d}-{1:02d}-{2:02d}r]   )r)   r   r   r   r^   r   s     r   _date_to_mysqlzMySQLConverter._date_to_mysql*  s5     )//
EK05	; ;;A6'??	Kr   c                     |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   strftimer^   r   s     r   _time_to_mysqlzMySQLConverter._time_to_mysql6  sP      	A>>-0077@@@~~j))00999r   c                 R    t          j        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   r^   r   s     r   _struct_time_to_mysqlz$MySQLConverter._struct_time_to_mysqlC  s%     }0%88??HHHr   c                    t          |j        dz  |j        z             }|j        r%d}|j        dk     rd|j        z
  }|dz  }n
|j        }nd}|j        dk     rd|z   }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}-i  <   r]   )absdayssecondsmicrosecondsdivmodr)   r^   )
r   r    r   r   mcshours	remainderminssecsresults
             r   _timedelta_to_mysqlz"MySQLConverter._timedelta_to_mysqlM  s     ej5(5=899 	,3CzA~~ 221(+C:>>)C#GT22	i,,t 	3ZZtT377FFZZtT22F}}W%%%r   c                 t    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)rX   r   r@   r^   r   s     r   _decimal_to_mysqlz MySQLConverter._decimal_to_mysqlm  s5     eW%% 	.u::$$W---tr   c                 
   d}dgt          |          z  }| j        smi | _        t          j                                        D ]G\  }}	 t          | d                    |                    | j        |d         <   8# t          $ r Y Dw xY w|D ]}|d         }||         dk    r|t          j        k    s||         |dz  }4	  | j        |         ||         |          ||<   n# t          $ r> 	 ||         
                    d          ||<   n# t          $ r ||         ||<   Y nw xY wY nGt          t          f$ r4}	d                    t          |	          |d                   |	_         d}	~	ww xY w|dz  }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   Nr1   r   rc   z{0} (field {1}))lenr   r   r3   r4   r(   r)   r*   r2   r5   re   rf   rg   ra   r@   messagetuple)
r   rowfieldsir   r7   r8   field
field_typerh   s
             r   row_to_pythonzMySQLConverter.row_to_pythony  s    F#& 	&(D#'n2244  
d7>.55d;;8= 8=D+DG44%   D  	 	EqJA!
im ; ;AQ
?D3J?ANNq		 ' ' '' #Ag 6 6F1II) ' ' ' #AF1III'	*   /66s3xxqJJ FAAV}}sT   1A::
BB C""
E--DE-D$!E-#D$$E-)E-9/E((E-c                      t          |          S )z.
        Returns value as float type.
        )rt   r   r    r3   s      r   _FLOAT_to_pythonzMySQLConverter._FLOAT_to_python  s     U||r   c                      t          |          S )z,
        Returns value as int type.
        rk   r   s      r   _INT_to_pythonzMySQLConverter._INT_to_python       5zzr   c                 T    |                     | j                  }t          |          S )z5
        Returns value as a decimal.Decimal.
        )re   r   r   )r   r    r3   vals       r   _DECIMAL_to_pythonz!MySQLConverter._DECIMAL_to_python  s#     ll4<((s||r   c                      t          |          S )z,
        Returns value as str type.
        r?   r   s      r   _strzMySQLConverter._str  r   r   c                     |}t          |          dk     rddt          |          z
  z  |z   }t          j        d|          d         S )z!Returns BIT columntype as integer   r0   z>Qr   )r   structunpack)r   r    dscint_vals       r   _BIT_to_pythonzMySQLConverter._BIT_to_python  sI    w<<!S\\!12W<G}T7++A..r   c                    t          |t          j                  r|S 	 |                    d          }t	          |          dk    r0t          d                    |t	          |                              	 t          j        t          |d                   t          |d                   t          |d                             S # t
          $ r Y dS w xY w# t          t
          f$ r# t          d                    |                    w xY w)	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      Nz2Could not convert {0} to python datetime.timedelta)	rX   datetimedatesplitr   rg   r)   rl   
IndexError)r   r    r   partss       r   _DATE_to_pythonzMySQLConverter._DATE_to_python  s    eX]++ 	L	KK%%E5zzQ  "$$*F5#e**$=$=? ? ?}Sq]]CaMM3uQx==QQQ   ttJ' 	 	 	DKK   	s+   AC 7AC 
CC CC 4Dc                    	 |                     d          \  }}t          |                    dd                    }n# t          t          f$ r |}d}Y nw xY w	 d |                     d          D             \  }}}|d         dk    s|d         dk    r	| | | }}}t          j        ||||	          S # t          t          t          f$ r* t	          t          	                    |d
                    w xY w)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                 ,    g | ]}t          |          S r;   rk   ).0ds     r   
<listcomp>z2MySQLConverter._TIME_to_python.<locals>.<listcomp>  s    "C"C"Ca3q66"C"C"Cr      :-   r   )r   minutesr   r   datetime.timedeltar    pytype)
r   rl   ljustra   rg   r   	timedeltar   CONVERT_ERRORr)   )r   r    r   hmsr   r   r   r   s           r   _TIME_to_pythonzMySQLConverter._TIME_to_python  s=   	T**JS#cii4(())CC:& 	 	 	CCCC		P"C"C399T??"C"C"CUD$Qx2~~qS#'%$Cd%E4.2F F F FIz2 	P 	P 	P]119M 2 O O P P P	Ps   ;> AAAB6 6AC7c                    t          |t          j                  r|S d}	 |                    d          \  }}t          |          dk    r<|                    d          \  }}t	          |                    dd                    }n|}d}d |                    d	          D             d
 |                    d          D             z   |gz   }t          |          dk     r0t          d                    |t          |                              	 t          j        | }n# t          $ r Y dS w xY wn># t          t          f$ r* t          t                              |d                    w xY w|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                 ,    g | ]}t          |          S r;   rk   r   r   s     r   r   z6MySQLConverter._DATETIME_to_python.<locals>.<listcomp>  s    777SVV777r   r   c                 ,    g | ]}t          |          S r;   rk   r   s     r   r   z6MySQLConverter._DATETIME_to_python.<locals>.<listcomp>  s    555SVV555r   r   r   r   r   )rX   r   r   r   rl   r   rg   r)   r   ra   r   )	r   r    r   datetime_valdate_time_r   r   dtvals	            r   _DATETIME_to_pythonz"MySQLConverter._DATETIME_to_python  s    eX.// 	L	P"[[..NUE5zzA~~"[[..
c#))At,,--77U[[%6%677755SYYt__55569<@E5zzA~~  "$$*F5#e**$=$=? ? ? #+#4e#<LL!      44  ! I& 	P 	P 	P]119M 2 O O P P P	P s0   C1D3 D!  D3 !
D/+D3 .D//D3 3;E.c                 h    	 t          |          }n # t          $ r t          d|z            w xY w|S )z#Returns YEAR column type as integerz"Failed converting YEAR to int (%s))rl   rg   )r   r    r3   r   s       r   _YEAR_to_pythonzMySQLConverter._YEAR_to_python-  sO    	Ku::DD 	K 	K 	KAEIJJJ	K s    /c                     d}|                     | j                  }|st                      S 	 t          |                    d                    }n # t          $ r t	          d|z            w xY w|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.)re   r   setr   rg   )r   r    r   set_typer   s        r   _SET_to_pythonzMySQLConverter._SET_to_python6  s     ll4<(( 	55L	P399S>>**HH 	P 	P 	PFNOOO	Ps   "A A.c                    | j         dk    r|S |p|d         t          j        k    r!| j        r|                    | j                   S |d         t
          j        z  r|                     ||          S |d         dk    r|S t          |t          t          f          r4| j        r-	 |                    | j                   S # t          $ r |cY S w xY w|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.
        rz   Nr      r   ?   )r   r   JSONr   re   r   SETr   rX   rY   rZ   rf   r   r    r   s      r   _STRING_to_pythonz MySQLConverter._STRING_to_pythonH  s     <8##L?1v''D,<'||DL1111v	% 7**5#6661v||eeY/00 	T5E 	||DL111%    s   $B> >CCc                     |9|d         t           j        z  r$|d         t           j        z  rt          |          S |                     ||          S )z!Convert BLOB data type to Python.Nr   )r   BLOBBINARYrY   r   r   s      r   _BLOB_to_pythonzMySQLConverter._BLOB_to_pythonc  sO    ?1v	& $3q6I4D+D $U||#%%eS111r   )NTFrJ   )3r&   rD   rE   rF   r   r<   rC   r-   r9   rm   rp   ru   rx   rw   r   r   r   r   r   r   r   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   _VAR_STRING_to_python_JSON_to_pythonr   _LONG_BLOB_to_python_MEDIUM_BLOB_to_python_TINY_BLOB_to_pythonr;   r   r   rH   rH   u   s        % % % %  8, , ,	< 	< 	<  B      - - -          D D D*
K 
K 
K: : :I I I& & &@
 
 
, , ,\    )    %O%%$O(    /   / / / /   . )P P P P4% % % %N /      $   0 .'O2 2 2 2 +,*r   rH   )rF   r   rr   r   r   decimalr   	constantsr   r   r   utilsr   custom_typesr	   r   objectr   rH   r;   r   r   <module>r     s   :            9 9 9 9 9 9 9 9 9 9             $ $ $ $ $ $@E E E E E E E EPw+ w+ w+ w+ w+' w+ w+ w+ w+ w+r   