
    vd@                     z   d Z 	 ddlZdZn	#  dZY nxY wddlZddlZddlm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mZ ddlmZmZmZ ddlmZmZ dZdZ G d de          Zd Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de!          Z" G d de!          Z#dS )z4Implementation of communication for MySQL X servers.    NTFwraps   )MySQL41AuthPlugin)InterfaceErrorOperationalErrorProgrammingErrorSchema)ProtocolMessageReaderWriter)Result	RowResult	DocResult)SqlStatementAddStatementzDROP DATABASE IF EXISTS `{0}`z#CREATE DATABASE IF NOT EXISTS `{0}`c                   6    e Zd Zd Zd Zd Zd Zd Zi fdZdS )SocketStreamc                 "    d | _         d| _        d S )NF)_socket_is_sslselfs    M/var/www/html/t/fyr/venv311/lib/python3.11/site-packages/mysqlx/connection.py__init__zSocketStream.__init__2   s        c                     t          |t                    rt          j        nt          j        }t          j        |t          j                  | _        | j                            |           d S N)
isinstancetuplesocketAF_INETAF_UNIXSOCK_STREAMr   connect)r   paramss_types      r   r%   zSocketStream.connect6   sP    #-fe#<#<P&.}VV-?@@V$$$$$r   c                     | j         t          d          g }|dk    r\| j                             |          }|dk    rt          d          |                    |           |t          |          z  }|dk    \d                    |          S )NMySQLx Connection not availabler   r   zUnexpected connection close)r   r   recvRuntimeErrorappendlenjoin)r   countbufdatas       r   readzSocketStream.read;   s    <"#DEEEaii<$$U++Ds{{"#@AAAJJtSYYE aii xx}}r   c                 f    | j         t          d          | j                             |           d S )Nr)   )r   r   sendallr   r1   s     r   r4   zSocketStream.sendallG   s5    <"#DEEET"""""r   c                 X    | j         sd S | j                                          d | _         d S r   )r   closer   s    r   r7   zSocketStream.closeL   s0    | 	Fr   c                    t           s#|                                  t          d          t          j        t          j                  }|                                 d|v rk	 |                    |d                    t          j        |_	        n=# t          t          j        f$ r$ |                                  t          d          w xY wd|v rk	 |                    |d                    t          j        |_        n=# t          t          j        f$ r$ |                                  t          d          w xY wd|v ro	 |                    |d         |                    dd                      nd# t          t          j        f$ r$ |                                  t          d          w xY wd|v r#|                                  t          d	          |                    | j                  | _        d
| _        d S )Nz'Python installation has no SSL support.zssl-cazInvalid CA certificate.zssl-crlzInvalid CRL.zssl-certzssl-keyzInvalid Client Certificate/Key.z Client Certificate not provided.T)SSL_AVAILABLEr7   r+   ssl
SSLContextPROTOCOL_SSLv23load_default_certsload_verify_locationsCERT_REQUIREDverify_modeIOErrorSSLErrorr   VERIFY_CRL_CHECK_CHAINverify_flagsload_cert_chaingetwrap_socketr   r   )r   ssl_optscontexts      r   set_sslzSocketStream.set_sslS   s    	JJJLLLHIII.!455""$$$x@--hx.@AAA&)&7##S\* @ @ @

$%>???@   5--hy.ABBB'*'A$$S\* 5 5 5

$^4445 !!H''(<LLD113 3 3 3S\* H H H

$%FGGGH (""JJLLL !CDDD**4<88s$   ",B :C	,C> >:D8 0E1 1:F+N)	__name__
__module____qualname__r   r%   r2   r4   r7   rJ    r   r   r   r   1   sx          % % %

 
 
# # #
    " ! ! ! ! ! !r   r   c                 <     t                      fd            }|S )Nc                     	  | g|R i |S # t           j        t          f$ r$ |                                  t	          d          w xY w)NzCannot connect to host.)r!   errorr+   
disconnectr   )r   argskwargsfuncs      r   wrapperz(catch_network_exception.<locals>.wrapperx   si    	<4.t...v...l+ 	< 	< 	<OO !:;;;	<s	    :A	r   )rU   rV   s   ` r   catch_network_exceptionrW   w   s3    
4[[< < < < [< Nr   c                   8   e Zd Zd Zd Zd Zd Zd Zd Ze	d             Z
e	d             Ze	d	             Ze	d
             Ze	d             Ze	dd            Ze	d             Ze	d             Ze	d             Ze	d             Ze	d             Zd Zd Zd ZdS )
Connectionc                     |                     d          | _        |                     d          | _        |                     d          | _        d | _        || _        t                      | _        d | _        d | _	        d S )Nuserpasswordschema)
rF   _user	_password_schema_active_resultsettingsr   streamreader_writerprotocolr   rb   s     r   r   zConnection.__init__   sh    \\&))
!j11||H--" "nn!r   c                 X    | j         "| j                                          d | _         d S d S r   )ra   	fetch_allr   s    r   fetch_active_resultzConnection.fetch_active_result   s6    *))+++"&D +*r   c                     d| j         v r(| j         d         | j                             dd          fS d| j         v r| j         d         S dS )Nhostport$  r!   )	localhostrm   )rb   rF   r   s    r   _connection_paramszConnection._connection_params   sS    T]""=($-*;*;FE*J*JJJt}$$=**$$r   c                    | j                             |                                            t          | j                   | _        t          | j                  | _        |                                  |                                  d S r   )	rc   r%   ro   r   rd   r   re   _handle_capabilities_authenticater   s    r   r%   zConnection.connect   sr    D33556660== !344!!###r   c                 $   | j                                         j        }|r$|d         j                                        dk    sBn	 | j                            dd          r#|                                  t          d          d S t          j
        dk     r@| j                            dd          r#|                                  t          d          d S | j                             d	           | j                            | j                   d S )
Nr   tlsFz
ssl-enablezSSL not enabled at server.)      	   z=The support for SSL is not available for this Python version.T)rt   )re   get_capabilitescapabilitiesnamelowerrb   rF   r7   r   sysversion_infor+   set_capabilitiesrc   rJ   r5   s     r   rq   zConnection._handle_capabilities   s   },,..;15 	Q""$$----}  u55 E

&'CDDDFi''}  u55 ,

" $+ , , ,F&&4&000DM*****r   c                 P   t          | j        | j                  }| j                            |                                           | j                                        }| j                            |                    |                     | j        	                                 d S r   )
r   r^   r_   re   send_auth_start	auth_nameread_auth_continuesend_auth_continuebuild_authentication_responseread_auth_ok)r   plugin
extra_datas      r   rr   zConnection._authenticate   s    "4:t~>>%%f&6&6&8&8999]5577
((00<<	> 	> 	>""$$$$$r   c                 >    | j                             d||           d S )Nsql)re   send_execute_statement)r   r   rS   s      r   send_sqlzConnection.send_sql   s"    ,,UC>>>>>r   c                     | j                             |           d }t          |t                    r|j        }t          | |          S r   )re   send_insertr   r   _idsr   )r   	statementidss      r   r   zConnection.send_insert   sF    !!),,,i.. 	!.CdC   r   c                     | j                             |           |j        rt          |           nt	          |           S r   )re   	send_find
_doc_basedr   r   r   r   s     r   findzConnection.find   s6    	***"+"6KyIdOOKr   c                 T    | j                             |           t          |           S r   )re   send_deleter   r   s     r   deletezConnection.delete   $    !!),,,d||r   c                 T    | j                             |           t          |           S r   )re   send_updater   r   s     r   updatezConnection.update   r   r   Tc                 X    | j                             |||           t          |           S r   )re   r   r   )r   	namespacecmdraise_on_failrS   s        r   execute_nonqueryzConnection.execute_nonquery   s(    ,,YTBBBd||r   c                     | j                             d||           t          |           }|                                 |j        dk    rt          d          |d         d         S )Nr   r   zNo data found)re   r   r   rh   r/   r   )r   r   rS   results       r   execute_sql_scalarzConnection.execute_sql_scalar   sb    ,,UC>>>4<1 111ay|r   c                 X    | j                             d||           t          |           S )Nxplugin)re   r   r   )r   r   rS   s      r   get_row_resultzConnection.get_row_result   s(    ,,YTBBBr   c                 6    | j                             |          S r   )re   read_rowr   r   s     r   r   zConnection.read_row   s    }%%f---r   c                 :    | j                             |           d S r   )re   close_resultr   s     r   r   zConnection.close_result   s    ""6*****r   c                 6    | j                             |          S r   )re   get_column_metadatar   s     r   r   zConnection.get_column_metadata   s    }00888r   c                     | j         j        d uS r   )rc   r   r   s    r   is_openzConnection.is_open   s    {"$..r   c                 d    |                                  sd S | j                                         d S r   )r   rc   r7   r   s    r   rR   zConnection.disconnect   s1    ||~~ 	Fr   c                    |                                  sd S | j        | j                                         | j                                         | j                                         | j                                         d S r   )r   ra   rh   re   
send_closeread_okrc   r7   r   s    r   r7   zConnection.close   sw    ||~~ 	F*))+++  """r   N)T)rK   rL   rM   r   ri   ro   r%   rq   rr   rW   r   r   r   r   r   r   r   r   r   r   r   r   rR   r7   rN   r   r   rY   rY      s         ' ' '
% % %  + + +$% % % ? ? ? ! ! ! L L L                 . . . + + + 9 9 9/ / /  
    r   rY   c                   R     e Zd Z fdZd Z fdZ fdZd Z fdZ fdZ	 xZ
S )XConnectionc                    t          t          |                               |           g | _        |                    dg           | _        d|v rL|d         rD| j                            |                    d          |                    dd           d           d| _        d| _        | 	                                 | j        
                    d d           d S )	Nroutersrk   rl   )rk   rl   Tc                     | d         S )NpriorityrN   )xs    r   <lambda>z&XConnection.__init__.<locals>.<lambda>  s
    : r   )keyreverse)superr   r   dependent_connectionspop_routersr,   _cur_router_can_failover_ensure_prioritiessortr   rb   	__class__s     r   r   zXConnection.__init__  s    k4  ))(333%'" Y33X(6"2M   V,, VT22" "   
 !!!!66EEEEEr   c                    d}d}| j         D ]@}|                    dd           }||dz  }||d<   n|dk    rt          dd          |dz  }Ad|cxk     rt          | j                   k     rn d S t          dd          d S )	Nr   d   r   r   z(The priorities must be between 0 and 100i  z\You must either assign no priority to any of the routers or give a priority for every routeri  )r   rF   r	   r-   )r   priority_countr   routerpris        r   r   zXConnection._ensure_priorities  s    m 	 	F**Z..C{!#%-z""s& (! ! !MHH~2222DM 2 2222222" $EFJL L L 32r   c                    | j         s.d| _        t          t          |                                           S | j        r	| j        du rd| _        d| _        | j         D ]}d|d<   | xj        dz  c_        | j         | j                 d         }| j         | j                 d         }| j        dk    rd| j         | j        dz
           d<   | j        t          | j                   dz
  k    rd| _        ||fS )	NFr   T	availabler   rk   rl   r   )r   r   r   r   ro   r   r-   )r   r   rk   rl   r   s       r   ro   zXConnection._connection_params)  s   } 	A!&Dd++>>@@@ ! 	+T%5%;%;!D!%D- + +&*{##A}T-.v6}T-.v6a=BDM$*1,-k:s4=11A555!&Dd}r   c                 D   d }| j         rM	 t          t          |                                           S # t          j        $ r}|}Y d }~nd }~ww xY w| j         Mt          | j                  dk    r"t          d	                    |                    t          dd          )Nr   Cannot connect to host: {0}z(Failed to connect to any of the routers.i  )
r   r   r   r%   r!   rQ   r-   r   r   format)r   rQ   errr   s      r   r%   zXConnection.connect@  s      	[$//77999<      	 t}"" !>!E!Ee!L!LMMMGNNNs   &3 AA		Ac                 :    | j                             |           d S r   )r   r,   )r   
connections     r   bind_connectionzXConnection.bind_connectionM  s    "))*55555r   c                     | j         r2| j                                                                          | j         2t          t          |                                            d S r   )r   r   r7   r   r   r   r   s    r   r7   zXConnection.closeP  s^    ( 	5&**,,22444 ( 	5k4  &&(((((r   c                     | j         r2| j                                                                          | j         2t          t          |                                            d S r   )r   r   rR   r   r   r   s    r   rR   zXConnection.disconnectU  s^    ( 	:&**,,77999 ( 	:k4  ++-----r   )rK   rL   rM   r   r   ro   r%   r   r7   rR   __classcell__r   s   @r   r   r     s        F F F F F L L L$    .O O O O O6 6 6) ) ) ) )
. . . . . . . . .r   r   c                   (     e Zd Z fdZ fdZ xZS )NodeConnectionc                 X    t          t          |                               |           d S r   )r   r   r   r   s     r   r   zNodeConnection.__init__\  s'    nd##,,X66666r   c                     	 t          t          |                                            d S # t          j        $ r'}t          d                    |                    d }~ww xY w)Nr   )r   r   r%   r!   rQ   r   r   )r   r   r   s     r   r%   zNodeConnection.connect_  sm    	L.$''//11111| 	L 	L 	L !>!E!Ec!J!JKKK	Ls   ', A""AA")rK   rL   rM   r   r%   r   r   s   @r   r   r   [  sZ        7 7 7 7 7L L L L L L L L Lr   r   c                   N    e 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S )BaseSessiona  Base functionality for Session classes through the X Protocol.

    This class encloses the core functionality to be made available on both
    the XSession and NodeSession classes, such functionality includes:

        - Accessing available schemas.
        - Schema management operations.
        - Enabling/disabling warning generation.
        - Retrieval of connection information.

    Args:
        settings (dict): Connection data used to connect to the database.
    c                     || _         d S r   )	_settingsrf   s     r   r   zBaseSession.__init__t  s    !r   c                 (    | j         j        j        d uS r   )_connectionrc   r   r   s    r   r   zBaseSession.is_openw  s    &.d::r   c                 "    t          | |          S )zRetrieves a Schema object from the current session by it's name.

        Args:
            name (string): The name of the Schema object to be retrieved.

        Returns:
            mysqlx.Schema: The Schema object with the given name.
        r
   r   rz   s     r   
get_schemazBaseSession.get_schemaz  s     dD!!!r   c                     | j         j                            d          r t          | | j         j        d                   S t	          d          )aK  Retrieves a Schema object from the current session by the schema
        name configured in the connection settings.

        Returns:
            mysqlx.Schema: The Schema object with the given name at connect
                           time.

        Raises:
            ProgrammingError: If default schema not provided.
        r]   zDefault schema not provided)r   rb   rF   r   r	   r   s    r   get_default_schemazBaseSession.get_default_schema  sK     $((22 	E$ 0 9( CDDD<===r   c                 n    | j                             dt                              |          d           dS )zDrops the schema with the specified name.

        Args:
            name (string): The name of the Schema object to be retrieved.
        r   TN)r   r   _DROP_DATABASE_QUERYr   r   s     r   drop_schemazBaseSession.drop_schema  s?     	))'..t44d	< 	< 	< 	< 	<r   c                     | j                             dt                              |          d           t	          | |          S )zCreates a schema on the database and returns the corresponding
        object.

        Args:
            name (string): A string value indicating the schema name.
        r   T)r   r   _CREATE_DATABASE_QUERYr   r   r   s     r   create_schemazBaseSession.create_schema  sE     	)))0066	> 	> 	>dD!!!r   c                 >    | j                             ddd           dS )z4Starts a transaction context on the server.
        r   zSTART TRANSACTIONTNr   r   r   s    r   start_transactionzBaseSession.start_transaction  s&     	))%1DdKKKKKr   c                 >    | j                             ddd           dS )zXCommits all the operations executed after a call to
        startTransaction().
        r   COMMITTNr   r   s    r   commitzBaseSession.commit  s%     	))%4@@@@@r   c                 >    | j                             ddd           dS )zYDiscards all the operations executed after a call to
        startTransaction().
        r   ROLLBACKTNr   r   s    r   rollbackzBaseSession.rollback  s%     	))%TBBBBBr   c                 8    | j                                          d S r   )r   r7   r   s    r   r7   zBaseSession.close  s         r   N)rK   rL   rM   __doc__r   r   r   r   r   r   r   r   r   r7   rN   r   r   r   r   f  s         " " "; ; ;	" 	" 	"> > >< < <	" 	" 	"L L L
A A AC C C! ! ! ! !r   r   c                   (     e Zd ZdZ fdZd Z xZS )XSessionaW  Enables interaction with a X Protocol enabled MySQL Product.

    The functionality includes:

    - Accessing available schemas.
    - Schema management operations.
    - Enabling/disabling warning generation.
    - Retrieval of connection information.

    Args:
        settings (dict): Connection data used to connect to the database.
    c                     t          t          |                               |           t          | j                  | _        | j                                         d S r   )r   r   r   r   r   r   r%   r   s     r   r   zXSession.__init__  sN    h&&x000&t~66  """""r   c                     |                                  st          d          t          | j                  }| j                            |j                   |S )Nz$XSession is not connected to a farm.)r   r   NodeSessionr   r   r   )r   nsesss     r   bind_to_default_shardzXSession.bind_to_default_shard  sP    ||~~ 	K"#IJJJDN++(():;;;r   )rK   rL   rM   r   r   r  r   r   s   @r   r   r     sQ         # # # # #
      r   r   c                   (     e Zd ZdZ fdZd Z xZS )r  at  Enables interaction with a X Protocol enabled MySQL Server.

    The functionality includes:

    - Accessing available schemas.
    - Schema management operations.
    - Enabling/disabling warning generation.
    - Retrieval of connection information.
    - Includes SQL Execution.

    Args:
        settings (dict): Connection data used to connect to the database.
    c                     t          t          |                               |           t          | j                  | _        | j                                         d S r   )r   r  r   r   r   r   r%   r   s     r   r   zNodeSession.__init__  sN    k4  ))(333)$.99  """""r   c                 ,    t          | j        |          S )z}Creates a :class:`mysqlx.SqlStatement` object to allow running the
        SQL statement on the target MySQL Server.
        )r   r   )r   r   s     r   r   zNodeSession.sql  s     D,c222r   )rK   rL   rM   r   r   r   r   r   s   @r   r  r    sQ         # # # # #
3 3 3 3 3 3 3r   r  )$r   r:   r9   r|   r!   	functoolsr   authenticationr   errorsr   r   r	   crudr   re   r   r   r   r   r   r   r   r   r   r   r   objectr   rW   rY   r   r   r   r   r  rN   r   r   <module>r     sa  0 ; :JJJMMMMM 



        - - - - - - F F F F F F F F F F       3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 7 > C C C C C6 C C CL  A A A A A A A AHR. R. R. R. R.* R. R. R.jL L L L LZ L L LT! T! T! T! T!& T! T! T!n    {   63 3 3 3 3+ 3 3 3 3 3s    