
    gi6                       d Z ddlm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
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 ddlmZ ddlmZ e
rddlmZ ej         j!        Z"ej         j#        Z$ej%        j!        Z!ej%        j#        Z#ej&        j'        Z'ej&        j(        Z(dZ)de)z  Z*dZ+ej,        dk    Z- G d dee                   Z. G d de          Z/ G d de/          Z0 G d de/          Z1dAd#Z2dAd$Z3dBd(Z4dBd)Z5 ej6        d*          j7        Z8 ej6        d+          j7        Z9 ej6        d*          j:        Z; ej6        d+          j:        Z<d,Z=d-Z>d.Z? ej@        d/          ZAd0d1d2d3d4d5d6d7ZBeBfdCd=ZC ej@        d>          ZDd? eBE                                D             ZFeFfdCd@ZGerejH        ZHejI        ZIejJ        ZJejK        ZKdS e2ZHe3ZIe4ZJe5ZKdS )Dz
psycopg copy support
    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAnyGenericMatchSequence   )adapt)errors)pq)BufferConnectionTypePQGenTransformer)connection_summary)_psycopg)	copy_from)
BaseCursori      i   darwinc                  \    e Zd ZU dZded<   dddd
ZddZddZddZddZ	ddZ
d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cursorBaseCursor[ConnectionType, Any]r   bool | Nonec               0   || _         |j        | _        | j        j        | _        |j        x}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+          || j        j                  | _        d| _        d S )NzAthe cursor should have performed a COPY operation; its status is z instead_tx)encodingF)r   
connectionpgconn_pgconnpgresultstatus
_directionCOPY_INCOPY_OUTeProgrammingErrorr   
ExecStatusnameboolbinary_tuplesgetattrr   r   BinaryFormatterr   TextFormatter	_encoding	_finished)selfr   r   resulttxs        b/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/psycopg/_copy_base.py__init__zBaseCopy.__init__K   s     +-_$6 	&$mDO'))do.I.I(T&(mDO&D&D&IT T T  
 &DO>&9V%9::F!&%66S%:KF:S:S 	P,R00DNN*28NOOODN    returnstrc                    | j         j         d| j         j         }t          | j                  }d| d| dt          |           ddS )N.< z at 0xx>)	__class__
__module____qualname__r   r&   id)r7   clsinfos      r:   __repr__zBaseCopy.__repr__g   sX    *JJT^-HJJ!$,//232222RXX22222r<   Nonec                2    | j         rt          d          d S )Nz!copy blocks can be used only once)r6   	TypeErrorr7   s    r:   _enterzBaseCopy._enterl   s(    > 	A?@@@	A 	Ar<   typesSequence[int | str]c                   | 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     r:   
<listcomp>z&BaseCopy.set_types.<locals>.<listcomp>   s:    PPPQZ3''@X-=-=a-@-@PPPr<   N)
r   adaptersrQ   r)   r*   r   transformerset_dumper_typesformatset_loader_types)r7   rQ   oidsr[   s      @r:   	set_typeszBaseCopy.set_typesp   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Tr<   PQGen[Buffer]c              #     K   | j         rt          d          S t          | j                  E d {V }t	          |t                    r|S d| _         |j        }||nd| j        _        t          d          S )Nr<   T)r6   
memoryviewr   r&   rV   command_tuplesr   	_rowcount)r7   resnrowss      r:   	_read_genzBaseCopy._read_gen   s      > 	#c??""4<00000000c:&& 	J  ").):#r<   PQGen[tuple[Any, ...] | None]c              #     K   |                                  E d {V x}sd S | j                            |          x}#|                                  E d {V  d| _        d S |S NT)rl   r   	parse_rowr6   )r7   datarows      r:   _read_row_genzBaseCopy._read_row_gen   s      $(NN$4$44444445 	4>++D111C:~~'''''''''!DN4
r<   PQGen[None]c              #     K   	 |                                  E d {V r	 |                                  E d {V d S d S # t          j        $ r Y d S w xY wN)rl   r,   QueryCanceledrO   s    r:   _end_copy_out_genzBaseCopy._end_copy_out_gen   s      	"nn........  #nn........      	 	 	DD	s   5= AA)r   r   r   r    )r=   r>   )r=   rL   )rQ   rR   r=   rL   )r=   rd   )r=   rm   )r=   rt   )__name__rF   rG   __doc____annotations__r;   rK   rP   rc   rl   rs   rx   rU   r<   r:   r   r   :   s            QU     83 3 3 3
A A A AU U U U:   &
 
 
 
     r<   r   c                      e Zd ZU dZded<   ddZedd            Zedd            Zedd            Z	edd            Z
dS )r   z@
    A class which understand a copy format (text, binary).
    z	pq.Formatr`   r^   r   c                H    || _         t                      | _        d| _        d S NF)r^   	bytearray_write_buffer	_row_mode)r7   r^   s     r:   r;   zFormatter.__init__   s!    &&[[r<   rq   r   r=   tuple[Any, ...] | Nonec                    d S rv   rU   r7   rq   s     r:   rp   zFormatter.parse_row   s    ADr<   bufferBuffer | strc                    d S rv   rU   r7   r   s     r:   writezFormatter.write   s    58Sr<   rr   Sequence[Any]c                    d S rv   rU   )r7   rr   s     r:   	write_rowzFormatter.write_row   s    7:sr<   c                    d S rv   rU   rO   s    r:   endzFormatter.end   s     Sr<   Nr^   r   rq   r   r=   r   r   r   r=   r   rr   r   r=   r   r=   r   )ry   rF   rG   rz   r{   r;   r   rp   r   r   r   rU   r<   r:   r   r      s              
 DDD ^D888 ^8::: ^:    ^   r<   r   c                  N     e Zd ZeZdd fdZddZddZddZddZ	ddZ
 xZS )r4   utf-8r^   r   r#   r>   c                X    t                                          |           || _        d S rv   )superr;   r5   )r7   r^   r#   rE   s      r:   r;   zTextFormatter.__init__   s&    %%%!r<   rq   r   r=   r   c                8    d }|rt          || j                  }|S rv   )parse_row_textr^   r7   rq   rvs      r:   rp   zTextFormatter.parse_row   s&    %) 	8d&677B	r<   r   r   c                >    |                      |          }d| _        |S ro   _ensure_bytes_signature_sentr7   r   rq   s      r:   r   zTextFormatter.write   "    !!&))#r<   rr   r   c                    d| _         t          || j        | j                   t	          | j                  t
          k    r| j        t                      c}| _        |S dS NTr<   )r   format_row_textr^   r   lenBUFFER_SIZEr   r7   rr   r   s      r:   r   zTextFormatter.write_row   sY     T-t/ABBBt!""[00)-);Y[[&FD&M3r<   c                <    | j         t                      c}| _         |S rv   )r   r   r   s     r:   r   zTextFormatter.end   s    %)%7""r<   c                d    t          |t                    r|                    | j                  S |S rv   )rV   r>   encoder5   r   s     r:   r   zTextFormatter._ensure_bytes   s/    dC   	;;t~...
 Kr<   )r   )r^   r   r#   r>   r   r   r   r   rq   r   r=   r   )ry   rF   rG   TEXTr`   r;   rp   r   r   r   r   __classcell__rE   s   @r:   r4   r4      s        F" " " " " " "      

 
 
 
          r<   r4   c                  L     e Zd ZeZd fdZddZddZddZddZ	ddZ
 xZS )r3   r^   r   c                X    t                                          |           d| _        d S r~   )r   r;   r   )r7   r^   rE   s     r:   r;   zBinaryFormatter.__init__   s)    %%%$r<   rq   r   r=   r   c                   d }| j         s\|d t          t                             t          k    rt          j        d          d| _         |t          t                    d          }|t
          k    rt          || j                  }|S )Nz5binary copy doesn't start with the expected signatureT)r   r   _binary_signaturer,   	DataError_binary_trailerparse_row_binaryr^   r   s      r:   rp   zBinaryFormatter.parse_row  s    %)# 	2,c+,,,-1BBBkK   $(D -..001D?""!$(899B	r<   r   r   c                >    |                      |          }d| _        |S ro   r   r   s      r:   r   zBinaryFormatter.write  r   r<   rr   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 r   )	r   r   r   r   format_row_binaryr^   r   r   r   r   s      r:   r   zBinaryFormatter.write_row  s     # 	("33#'D #t/1CDDDt!""[00)-);Y[[&FD&M3r<   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 rv   )r   r   r   r   r   r   r   s     r:   r   zBinaryFormatter.end&  sv     # 	2"33/1^ 	2 /1%)%7""r<   c                N    t          |t                    rt          d          |S )Nz6cannot copy str data in binary mode: use bytes instead)rV   r>   rN   r   s     r:   r   zBinaryFormatter._ensure_bytes9  s*    dC   	TUUU
 Kr<   r   r   r   r   r   r   )ry   rF   rG   BINARYr`   r;   rp   r   r   r   r   r   r   s   @r:   r3   r3      s        F% % % % % %       
       &       r<   r3   rr   r   r9   r   outr   r=   rL   c                    | s|dz  }dS |                     | t          gt          |           z            }|D ].}|| t                              t
          |          ndz  }|dz  }/d|dd<   dS )z6Convert a row of objects to the data to send for copy.   
N   \N   	rf   )dump_sequencePY_TEXTr   _dump_resub	_dump_subrr   r9   r   adaptedbs        r:   _format_row_textr   C  s     usWIC$899G  Q]x||Iq)))FuCHHHr<   c                   |                     | t          gt          |           z            }|t          t          |                     z  }|D ]3}|%|t	          t          |                    z  }||z  })|t
          z  }4dS )z=Convert a row of objects to the data to send for binary copy.N)r   	PY_BINARYr   
_pack_int2
_pack_int4_binary_nullr   s        r:   _format_row_binaryr   Q  s    sYK#c(($:;;G:c#hhC    =:c!ff%%%C1HCC<CC   r<   rq   r   tuple[Any, ...]c                    t          | t                    st          |           } |                     d          }|d         d d         |d<   d |D             }|                    |          S )Nr   rf   c                ^    g | ]*}|d k    rdnt                               t          |          +S )r   N)_load_rer   	_load_sub)rY   fs     r:   r\   z#_parse_row_text.<locals>.<listcomp>b  s2    
O
O
O11;;44HLLA$>$>
O
O
Or<   )rV   bytessplitload_sequence)rq   r9   fieldsrr   s       r:   _parse_row_textr   ]  sl    dE"" T{{ZZFCRCF2J
O
O
O
O
OCC   r<   c                <   g }t          | d          d         }d}t          |          D ]^}t          | |          d         }|dz  }|dk    r&|                    | |||z                       ||z  }I|                    d            _|                    |          S )Nr      r   )_unpack_int2range_unpack_int4appendr   )rq   r9   rr   nfieldsposilengths          r:   _parse_row_binaryr   f  s    !C4##A&G
C7^^  dC((+qQ;;JJtC#,./0006MCCJJtC   r<   z!hz!is   PGCOPY

         s   s   s
   [	
\\]s   \bs   \ts   \ns   \vs   \fs   \rs   \\)   r   r               \mMatch[bytes]__mapdict[bytes, bytes]r   c                8    ||                      d                   S Nr   groupr   r   s     r:   r   r         r<   s   \\[btnvfr\\]c                    i | ]\  }}||	S rU   rU   )rY   kvs      r:   
<dictcomp>r     s    222tq!a222r<   c                8    ||                      d                   S r   r   r   s     r:   r   r     r   r<   )rr   r   r9   r   r   r   r=   rL   )rq   r   r9   r   r=   r   )r   r   r   r   r=   r   )Lrz   
__future__r   resysstructabcr   r   typingr   r   r   r	   r
    r   r   r,   r   r   r   r   r   pq.miscr   _cmoduler   
generatorsr   _cursor_baser   PyFormatr   r   r   r   Formatr.   r*   r+   r   MAX_BUFFER_SIZE
QUEUE_SIZEplatformPREFER_FLUSHr   r   r4   r3   r   r   r   r   Structpackr   r   unpack_fromr   r   r   r   r   compiler   
_dump_replr   r   items
_load_replr   r   r   r   r   rU   r<   r:   <module>r     s    # " " " " " 				 



  # # # # # # # # ? ? ? ? ? ? ? ? ? ? ? ? ? ?                   ; ; ; ; ; ; ; ; ; ; ; ; ' ' ' ' ' '       ! ! ! ! ! ! )((((((
.
N!		y~		
-
=!
 
 k/ 
 |x'w w w w ww~& w w wt! ! ! ! ! ! ! !2* * * * *I * * *ZF F F F Fi F F FR   	  	  	  	 ! ! ! !! ! ! !  V]4  %
V]4  %
v}T"".v}T"". 
 "2:+,, 
 <F      2:)**22z//11222
 <F     
  
).O 2,N0 'O*$N(r<   