
    	i[                     z    d dl Z d dlZd dlZ d dlmZmZmZ d dlmZ dZ	dZ
dZ G d de j        j                  ZdS )	    N)STATUS	TPC_STATEINFO)versionutf8mb4utf8mb4_general_ci@   c                       e Zd ZdZd Z fdZej        j        fdZ	 fdZ
d Zd Zd Zd	 Zd
efdZd ZdefdZd Zd Z G d de          Zd Zd1dZd Zd1dZd Zed             Zed             Zej         d             Zed             Z!ed             Z"ed             Z#ed             Z$ed              Z%ed!             Z&ed"             Z'ed#             Z(ed$             Z)ed%             Z*ed&             Z+ed'             Z,ed(             Z-ed)             Z.ed*             Z/ed+             Z0ed,             Z1e1j         d-             Z1ed.             Z2ed/             Z3e"Z4ed0             Z5 xZ6S )2
Connectionz
    MariaDB Connector/Python Connection Object

    Handles the connection to a MariaDB or MySQL database server.
    It encapsulates a database session.

    Connections are created using the method mariadb.connect()
    c                 <    | j         rt          j        d          d S )Nz#Invalid connection or not connected)_closedmariadbProgrammingErrorselfs    K/srv/django_bis/venv311/lib/python3.11/site-packages/mariadb/connections.py_check_closedzConnection._check_closed*   s0    < 	<* ,; < < <	< 	<    c                    d| _         d| _        d| _        d| _         d| _        d| _        t
          j        | _        d| _        |	                    dd          }|	                    dd          }|	                    dd          | _
        d|v ra|                    d          }t          j        t          j                  t          j        d          k     rd	|v rt          j        d
          d|v rMt#          |d         t$                    s2|	                    dd          }dD ]}||v r||         |d|z  <   d|d<    t'                      j        |i | || _        || _        dS )zh
        Establishes a connection to a database server and returns a connection
        object.
        Nr   
autocommitF	reconnect	converterhostz3.3.0,z>Host failover list requires MariaDB Connector/C 3.3.0 or newerssl)cacertcapathkeycipherzssl_%sT)_socket_used_last_executed_statement_Connection__pool_Connection__last_usedr   NONE	tpc_state_xidpop
_convertergetr   Versionr   mariadbapi_versionr   
isinstanceboolsuper__init__r   auto_reconnect)	r   argskwargsr   r   r   r   r   	__class__s	           r   r1   zConnection.__init__/   sy    
(,%"	ZZe44
JJ{E22	 **[$77 V::f%%Dw9::w''( (,/4KK. 0: ; ; ; F??:fUmT#B#B?**UD))C@ 6 6#::-0XF8c>* F5M$)&)))$'r   c                     |                                    || fi |}t          |t          j        j                  st          j        d|z            |S )ad  
        Returns a new cursor object for the current connection.

        If no cursorclass was specified, a cursor with default mariadb.Cursor class will be created.

        Optional keyword parameters:

        - **buffered** (default: ``True``) - If disabled, the result will be unbuffered, which means before executing another statement with the same connection, the entire result set must be fetched. Please note that the default was False for MariaDB Connector/Python versions < 1.1.0.
        - **dictionary** (default: ``False``) - Return fetch values as dictionary when enabled.
        - **named_tuple** (default: ``False``) - Return fetch values as named tuple. This feature exists for compatibility reasons and should be avoided due to possible inconsistency.
        - **cursor_type** (default: ``CURSOR.NONE``) - If cursor_type is set to CURSOR.READ_ONLY, a cursor is opened for the statement invoked with cursors execute() method.
        - **prepared** (default: ``False``) - When enabled, the cursor will remain in prepared state after the first execute() method was called. Further calls to execute() method will ignore the SQL statement.
        - **binary** (default: ``False``) - Always execute statement in MariaDB client/server binary protocol.

        In versions prior to 1.1.0 results were unbuffered by default, which means before executing another statement with the same connection, the entire result set must be fetched.

        fetch* methods of the cursor class by default return result set values as a tuple, unless dictionary or named_tuple was specified. The latter one exists for compatibility reasons and should be avoided due to possible inconsistency in case two or more fields in a result set have the same name.

        If cursor_type is set to CURSOR.READ_ONLY, a cursor is opened for the statement invoked with cursors execute() method.
        z'%s is not an instance of mariadb.cursor)r   r.   r   _mariadbcursorr   )r   cursorclassr4   r8   s       r   r8   zConnection.cursorY   sq    * 	T,,V,,&'"2"9:: 	F* ,<>D,E F F Fr   c                     |                                   | j        r| j                            |            d S t                                                       d S N)r   r$   _close_connectionr0   close)r   r5   s    r   r=   zConnection.closeu   sR    ! 	"44T:::::GGMMOOOOOr   c                 0    |                                   	 | S r;   )r   r   s    r   	__enter__zConnection.__enter__|   s    +r   c                 X    |                                   	 |                                  d S r;   )r   r=   )r   exc_typeexc_valexc_tbs       r   __exit__zConnection.__exit__   s(    

r   c                     |                                   | j        t          j        k    rt	          j        d          |                     d           |                                  dS )zA
        Commit any pending transaction to the database.
        z6commit() is not allowed if a TPC transaction is activeCOMMITNr   r'   r   r&   r   r   _execute_command_read_responser   s    r   commitzConnection.commit   so    
 	>IN*** ,I J J Jh'''r   c                     |                                   | j        t          j        k    rt	          j        d          |                     d           |                                  dS )av  
        Causes the database to roll back to the start of any pending
        transaction

        Closing a connection without committing the changes first will
        cause an implicit rollback to be performed.
        Note that rollback() will not work as expected if autocommit mode
        was set to True or the storage engine does not support transactions."
        z8rollback() is not allowed if a TPC transaction is activeROLLBACKNrG   r   s    r   rollbackzConnection.rollback   so     	>IN*** ,G H H Hj)))r   idc                     |                                   t          |t                    st          j        d          d|z  }|                     |           |                                  dS )z
        This function is used to ask the server to kill a database connection
        specified by the processid parameter.

        The connection id can be retrieved by `SHOW PROCESSLIST` SQL command.
        zid must be of type int.zKILL %sN)r   r.   intr   r   rH   rI   )r   rN   stmts      r   killzConnection.kill   sp     	"c"" 	F*+DEEE2~d###r   c                     |                                   |                     d           |                                  dS )z
        Start a new transaction which can be committed by .commit() method,
        or canceled by .rollback() method.
        BEGINN)r   rH   rI   r   s    r   beginzConnection.begin   sB    
 	g&&&r   new_dbc                 <    |                                   || _        dS )z
        Gets the default database for the current connection.

        The default database can also be obtained or changed by database
        attribute.
        N)r   database)r   rV   s     r   	select_dbzConnection.select_db   s!     	r   c                     | j         S )z
        Returns a tuple representing the version of the connected server in
        the following format: (MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
        )server_version_infor   s    r   get_server_versionzConnection.get_server_version   s     ''r   c                     |                                   | j        sdS |                                 }|                    d           |                                }~|S )zT
        Shows error, warning and note messages from last executed command.
        NzSHOW WARNINGS)r   warningsr8   executefetchall)r   r8   rets      r   show_warningszConnection.show_warnings   s[    
 	 	4'''oo
r   c                   "     e Zd ZdZ fdZ xZS )Connection.xida  
        xid(format_id: int, global_transaction_id: str, branch_qualifier: str)

        Creates a transaction ID object suitable for passing to the .tpc_*()
        methods of this connection.

        Parameters:

        - format_id: Format id. Default to value `0`.

        - global_transaction_id: Global transaction qualifier, which must be
          unique. The maximum length of the global transaction id is
          limited to 64 characters.

        - branch_qualifier: Branch qualifier which represents a local
          transaction identifier. The maximum length of the branch qualifier
          is limited to 64 characters.

        c                 v   t          |t                    s't          j        dt	          |          j                  t          |t                    s't          j        dt	          |          j                  t          |t                    s't          j        dt	          |          j                  t          |          t          k    rt          j        d          t          |          t          k    rt          j        d          |dk    rd}t                                          | |||f          S )Nzargument 1 must be int, not %szargument 2 must be str, not %szargument 3 must be str, not %sz*Maximum length of transaction_id exceeded.z,Maximum length of branch_qualifier exceeded.r      )r.   rP   r   r   type__name__str__mane__len_MAX_TPC_XID_SIZEr0   __new__)r   	format_idtransaction_idbranch_qualifierr5   s       r   rm   zConnection.xid.__new__   s^   i-- I. 08/3I/GI I I nc22 N. 08/3N/C/C/LN N N .44 N. 08/3N/C/C/LN N N >""%666. 0J K K K#$$'888. 0L M M MA~~	77??4)*8*:*< = = =r   )rh   
__module____qualname____doc__rm   __classcell__r5   s   @r   xidrd      sB        	 	&	= 	= 	= 	= 	= 	= 	= 	= 	=r   rv   c                    |                                   t          |          j        dk    r't          j        dt          |          j                  d|d         d|d         d|d         }	 |                     |           |                                  n# t          j        $ r  w xY wt          j	        | _
        || _        d	S )
a?  
        Parameter:
          xid: xid object which was created by .xid() method of connection
               class

        Begins a TPC transaction with the given transaction ID xid.

        This method should be called outside a transaction
        (i.e., nothing may have been executed since the last .commit()
        or .rollback()).
        Furthermore, it is an error to call .commit() or .rollback() within
        the TPC transaction. A ProgrammingError is raised if the application
        calls .commit() or .rollback() during an active TPC transaction.
        rv   argument 1 must be xid not %sz
XA BEGIN 'rf   ','   ',r   N)r   rg   rh   r   r   rH   rI   Errorr   XIDr'   r(   r   rv   rQ   s      r   	tpc_beginzConnection.tpc_begin  s      	99&&* ,459#YY5GI I I ' +.a&&&#a&&&#a&&A	!!$'''!!!!} 	 	 		"			s   2)B B-Nc                    |                                   |s| j        }| j        t          j        k    rt          j        d          |)| j        t          j        k    rt          j        d          |rAt          |          j	        dk    r)t          j        dt          |          j	        z            | j        t          j        k     rtd|d         d|d	         d
|d         }| 
                    |           	 |                                  n,# t
          j        $ r d| _        t          j        | _         w xY wd|d         d|d	         d
|d         }| j        t          j        k     r|dz   }	 | 
                    |           |                                  n,# t
          j        $ r d| _        t          j        | _         w xY wd| _        t          j        | _        dS )a  
        Optional parameter:

        - xid
          : xid object which was created by .xid() method of connection class.

        When called with no arguments, .tpc_commit() commits a TPC transaction
        previously prepared with .tpc_prepare().

        If .tpc_commit() is called prior to .tpc_prepare(), a single phase
        commit is performed. A transaction manager may choose to do this if
        only a single resource is participating in the global transaction.
        When called with a transaction ID xid, the database commits the given
        transaction. If an invalid transaction ID is provided,
        a ProgrammingError will be raised.
        This form should be called outside a transaction, and
        is intended for use in recovery.
        Transaction not started.NzTransaction is not prepared.rv   rx   XA END 'rf   ry   rz   r{   r   zXA COMMIT 'z
 ONE PHASE)r   r(   r'   r   r&   r   r   PREPARErg   rh   rH   rI   r|   r~   s      r   
tpc_commitzConnection.tpc_commit*  s   ( 	 	)C>Y^++*+EFFF;4>Y->>>*+IJJJ 	J499%..* ,46:3ii6H,I J J J >I----,/FFFCFFFCFFCD!!$'''##%%%%=    	!*
 ,/q6663q6663q66B>I---,&D	!!$'''!!!!} 	 	 	DI&^DN	 	"s   =D )D;6)F   )G	c                    |                                   | j        t          j        k    rt	          j        d          | j        t          j        k    rt	          j        d          | j        }d|d         d|d         d|d         }	 |                     |           | 	                                 n,# t          j
        $ r d	| _        t          j        | _         w xY wd
|d         d|d         d|d         }	 |                     |           | 	                                 n,# t          j
        $ r d	| _        t          j        | _         w xY wt          j        | _        d	S )aC  
        Performs the first phase of a transaction started with .tpc_begin().
        A ProgrammingError will be raised if this method was called outside
        a TPC transaction.

        After calling .tpc_prepare(), no statements can be executed until
        .tpc_commit() or .tpc_rollback() have been called.
        r   z)Transaction is already in prepared state.r   rf   ry   rz   r{   r   NzXA PREPARE ')r   r'   r   r&   r   r   r   r(   rH   rI   r|   r~   s      r   tpc_preparezConnection.tpc_preparec  s    	>Y^++*+EFFF>Y...* ,= > > > i(+AAA?	!!$'''!!!!} 	 	 	DI&^DN	 " -0FFFCFFFCFFC	!!$'''!!!!} 	 	 	DI&^DN	
 #*s   )B6 6)C )D* *)Ec                 4   |                                   | j        t          j        k    rt	          j        d          |rAt          |          j        dk    r)t	          j        dt          |          j        z            |s| j        }| j        t          j	        k     rtd|d         d|d         d|d	         }| 
                    |           	 |                                  n,# t          j        $ r d
| _        t          j        | _         w xY wd|d         d|d         d|d	         }	 | 
                    |           |                                  n,# t          j        $ r d
| _        t          j        | _         w xY wt          j	        | _        d
S )a  
        Parameter:
           xid: xid object which was created by .xid() method of connection
                class

        Performs the first phase of a transaction started with .tpc_begin().
        A ProgrammingError will be raised if this method outside a TPC
        transaction.

        After calling .tpc_prepare(), no statements can be executed until
        .tpc_commit() or .tpc_rollback() have been called.
        r   rv   rx   r   rf   ry   rz   r{   r   NzXA ROLLBACK ')r   r'   r   r&   r   r   rg   rh   r(   r   rH   rI   r|   r~   s      r   tpc_rollbackzConnection.tpc_rollback  s    	>Y^++*+EFFF 	J499%..* ,46:3ii6H,I J J J  	)C>I----,/FFFCFFFCFFCD!!$'''##%%%%=    	!*
 .1VVVSVVVSVVD	!!$'''!!!!} 	 	 	DI&^DN	
 #*s   C' ')D1)E )Fc                     |                                   |                                 }|                    d           |                                }~|S )z
        Returns a list of pending transaction IDs suitable for use with
        tpc_commit(xid) or .tpc_rollback(xid).
        z
XA RECOVER)r   r8   r_   r`   )r   r8   results      r   tpc_recoverzConnection.tpc_recover  sL     	|$$$""r   c                     t          j        t          j                  t          j        d          k     rdS |                                  | j        r|                     t          j                  S dS )z!Get peer certificate information.3.4.2N)	r   r,   r   r-   r   _tls_mariadb_get_infor   TLS_PEER_CERT_INFOr   s    r   tls_peer_cert_infozConnection.tls_peer_cert_info  si     ?7566w''( (49 	C))$*ABBBtr   c                 h    |                                   |                     t          j                  S )z+Get the current database of the connection.)r   r   r   SCHEMAr   s    r   rX   zConnection.database  s,     	%%dk222r   c                     |                                   	 |                     dt          |          z             |                                  dS # t          j        $ r  w xY w)zSet default database.zUSE %sN)r   rH   ri   rI   r   r|   )r   schemas     r   rX   zConnection.database  sr     		!!(S[["8999!!!!!} 	 	 		s   9A A"c                 h    |                                   |                     t          j                  S )z
        Returns the username for the current connection or empty
        string if it can't be determined, e.g., when using socket
        authentication.
        )r   r   r   USERr   s    r   userzConnection.user  s,     	%%di000r   c                     t           S )zd
        Client character set.

        For MariaDB Connector/Python, it is always utf8mb4.
        )_DEFAULT_CHARSETr   s    r   character_setzConnection.character_set  s
      r   c                 h    |                                   |                     t          j                  S )zClient capability flags.)r   r   r   CLIENT_CAPABILITIESr   s    r   client_capabilitieszConnection.client_capabilities  -     	%%d&>???r   c                 h    |                                   |                     t          j                  S )zServer capability flags.)r   r   r   SERVER_CAPABILITIESr   s    r   server_capabilitieszConnection.server_capabilities  r   r   c                 h    |                                   |                     t          j                  S )z_
        Extended server capability flags (only for MariaDB
        database servers).
        )r   r   r   EXTENDED_SERVER_CAPABILITIESr   s    r   extended_server_capabilitiesz'Connection.extended_server_capabilities  s-     	%%d&GHHHr   c                 h    |                                   |                     t          j                  S )zq
        Database server TCP/IP port. This value will be 0 in case of an unix
        socket connection.
        )r   r   r   PORTr   s    r   server_portzConnection.server_port  s,     	%%di000r   c                 h    |                                   |                     t          j                  S )zUnix socket name.)r   r   r   UNIX_SOCKETr   s    r   unix_socketzConnection.unix_socket  s-     	%%d&6777r   c                 h    |                                   |                     t          j                  S )z&Name or IP address of database server.)r   r   r   HOSTr   s    r   server_namezConnection.server_name  s,     	%%di000r   c                     t           S )zClient character set collation)_DEFAULT_COLLATIONr   s    r   	collationzConnection.collation&  s
     "!r   c                 h    |                                   |                     t          j                  S )z-Server version in alphanumerical format (str))r   r   r   SERVER_VERSIONr   s    r   server_infozConnection.server_info,  s-     	%%d&9:::r   c                 z    |                                   | j        r|                     t          j                  S dS )z0TLS cipher suite if a secure connection is used.N)r   r   r   r   
SSL_CIPHERr   s    r   
tls_cipherzConnection.tls_cipher3  s;     	9 	;))$/:::tr   c                 z    |                                   | j        r|                     t          j                  S dS )z4TLS protocol version if a secure connection is used.N)r   r   r   r   TLS_VERSIONr   s    r   tls_versionzConnection.tls_version<  s<     	9 	<))$*:;;;tr   c                     t          j        t          j                  t          j        d          k     rdS |                                  | j        r|                     t          j                  S dS )z8Returns the result of the peer certificate verification.r   N)	r   r,   r   r-   r   r   r   r   TLS_VERIFY_STATUSr   s    r   _tls_verify_statuszConnection._tls_verify_statusE  si     ?7566w''( (49 	B))$*@AAAtr   c                 h    |                                   |                     t          j                  S )z,
        Return server status flags
        )r   r   r   SERVER_STATUSr   s    r   server_statuszConnection.server_statusR  s-     	%%d&8999r   c                 h    |                                   |                     t          j                  S )z
        Server version in numerical format.

        The form of the version number is
        VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH
        )r   r   r   SERVER_VERSION_IDr   s    r   server_versionzConnection.server_version[  s-     	%%d&<===r   c                     |                                   | j        }t          |dz            t          |dz  dz            |dz  fS )zW
        Returns numeric version of connected database server in tuple format.
        i'  d   )r   r   rP   )r   r   s     r   r[   zConnection.server_version_infog  sR     	%GeO$$Wu_+,,# 	r   c                 l    |                                   t          | j        t          j        z            S )a8  
        Toggles autocommit mode on or off for the current database connection.

        Autocommit mode only affects operations on transactional table types.
        Be aware that rollback() will not work if autocommit mode was switched
        on.

        By default, autocommit mode is set to False.
        )r   r/   r   r   
AUTOCOMMITr   s    r   r   zConnection.autocommits  s/     	D&)::;;;r   c                     |                                   t          |          | j        k    rd S 	 |                     dt	          |          z             |                                  d S # t          j        $ r  w xY w)NzSET AUTOCOMMIT=%s)r   r/   r   rH   rP   rI   r   r|   )r   modes     r   r   zConnection.autocommit  s    ::((F	!!"5D		"ABBB!!!!!} 	 	 		s   9A+ +A<c                     |                                  }| j        st          j        |          | _        n7|| j                                        k    rt          j        |          | _        | j        S )z/Returns the socket used for database connection)fileno)_get_socketr!   socketr   )r   fnos     r   r   zConnection.socket  sj       | 	5!=444DLLDL''))))!=444DL|r   c                     |                                   	 |                                  n# t          j        $ r Y dS w xY wdS )z
        Returns true if the connection is alive.

        A ping command will be sent to the server for this purpose,
        which means this function might fail if there are still
        non-processed pending result sets.
        FT)r   pingr   r|   r   s    r   openzConnection.open  sS     		IIKKKK} 	 	 	55	ts   + >>c                 8    |                                   | j        S )z)
        Alias for connection_id
        )r   connection_idr   s    r   	thread_idzConnection.thread_id  s     	!!r   r;   )7rh   rq   rr   rs   r   r1   r   cursorsCursorr8   r=   r?   rD   rJ   rM   rP   rR   rU   ri   rY   r\   rb   tuplerv   r   r   r   r   r   propertyr   rX   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[   r   r   r   character_set_namer   rt   ru   s   @r   r   r       sH        < < <
(( (( (( (( ((T ")!7    8        
 
 
  $s      	 	 	 	 	( ( (  += += += += +=e += += +=Z  :7( 7( 7( 7(r$+ $+ $+L++ ++ ++ ++Z   	 	 X	 3 3 X3 _  _ 1 1 X1     X  @ @ X@ @ @ X@ I I XI 1 1 X1 8 8 X8 1 1 X1 " " X"
 ; ; X;   X   X 
 
 X
 : : X: 	> 	> X	> 	 	 X	 < < X<    	 	 X	   X" '" " X" " " " "r   r   )r   r   mariadb.cursorsmariadb.constantsr   r   r   	packagingr   r   r   rl   r7   
connectionr    r   r   <module>r      s   (       5 5 5 5 5 5 5 5 5 5       )  T
" T
" T
" T
" T
"!, T
" T
" T
" T
" T
"r   