
    Dh@                     L   d Z 	 ddlZdZ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#y#  dZY xY w)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                   4    e Zd Zd Zd Zd Zd Zd Zi fdZy)SocketStreamc                      d | _         d| _        y )NF)_socket_is_sslselfs    Q/var/www/html/civilisation/venv/lib/python3.12/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                  j                  |       y N)
isinstancetuplesocketAF_INETAF_UNIXSOCK_STREAMr   connect)r   paramss_types      r   r%   zSocketStream.connect6   sD    #-fe#<&..}}VV-?-?@V$r   c                     | j                   t        d      g }|dkD  rP| j                   j                  |      }|dk(  rt        d      |j	                  |       |t        |      z  }|dkD  rPdj                  |      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ai<<$$U+Ds{"#@AAJJtSYE ai xx}r   c                 h    | j                   t        d      | j                   j                  |       y )Nr)   )r   r   sendallr   r1   s     r   r4   zSocketStream.sendallG   s*    <<"#DEET"r   c                 `    | j                   sy | j                   j                          d | _         y r   )r   closer   s    r   r7   zSocketStream.closeL   s#    ||r   c                    t         s| j                          t        d      t        j                  t        j
                        }|j                          d|v r*	 |j                  |d          t        j                  |_	        d|v r*	 |j                  |d          t        j                  |_        d|v r'	 |j                  |d   |j!                  dd              nd|v r| j                          t        d	      |j#                  | j$                        | _        d
| _        y # t        t        j                  f$ r | j                          t        d      w xY w# t        t        j                  f$ r | j                          t        d      w xY w# t        t        j                  f$ r | j                          t        d      w xY w)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   JJLHII..!4!45""$x@--hx.@A&)&7&7#  5--hy.AB'*'A'A$ !H''(<LLD13
 ("JJL !CDD**4<<8- S\\* @

$%>??@ S\\* 5

$^445 S\\* H

$%FGGHs$   )D# )E 6%F #5E5F5GN)	__name__
__module____qualname__r   r%   r2   r4   r7   rJ    r   r   r   r   1   s%    %

#
  " !r   r   c                 .     t                fd       }|S )Nc                     	  | g|i |S # t         j                  t        f$ r | j                          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   sK    	<.t.v..l+ 	<OO !:;;	<s	    5Ar   )rU   rV   s   ` r   catch_network_exceptionrW   w   s     
4[< < Nr   c                       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y)
Connectionc                     |j                  d      | _        |j                  d      | _        |j                  d      | _        d | _        || _        t               | _        d | _        d | _	        y )Nuserpasswordschema)
rF   _user	_password_schema_active_resultsettingsr   streamreader_writerprotocolr   rb   s     r   r   zConnection.__init__   sY    \\&)
!j1||H-" "n!r   c                 `    | j                   "| j                   j                          d | _         y y r   )ra   	fetch_allr   s    r   fetch_active_resultzConnection.fetch_active_result   s-    *))+"&D +r   c                     d| j                   v r+| j                   d   | j                   j                  dd      fS d| j                   v r| j                   d   S y)Nhostport$  r!   )	localhostrm   )rb   rF   r   s    r   _connection_paramszConnection._connection_params   sQ    T]]"==($--*;*;FE*JJJt}}$==**$r   c                     | j                   j                  | j                                t        | j                         | _        t        | j                        | _        | j                          | j                          y r   )	rc   r%   ro   r   rd   r   re   _handle_capabilities_authenticater   s    r   r%   zConnection.connect   sX    D33560= !3!34!!#r   c                    | j                   j                         j                  }|r!|d   j                  j	                         dk(  s:n	 | j
                  j                  dd      r| j                          t        d      y t        j                  dk  r8| j
                  j                  dd      r| j                          t        d      y | j                   j                  d	       | j                  j                  | j
                         y )
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""$-5}}  u5

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

" $+ , ,&&4&0DMM*r   c                 R   t        | j                  | j                        }| j                  j	                  |j                                | j                  j                         }| j                  j                  |j                  |             | j                  j                          y 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   ss    "4::t~~>%%f&6&6&89]]557
((00<	>""$r   c                 >    | j                   j                  d||       y )Nsql)re   send_execute_statement)r   r   rS   s      r   send_sqlzConnection.send_sql   s    ,,UC>r   c                     | j                   j                  |       d }t        |t              r|j                  }t        | |      S r   )re   send_insertr   r   _idsr   )r   	statementidss      r   r   zConnection.send_insert   s9    !!),i...CdC  r   c                 |    | j                   j                  |       |j                  rt        |       S t	        |       S r   )re   	send_find
_doc_basedr   r   r   r   s     r   findzConnection.find   s/    	*"+"6"6yKIdOKr   c                 N    | j                   j                  |       t        |       S r   )re   send_deleter   r   s     r   deletezConnection.delete       !!),d|r   c                 N    | j                   j                  |       t        |       S r   )re   send_updater   r   s     r   updatezConnection.update   r   r   c                 R    | j                   j                  |||       t        |       S r   )re   r   r   )r   	namespacecmdraise_on_failrS   s        r   execute_nonqueryzConnection.execute_nonquery   s"    ,,YTBd|r   c                     | j                   j                  d||       t        |       }|j                          |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   sR    ,,UC>4<<1 11ay|r   c                 R    | j                   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r   c                 8    | j                   j                  |      S r   )re   read_rowr   r   s     r   r   zConnection.read_row   s    }}%%f--r   c                 :    | j                   j                  |       y r   )re   close_resultr   s     r   r   zConnection.close_result   s    ""6*r   c                 8    | j                   j                  |      S r   )re   get_column_metadatar   s     r   r   zConnection.get_column_metadata   s    }}0088r   c                 2    | j                   j                  d uS r   )rc   r   r   s    r   is_openzConnection.is_open   s    {{""$..r   c                 Z    | j                         sy | j                  j                          y r   )r   rc   r7   r   s    r   rR   zConnection.disconnect   s    ||~r   c                    | j                         sy | j                  | j                  j                          | j                  j	                          | j                  j                          | j                  j                          y r   )r   ra   rh   re   
send_closeread_okrc   r7   r   s    r   r7   zConnection.close   s\    ||~*))+  "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           . . + + 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                 l   t         t        |   |       g | _        |j	                  dg       | _        d|v rB|d   r=| j
                  j                  |j	                  d      |j	                  dd       d       d| _        d| _        | j                          | j
                  j                  d d       y )	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%'" Y3X(6"2MM   V, VT2" 
 !!6Er   c                     d}d}| j                   D ]7  }|j                  dd       }||dz  }||d<   n|dkD  rt        dd      |dz  }9 d|cxk  rt        | j                         k  rn y t        dd      y )	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    mm 	F**Z.C{!#%-z"s& (! !MH	 ~2DMM 22" $EFJL L 3r   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kD  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   }}!&Dd>@@ !!T%5%5%;!D!%D-- +&*{#+ 	A}}T--.v6}}T--.v6a=BDMM$**1,-k:s4==1A55!&Dd}r   c                 "   d }| j                   r	 t        t        |          S t        | j                        dk  rt        dj                  |            t        dd      # t        j
                  $ r}|}Y d }~nd }~ww xY w| j                   rn)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99 t}}" !>!E!Ee!LMMGNN <<    s   A! !B 4A;;B c                 :    | j                   j                  |       y r   )r   r,   )r   
connections     r   bind_connectionzXConnection.bind_connectionM  s    ""))*5r   c                     | j                   r5| j                   j                         j                          | j                   r5t        t        |           y r   )r   r   r7   r   r   r   r   s    r   r7   zXConnection.closeP  s?    ((&&**,224 ((k4&(r   c                     | j                   r5| j                   j                         j                          | j                   r5t        t        |           y r   )r   r   rR   r   r   r   s    r   rR   zXConnection.disconnectU  s?    ((&&**,779 ((k4+-r   )rK   rL   rM   r   r   ro   r%   r   r7   rR   __classcell__r   s   @r   r   r     s.    F L$.O6)
. .r   r   c                   (     e Zd Z fdZ fdZ xZS )NodeConnectionc                 ,    t         t        |   |       y r   )r   r   r   r   s     r   r   zNodeConnection.__init__\  s    nd,X6r   c                     	 t         t        |           y # t        j                  $ r}t        dj                  |            d }~ww xY w)Nr   )r   r   r%   r!   rQ   r   r   )r   r   r   s     r   r%   zNodeConnection.connect_  sC    	L.$/1|| 	L !>!E!Ec!JKK	Ls    AAA)rK   rL   rM   r   r%   r   r   s   @r   r   r   [  s    7L Lr   r   c                   L    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y)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                     || _         y r   )	_settingsrf   s     r   r   zBaseSession.__init__t  s	    !r   c                 F    | 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                  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  sH     $$((2$ 0 0 9 9( CDD<==r   c                 d    | j                   j                  dt        j                  |      d       y)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4d	<r   c                 z    | j                   j                  dt        j                  |      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  s8     	)))006	>dD!!r   c                 >    | j                   j                  ddd       y)z4Starts a transaction context on the server.
        r   zSTART TRANSACTIONTNr   r   r   s    r   start_transactionzBaseSession.start_transaction  s     	))%1DdKr   c                 >    | j                   j                  ddd       y)zXCommits all the operations executed after a call to
        startTransaction().
        r   COMMITTNr   r   s    r   commitzBaseSession.commit  s     	))%4@r   c                 >    | j                   j                  ddd       y)zYDiscards all the operations executed after a call to
        startTransaction().
        r   ROLLBACKTNr   r   s    r   rollbackzBaseSession.rollback  s     	))%TBr   c                 8    | j                   j                          y 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
A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
                  j                          y r   )r   r   r   r   r   r   r%   r   s     r   r   zXSession.__init__  s5    h&x0&t~~6  "r   c                     | j                         st        d      t        | j                        }| 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  sD    ||~"#IJJDNN+(():):;r   )rK   rL   rM   r   r   r  r   r   s   @r   r   r     s    #
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
                  j                          y r   )r   r  r   r   r   r   r%   r   s     r   r   zNodeSession.__init__  s5    k4)(3)$..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2r   )rK   rL   rM   r   r   r   r   r   s   @r   r  r    s    #
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     s   0 ;M    - F F  3 0 0 1 7 > C6 CLA AHR.* R.jLZ LT!& T!n{ 63+ 3wMs   B B#