
    ,h3                    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:dZ;dZ< G d d      Z=y)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                  F   e Zd ZdZed#d       Zed$d       Zed$d       Zed%d       Zed&d       Z	e	 	 	 d'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d(d	       Z
eded
eddddddf
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d       Zeed
ef	 	 	 	 	 	 	 d*d       Zed+d,d       Zed-d.d       Zed/d       Zed0d       Zed1d       Zed2d3d       Zd4dZed5d6d       Z	 	 d7	 	 	 	 	 	 	 	 	 d8dZe	 	 	 	 	 	 d9d       Ze	 	 	 	 	 	 d:d       Ze	 d;	 	 	 	 	 d<d       Ze	 	 	 	 	 	 d=d       Zed>d       Z	 d2	 	 	 	 	 	 	 d?dZ	 	 	 d@	 	 	 	 	 	 	 	 	 	 	 dAdZedBd       ZedCd       Z e	 	 	 	 dDd       Z!e	 	 	 	 dEd        Z"edFd!       Z#	 	 	 	 	 	 	 dG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dHd"Z$y)IMySQLProtocolzRImplements MySQL client/server protocol

    Create and parses MySQL packets.
    c                :    | 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   )pkts    Z/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/protocol.pyparse_auth_more_dataz"MySQLProtocol.parse_auth_more_data^   s(    $ 1v00 !EFF12w    c                    | d   t         k7  rt        d      t        j                  | dd d      \  } }| r| d   dk(  r| dd } |j	                         | 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
        r1   z'Failed parsing AuthSwitchRequest packetr2   N    endr   )AUTH_SWITCH_STATUSr   r   read_stringdecode)r4   plugin_names     r5   parse_auth_switch_requestz'MySQLProtocol.parse_auth_switch_requestt   sf    ( q6'' !JKK ,,SW'B[3r7a<cr(C!!#S((r7   c                    t        j                  | dd d      \  } }|t        k7  rt        d      t        j                  | d      \  } }|j                         | 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
        r1   Nr   z.Failed parsing AuthNextFactor packet (invalid)r9   r:   )r   read_int
MFA_STATUSr   r>   r?   )r4   statusr@   s      r5   parse_auth_next_factorz$MySQLProtocol.parse_auth_next_factor   s]    * nnSWa0VZ !QRR ,,Sg>[!!#S((r7   c           
     V     D ]  } |   	d |<    t         fd D              t         j                               z   t         j                               z   }t	        j
                  d|      g} D ]  }|j                  t	        j
                  dt        |                   |j                  |j                                |j                  t	        j
                  dt         |                      |j                   |   j                                 dj                  |      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
         c              3  R   K   | ]  }t        |      t        |         z      y wN)len).0x
conn_attrss     r5   	<genexpr>z0MySQLProtocol.make_conn_attrs.<locals>.<genexpr>   s#     @AZ]++@s   $'<Br7   )	sumrK   keysvaluesstructpackappendencodejoin)rN   	attr_nameconn_attrs_lenconn_attrs_packets   `   r5   make_conn_attrszMySQLProtocol.make_conn_attrs   s    $ 	+I)$,(*
9%	+ @Z@@*//#$%*##%&' 	 $[[~>?# 	EI$$V[[s9~%FG$$Y%5%5%78$$V[[s:i;P7Q%RS$$Z	%:%A%A%CD		E
 xx)**r7   c                V    | t         j                  z  r|r|j                         dz   S 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
        r9   )r   CONNECT_WITH_DBrW   )client_flagsdatabases     r5   connect_with_dbzMySQLProtocol.connect_with_db   s4    " j888X OO'	
 	
r7   NFc                T   |s|dk(  rdt        |||      fS |i }	  t        ||      |||      } |j                  | fi |}|t	        d|j
                         t        j                  t        |            |z   }||fS # t        t        f$ r}	t	        d|	       |	d}	~	ww xY w)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.
        rH   r9   )ssl_enabledNzFailed authentication: z8Got NULL auth response while authenticating with plugin )	r   r   auth_response	TypeErrorr   namer   lc_intrK   )
	auth_datausernamepasswordauth_pluginauth_plugin_classrc   plugin_configauth_strategyrd   errs
             r5   auth_plugin_first_responsez(MySQLProtocol.auth_plugin_first_response   s    D K2->(    M	KKOK9JK(M 8M77	S]SM   ',,-/ 
 S%78=Hm++ >* 	K #:3%!@AsJ	Ks   (B B'B""B'r   c           	     \   |j                         }g }|
rt        j                  d       t        j                  d       | t        d      d| j	                  d      t        d      d	 |xs | d   }t        j                  d
|       |
rB|j                  t        j                  dt        |       dt        j                  |             n9d}|j                  t        j                  d| t        |       d||||             t        j                  | d   ||||||      \  }}|j                  |       |j                  t        j                  ||             |
r%|j                  t        j                  d|             |t         j"                  z  r"|j                  |j                         dz          |t         j$                  z  r&|	$|j                  t        j'                  |	             dj)                  |      |fS # t
        t        f$ r}t        d| d	      dd}~ww xY w)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 handshakerh   z$Handshake misses authentication infork   z-Handshake misses authentication plugin info ()z#The provided initial strategy is %srP   sxxxxxxxxxxxxxxxxxxxxxxxxz<IIB)rh   ri   rj   rk   rl   rc   rm   <Hr9   r7   )rW   r   debugr   getre   KeyErrorrV   rT   rU   rK   r   CHANGE_USERr/   rp   ra   r   PLUGIN_AUTHCONNECT_ARGSr\   rX   )	handshakeri   rj   r`   charsetr_   max_allowed_packetrk   rl   rN   is_change_user_requestrc   rm   
b_usernameresponse_payloadro   fillerrd   rn   s                      r5   	make_authzMySQLProtocol.make_auth  s,   f __&
!LL6734"#9:D==%-"#IJPTT	%A=)AK 	:KH!##Z),)) F##6(3z?"326 & (5'O'O,#/#' (P (
$} 	. 	 = =lH UV "##FKKg$>? *000##K$6$6$87$BC :222
8N##M$A$A*$MNxx()=88m 8$ 	"?uAF	s   +	H H+H&&H+c                    dj                  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
        r7   s                          )rX   r   	int4store	int1store)r}   r_   r~   s      r5   make_auth_sslzMySQLProtocol.make_auth_ssl  sB    0 xx- 23(	
 	
r7   c                >    t        j                  |       }||S ||z   S )z(Make a MySQL packet containing a command)r   r   )commandargumentdatas      r5   make_commandzMySQLProtocol.make_command  s&     w''t<TH_<r7   c                X    t        j                  |       t        j                  |      z   S )z0Make a MySQL packet with Fetch Statement command)r   r   )statement_idrowss     r5   make_stmt_fetchzMySQLProtocol.make_stmt_fetch  s!     |,ut/DDDr7   c           	        i }t        j                  d| dd       d   |d<   |d   t        k7  rt        d|d    dt               t	        j
                  | dd d	      \  } |d
<   t        j                  d| dd       \  |d<   }}|d<   |d<   }}|d
   j                         |d
<   | dd } t	        j                  ||z         }d}|t        j                  z  r*|rt        d|dz
        nd}| d| }| |d } |d   dk(  r|dd }|t        j                  z  rVd| vr|d
   j                  d      r	d| c} |d<   nt	        j
                  | d	      \  } |d<   |d   j                  d      |d<   nd|d<   ||z   |d<   ||d<   |S )zParse a MySQL Handshake-packet.<xxxxBr   r2   protocolz$Protocol mismatch; server version = z, client version = Nr9   r:   server_version_originalz<I8sx2sBH2sBxxxxxxxxxx   server_threadidr}   server_statusr7         r<   z5.5.8rk   utf-8mysql_native_passwordrh   capabilities)rT   unpackPROTOCOL_VERSIONr   r   r>   r?   intreadr   SECURE_CONNECTIONminrz   
startswith)	packetres
auth_data1capabilities1capabilities2auth_data_lengthr   
auth_data2sizes	            r5   parse_handshakezMySQLProtocol.parse_handshake  s     --&1+>qAJz?..6s:6G H$$4#57  271B1B12JG2
.-. MM2F1RLA	
!"	N ),-F)G)N)N)P%&}}]]%BC
*6664D3r+a/0"D$JDE]F"~"'_
*000f$-F)G)R)R* 14V,]+/4/@/@W/U,]+!$]!3!:!:7!CC!8C%
2K*N
r7   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 } | r2t        j
                  |       \  } |d<   |d   j                  d      |d<   |S # t        $ r}t        d      |d}~ww xY w)zParse a MySQL OK-packetr1   r   z#Failed parsing OK packet (invalid).r   r2   field_countNaffected_rows	insert_idz<HHstatus_flagwarning_countinfo_msgr   zFailed parsing OK packet.)r   rT   r   r   read_lc_intread_lc_stringr?   
ValueError)r   	ok_packetro   s      r5   parse_okzMySQLProtocol.parse_ok  s    ayA~ !FGG		G'-}}Xva{'KA'NIm$161B1B6!":1N.FIo.-2->->v-F*FIk* eVAa[1-(/*ABZF050D0DV0L-	*-(1*(=(D(DW(M	*%   	G !<=3F	Gs   B3C 	C&C!!C&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 setr1   Nr   zFailed parsing column count)r   r   rT   errorr   r   )r   countro   s      r5   parse_column_countz MySQLProtocol.parse_column_count  sQ    	I%%fQRj1!4ELj) 	I !>?SH	Is    A	AA	c           	         t        j                  | dd       \  } }t        j                  |       \  } }t        j                  |       \  } }t        j                  |       \  } }t        j                  |       \  } }t        j                  |       \  } }	 t        j                  d|       \  }}}}}|j                  |      |dddd| t        j                  z  ||f	S # t        j                  $ r t        d      dw xY w)zParse a MySQL column-packet.r1   Nz	<xHIBHBxxz!Failed parsing column information)	r   r   rT   r   r   r   r?   r   NOT_NULL)r   encoding_rf   r}   column_typeflagss          r5   parse_columnzMySQLProtocol.parse_column  s    ((4	((0	((0	((0	++F3((0			P k62 KK!FY'''

 
	
 || 	P !DE4O	Ps   C  C=c                   |d   dk(  r| j                  |      S d}i }	 t        j                  d|      }|d   dk(  rt        |      dk  st	        |      |d	   |d
<   |d   |d<   |S # t        j                  $ r}t	        |      |d}~ww xY w)zParse a MySQL EOF-packetr1   r   zFailed parsing EOF packet.z<xxxBBHHNr   r)   	   r*   r      r   )r   rT   r   r   r   rK   )selfr   err_msgr   unpackedro   s         r5   	parse_eofzMySQLProtocol.parse_eof6  s    !9>==((.	3}}Z8H s"s6{a'7 ))'{O%a[M
 || 	3 )s2	3s   A( (B;BBc                $   d}i }dg}d}|r| dd j                  d      }n| j                  d      }|D ]T  }	 |j                  dd      D cg c]  }|j                          c}\  }}|j	                  d      }	 t        |      ||<   V |S c c}w # t        $ r}	t        |      |	d}	~	ww xY w# t        t        f$ rQ 	 t        |j	                  d            ||<   n/# t        $ r#}	t        | d	| d
t        |       d      |	d}	~	ww xY wY w xY w)zParse the statistics packetz)Failed getting COM_STATISTICS informationr7   r1   Ns        :r*   r   z (:rr   )
splitstripr   r   r?   intrx   r   r   repr)
r   with_headererrmsgr   pairslblpairvvalro   s
             r5   parse_statisticszMySQLProtocol.parse_statisticsJ  s8    =.012J$$[1ELL-E 	SD6/3zz$/BC!AGGICS
 **W%CSs8C	S 
 D 6$V,#56 j) SS&szz'':;CH' S(F82cU!DI;a)HIsRS Ss_   BB#B;B/B	B,B''B,/D?CD	D	&DD		DDc                   |}g }d}d}d}		 |s|	|k(  r	 ||fS |j                  |      }
|
j                  d      r|
dd g}|j                  |      }
|
j                  d      r7|j                  |
dd        |j                  |      }
|
j                  d      r7|j                  |
dd        t        j                  dj                  |            }nG|
d   dk(  r|
d   dk  r| j                  |
      }d}n#d}t        j                  t        |
dd             }|||j                  |       n||t        |
      |	dz  }	9)	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   s   r1   r7   r)      r   )	recvr   rV   r   read_lc_string_listrX   r   bytesr   )r   sockversionr   read_timeoutr   r   eofrowdatair   datass               r5   read_text_resultzMySQLProtocol.read_text_resultg  s\   $ a5j* Sy) YY|,F  1<0''8LL,!YY|4F ''8 VABZ(33CHHUODc!fQi!mnnV,33E&*4EF{w2G$#F++FA- r7   c                   |d   t         j                  k(  rd}d}nk|d   t         j                  k(  rd}d}nP|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|j                         }| |d t        j                  || d|       d   fS )z%Parse an integer from a binary packetr   <b<hr*   <ir1   <qr   zUnknown integer typer   Nr   )r   TINYSHORTINT24LONGLONGLONGr   r   UNSIGNEDupperrT   r   )r   fieldformat_lengths       r5   _parse_binary_integerz#MySQLProtocol._parse_binary_integer  s    
 8y~~%GF1X(GF1X)//9>>::GF1X+++GF 3448i(((mmoGvwwq8H!I!!LMMr7   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   <dr1   z<fNr   )r   DOUBLErT   r   )r   r   r   r   s       r5   _parse_binary_floatz!MySQLProtocol._parse_binary_float  sR    
 8y'''FGFGvwwq8H!I!!LMMr7   c                j    t        j                  |       \  } }| t        |j                  |            fS )z(Parse a New Decimal from a binary packet)r   r   r   r?   )r   r}   values      r5   _parse_binary_new_decimalz'MySQLProtocol._parse_binary_new_decimal  s1    
  ..v6W 5677r7   c           	        | d   }d}|dk(  rzt        j                  d| dd       d   }| d   }| d   }|t        j                  t        j                  fv rt        j
                  |||      }nt        j                  |||      }np|dk\  rkd}|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   Nr1   ru   r   r   )yearmonthdayr      <Ir   r2      )r   r   r   hourminutesecondmicrosecond)rT   r   r   DATETIME	TIMESTAMPdatetimedate)r   
field_typer   r   r   r   r   mcss           r5   _parse_binary_timestampz%MySQLProtocol._parse_binary_timestamp  s    CGQ;==va{3A6D1IE)Ci00)2E2EFF ))t5cJ 4u#Fq[C|mmD&VaZ*@A!D%%]]4!5a8Qi1IAYayayE vz|$e,,r7   c                D   | d   }|s| dd t        j                         fS | d|dz    }d}|dkD  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  r2   r<   r   r  )dayssecondsmicrosecondsminuteshours)r
  	timedeltarT   r   )r   r   r   r  r  tmps         r5   _parse_binary_timez MySQLProtocol._parse_binary_time  s     12J 2 2 455a&1*%A:--d12h/2C}}T4!9-a07a<BJD  GGq'
 vz|$c**r7   c                j   t        |      dz   dz   dz  }|d| D cg c]  }t        |       }}||d }g }d}t        |      D ]  \  }	}
|t        |	dz   dz           d|	dz   dz  z  z  r|j                  d       9|
d   t        j
                  t        j                  t        j                  t        j                  t        j                  fv r'| j                  ||
      \  }}|j                  |       |
d   t        j                  t        j                  fv r'| j                  ||
      \  }}|j                  |       |
d   t        j                  t        j                  fv r(| j!                  ||      \  }}|j                  |       K|
d   t        j"                  t        j$                  t        j&                  fv r+| j)                  ||
d         \  }}|j                  |       |
d   t        j*                  k(  r'| j-                  |      \  }}|j                  |       |
d   t        j.                  k(  r>t1        j2                  |      \  }}|j                  t5        j6                  |             ;|
d   t8        j:                  k(  s|
d   dk(  r+t1        j2                  |      \  }}|j                  |       t1        j2                  |      \  }}	 |j                  |j=                  |              tA        |      S c c}w # t>        $ r |j                  |       Y w xY w)z(Parse values from a binary result packetr   r*   r   r   Nr   ?   )!rK   r   	enumeraterV   r   r   r   r   r   r   r   r   FLOATr   DECIMAL
NEWDECIMALr   r  DATEr	  r  TIMEr  VECTORr   r   r   _vector_to_pythonr   BINARYr?   UnicodeDecodeErrortuple)r   fieldsr   r}   null_bitmap_lengthr   null_bitmaprS   r   posr   s              r5   _parse_binary_valuesz"MySQLProtocol._parse_binary_values  s    "&kAo1a7'-a0B'CD!s1vDD*+,$(#F+ )	)JC3a1}-.!a1}2DEd#Qx""  !% : :65 Ie$qi..	@@ $ 8 8 Ge$qi//1E1EFF $ > >vw Oe$q""## 
 #'">">vuQx"Pe$qY^^+"&"9"9&"Ae$qY---"'"6"6v">n>>uEFqY---qR"'"6"6v">e$"'"6"6v">)MM%,,w"78O)	)V V}a EZ * )MM%()s   L! LL21L2c                8   g }d}d}d}		 |	 ||fS |	|k(  r	 ||fS t        |j                  |            }
|
d   dk(  r| j                  |
      }d}n |
d   dk(  rd}| j                  ||
dd |      }|||j	                  |       n||t        |
      |	dz  }	)zwRead MySQL binary protocol result

        Reads all or given number of binary resultset rows from the socket.
        Nr   r1   r)   r2   r   )r   r   r   r)  rV   r   )r   r   columnsr   r}   r   r   r   rS   r   r   s              r5   read_binary_resultz MySQLProtocol.read_binary_result8  s      c{ Ez c{ 499\23FayCnnV,a227F12JP{v1F##F++FA! r7   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<   |S # t        $ r}t        d      |d}~ww xY w)z(Parse a MySQL Binary Protocol OK packet.r1   r   zFailed parsing Binary OK packetr2   Nr   r*   num_columns
num_paramsr   r   )r   r   rC   r   )r   ok_pktro   s      r5   parse_binary_prepare_okz%MySQLProtocol.parse_binary_prepare_ok^  s     ayA~ !BCC	M-2^^F12J-J*FF>*,1NN61,E)FF=)+0>>&!+D(FF<(ABZF.3nnVQ.G+FF?+   	M !BCL	Ms   A8B 	B+B&&B+c                   d}d}| dk  r[| dk\  rd}t         j                  }n| dk\  rd}t         j                  }n| dk\  rd}t         j                  }nod	}t         j                  }n\d
}| dk  rd}t         j                  }nB| 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+   rP   i  ru   l    r  z<Q)r   r   r   r   r   rT   rU   )r   r  r   r   s       r5   prepare_binary_integerz$MySQLProtocol.prepare_binary_integerp  s     
19}&^^
&&__
+%&^^
&//
E|&^^
%&__
*$&^^
&//
GU+Z??r7   c                h   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|j                  t        j                  | j                        t        j                  | j                        t        j                  | j                         g       | j"                  dkD  r.|j%                  t        j&                  | j"                               dj)                  |      }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   r7   )
isinstancer
  r   r  r  r  r   r   	int2storer   r   r   r   extendr  r  r  r  rV   r   rX   rK   )r   r  chunkspackeds       r5   prepare_binary_timestampz&MySQLProtocol.prepare_binary_timestamp  s$    eX../"++Jx}}-"JQRR OOEJJ'OOEKK(OOEII&
 eX../MMOOEJJ/OOELL1OOELL1   1$eooe.?.?@A&!s6{+f4j@@r7   c                d   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      \  }}|j                  t        j                  t        | j                              t        j                  |      t        j                  |      t        j                  |      g       | j                   }n|j                  t        j                  d      t        j                  | j"                        t        j                  | j$                        t        j                  | j&                        g       | j(                  }|r$|j+                  t        j                  |             |j-                  t        j                  |             dj/                  |      }	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  <   r7   )r6  r
  r  timer   r   r  r   r  divmodr  r8  r   r   absr   r  r  r  r  r  rV   
appendleftrX   rK   )
r   r  negativer  r9  r  	remainderminssecsr:  s
             r5   prepare_binary_timez!MySQLProtocol.prepare_binary_time  s    %("4"4hmm!DERSS^^
$RyeX//0zzA~!'t!<UI!)R0LT4MMOOC

O4OOE*OOD)OOD)	 $$CMMOOA&OOEJJ/OOELL1OOELL1	 ##CMM%//#./%//(34&!s6{+f4j@@r7   c                v    dj                  t        j                  |       t        j                  |      |g      S )zMPrepare long data for prepared statements

        Returns a string.
        r7   )rX   r   r   r7  )	statementparamr   s      r5   prepare_stmt_send_long_dataz)MySQLProtocol.prepare_stmt_send_long_data  s,     xx3U__U5KTRSSr7   c	           
        d}	dgt        |      dz   dz  z  }
g }g }d}t        |      }g }|s|n|t        z   }|dk(  rd}|i }|r;t        |      }|D ]  \  }}|j                  |        dgt        |      dz   dz  z  }
|s|r)|t        |      k7  rt	        d      t        |      D ]  \  }}d}|_|
|dz  xx   d|dz  z  z  cc<   |j                  t        j                  t        j                        t        j                  |      z          j||v r,||   d   rt        j                  }nt        j                  }nt        |t              r(| j                  |      \  }}}|j                  |       nt        |t              rS|j!                  |      }|j                  t        j"                  t        |            |z          t        j                  }n;t        |t$              rB|j                  t        j"                  t        |            |z          t        j                  }nt        |t&              rr|j                  t        j"                  t        t        |      j!                  |                  t        |      j!                  |      z          t        j(                  }ngt        |t*              r7|j                  t-        j.                  d	|             t        j0                  }n t        |t2        j2                  t2        j4                  f      r&| j7                  |      \  }}|j                  |       nt        |t2        j8                  t2        j:                  f      r&| j=                  |      \  }}|j                  |       n|r[t        |      j!                  |      }|j                  t        j"                  t        |            |z          t        j                  }n#t?        d
|j@                  jB                   d      |j                  t        j                  |      t        j                  |      z          |s|dz   |kD  s|||z
     d   j!                  |      }|j                  t        j"                  t        |            |z           t        jD                  |      t        j                  |      t        jD                  |	      g}||t        |      z   }n|}|r|$|j                  t        j"                  |             |jG                  |
D cg c]  }t-        j.                  d|       c}       |j                  t        j                  d             d}|D ]K  }|j                  |       |1|dz   |kD  r|j                  |||z
            n|j                  d       |dz  }M |D ]  }|j                  |        djI                  |      S c c}w )z6Make a MySQL packet with the Statement Execute commandr   r   r   r   r7   utf8mb4utf8zTFailed executing prepared statement: data values does not match number of parametersr   z&MySQL binary protocol can not handle 'z	' objectsBr9   )%rK   r   listrV   r   r  r   r   r   NULLBLOBSTRINGr6  r   r4  strrW   rg   r   r   r  floatrT   rU   r   r
  r  r;  r  r>  rF  r   	__class____name__r   r8  rX   )r   r   r   
parametersr   long_data_usedr}   query_attrsconverter_str_fallbackiteration_countr'  rS   typesr:  data_lenquery_attr_namesr   attr_valr(  r   _flagsr  rf   r   parameter_countbitr   a_typea_values                                r5   make_stmt_executezMySQLProtocol.make_stmt_execute  s    cc$i!m12 t9(*(e6O.OiG!N:D* &8H%&##d)a-A!56K3z?*$2  (o 8L
U=+qS1W~=+LL	7%//&:QQ .(%c*1-%.^^
 &/%5%5
s+
 33E:	"MM&)s+!LL1EMM%,,s5z":U"BC!*!1!1Ju-MM%,,s5z":U"BC!*!1!1Jw/MMSU):):7)C%DEe*++G45 "+!2!2Ju-MM&++dE":;!*!1!1J(9(98=='IJ+/+H+H+O(VZMM&)(:(:HMM'JK+/+C+CE+J(VZMM&)+J--g6EMM%,,s5z":U"BC!*!1!1J*!OO445Y@  U__Z85??6;RRS37X#5&sX~6q9@@ID$++ELLT,Cd,JKq8Lt OOL)OOE"OOO,
 "&[)99O&O&ell?;<MMKHS6;;sC0HIMM%//!,-E f%
 *qy8+&6ux7G&HIg.
 " 'g&' xx) Is   6W9)r4   r   returnr   )r4   r   rf  zTuple[str, bytes])rN   zDict[str, str]rf  r   )r_   r   r`   Optional[str]rf  r   )NFN)rh   r   ri   rS  rj   rS  rk   rS  rl   rg  rc   boolrm   Optional[Dict[str, Any]]rf  Tuple[bytes, MySQLAuthPlugin])r|   r#   ri   rS  rj   rS  r`   rg  r}   r   r_   r   r~   r   rk   rg  rl   rg  rN   zOptional[Dict[str, str]]r   rh  rc   rh  rm   ri  rf  rj  )r}   r   r_   r   r~   r   rf  r   rJ   )r   r   r   zOptional[bytes]rf  r   )r   )r   r   r   r   rf  r   )r   r   rf  r#   )r   r   rf  r$   )r   r   rf  Optional[int])r   )r   r   r   rS  rf  r!   )r   r   rf  r"   )T)r   r   r   rh  rf  r%   )r   N)
r   r'   r   zTuple[int, ...]r   r   r   rk  rf  zATuple[List[Tuple[Optional[bytes], ...]], Optional[EofPacketType]])r   r   r   r!   rf  Tuple[bytes, int])r   r   r   r!   rf  zTuple[bytes, float])rM  )r   r   r}   rS  rf  zTuple[bytes, Decimal])r   r   r  r   rf  z?Tuple[bytes, Optional[Union[datetime.date, datetime.datetime]]])r   r   rf  z Tuple[bytes, datetime.timedelta])r%  List[DescriptionType]r   r   r}   rS  rf  zTuple[BinaryProtocolType, ...])r   r   N)r   r'   r+  rm  r   r   r}   rS  r   rk  rf  zDTuple[List[Tuple[BinaryProtocolType, ...]], Optional[EofPacketType]])r   r   rf  zDict[str, int])r   r   rf  zTuple[bytes, int, int])r   z'Union[datetime.date, datetime.datetime]rf  rl  )r   z(Union[datetime.timedelta, datetime.time]rf  rl  )rH  r   rI  r   r   r   rf  r   ) rn  r   NrM  NF)r   r   r   zSequence[BinaryProtocolType]rW  r   r   r   rX  z Optional[Dict[int, Tuple[bool]]]r}   rS  rY  z.Optional[List[Tuple[str, BinaryProtocolType]]]rZ  rh  rf  r   )%rV  
__module____qualname____doc__staticmethodr6   rA   rF   r\   ra   rp   DEFAULT_CHARSET_IDDEFAULT_MAX_ALLOWED_PACKETr   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r)  r,  r1  r4  r;  rF  rJ  re  rn  r7   r5   r/   r/   X   s   
  * ) )4 ) )4 + +< 
 
(  ,0!26:,:,:, :, 	:,
 ):, :, 0:, 
':, :,x 
 #')"<%)+//3',!26w9 w9w9 w9  	w9
 w9 w9  w9 #w9 )w9 -w9 !%w9 w9 0w9 
'w9 w9r )"<


  
 
	
 
@ = =
 E E 1 1f  , I I 
 
@(  @ &*.. !. 	.
 $.
.` NN-N	N N2 NN-N	N N &,88 #8	8 8 --- 
I- -> + +4 	8%8 8 	8
 
(8| &*$$ '$ 	$
 $ $$
$L  " @ @B (A6(A	(A (AT 5A75A	5A 5An T T .0!;?FJ',E E  +E  	E 
 E  9E  E  DE  !%E  
E r7   r/   )>rq  
__future__r   r
  rT   collectionsr   decimalr   r   typingr   r   r	   r
   r   r   r   r   r   rH   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=   r3   	OK_STATUS
EOF_STATUSrD   
ERR_STATUSLOCAL_INFILE_STATUSrs  rt  r/   rn  r7   r5   <module>r     s   : 3 "    -
 
 
   ' R R  5 M   $    	


  ' j  j r7   