
    g                    P   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZmZmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZ ddlmZm Z m!Z!m"Z" ddl#m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 erddl1m2Z2 dZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ: G d d          Z;dS )z,Implements the MySQL Client/Server protocol.    )annotationsN)deque)DecimalDecimalException)	TYPE_CHECKINGAnyDequeDictListOptionalSequenceTupleUnion   )utils)PARAMETER_COUNT_AVAILABLE
ClientFlag	FieldFlag	FieldType	ServerCmd)MySQLConverter)DatabaseErrorInterfaceErrorProgrammingErrorget_exception)logger)MySQLAuthPluginget_auth_plugin)"MySQLCachingSHA2PasswordAuthPlugin)BinaryProtocolTypeDescriptionTypeEofPacketTypeHandShakeTypeOkPacketTypeStatsPacketType
StrOrBytes)MySQLSocket
            -   i   @c                     e Zd ZdZedtd            Zedud            Zedud            Zedvd            Zedwd            Z	e	 	 	 dxdyd            Z
edededdddddf
dzd&            Zeedefd{d'            Zed|d}d+            Zed~dd/            Zedd1            Zedd3            Zedd5            Zeddd9            Zdd;Zeddd?            Z	 	 dddGZeddJ            ZeddL            Ze	 dddO            ZeddR            ZeddT            Z	 dddXZ	 	 	 ddd[Zedd]            Zedd`            Z eddb            Z!eddd            Z"eddh            Z#	 	 	 	 	 	 	 dddsZ$dS )MySQLProtocolzRImplements MySQL client/server protocol

    Create and parses MySQL packets.
    pktbytesreturnc                V    | d         t           k    st          d          | dd         S )a  Parse a MySQL auth more data packet.

        Args:
            pkt: Packet representing an `auth more data` response.

        Returns:
            auth_data: Authentication method data (see [1]).

        Raises:
            InterfaceError: If packet's status tag doesn't
                            match `protocol.EXCHANGE_FURTHER_STATUS`.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_auth_more_data.html
           z"Failed parsing AuthMoreData packet   N)EXCHANGE_FURTHER_STATUSr   )r/   s    d/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/mysql/connector/protocol.pyparse_auth_more_dataz"MySQLProtocol.parse_auth_more_data\   s0    $ 1v000 !EFFF122w    Tuple[str, bytes]c                    | d         t           k    rt          d          t          j        | dd         d          \  } }| r| d         dk    r
| dd         } |                                | fS )	a|  Parse a MySQL auth switch request packet.

        Args:
            pkt: Packet representing an `auth switch request` response.

        Returns:
            plugin_name: Name of the client authentication plugin to switch to.
            plugin_provided_data: Plugin provided data (see [1]).

        Raises:
            InterfaceError: If packet's status tag doesn't
                            match `protocol.AUTH_SWITCH_STATUS`.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/                latest/page_protocol_connection_phase_packets_protocol_
                auth_switch_request.html
        r3   z'Failed parsing AuthSwitchRequest packetr4   N    endr   )AUTH_SWITCH_STATUSr   r   read_stringdecode)r/   plugin_names     r6   parse_auth_switch_requestz'MySQLProtocol.parse_auth_switch_requestr   s}    ( q6''' !JKKK ,SW'BBB[ 	3r7a<<crc(C!!##S((r8   c                    t          j        | dd         d          \  } }|t          k    rt          d          t          j        | d          \  } }|                                | fS )a  Parse a MySQL auth next factor packet.

        Args:
            pkt: Packet representing an `auth next factor` response.

        Returns:
            plugin_name: Name of the client authentication plugin.
            plugin_provided_data: Initial authentication data for that
                                  client plugin (see [1]).

        Raises:
            InterfaceError: If packet's packet type doesn't
                            match `protocol.MFA_STATUS`.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_auth_                next_factor_request.html
        r3   Nr   z.Failed parsing AuthNextFactor packet (invalid)r;   r<   )r   read_int
MFA_STATUSr   r@   rA   )r/   statusrB   s      r6   parse_auth_next_factorz$MySQLProtocol.parse_auth_next_factor   sl    * nSWa00VZ !QRRR ,Sg>>>[!!##S((r8   
conn_attrsDict[str, str]c           
          D ]} |         d |<   t           fd D                       t                                                     z   t                                                     z   }t	          j        d|          g} D ]}|                    t	          j        dt          |                               |                    |                                           |                    t	          j        dt           |                                        |                     |                                                    d                    |          S )az  Encode the connection attributes.

        Args:
            conn_attrs: Connection attributes.

        Returns:
            serialized_conn_attrs: Serialized connection attributes as per [1].

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_handshake_response.html
        N c              3  b   K   | ])}t          |          t          |                   z   V  *d S N)len).0xrI   s     r6   	<genexpr>z0MySQLProtocol.make_conn_attrs.<locals>.<genexpr>   s:      @@AZ]+++@@@@@@r8   <Br8   )	sumrO   keysvaluesstructpackappendencodejoin)rI   	attr_nameconn_attrs_lenconn_attrs_packets   `   r6   make_conn_attrszMySQLProtocol.make_conn_attrs   s[    $ 	+ 	+I)$,(*
9%@@@@Z@@@@@*//##$$%*##%%&&' 	 $[~>>?# 	E 	EI$$V[s9~~%F%FGGG$$Y%5%5%7%7888$$V[s:i;P7Q7Q%R%RSSS$$Z	%:%A%A%C%CDDDDxx)***r8   client_flagsintdatabaseOptional[str]c                V    | t           j        z  r|r|                                dz   ndS )a  Prepare database string for handshake response.

        Args:
            client_flags: Integer representing client capabilities flags.
            database: Initial database name for the connection.

        Returns:
            serialized_database: Serialized database name as per [1].

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_handshake_response.html
        r;   )r   CONNECT_WITH_DBrZ   )r`   rb   s     r6   connect_with_dbzMySQLProtocol.connect_with_db   s8    " j88=EHOO''	
r8   NF	auth_datausernamestrpasswordauth_pluginauth_plugin_classssl_enabledboolplugin_configOptional[Dict[str, Any]]Tuple[bytes, MySQLAuthPlugin]c                t   |s|dk    rdt          |||          fS |i }	  t          ||          |||          } |j        | fi |}n,# t          t          f$ r}	t	          d|	           |	d}	~	ww xY w|t	          d|j                   t          j        t          |                    |z   }||fS )a  Prepare the first authentication response.

        Args:
            auth_data: Authorization data from initial handshake.
            username: Account's username.
            password: Account's password.
            client_flags: Integer representing client capabilities flags.
            auth_plugin: Authorization plugin name.
            auth_plugin_class: Authorization plugin class (has higher precedence
                               than the authorization plugin name).
            ssl_enabled: Whether SSL is enabled or not.
            plugin_config: Custom configuration to be passed to the auth plugin
                           when invoked. The parameters defined here will override
                           the ones defined in the auth plugin itself.

        Returns:
            auth_response: Authorization plugin response.
            auth_strategy: Authorization plugin instance created based
                           on the provided `auth_plugin` and `auth_plugin_class`
                           parameters.

        Raises:
            InterfaceError: If authentication fails or when got a NULL auth response.
        rL   r;   )rm   NzFailed authentication: z8Got NULL auth response while authenticating with plugin )	r   r   auth_response	TypeErrorr   namer   lc_intrO   )
rg   rh   rj   rk   rl   rm   ro   auth_strategyrs   errs
             r6   auth_plugin_first_responsez(MySQLProtocol.auth_plugin_first_response   s/   D  	K2-->(      M	KKOK9JKK(  M 8M7	SS]SSMM>* 	K 	K 	K !@3!@!@AAsJ	K   /',/ /  
 S%7%788=Hm++s   *A A6A11A6r   	handshaker#   charsetmax_allowed_packetOptional[Dict[str, str]]is_change_user_requestc           	        |                                 }g }|
rt          j        d           t          j        d           | t          d          d|                     d          t          d          d	 |p| d         }n-# t
          t          f$ r}t          d| d	          dd}~ww xY wt          j        d
|           |
rE|                    t          j	        dt          |           dt          j        |                     n@d}|                    t          j	        d| t          |           d||||                     t                              | d         ||||||          \  }}|                    |           |                    t                              ||                     |
r(|                    t          j	        d|                     |t           j        z  r*|                    |                                 dz              |t           j        z  r/|	-|                    t                              |	                     d                    |          |fS )a  Make a MySQL Authentication packet.

        Args:
            handshake: Initial handshake.
            username: Account's username.
            password: Account's password.
            database: Initial database name for the connection
            charset: Client charset (see [2]), only the lower 8-bits.
            client_flags: Integer representing client capabilities flags.
            max_allowed_packet: Maximum packet size.
            auth_plugin: Authorization plugin name.
            auth_plugin_class: Authorization plugin class (has higher precedence
                               than the authorization plugin name).
            conn_attrs: Connection attributes.
            is_change_user_request: Whether is a `change user request` operation or not.
            ssl_enabled: Whether SSL is enabled or not.
            plugin_config: Custom configuration to be passed to the auth plugin
                           when invoked. The parameters defined here will override
                           the one defined in the auth plugin itself.

        Returns:
            handshake_response: Handshake response as per [1].
            auth_strategy: Authorization plugin instance created based
                           on the provided `auth_plugin` and `auth_plugin_class`.

        Raises:
            ProgrammingError: Handshake misses authentication info.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_handshake_response.html

            [2]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
        zGot a `change user` requestzStarting authorization phaseNzGot a NULL handshakerg   z$Handshake misses authentication infork   z-Handshake misses authentication plugin info ()z#The provided initial strategy is %srS   sxxxxxxxxxxxxxxxxxxxxxxxxz<IIB)rg   rh   rj   rk   rl   rm   ro   <Hr;   r8   )rZ   r   debugr   getrt   KeyErrorrY   rW   rX   rO   r   CHANGE_USERr.   ry   rf   r   PLUGIN_AUTHCONNECT_ARGSr_   r[   )rz   rh   rj   rb   r{   r`   r|   rk   rl   rI   r~   rm   ro   
b_usernameresponse_payloadrx   fillerrs   rw   s                      r6   	make_authzMySQLProtocol.make_auth  s   f __&&
! 	8L67773444"#9::D==%%-"#IJJPTT	%A=)AKK8$ 	 	 	"FFFF 	
 	:KHHH! 	##,Z,,,)     F##6663z??666 &    (5'O'O,#/#' (P (
 (
$} 	... 	 = =lH U UVVV " 	@##FKg$>$>??? *00 	D##K$6$6$8$87$BCCC :22 	O
8N##M$A$A*$M$MNNNxx())=88s   9
B B.B))B.c                    d                     t          j        |          t          j        |          t          j        |           dg          S )a  Make a SSL authentication packet (see [1]).

        Args:
            charset: Client charset (see [2]), only the lower 8-bits.
            client_flags: Integer representing client capabilities flags.
            max_allowed_packet: Maximum packet size.

        Returns:
            ssl_request_pkt: SSL connection request packet.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_ssl_request.html

            [2]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
        r8   s                          )r[   r   	int4store	int1store)r{   r`   r|   s      r6   make_auth_sslzMySQLProtocol.make_auth_ssl  sJ    0 xx-- 233((	
 
 	
r8   commandargumentOptional[bytes]c                <    t          j        |           }||n||z   S )z(Make a MySQL packet containing a command)r   r   )r   r   datas      r6   make_commandzMySQLProtocol.make_command  s&     w'''ttTH_<r8   r   statement_idrowsc                T    t          j        |           t          j        |          z   S )z0Make a MySQL packet with Fetch Statement command)r   r   )r   r   s     r6   make_stmt_fetchzMySQLProtocol.make_stmt_fetch  s#     |,,ut/D/DDDr8   packetc           	        i }t          j        d| dd                   d         |d<   |d         t          k    r t          d|d          dt                     t	          j        | dd         d	          \  } |d
<   t          j        d| dd                   \  |d<   }}|d<   |d<   }}|d
                                         |d
<   | dd         } t	          j        ||z             }d}|t          j	        z  rA|rt          d|dz
            nd}| d|         }| |d         } |d         dk    r
|dd         }|t          j        z  rcd| vr$|d
                             d          r	d| c} |d<   nt	          j        | d	          \  } |d<   |d                             d          |d<   nd|d<   ||z   |d<   ||d<   |S )zParse a MySQL Handshake-packet.<xxxxBr   r4   protocolz$Protocol mismatch; server version = z, client version = Nr;   r<   server_version_originalz<I8sx2sBH2sBxxxxxxxxxx   server_threadidr{   server_statusr8         r>   z5.5.8rk   utf-8mysql_native_passwordrg   capabilities)rW   unpackPROTOCOL_VERSIONr   r   r@   rA   intreadr   SECURE_CONNECTIONminr   
startswith)	r   res
auth_data1capabilities1capabilities2auth_data_lengthr   
auth_data2sizes	            r6   parse_handshakezMySQLProtocol.parse_handshake  sA     -&1+>>qAJz?...7s: 7 7$47 7   271B122JG2
 2
 2
.-. M2F1R4LAA	
!"	N ),-F)G)N)N)P)P%&}]]%BCC
*66 	-4DL3r+a/000"D$JDEE]F"~""'_
*00 
	9f$$-F)G)R)R* *$ 14V,]++/4/@W/U/U/U,]+!$]!3!:!:7!C!CC!8C%
2K*N
r8   r$   c                    | d         dk    st          d          i }	 t          j        d| dd                   d         |d<   t          j        | dd                   \  } |d<   t          j        |           \  } |d	<   t          j        d
| dd                   \  |d<   |d<   | dd         } | r8t          j        |           \  } |d<   |d                             d          |d<   n"# t          $ r}t          d          |d}~ww xY w|S )zParse a MySQL OK-packetr3   r   z#Failed parsing OK packet (invalid).r   r4   field_countNaffected_rows	insert_idz<HHstatus_flagwarning_countinfo_msgr   zFailed parsing OK packet.)r   rW   r   r   read_lc_intread_lc_stringrA   
ValueError)r   	ok_packetrx   s      r6   parse_okzMySQLProtocol.parse_ok  s@    ayA~~ !FGGG		G'-}Xvac{'K'KA'NIm$161B6!"":1N1N.FIo.-2->v-F-F*FIk* eVAaC[11-(/*ABBZF N050DV0L0L-	*-(1*(=(D(DW(M(M	*% 	G 	G 	G !<==3F	Gs   CC, ,
D6DDOptional[int]c                    	 t          j        | dd                   d         }|S # t          j        t          f$ r}t          d          |d}~ww xY w)z=Parse a MySQL packet with the number of columns in result setr3   Nr   zFailed parsing column count)r   r   rW   errorr   r   )r   countrx   s      r6   parse_column_countz MySQLProtocol.parse_column_count
  se    	I%fQRRj11!4ELj) 	I 	I 	I !>??SH	Is   #& AAAr   encodingr!   c           	        t          j        | dd                   \  } }t          j        |           \  } }t          j        |           \  } }t          j        |           \  } }t          j        |           \  } }t          j        |           \  } }	 t          j        d|           \  }}}}}n## t          j        $ r t          d          dw xY w|                    |          |dddd| t          j        z  ||f	S )zParse a MySQL column-packet.r3   Nz	<xHIBHBxxz!Failed parsing column information)	r   r   rW   r   r   r   rA   r   NOT_NULL)r   r   _ru   r{   column_typeflagss          r6   parse_columnzMySQLProtocol.parse_column  s     (44	(00	(00	(00	+F33(00			P k622| 	P 	P 	P !DEE4O	P KK!!FY''

 
	
s   B0 0 Cr"   c                T   |d         dk    r|                      |          S d}i }	 t          j        d|          }n'# t          j        $ r}t	          |          |d}~ww xY w|d         dk    rt          |          dk    st	          |          |d	         |d
<   |d         |d<   |S )zParse a MySQL EOF-packetr3   r   zFailed parsing EOF packet.z<xxxBBHHNr   r)   	   r*   r      r   )r   rW   r   r   r   rO   )selfr   err_msgr   unpackedrx   s         r6   	parse_eofzMySQLProtocol.parse_eof4  s    !9>>==(((.	3}Z88HH| 	3 	3 	3 ))s2	3 s""s6{{a'7'7 )))'{O%a[M
s   = A!AA!Twith_headerr%   c                V   d}i }dg}d}|r| dd                              d          }n|                      d          }|D ]}	 d |                     dd          D             \  }}n"# t          $ r}t          |          |d}~ww xY w|                    d	          }	 t	          |          ||<   r# t
          t          f$ rb 	 t          |                    d	                    ||<   n8# t          $ r+}t          | d
| dt          |           d          |d}~ww xY wY w xY w|S )zParse the statistics packetz)Failed getting COM_STATISTICS informationr8   r3   Ns     c                6    g | ]}|                                 S  )strip)rP   vs     r6   
<listcomp>z2MySQLProtocol.parse_statistics.<locals>.<listcomp>V  s     CCC!AGGIICCCr8      :r*   r   z (:r   )	splitr   r   rA   ra   r   r   r   repr)	r   r   errmsgr   pairslblpairvalrx   s	            r6   parse_statisticszMySQLProtocol.parse_statisticsH  s    =.0 	.122J$$[11EELL--E 	S 	SD6CCtzz$/B/BCCCSS 6 6 6$V,,#56 **W%%CSs88Cj) S S SS&szz'':':;;CHH' S S S(F)H)Hc)H)HDII)H)H)HIIsRS HS
 
sN   #A((
B2BB B33D&%C+*D&+
D 5&DD  D&%D&sockr'   versionTuple[int, ...]r   read_timeoutATuple[List[Tuple[Optional[bytes], ...]], Optional[EofPacketType]]c                (   |}g }d}d}d}		 |s|	|k    rnz|                     |          }
|
                    d          r|
dd         g}|                     |          }
|
                    d          rG|                    |
dd                    |                     |          }
|
                    d          G|                    |
dd                    t          j        d                    |                    }n[|
d         dk    r$|
d         dk     r|                     |
          }d}n+d}t          j        t          |
dd                             }|||                    |           n||t          |
          |	d	z  }	||fS )
zRead MySQL text result

        Reads all or given number of rows from the socket.

        Returns a tuple with 2 elements: a list with all rows and
        the EOF packet.
        Nr   Ts   r3   r8   r)      r   )	recvr   rY   r   read_lc_string_listr[   r   r0   r   )r   r   r   r   r   r   r   eofrowdatair   datass               r6   read_text_resultzMySQLProtocol.read_text_resulte  s   $ 	 a5jjYY|,,F  11 G<00''88 5LL,,,!YY|44F ''88 5 VABBZ(((3CHHUOODDc!!fQi!mmnnV,,3E&*4E4EFF{w2G$$$$#F+++FA-	. Syr8   fieldTuple[bytes, int]c                   |d         t           j        k    rd}d}nj|d         t           j        k    rd}d}nO|d         t           j        t           j        fv rd}d}n*|d         t           j        k    rd}d}nt          d	          |d
         t          j        z  r|	                                }| |d         t          j        || d|                   d         fS )z%Parse an integer from a binary packetr   <b<hr*   <ir3   <qr   zUnknown integer typer   Nr   )r   TINYSHORTINT24LONGLONGLONGr   r   UNSIGNEDupperrW   r   )r   r   format_lengths       r6   _parse_binary_integerz#MySQLProtocol._parse_binary_integer  s    
 8y~%%GFF1X((GFF1X)/9>:::GFF1X+++GFF 34448i(( 	&mmooGvwwwqx8H!I!I!!LMMr8   Tuple[bytes, float]c                    |d         t           j        k    rd}d}nd}d}| |d         t          j        || d|                   d         fS )z)Parse a float/double from a binary packetr   r   <dr3   z<fNr   )r   DOUBLErW   r   )r   r   r  r  s       r6   _parse_binary_floatz!MySQLProtocol._parse_binary_float  sY    
 8y'''FGGFGvwwwqx8H!I!I!!LMMr8   utf8Tuple[bytes, Decimal]c                x    t          j        |           \  } }| t          |                    |                    fS )z(Parse a New Decimal from a binary packet)r   r   r   rA   )r   r{   values      r6   _parse_binary_new_decimalz'MySQLProtocol._parse_binary_new_decimal  s7    
  .v66W 5 56677r8   
field_type?Tuple[bytes, Optional[Union[datetime.date, datetime.datetime]]]c           	     V   | d         }d}|dk    r}t          j        d| dd                   d         }| d         }| d         }|t          j        t          j        fv rt          j        |||          }nt          j        |||          }n|dk    rd}|d	k    r&t          j        d
| d|dz                      d         }t          j        t          j        d| dd                   d         | d         | d         | d         | d         | d         |          }| |dz   d         |fS )z&Parse a timestamp from a binary packetr   Nr3   r   r   r   )yearmonthdayr      <Ir   r4      )r  r  r  hourminutesecondmicrosecond)rW   r   r   DATETIME	TIMESTAMPdatetimedate)r   r  r  r  r  r  r  mcss           r6   _parse_binary_timestampz%MySQLProtocol._parse_binary_timestamp  s?    CGQ;;=vac{33A6D1IE)Ci0)2EFFF )t5cJJJ 4u#FFFq[[C||mD&VaZ*@AA!D%]4!55a8Qi1IAYayay  E vz||$e,,r8    Tuple[bytes, datetime.timedelta]c                   | d         }|s| dd         t          j                    fS | d|dz            }d}|dk    r#t          j        d|dd                   d         }t          j        d|dd                   d         }|d         dk    r|dz  }t          j        ||d         ||d	         |d         
          }| |dz   d         |fS )z'Parse a time value from a binary packetr   r   Nr   r  r4   r>   r   r  )dayssecondsmicrosecondsminuteshours)r  	timedeltarW   r   )r   r  r   r!  r%  tmps         r6   _parse_binary_timez MySQLProtocol._parse_binary_time  s      	6122J 2 4 455a&1*n%A::-d122h//2C}T4!9--a07a<<BJD GGq'
 
 
 vz||$c**r8   fieldsList[DescriptionType]Tuple[BinaryProtocolType, ...]c                   t          |          dz   dz   dz  }d |d|         D             }||d         }g }d}t          |          D ]\  }}	|t          |dz   dz                     d|dz   dz  z  z  r|                    d           C|	d         t          j        t          j        t          j        t          j        t          j	        fv r/| 
                    ||	          \  }}|                    |           |	d         t          j        t          j        fv r0|                     ||	          \  }}|                    |           |	d         t          j        t          j        fv r0|                     ||          \  }}|                    |           S|	d         t          j        t          j        t          j        fv r6|                     ||	d                   \  }}|                    |           |	d         t          j        k    r/|                     |          \  }}|                    |           |	d         t          j        k    r@t1          j        |          \  }}|                    t5          j        |                     O|	d         t8          j        k    s|	d         dk    r.t1          j        |          \  }}|                    |           t1          j        |          \  }}	 |                    |                    |                     # t>          $ r |                    |           Y w xY wtA          |          S )	z(Parse values from a binary result packetr   r*   r   c                ,    g | ]}t          |          S r   )ra   )rP   r   s     r6   r   z6MySQLProtocol._parse_binary_values.<locals>.<listcomp>  s    DDD!s1vvDDDr8   r   Nr   ?   )!rO   	enumeratera   rY   r   r   r   r   r   r   r  r	  FLOATr
  DECIMAL
NEWDECIMALr  r  DATEr  r"  TIMEr,  VECTORr   r   r   _vector_to_pythonr   BINARYrA   UnicodeDecodeErrortuple)
r   r-  r   r{   null_bitmap_lengthnull_bitmaprV   r  posr   s
             r6   _parse_binary_valuesz"MySQLProtocol._parse_binary_values  sE    "&kkAo1a7DDva0B.B'CDDD*++,$(#F++ )	) )	)JC3a1}--.!a1}2DE d###Qx"   !% : :65 I Ie$$$$qi.	@@@ $ 8 8 G Ge$$$$qi/1EFFF $ > >vw O Oe$$$$q"#  
 #'">">vuQx"P"Pe$$$$qY^++"&"9"9&"A"Ae$$$$qY---"'"6v">">n>uEEFFFFqY---qR"'"6v">">e$$$$"'"6v">">)MM%,,w"7"78888) ) ) )MM%((((() V}}s   (L++MMcolumnsDTuple[List[Tuple[BinaryProtocolType, ...]], Optional[EofPacketType]]c                |   g }d}d}d}		 |n|	|k    rnt          |                    |                    }
|
d         dk    r|                     |
          }d}n-|
d         dk    r!d}|                     ||
dd         |          }|||                    |           n||t          |
          |	dz  }	||fS )zwRead MySQL binary protocol result

        Reads all or given number of binary resultset rows from the socket.
        Nr   Tr3   r)   r4   r   )r0   r   r   rA  rY   r   )r   r   rB  r   r{   r   r   r   rV   r   r   s              r6   read_binary_resultz MySQLProtocol.read_binary_result6  s     	Ezz499\2233FayCnnV,,a227F122JPP{v1F#####F+++FA!	" c{r8   Dict[str, int]c                   | d         dk    st          d          i }	 t          j        | dd         d          \  } |d<   t          j        | d          \  } |d<   t          j        | d          \  } |d	<   | d
d         } t          j        | d          \  } |d<   n"# t          $ r}t          d          |d}~ww xY w|S )z(Parse a MySQL Binary Protocol OK packet.r3   r   zFailed parsing Binary OK packetr4   Nr   r*   num_columns
num_paramsr   r   )r   r   rE   r   )r   ok_pktrx   s      r6   parse_binary_prepare_okz%MySQLProtocol.parse_binary_prepare_ok\  s     ayA~~ !BCCC	M-2^F122J-J-J*FF>*,1N61,E,E)FF=)+0>&!+D+D(FF<(ABBZF.3nVQ.G.G+FF?++ 	M 	M 	M !BCCL	M s   A>B 
B=(B88B=r  Tuple[bytes, int, int]c                   d}d}| dk     rN| dk    rd}t           j        }n| dk    rd}t           j        }ns| dk    rd}t           j        }n^d	}t           j        }nOd
}| dk    rd}t           j        }n8| dk    rd}t           j        }n#| dk    rd}t           j        }nt           j        }d}t          j        ||           ||fS )z0Prepare an integer for the MySQL binary protocolNr   ir   i r   i   r   r      r+   rS   i  r   l    r  z<Q)r   r   r   r   r   rW   rX   )r  r  r   r  s       r6   prepare_binary_integerz$MySQLProtocol.prepare_binary_integern  s     
199}}&^

&&_

+%%&^

&/

E||&^

%&_

*$$&^

&/
GU++Z??r8   'Union[datetime.date, datetime.datetime]c                "   t          | t          j                  rt          j        }n6t          | t          j                  rt          j        }nt          d          t          j        | j	                  t          j
        | j                  t          j
        | j                  g}t          | t          j                  r|                    t          j
        | j                  t          j
        | j                  t          j
        | j                  g           | j        dk    r,|                    t          j        | j                             d                    |          }t          j
        t+          |                    |z   |fS )a  Prepare a timestamp object for the MySQL binary protocol

        This method prepares a timestamp of type datetime.datetime or
        datetime.date for sending over the MySQL binary protocol.
        A tuple is returned with the prepared value and field type
        as elements.

        Raises ValueError when the argument value is of invalid type.

        Returns a tuple.
        z2Argument must a datetime.datetime or datetime.dater   r8   )
isinstancer  r   r  r   r7  r   r   	int2storer  r   r  r  extendr  r  r  r  rY   r   r[   rO   )r  r  chunkspackeds       r6   prepare_binary_timestampz&MySQLProtocol.prepare_binary_timestamp  sG    eX.// 	S"+JJx}-- 	S"JJQRRR OEJ''OEK((OEI&&
 eX.// 		BMMOEJ//OEL11OEL11    1$$eoe.?@@AAA&!!s6{{++f4j@@r8   (Union[datetime.timedelta, datetime.time]c                H   t          | t          j        t          j        f          st	          d          t
          j        }d}d}t          g           }t          | t          j                  r| j        dk     rd}t          | j
        d          \  }}t          |d          \  }}|                    t          j        t          | j                            t          j        |          t          j        |          t          j        |          g           | j        }nw|                    t          j        d          t          j        | j                  t          j        | j                  t          j        | j                  g           | j        }|r'|                    t          j        |                     |                    t          j        |                     d                    |          }	t          j        t1          |	                    |	z   |fS )a  Prepare a time object for the MySQL binary protocol

        This method prepares a time object of type datetime.timedelta or
        datetime.time for sending over the MySQL binary protocol.
        A tuple is returned with the prepared value and field type
        as elements.

        Raises ValueError when the argument value is of invalid type.

        Returns a tuple.
        z3Argument must a datetime.timedelta or datetime.timer   Nr   i  <   r8   )rR  r  r*  timer   r   r8  r   r%  divmodr&  rT  r   r   absr   r'  r  r  r  r  rY   
appendleftr[   rO   )
r  r  negativer!  rU  r)  	remainderminssecsrV  s
             r6   prepare_binary_timez!MySQLProtocol.prepare_binary_time  s    %("4hm!DEE 	TRSSS^
$RyyeX/00 	$zA~~!'t!<!<UI!)R00LT4MMOC
OO44OE**OD))OD))	   $CCMMOA&&OEJ//OEL11OEL11	   #C 	0MM%/#..///%/(33444&!!s6{{++f4j@@r8   	statementparamr   c                z    d                     t          j        |           t          j        |          |g          S )zMPrepare long data for prepared statements

        Returns a string.
        r8   )r[   r   r   rS  )rd  re  r   s      r6   prepare_stmt_send_long_dataz)MySQLProtocol.prepare_stmt_send_long_data  s1     xx33U_U5K5KTRSSSr8   r   Sequence[BinaryProtocolType]
parametersr   r   long_data_used Optional[Dict[int, Tuple[bool]]]query_attrs.Optional[List[Tuple[str, BinaryProtocolType]]]converter_str_fallbackc	           
        d}	dgt          |          dz   dz  z  }
g }g }d}t          |          }g }|s|n	|t          z   }|dk    rd}|i }|rEt          |          }|D ]\  }}|                    |           dgt          |          dz   dz  z  }
|s|r|t          |          k    rt	          d	          t          |          D ]Z\  }}d}|`|
|dz  xx         d|dz  z  z  cc<   |                    t          j        t          j	                  t          j        |          z              j||v r*||         d         rt          j
        }n.t          j        }n t          |t                    r0|                     |          \  }}}|                    |           nt          |t                    rZ|                    |          }|                    t          j        t          |                    |z              t          j        }nlt          |t$                    rE|                    t          j        t          |                    |z              t          j        }nt          |t&                    r|                    t          j        t          t          |                              |                              t          |                              |          z              t          j        }nxt          |t*                    r6|                    t-          j        d
|                     t          j        }n-t          |t2          j        t2          j        f          r.|                     |          \  }}|                    |           nt          |t2          j        t2          j        f          r.|                     |          \  }}|                    |           n|rft          |                              |          }|                    t          j        t          |                    |z              t          j        }nt?          d|j         j!         d          |                    t          j        |          t          j        |          z              |rd|dz   |k    r[|||z
           d                             |          }|                    t          j        t          |                    |z              \t          j"        |          t          j        |          t          j"        |	          g}||t          |          z   }n|}|r|'|                    t          j        |                     |#                    d |
D                        |                    t          j        d                     d}|D ][}|                    |           |=|dz   |k    r|                    |||z
                      n|                    d           |dz  }\|D ]}|                    |           d$                    |          S )z6Make a MySQL packet with the Statement Execute commandr   r   r   r   r8   utf8mb4r  NzTFailed executing prepared statement: data values does not match number of parametersr  z&MySQL binary protocol can not handle 'z	' objectsc                8    g | ]}t          j        d |          S )B)rW   rX   )rP   bits     r6   r   z3MySQLProtocol.make_stmt_execute.<locals>.<listcomp>l  s$    HHHS6;sC00HHHr8   r;   )%rO   r   listrY   r   r3  r   r   r   NULLBLOBSTRINGrR  ra   rO  ri   rZ   rv   r0   r   r5  floatrW   rX   r	  r  r   rW  r*  r[  rc  r   	__class____name__r   rT  r[   )r   r   r   ri  r   rj  r{   rl  rn  iteration_countr?  rV   typesrV  data_lenquery_attr_namesr   attr_valr@  r  _flagsr  ru   r   parameter_countr   a_typea_values                               r6   make_stmt_executezMySQLProtocol.make_stmt_execute  sY    cc$ii!m12 t99(*(Oe6O.OiG!N 	7::D* & &8H%%%%##d))a-A!56K >	L >	L3z??**$2   (oo 8L 8L
U=+++qS1W~=+++LL	77%/&:Q:QQ   .((%c*1- 6%.^

 &/%5

s++ %
 33E::	"MM&))))s++ !LL11EMM%,s5zz":":U"BCCC!*!1JJu-- MM%,s5zz":":U"BCCC!*!1JJw// MMSU):):7)C)C%D%DEEe**++G445   "+!2JJu-- MM&+dE":":;;;!*!1JJ(98='IJJ +/+H+H+O+O(VZMM&))))(:HM'JKK +/+C+CE+J+J(VZMM&))))+ JJ--g66EMM%,s5zz":":U"BCCC!*!1JJ*@!O4@ @ @   U_Z885?6;R;RRSSS L37X#5#5&sX~6q9@@IID$++ELT,C,Cd,JKKKOL))OE""OO,,
 "&[)9)99OO&O 	'&el?;;<<<MMHHKHHHIIIMM%/!,,---E  f%%%
 *qy8++&6ux7G&HIIIIg...
! ' 'g&&&&xxr8   )r/   r0   r1   r0   )r/   r0   r1   r9   )rI   rJ   r1   r0   )r`   ra   rb   rc   r1   r0   )NFN)rg   r0   rh   ri   rj   ri   rk   ri   rl   rc   rm   rn   ro   rp   r1   rq   )rz   r#   rh   ri   rj   ri   rb   rc   r{   ra   r`   ra   r|   ra   rk   rc   rl   rc   rI   r}   r~   rn   rm   rn   ro   rp   r1   rq   )r{   ra   r`   ra   r|   ra   r1   r0   rN   )r   ra   r   r   r1   r0   )r   )r   ra   r   ra   r1   r0   )r   r0   r1   r#   )r   r0   r1   r$   )r   r0   r1   r   )r   )r   r0   r   ri   r1   r!   )r   r0   r1   r"   )T)r   r0   r   rn   r1   r%   )r   N)
r   r'   r   r   r   ra   r   r   r1   r   )r   r0   r   r!   r1   r   )r   r0   r   r!   r1   r  )r  )r   r0   r{   ri   r1   r  )r   r0   r  ra   r1   r  )r   r0   r1   r#  )r-  r.  r   r0   r{   ri   r1   r/  )r   r   N)r   r'   rB  r.  r   ra   r{   ri   r   r   r1   rC  )r   r0   r1   rF  )r  ra   r1   rL  )r  rP  r1   r   )r  rX  r1   r   )rd  ra   re  ra   r   r0   r1   r0   )r   r   r   Nr  NF)r   ra   r   rh  ri  r   r   ra   rj  rk  r{   ri   rl  rm  rn  rn   r1   r0   )%rz  
__module____qualname____doc__staticmethodr7   rC   rH   r_   rf   ry   DEFAULT_CHARSET_IDDEFAULT_MAX_ALLOWED_PACKETr   r   r   r   r   r   r   r   r   r   r   r  r
  r  r"  r,  rA  rE  rK  rO  rW  rc  rg  r  r   r8   r6   r.   r.   V   s}        
    \* ) ) ) \)4 ) ) ) \)4 + + + \+< 
 
 
 \
(  ,0!26:, :, :, :, \:,x 
 #')"<%)+//3',!26w9 w9 w9 w9 \w9r )"<
 
 
 
 \
@ = = = = \=
 E E E E \E 1 1 1 \1f    \, I I I \I 
 
 
 
 \
@   (     \@ &*. . . . .` N N N \N2 N N N \N &,8 8 8 8 \8 - - - \-> + + + \+4 	8 8 8 8 8| &*$ $ $ $ $L    \" @ @ @ \@B (A (A (A \(AT 5A 5A 5A \5An T T T \T .0!;?FJ',E  E  E  E  E  E  E r8   r.   )<r  
__future__r   r  rW   collectionsr   decimalr   r   typingr   r   r	   r
   r   r   r   r   r   rL   r   	constantsr   r   r   r   r   
conversionr   errorsr   r   r   r   r   pluginsr   r   plugins.caching_sha2_passwordr   r|  r    r!   r"   r#   r$   r%   r&   networkr'   r   r?   r5   	OK_STATUSrF   
ERR_STATUSr  r  r.   r   r8   r6   <module>r     sx  : 3 2 " " " " " "         - - - - - - - -
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                    ' & & & & & R R R R R R R R R R R R       5 5 5 5 5 5 5 5 M M M M M M                   %$$$$$$    	

 ' j  j  j  j  j  j  j  j  j  j r8   