
    *Jf{u              	       f   d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0 erddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ej:        j;        Z<ej:        j=        Z>ej?        j;        Z;ej?        j=        Z=ej@        jA        ZAej@        jB        ZBejC        jD        ZDdZEdeEz  ZFdZGejH        dk    ZI G d dee!                   ZJ G d deJd                   ZK G d de          ZL G d  d!eL          ZM G d" d#eM          ZN G d$ d%eL          ZO G d& d'eJd(                   ZP G d) d*e          ZQ G d+ d,eQ          ZR G d- d.eR          ZS G d/ d0e          ZT G d1 d2eT          ZU G d3 d4eT          ZV	 dSd5ee         d6e#d7eeW         d8eWfd9ZX	 dSd5ee         d6e#d7eeW         d8eWfd:ZYd;e d6e#d8eed<f         fd=ZZd;e d6e#d8eed<f         fd>Z[ ej\        d?          j]        Z^ ej\        d@          j]        Z_ ej\        d?          j`        Za ej\        d@          j`        ZbdAZcdBZddCZe ejf        dD          ZgdEdFdGdHdIdJdKdLZhehfdMeei         dNeeieif         d8eifdOZj ejf        dP          ZkdQ ehl                                D             ZmemfdMeei         dNeeieif         d8eifdRZne*re*jo        Zoe*jp        Zpe*jq        Zqe*jr        ZrdS eXZoeYZpeZZqe[ZrdS )Tz
psycopg copy support
    N)ABCabstractmethod)TracebackType)AnyAsyncIteratorDictGenericIteratorListMatchIO)OptionalSequenceTupleTypeUnionTYPE_CHECKING   )pq)adapt)errors)BufferConnectionTypePQGenTransformer)create_taskSelf)connection_summary)_psycopg)pgconn_encoding)	copy_fromcopy_tocopy_end)
BaseCursorCursor)AsyncCursor)
Connection)AsyncConnectioni      i   darwinc                       e Zd ZU dZded<   dddddee         fd	Zd
efdZ	ddZ
deeeef                  d
dfdZd
ee         fdZd
eeeedf                           fdZdee         d
ed         fdZdS )BaseCopya  
    Base implementation for the copy user interface.

    Two subclasses expose real methods with the sync/async differences.

    The difference between the text and binary format is managed by two
    different `Formatter` subclasses.

    Writing (the I/O part) is implemented in the subclasses by a `Writer` or
    `AsyncWriter` instance. Normally writing implies sending copy data to a
    database, but a different writer might be chosen, e.g. to stream data into
    a file for later use.
    	Formatter	formatterNbinarycursorzBaseCursor[ConnectionType, Any]r0   c                @   || _         |j        | _        | j        j        | _        |j        }|ra|j        | _        | j        t          k    rD| j        t          k    r4t          j
        dt          j        | j                  j         d          nt          | _        |t          |o|j                  }t!          |dd           pt#          j        |          }|rt'          |          | _        n(t+          |t-          | j                            | _        d| _        d S )NzAthe cursor should have performed a COPY operation; its status is z instead_tx)encodingF)r1   
connectionpgconn_pgconnpgresultstatus
_directionCOPY_INCOPY_OUTeProgrammingErrorr   
ExecStatusnameboolbinary_tuplesgetattrr   r   BinaryFormatterr.   TextFormatterr    	_finished)selfr1   r0   resulttxs        O/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/psycopg/copy.py__init__zBaseCopy.__init__U   s     +- 	&$mDO'))do.I.I(T&(mDO&D&D&IT T T  
 &DO>&9V%9::F!&%66S%:KF:S:S 	W,R00DNN*28U8UVVVDN    returnc                     | j         j         d| j         j         }t          | j                  }d| d| dt          |           ddS )N.< z at 0xx>)	__class__
__module____qualname__r   r7   id)rG   clsinfos      rJ   __repr__zBaseCopy.__repr__u   sX    *JJT^-HJJ!$,//232222RXX22222rL   c                 2    | j         rt          d          d S )Nz!copy blocks can be used only once)rF   	TypeErrorrG   s    rJ   _enterzBaseCopy._enterz   s(    > 	A?@@@	A 	ArL   typesc                    | j         j        j        fd|D             }| j        t          k    r,| j        j                            || j        j                   dS | j        j        	                    || j        j                   dS )a  
        Set the types expected in a COPY operation.

        The types must be specified as a sequence of oid or PostgreSQL type
        names (e.g. ``int4``, ``timestamptz[]``).

        This operation overcomes the lack of metadata returned by PostgreSQL
        when a COPY operation begins:

        - On :sql:`COPY TO`, `!set_types()` allows to specify what types the
          operation returns. If `!set_types()` is not used, the data will be
          returned as unparsed strings or bytes instead of Python objects.

        - On :sql:`COPY FROM`, `!set_types()` allows to choose what type the
          database expects. This is especially useful in binary copy, because
          PostgreSQL will apply no cast rule.

        c                 h    g | ].}t          |t                    r|n                    |          /S  )
isinstanceintget_oid).0tregistrys     rJ   
<listcomp>z&BaseCopy.set_types.<locals>.<listcomp>   s:    PPPQZ3''@X-=-=a-@-@PPPrL   N)
r1   adaptersr_   r:   r;   r.   transformerset_dumper_typesformatset_loader_types)rG   r_   oidsrh   s      @rJ   	set_typeszBaseCopy.set_types~   s    & ;'-PPPP%PPP?g%%N&77dn>STTTTTN&77dn>STTTTTrL   c              #      K   | j         rt          d          S t          | j                  E d {V }t	          |t                    r|S d| _         |j        }||nd| j        _        t          d          S )NrL   T)rF   
memoryviewr!   r7   rc   command_tuplesr1   	_rowcount)rG   resnrowss      rJ   	_read_genzBaseCopy._read_gen   s      > 	#c??""4<00000000c:&& 	J  ").):#rL   .c              #      K   |                                  E d {V }|sd S | j                            |          }|#|                                  E d {V  d| _        d S |S NT)rx   r.   	parse_rowrF   )rG   datarows      rJ   _read_row_genzBaseCopy._read_row_gen   s      ..******** 	4n&&t,,;~~'''''''''!DN4
rL   excc              #     K   |sd S | j         j        t          k    rd S | j                                         	 |                                 E d {V r	 |                                 E d {V d S d S # t          j        $ r Y d S w xY wN)r7   transaction_statusACTIVEr5   cancelrx   r=   QueryCanceledrG   r   s     rJ   _end_copy_out_genzBaseCopy._end_copy_out_gen   s       	F<*f44 F 	   	"nn........  #nn........      	 	 	DD	s   5A1 1BBrM   N)__name__rU   rV   __doc____annotations__r   rA   rK   strrZ   r^   r   r   rd   rp   r   r   rx   r   r   r~   BaseExceptionr   rb   rL   rJ   r,   r,   D   sG           "&	  1 	   @3# 3 3 3 3
A A A AUxc3h8 UT U U U U:5=    &uXeCHo%>?    Xm%< t      rL   r,   c                       e Zd ZU dZdZded<   dddddd	ee         ded         f fd
ZdefdZ	dee
e                  dee         dee         ddfdZdee         fdZdefdZdeeedf                  fdZdeeedf                  fdZdeeef         ddfdZdee         ddfdZdee         ddfdZ xZS )Copya]  Manage a :sql:`COPY` operation.

    :param cursor: the cursor where the operation is performed.
    :param binary: if `!True`, write binary format.
    :param writer: the object to write to destination. If not specified, write
        to the `!cursor` connection.

    Choosing `!binary` is not necessary if the cursor has executed a
    :sql:`COPY` operation, because the operation result describes the format
    too. The parameter is useful when a `!Copy` object is created manually and
    no operation is performed on the cursor, such as when using ``writer=``\
    `~psycopg.copy.FileWriter`.

    psycopgWriterwriterNr0   r   r1   Cursor[Any]r0   c                    t                                          ||           |st          |          }|| _        |j        | _        d S Nr/   )superrK   LibpqWriterr   write_writerG   r1   r0   r   rT   s       rJ   rK   zCopy.__init__   sK     	/// 	) ((FlrL   rM   c                 .    |                                   | S r   r^   r]   s    rJ   	__enter__zCopy.__enter__   s    rL   exc_typeexc_valexc_tbc                 0    |                      |           d S r   finishrG   r   r   r   s       rJ   __exit__zCopy.__exit__   s     	GrL   c              #   B   K   	 |                                  }|sdS |V  )z5Implement block-by-block iteration on :sql:`COPY TO`.TNreadrG   r|   s     rJ   __iter__zCopy.__iter__  s3      	99;;D JJJ		rL   c                 Z    | j                             |                                           S )z
        Read an unparsed row after a :sql:`COPY TO` operation.

        Return an empty string when the data is finished.
        r5   waitrx   r]   s    rJ   r   z	Copy.read	  s$     ##DNN$4$4555rL   .c              #   B   K   	 |                                  }|dS |V  )z
        Iterate on the result of a :sql:`COPY TO` operation record by record.

        Note that the records returned will be tuples of unparsed strings or
        bytes, unless data types are specified using `set_types()`.
        TNread_rowrG   records     rJ   rowsz	Copy.rows  s0      	]]__F~LLL		rL   c                 Z    | j                             |                                           S )a  
        Read a parsed row of data from a table after a :sql:`COPY TO` operation.

        Return `!None` when the data is finished.

        Note that the records returned will be tuples of unparsed strings or
        bytes, unless data types are specified using `set_types()`.
        r5   r   r~   r]   s    rJ   r   zCopy.read_row  s&     ##D$6$6$8$8999rL   bufferc                 l    | j                             |          }|r|                     |           dS dS )z
        Write a block of data to a table after a :sql:`COPY FROM` operation.

        If the :sql:`COPY` is in binary format `!buffer` must be `!bytes`. In
        text mode it can be either `!bytes` or `!str`.
        Nr.   r   r   rG   r   r|   s      rJ   r   z
Copy.write)  sC     ~##F++ 	KK	 	rL   r}   c                 l    | j                             |          }|r|                     |           dS dS )z=Write a record to a table after a :sql:`COPY FROM` operation.Nr.   	write_rowr   rG   r}   r|   s      rJ   r   zCopy.write_row4  sA    ~'',, 	KK	 	rL   r   c                 &   | j         t          k    rS| j                                        }|r|                     |           | j                            |           d| _        dS | j        	                    | 
                    |                     dS )a  Terminate the copy operation and free the resources allocated.

        You shouldn't need to call this function yourself: it is usually called
        by exit. It is available if, despite what is documented, you end up
        using the `Copy` object outside a block.
        TNr:   r;   r.   endr   r   r   rF   r5   r   r   rG   r   r|   s      rJ   r   zCopy.finish:  s     ?g%%>%%''D "D!!!Ks###!DNNNO  !7!7!<!<=====rL   )r   rU   rV   r   r   r   rA   rK   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__rT   s   @rJ   r   r      s          J "&%)# # ## 	#
 "# # # # # #4    4./ -( '	
 
   (6*    6f 6 6 6 6huS#X/    	:(5c?3 	: 	: 	: 	:	E&#+. 	4 	 	 	 	Xc] t    >(=1 >d > > > > > > > >rL   r   zConnection[Any]c                   P    e Zd ZdZededdfd            Zddee         ddfdZ	dS )	r   z/
    A class to write copy data somewhere.
    r|   rM   Nc                     dS )z1
        Write some data to destination.
        Nrb   r   s     rJ   r   zWriter.writeP  s	    
 	rL   r   c                     dS )z
        Called when write operations are finished.

        If operations finished with an error, it will be passed to ``exc``.
        Nrb   r   s     rJ   r   zWriter.finishW  s	     	rL   r   
r   rU   rV   r   r   r   r   r   r   r   rb   rL   rJ   r   r   K  sx          & T    ^ (=1 T      rL   r   c                   H    e Zd ZdZddZdeddfdZdd	ee         ddfd
Z	dS )r   z?
    A `Writer` to write copy data to a Postgres database.
    r1   r   c                 N    || _         |j        | _        | j        j        | _        d S r   r1   r5   r6   r7   rG   r1   s     rJ   rK   zLibpqWriter.__init__e  #     +-rL   r|   rM   Nc           
      v   t          |          t          k    r6| j                            t	          | j        |t                               d S t          dt          |          t                    D ]F}| j                            t	          | j        |||t          z            t                               Gd S Nflushr   lenMAX_BUFFER_SIZEr5   r   r"   r7   PREFER_FLUSHrangerG   r|   is      rJ   r   zLibpqWriter.writej  s    t99'' O  t<!P!P!PQQQQQ 1c$ii99  $$d1q?/B+B&C<      rL   r   c                 <   |rCdt          |          j         d| }|                    t          | j                  d          }nd }	 | j                            t          | j        |                    }|g| j        _	        d S # t          j        $ r |s Y d S w xY wNzerror from Python: z - replacetyperV   encoder    r7   r5   r   r#   r1   _resultsr=   r   rG   r   msgbmsgrv   s        rJ   r   zLibpqWriter.finishy  s     	HS		(>HH3HHC::odl;;YGGDDD		)/&&xd'C'CDDC %(5DK   	  	 	 	   	s   	-B BBr1   r   r   
r   rU   rV   r   rK   r   r   r   r   r   rb   rL   rJ   r   r   `  s         . . . .
& T    ) )(=1 )T ) ) ) ) ) )rL   r   c                   ^     e Zd ZdZd fdZddZdeddfd	Zdd
ee	         ddf fdZ
 xZS )QueuedLibpqWritera  
    A writer using a buffer to queue data to write to a Postgres database.

    `write()` returns immediately, so that the main thread can be CPU-bound
    formatting messages, while a worker thread can be IO-bound waiting to write
    on the connection.
    r1   r   c                     t                                          |           t          j        t                    | _        d | _        d | _        d S N)maxsize)r   rK   queueQueue
QUEUE_SIZE_queue_worker_worker_errorrG   r1   rT   s     rJ   rK   zQueuedLibpqWriter.__init__  sD       +0;z+J+J+J376:rL   rM   Nc                     	 	 | j                             dd          }|sdS | j                            t	          | j        |t                               U# t          $ r}|| _        Y d}~dS d}~ww xY w)zPush data to the server when available from the copy queue.

        Terminate reading when the queue receives a false-y value, or in case
        of error.

        The function is designed to be run in a separate thread.
        TiQ )blocktimeoutr   N)	r   getr5   r   r"   r7   r   r   r   )rG   r|   exs      rJ   workerzQueuedLibpqWriter.worker  s    	$V{T<HH E$$WT\4|%T%T%TUUU	V
  	$ 	$ 	$!#D	$s   A 5A 
A4"A//A4r|   c                    | j         sDt          j        | j                  | _         d| j         _        | j                                          | j        r| j        t          |          t          k    r| j	        
                    |           d S t          dt          |          t                    D ],}| j	        
                    |||t          z                       -d S )N)targetTr   )r   	threadingThreadr   daemonstartr   r   r   r   putr   r   s      rJ   r   zQueuedLibpqWriter.write  s    | 	!$+4;???DL"&DLL     	%$$t99'' KOOD!!!!! 1c$ii99 ? ?Q_)<%< =>>>>? ?rL   r   c                     | j                             d           | j        r | j                                         d | _        | j        r| j        t                                          |           d S NrL   )r   r   r   joinr   r   r   rG   r   rT   s     rJ   r   zQueuedLibpqWriter.finish  sn    < 	 LDL  	%$$srL   r   r   r   r   rU   rV   r   rK   r   r   r   r   r   r   r   r   s   @rJ   r   r     s         ; ; ; ; ; ;$ $ $ $$?& ?T ? ? ? ?* (=1 T          rL   r   c                   :    e Zd ZdZdee         fdZdeddfdZdS )
FileWriterz
    A `Writer` to write copy data to a file-like object.

    :param file: the file where to write copy data. It must be open for writing
        in binary mode.
    filec                     || _         d S r   )r  )rG   r  s     rJ   rK   zFileWriter.__init__  s    			rL   r|   rM   Nc                 :    | j                             |           d S r   )r  r   r   s     rJ   r   zFileWriter.write  s    	rL   )	r   rU   rV   r   r   bytesrK   r   r   rb   rL   rJ   r  r    s_         RY    & T      rL   r  c                       e Zd ZU dZdZded<   dddddd	ee         ded         f fd
ZdefdZ	dee
e                  dee         dee         ddfdZdee         fdZdefdZdeeedf                  fdZdeeedf                  fdZdeeef         ddfdZdee         ddfdZdee         ddfdZ xZS )	AsyncCopyz-Manage an asynchronous :sql:`COPY` operation.r   AsyncWriterr   Nr   r1   AsyncCursor[Any]r0   c                    t                                          ||           |st          |          }|| _        |j        | _        d S r   )r   rK   AsyncLibpqWriterr   r   r   r   s       rJ   rK   zAsyncCopy.__init__  sK     	/// 	.%f--FlrL   rM   c                 2   K   |                                   | S r   r   r]   s    rJ   
__aenter__zAsyncCopy.__aenter__  s      rL   r   r   r   c                 @   K   |                      |           d {V  d S r   r   r   s       rJ   	__aexit__zAsyncCopy.__aexit__  s2       kk'"""""""""""rL   c                P   K   	 |                                   d {V }|sd S |W V  $r   r   r   s     rJ   	__aiter__zAsyncCopy.__aiter__  sG      	$$$$$$D JJJJ		rL   c                 j   K   | j                             |                                            d {V S r   r   r]   s    rJ   r   zAsyncCopy.read
  s8      _))$..*:*:;;;;;;;;;rL   .c                P   K   	 |                                   d {V }|d S |W V  $r   r   r   s     rJ   r   zAsyncCopy.rows  sD      	==??******F~LLLL		rL   c                 j   K   | j                             |                                            d {V S r   r   r]   s    rJ   r   zAsyncCopy.read_row  s:      _))$*<*<*>*>?????????rL   r   c                 |   K   | j                             |          }|r|                     |           d {V  d S d S r   r   r   s      rJ   r   zAsyncCopy.write  sW      ~##F++ 	$++d###########	$ 	$rL   r}   c                 |   K   | j                             |          }|r|                     |           d {V  d S d S r   r   r   s      rJ   r   zAsyncCopy.write_row  sW      ~'',, 	$++d###########	$ 	$rL   r   c                 N  K   | j         t          k    r_| j                                        }|r|                     |           d {V  | j                            |           d {V  d| _        d S | j        	                    | 
                    |                     d {V  d S rz   r   r   s      rJ   r   zAsyncCopy.finish!  s      ?g%%>%%''D (kk$'''''''''+$$S)))))))))!DNNN/&&t'='=c'B'BCCCCCCCCCCCrL   )r   rU   rV   r   r   r   rA   rK   r   r  r   r   r   r  r   r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   s   @rJ   r
  r
    s        77J "&*.# # #"# 	#
 '# # # # # #$    #4./# -(# '	#
 
# # # #v!6    <F < < < <M%S/:    @sCx 9 @ @ @ @$%"4 $ $ $ $ $
$8C= $T $ $ $ $
D 7 DD D D D D D D D DrL   r
  zAsyncConnection[Any]c                   P    e Zd ZdZededdfd            Zddee         ddfdZ	dS )	r  zG
    A class to write copy data somewhere (for async connections).
    r|   rM   Nc                 
   K   d S r   rb   r   s     rJ   r   zAsyncWriter.write1  s      14rL   r   c                 
   K   d S r   rb   r   s     rJ   r   zAsyncWriter.finish4  s      rL   r   r   rb   rL   rJ   r  r  ,  sl          444444 ^4  7 4      rL   r  c                   H    e Zd ZdZddZdeddfdZdd	ee         ddfd
Z	dS )r  zE
    An `AsyncWriter` to write copy data to a Postgres database.
    r1   r  c                 N    || _         |j        | _        | j        j        | _        d S r   r   r   s     rJ   rK   zAsyncLibpqWriter.__init__=  r   rL   r|   rM   Nc           
        K   t          |          t          k    r<| j                            t	          | j        |t                               d {V  d S t          dt          |          t                    D ]L}| j                            t	          | j        |||t          z            t                               d {V  Md S r   r   r   s      rJ   r   zAsyncLibpqWriter.writeB  s      t99'' /&&wt|T'V'V'VWWWWWWWWWWW 1c$ii99  o**d1q?/B+B&C<            rL   r   c                 L  K   |rCdt          |          j         d| }|                    t          | j                  d          }nd }	 | j                            t          | j        |                     d {V }|g| j        _	        d S # t          j        $ r |s Y d S w xY wr   r   r   s        rJ   r   zAsyncLibpqWriter.finishQ  s       	HS		(>HH3HHC::odl;;YGGDDD		),,XdlD-I-IJJJJJJJJC %(5DK   	  	 	 	   	s   3B B#"B#r1   r  r   r   rb   rL   rJ   r  r  8  s         . . . .
 4    ) ) 7 )4 ) ) ) ) ) )rL   r  c                   ^     e Zd ZdZd fdZddZdeddfd	Zdd
ee	         ddf fdZ
 xZS )AsyncQueuedLibpqWriterz
    An `AsyncWriter` using a buffer to queue data to write.

    `write()` returns immediately, so that the main thread can be CPU-bound
    formatting messages, while a worker thread can be IO-bound waiting to write
    on the connection.
    r1   r  c                     t                                          |           t          j        t                    | _        d | _        d S r   )r   rK   asyncior   r   r   r   r   s     rJ   rK   zAsyncQueuedLibpqWriter.__init__n  s:       -4]:-N-N-N7;rL   rM   Nc                    K   	 | j                                          d{V }|sdS | j                            t	          | j        |                     d{V  W)zPush data to the server when available from the copy queue.

        Terminate reading when the queue receives a false-y value.

        The function is designed to be run in a separate task.
        TN)r   r   r5   r   r"   r7   r   s     rJ   r   zAsyncQueuedLibpqWriter.workert  sv      	D********D /&&wt|T'B'BCCCCCCCCC		DrL   r|   c                   K   | j         s&t          |                                           | _         t          |          t          k    r"| j                            |           d {V  d S t          dt          |          t                    D ]2}| j                            |||t          z                       d {V  3d S Nr   )r   r   r   r   r   r   r   r   r   s      rJ   r   zAsyncQueuedLibpqWriter.write  s      | 	6&t{{}}55DLt99'' +//$''''''''''' 1c$ii99 E Ekood1q?/B+B&CDDDDDDDDDDE ErL   r   c                    K   | j                             d           d {V  | j        r&t          j        | j                   d {V  d | _        t                                          |           d {V  d S r   )r   r   r   r&  gatherr   r   r  s     rJ   r   zAsyncQueuedLibpqWriter.finish  s      kooc"""""""""< 	 ..........DLggnnS!!!!!!!!!!!rL   r"  r   r   r  r   s   @rJ   r$  r$  e  s         < < < < < <D D D DE E4 E E E E" " 7 "4 " " " " " " " " " "rL   r$  c                       e Zd ZU dZej        ed<   defdZe	de
deeedf                  fd            Ze	d	ee
ef         de
fd
            Ze	dee         de
fd            Ze	de
fd            ZdS )r-   z@
    A class which understand a copy format (text, binary).
    rm   rk   c                 H    || _         t                      | _        d| _        d S NF)rk   	bytearray_write_buffer	_row_mode)rG   rk   s     rJ   rK   zFormatter.__init__  s!    &&[[rL   r|   rM   .c                     d S r   rb   r   s     rJ   r{   zFormatter.parse_row  s    DGCrL   r   c                     d S r   rb   rG   r   s     rJ   r   zFormatter.write  s    ;>3rL   r}   c                     d S r   rb   )rG   r}   s     rJ   r   zFormatter.write_row  s    7:srL   c                     d S r   rb   r]   s    rJ   r   zFormatter.end  s     SrL   N)r   rU   rV   r   r   Formatr   r   rK   r   r   r   r   r   r{   r   r   r   r   r   r   rb   rL   rJ   r-   r-     s           IK    
 GfG%S/)BGGG ^G>E&#+.>6>>> ^>:Xc]:v::: ^: V    ^   rL   r-   c                        e Zd ZeZddedef fdZdede	e
edf                  fdZd	eeef         defd
Zdee         defdZdefdZdeeef         defdZ xZS )rE   utf-8rk   r4   c                 X    t                                          |           || _        d S r   )r   rK   	_encoding)rG   rk   r4   rT   s      rJ   rK   zTextFormatter.__init__  s&    %%%!rL   r|   rM   .c                 4    |rt          || j                  S d S r   )parse_row_textrk   r   s     rJ   r{   zTextFormatter.parse_row  s"     	!$(89994rL   r   c                 >    |                      |          }d| _        |S rz   _ensure_bytes_signature_sentr   s      rJ   r   zTextFormatter.write  "    !!&))#rL   r}   c                     d| _         t          || j        | j                   t	          | j                  t
          k    r| j        t                      c}| _        |S dS NTrL   )r1  format_row_textrk   r0  r   BUFFER_SIZEr/  rG   r}   r   s      rJ   r   zTextFormatter.write_row  sY     T-t/ABBBt!""[00)-);Y[[&FD&M3rL   c                 <    | j         t                      c}| _         |S r   )r0  r/  r4  s     rJ   r   zTextFormatter.end  s    %)%7""rL   c                 d    t          |t                    r|                    | j                  S |S r   )rc   r   r   r;  r   s     rJ   r@  zTextFormatter._ensure_bytes  s/    dC   	;;t~...
 KrL   )r9  )r   rU   rV   TEXTrm   r   r   rK   r   r   r   r   r{   r   r   r   r   r   r@  r   r   s   @rJ   rE   rE     s       F" "K "3 " " " " " "f %S/)B    E&#+. 6    

Xc] 
v 
 
 
 
V    %"4         rL   rE   c                        e Zd ZeZdef fdZdedee	e
df                  fdZdeeef         defdZd	ee
         defd
ZdefdZdeeef         defdZ xZS )rD   rk   c                 X    t                                          |           d| _        d S r.  )r   rK   rA  )rG   rk   rT   s     rJ   rK   zBinaryFormatter.__init__  s)    %%%$rL   r|   rM   .c                    | j         s]|d t          t                             t          k    rt          j        d          d| _         |t          t                    d          }n|t
          k    rd S t          || j                  S )Nz5binary copy doesn't start with the expected signatureT)rA  r   _binary_signaturer=   	DataError_binary_trailerparse_row_binaryrk   r   s     rJ   r{   zBinaryFormatter.parse_row  s    # 		,c+,,,-1BBBkK   $(D -..001DD_$$4d&6777rL   r   c                 >    |                      |          }d| _        |S rz   r?  r   s      rJ   r   zBinaryFormatter.write  rB  rL   r}   c                    d| _         | j        s| xj        t          z  c_        d| _        t	          || j        | j                   t          | j                  t          k    r| j        t                      c}| _        |S dS rD  )	r1  rA  r0  rN  format_row_binaryrk   r   rF  r/  rG  s      rJ   r   zBinaryFormatter.write_row  s     # 	("33#'D #t/1CDDDt!""[00)-);Y[[&FD&M3rL   c                     | j         s+| xj        t          z  c_        | xj        t          z  c_        n| j        r| xj        t          z  c_        | j        t                      c}| _        |S r   )rA  r0  rN  rP  r1  r/  r4  s     rJ   r   zBinaryFormatter.end  sv     # 	2"33/1^ 	2 /1%)%7""rL   c                 N    t          |t                    rt          d          |S )Nz6cannot copy str data in binary mode: use bytes instead)rc   r   r\   r   s     rJ   r@  zBinaryFormatter._ensure_bytes  s*    dC   	TUUU
 KrL   )r   rU   rV   BINARYrm   r   rK   r   r   r   r   r{   r   r   r   r   r   r   r@  r   r   s   @rJ   rD   rD     s       F%K % % % % % %8f 8%S/)B 8 8 8 8E&#+. 6    
Xc] v     V    &%"4         rL   rD   r}   rI   outrM   c                    |t                      }| s|dz  }|S | D ]b}|T|                    |t                    }|                    |          }|t                              t          |          z  }n|dz  }|dz  }cd|dd<   |S )z6Convert a row of objects to the data to send for copy.N   
   \N   	rr   )r/  
get_dumperPY_TEXTdump_dump_resub	_dump_sub)r}   rI   rX  itemdumperbs         rJ   _format_row_textrf  %  s     {kk u
  ]]411FD!!A8<<	1---CC6MCuCHJrL   c                 (   |t                      }|t          t          |                     z  }|                    | t          gt          |           z            }|D ]3}|%|t          t          |                    z  }||z  })|t          z  }4|S )z=Convert a row of objects to the data to send for binary copy.)r/  
_pack_int2r   dump_sequence	PY_BINARY
_pack_int4_binary_null)r}   rI   rX  adaptedre  s        rJ   _format_row_binaryrn  =  s     {kk:c#hhCsYK#c(($:;;G    =:c!ff%%%C1HCC<CCJrL   r|   .c                     t          | t                    st          |           } |                     d          }|d         d d         |d<   d |D             }|                    |          S )Nr\  rr   c                 ^    g | ]*}|d k    rdnt                               t          |          +S )r[  N)_load_rera  	_load_sub)rf   fs     rJ   ri   z#_parse_row_text.<locals>.<listcomp>U  s2    
O
O
O11;;44HLLA$>$>
O
O
OrL   )rc   r  splitload_sequence)r|   rI   fieldsr}   s       rJ   _parse_row_textrw  P  sl    dE"" T{{ZZFCRCF2J
O
O
O
O
OCC   rL   c                 <   g }t          | d          d         }d}t          |          D ]^}t          | |          d         }|dz  }|dk    r&|                    | |||z                       ||z  }I|                    d            _|                    |          S )Nr      r)   )_unpack_int2r   _unpack_int4appendru  )r|   rI   r}   nfieldsposr   lengths          rJ   _parse_row_binaryr  Y  s    "$C4##A&G
C7^^  dC((+qQ;;JJtC#,./0006MCCJJtC   rL   z!hz!is   PGCOPY

         s   s   s
   [	
\\]s   \bs   \ts   \ns   \vs   \fs   \rs   \\)   r\  rZ              \m__mapc                 8    ||                      d                   S r)  groupr  r  s     rJ   rb  rb        rL   s   \\[btnvfr\\]c                     i | ]\  }}||	S rb   rb   )rf   kvs      rJ   
<dictcomp>r    s    222tq!a222rL   c                 8    ||                      d                   S r)  r  r  s     rJ   rr  rr    r  rL   r   )sr   resysr   structr&  r   abcr   r   r_   r   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r    r   r   r   r=   r   r   r   r   _compatr   r   pq.miscr   _cmoduler   
_encodingsr    
generatorsr!   r"   r#   r1   r$   r%   cursor_asyncr&   r5   r'   connection_asyncr(   PyFormatrJ  r^  rW  rj  r7  r?   r;   r<   TransactionStatusr   rF  r   r   platformr   r,   r   r   r   r   r  r
  r  r  r$  r-   rE   rD   r/  rf  rn  rw  r  Structpackrh  rk  unpack_fromrz  r{  rN  rP  rl  compiler`  
_dump_replr  rb  rq  items
_load_replrr  rE  rT  r=  rQ  rb   rL   rJ   <module>r     sj    
			 



        # # # # # # # #       O O O O O O O O O O O O O O O O O O O O H H H H H H H H H H H H H H H H                   ; ; ; ; ; ; ; ; ; ; ; ; & & & & & & & & ' ' ' ' ' '       ' ' ' ' ' ' 4 4 4 4 4 4 4 4 4 4 2********))))))&&&&&&111111
.
N!		y~		
-
=!			$
 
 k/ 
 |x'J J J J Jw~& J J JZw> w> w> w> w>8%& w> w> w>t    S   **) *) *) *) *)& *) *) *)ZB B B B B B B BJ       HD HD HD HD HD/0 HD HD HDV	 	 	 	 	# 	 	 	*) *) *) *) *){ *) *) *)Z1" 1" 1" 1" 1"- 1" 1" 1"h! ! ! ! ! ! ! !2) ) ) ) )I ) ) )XD D D D Di D D DP EI 	#'.6y.A   2 EI 	#'.6y.A   &!& !k !eCHo ! ! ! !!F ! !c3h ! ! ! !  V]4  %
V]4  %
v}T"".v}T"". 
 "2:+,, 
 <F  u d5%<&8 %     2:)**22z//11222
 <F  u d5%<&8 %    
  
).O 2,N0 'O*$N(rL   