o
    6	f                     @   sp   d Z ddlZddlZddlmZm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
    N)	b64encode	b64decode)OptionalUnion)	dataclassreplacez^(\d+)_([^_]*)_([^_]*)$T)frozenc                   @   s2  e Zd ZU dZee ed< eed< ee ed< dZee	j
 ed< dZee ed< dZee ed< ed	ed
d fddZd
efddZd
efddZded
eeedf fddZed	ed
d fddZedee dedee d
d fddZd
efddZed
efddZedede	j
deded
d f
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`).

    	format_idgtridbqualN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   E/home/ertert/spirit/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__c                 C      dS )N   r   r   r   r   r   __len__.   s   zXid.__len__indexc                 C   s   | j | j| jf| S r   )r
   r   r   )r   r    r   r   r   __getitem__1   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
ValueErrorintgroupr   decode
from_parts)r   r   mr
   r   r   r   r   r   r   4   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   R   s
   zXid._as_tidc                 C   r   )Nz<SELECT gid, prepared, owner, database FROM pg_prepared_xactsr   )r   r   r   r   _get_recover_queryh   s   zXid._get_recover_querygidc                 C   s   t |}t||||dS )N)r   r   r   )r	   r   r   )r   r4   r   r   r   xidr   r   r   _from_recordl   s   
zXid._from_record)__name__
__module____qualname____doc__r   r'   __annotations__strr   dtdatetimer   r   classmethodr   r   r   r   r!   r   r*   r   r3   r6   r   r   r   r   r	      sP   
 

r	   psycopg)r:   rer>   r=   base64r   r   typingr   r   dataclassesr   r   compiler$   r	   r8   r   r   r   r   <module>   s    

c