U
    ڲg                     @  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ddZ	ddddZ
ddddZdddddZedd dddZedddd dddZdddd Zeddd!d"Zedd#ddd 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`).

    z
int | None	format_idstrgtridz
str | NonebqualNzdt.datetime | Nonepreparedownerdatabase)sreturnc                 C  s2   z|  |W S  tk
r,   td|d Y S X dS )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   0/tmp/pip-unpacked-wheel-b_ea6rx_/psycopg/_tpc.pyfrom_string!   s    zXid.from_string)r   c                 C  s   |   S N)_as_tidselfr   r   r   __str__,   s    zXid.__str__intc                 C  s   dS )N   r   r   r   r   r   __len__/   s    zXid.__len__zint | str | None)indexr   c                 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_string)r	   r   r   r   c                 C  s|   |d k	r`|d krt dd|  kr,dk s6n tdt|dkrJtdt|dkrptdn|d krpt 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ks| jdk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   ZegtridZebqualr   r   r   r   Q   s
    zXid._as_tidc                 C  s   dS )Nz<SELECT gid, prepared, owner, database FROM pg_prepared_xactsr   )r   r   r   r   _get_recover_queryg   s    zXid._get_recover_queryzdt.datetime)gidr   r   r   r   c                 C  s   t |}t||||dS )N)r   r   r   )r   r   r   )r   r1   r   r   r   xidr   r   r   _from_recordk   s    
zXid._from_record)__name__
__module____qualname____doc____annotations__r   r   r   classmethodr   r   r   r!   r   r)   r   r0   r3   r   r   r   r   r      s*   


r   Zpsycopg)r7   
__future__r   redatetimedtbase64r   r   Zdataclassesr   r   compiler$   r   r5   r   r   r   r   <module>   s   
a