o
    "4g                     @  sl   d Z ddlmZ ddlZddlZddlmZmZ ddl	m
Z
mZ edZe
ddG d	d
 d
Zde_dS )z"
psycopg two-phase commit support
    )annotationsN)	b64encode	b64decode)	dataclassreplacez^(\d+)_([^_]*)_([^_]*)$T)frozenc                   @  s   e Zd ZU dZded< ded< ded< dZd	ed
< dZded< dZded< ed&ddZ	d'ddZ
d(ddZd)ddZed&ddZed*ddZd'ddZed'd d!Zed+d$d%ZdS ),XidzA two-phase commit transaction identifier.

    The object can also be unpacked as a 3-item tuple (`format_id`, `gtrid`,
    `bqual`).

    
int | None	format_idstrgtrid
str | NonebqualNzdt.datetime | Nonepreparedownerdatabasesreturnc                 C  s,   z|  |W S  ty   td|d Y S w )zTry to parse an XA triple from the string.

        This may fail for several reasons. In such case return an unparsed Xid.
        N)_parse_string	Exceptionr   )clsr    r   U/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_tpc.pyfrom_string!   s
   zXid.from_stringc                 C  s   |   S N)_as_tidselfr   r   r   __str__,   s   zXid.__str__intc                 C     dS )N   r   r   r   r   r   __len__/   s   zXid.__len__indexint | str | Nonec                 C  s   | j | j| jf| S r   )r
   r   r   )r   r#   r   r   r   __getitem__2   s   zXid.__getitem__c                 C  sV   t |}|stdt|d}t|d }t|d }| |||S )Nzbad Xid format      r!   )_re_xidmatch
ValueErrorr   groupr   decode
from_parts)r   r   mr
   r   r   r   r   r   r   5   s   
zXid._parse_stringc                 C  s   |d ur3|d u rt dd|  krdk std tdt|dkr(tdt|dkr2tdn|d u r;t dt|||S )	Nz,if format_id is specified, bqual must be toor   l        z/format_id must be a non-negative 32-bit integer@   z&bqual must be not longer than 64 charsz&gtrid must be not longer than 64 charsz,if format_id is None, bqual must be None too)	TypeErrorr*   lenr   )r   r
   r   r   r   r   r   r-   @   s   zXid.from_partsc                 C  sT   | j du s
| jdu r| jS t| j  }t| j  }| j  d| d| S )a  
        Return the PostgreSQL transaction_id for this XA xid.

        PostgreSQL wants just a string, while the DBAPI supports the XA
        standard and thus a triple. We use the same conversion algorithm
        implemented by JDBC in order to allow some form of interoperation.

        see also: the pgjdbc implementation
          http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/jdbc/pgjdbc/org/
            postgresql/xa/RecoveredXid.java?rev=1.2
        N_)r
   r   r   r   encoder,   )r   egtridebqualr   r   r   r   Q   s
   zXid._as_tidc                 C  r    )Nz<SELECT gid, prepared, owner, database FROM pg_prepared_xactsr   )r   r   r   r   _get_recover_queryg   s   zXid._get_recover_querygiddt.datetimec                 C  s   t |}t||||dS )N)r   r   r   )r   r   r   )r   r7   r   r   r   xidr   r   r   _from_recordk   s   
zXid._from_record)r   r   r   r   )r   r   )r   r   )r#   r   r   r$   )r
   r	   r   r   r   r   r   r   )
r7   r   r   r8   r   r   r   r   r   r   )__name__
__module____qualname____doc____annotations__r   r   r   classmethodr   r   r"   r%   r   r-   r   r6   r:   r   r   r   r   r      s,   
 





r   psycopg)r>   
__future__r   redatetimedtbase64r   r   dataclassesr   r   compiler(   r   r<   r   r   r   r   <module>   s    

a