o
    DDi76                     @  sF  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 ddl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
rdd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*dZ+ej,dkZ-G dd dee Z.G dd deZ/G dd de/Z0G dd de/Z1dId$d%Z2dId&d'Z3dJd+d,Z4dJd-d.Z5e6d/j7Z8e6d0j7Z9e6d/j:Z;e6d0j:Z<d1Z=d2Z>d3Z?e@d4ZAd5d6d7d8d9d:d;d<ZBeBfdKdBdCZCe@dDZDdEdF eBE D ZFeFfdKdGdHZGerejHZHejIZIejJZJejKZKdS e2ZHe3ZIe4ZJe5ZKdS )Lz
psycopg copy support
    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAnyGeneric)Sequence   )adapt)errors)pq)BufferConnectionTypePQGenTransformer)connection_summary)_psycopg)	copy_from)
BaseCursori      i   darwinc                   @  sf   e Zd ZU dZded< ddd d
dZd!ddZd"ddZd#ddZd$ddZ	d%ddZ
d&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                C  s   || _ |j| _| jj| _|j }r.|j| _| jtkr-| jtkr-t	
dt| jj dnt| _|d u r<t|o:|j}t|dd pFt|}|rOt|| _n	t|| jjd| _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tx r6   K/var/www/Datamplify/venv/lib/python3.10/site-packages/psycopg/_copy_base.py__init__L   s(   


zBaseCopy.__init__returnstrc                 C  s>   | 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)r3   clsinfor6   r6   r7   __repr__h   s   
zBaseCopy.__repr__Nonec                 C  s   | j rtdd S )Nz!copy blocks can be used only once)r2   	TypeErrorr3   r6   r6   r7   _enterm   s   zBaseCopy._entertypesSequence[int | str]c                   sV   | j jj  fd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                   s$   g | ]}t |tr|n |qS r6   )
isinstanceintget_oid).0tregistryr6   r7   
<listcomp>      $ z&BaseCopy.set_types.<locals>.<listcomp>N)
r   adaptersrK   r%   r&   r   transformerset_dumper_typesformatset_loader_types)r3   rK   oidsr6   rR   r7   	set_typesq   s
   

zBaseCopy.set_typesPQGen[Buffer]c                 c  sV    | j rtdS t| jE d H }t|tr|S d| _ |j}|d ur#|nd| j_tdS )N    T)r2   
memoryviewr   r"   rM   command_tuplesr   	_rowcount)r3   resnrowsr6   r6   r7   	_read_gen   s   
zBaseCopy._read_genPQGen[tuple[Any, ...] | None]c                 c  sH    |   E d H  }sd S | j| }d u r"|   E d H  d| _d S |S NT)re   r   	parse_rowr2   )r3   datarowr6   r6   r7   _read_row_gen   s   zBaseCopy._read_row_genPQGen[None]c                 c  sD    z|   E d H r	 |   E d H s	W d S W d S  tjy!   Y d S w N)re   r(   QueryCanceledrI   r6   r6   r7   _end_copy_out_gen   s   zBaseCopy._end_copy_out_gen)r   r   r   r   )r9   r:   )r9   rG   )rK   rL   r9   rG   )r9   r]   )r9   rf   )r9   rl   )__name__rA   rB   __doc____annotations__r8   rF   rJ   r\   re   rk   ro   r6   r6   r6   r7   r   ;   s   
 




r   c                   @  s\   e Zd ZU dZded< dddZedddZedddZedddZ	edddZ
dS )r   z@
    A class which understand a copy format (text, binary).
    z	pq.FormatrY   rW   r   c                 C  s   || _ t | _d| _d S NF)rW   	bytearray_write_buffer	_row_moder3   rW   r6   r6   r7   r8      s   
zFormatter.__init__ri   r   r9   tuple[Any, ...] | Nonec                 C     d S rm   r6   r3   ri   r6   r6   r7   rh         zFormatter.parse_rowbufferBuffer | strc                 C  ry   rm   r6   r3   r|   r6   r6   r7   write   r{   zFormatter.writerj   Sequence[Any]c                 C  ry   rm   r6   )r3   rj   r6   r6   r7   	write_row   r{   zFormatter.write_rowc                 C  ry   rm   r6   rI   r6   r6   r7   end   r{   zFormatter.endNrW   r   ri   r   r9   rx   r|   r}   r9   r   rj   r   r9   r   r9   r   )rp   rA   rB   rq   rr   r8   r   rh   r   r   r   r6   r6   r6   r7   r      s   
 
r   c                      sV   e Zd ZeZdd fddZdddZdddZdddZdddZ	d ddZ
  ZS )!r0   utf-8rW   r   r   r:   c                   s   t  | || _d S rm   )superr8   r1   )r3   rW   r   r@   r6   r7   r8         
zTextFormatter.__init__ri   r   r9   rx   c                 C  s   d }|r
t || j}|S rm   )parse_row_textrW   r3   ri   rvr6   r6   r7   rh      s   zTextFormatter.parse_rowr|   r}   c                 C     |  |}d| _|S rg   _ensure_bytes_signature_sentr3   r|   ri   r6   r6   r7   r         
zTextFormatter.writerj   r   c                 C  s<   d| _ t|| j| j t| jtkr| jt }| _|S dS NTr^   )rv   format_row_textrW   ru   lenBUFFER_SIZErt   r3   rj   r|   r6   r6   r7   r      s   zTextFormatter.write_rowc                 C  s   | j t }| _ |S rm   )ru   rt   r~   r6   r6   r7   r      s   zTextFormatter.endc                 C  s   t |tr|| jS |S rm   )rM   r:   encoder1   rz   r6   r6   r7   r      s   
zTextFormatter._ensure_bytes)r   )rW   r   r   r:   r   r   r   r   ri   r}   r9   r   )rp   rA   rB   TEXTrY   r8   rh   r   r   r   r   __classcell__r6   r6   r   r7   r0      s    



r0   c                      sT   e Zd ZeZd fddZdd	d
ZdddZdddZdddZ	dddZ
  ZS )r/   rW   r   c                   s   t  | d| _d S rs   )r   r8   r   rw   r   r6   r7   r8      r   zBinaryFormatter.__init__ri   r   r9   rx   c                 C  sV   d }| j s|d tt tkrt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_binaryrW   r   r6   r6   r7   rh     s   zBinaryFormatter.parse_rowr|   r}   c                 C  r   rg   r   r   r6   r6   r7   r     r   zBinaryFormatter.writerj   r   c                 C  sV   d| _ | js|  jt7  _d| _t|| j| j t| jtkr)| jt }| _|S dS r   )	rv   r   ru   r   format_row_binaryrW   r   r   rt   r   r6   r6   r7   r     s   zBinaryFormatter.write_rowc                 C  sL   | j s|  jt7  _|  jt7  _n
| jr|  jt7  _| jt }| _|S rm   )r   ru   r   r   rv   rt   r~   r6   r6   r7   r   '  s   zBinaryFormatter.endc                 C  s   t |tr	td|S )Nz6cannot copy str data in binary mode: use bytes instead)rM   r:   rH   rz   r6   r6   r7   r   :  s   
zBinaryFormatter._ensure_bytesr   r   r   r   r   r   )rp   rA   rB   BINARYrY   r8   rh   r   r   r   r   r   r6   r6   r   r7   r/      s    



r/   rj   r   r5   r   outrt   r9   rG   c                 C  sd   | s|d7 }dS | | tgt|  }|D ]}||dur"tt|nd7 }|d7 }qd|dd< dS )z6Convert a row of objects to the data to send for copy.   
N   \N   	r_   )dump_sequencePY_TEXTr   _dump_resub	_dump_subrj   r5   r   adaptedbr6   r6   r7   _format_row_textD  s   
r   c                 C  s^   | | tgt|  }|tt| 7 }|D ]}|dur(|tt|7 }||7 }q|t7 }qd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   r6   r6   r7   _format_row_binaryR  s   

r   ri   r   tuple[Any, ...]c                 C  sH   t | ts	t| } | d}|d d d |d< dd |D }||S )Nr   r_   c                 S  s$   g | ]}|d kr
dnt t|qS )r   N)_load_rer   	_load_sub)rP   fr6   r6   r7   rT   c  rU   z#_parse_row_text.<locals>.<listcomp>)rM   bytessplitload_sequence)ri   r5   fieldsrj   r6   r6   r7   _parse_row_text^  s   


r   c                 C  sv   g }t | dd }d}t|D ]&}t| |d }|d7 }|dkr0|| |||   ||7 }q|d  q||S )Nr      r   )_unpack_int2range_unpack_int4appendr   )ri   r5   rj   nfieldsposilengthr6   r6   r7   _parse_row_binaryg  s   

r   z!hz!is   PGCOPY

         s   s   s
   [	
\\]s   \bs   \ts   \ns   \vs   \fs   \rs   \\)   r   r               \mre.Match[bytes]__mapdict[bytes, bytes]r   c                 C     ||  d S Nr   groupr   r   r6   r6   r7   r        r   s   \\[btnvfr\\]c                 C  s   i | ]\}}||qS r6   r6   )rP   kvr6   r6   r7   
<dictcomp>  s    r   c                 C  r   r   r   r   r6   r6   r7   r     r   r   )rj   r   r5   r   r   rt   r9   rG   )ri   r   r5   r   r9   r   )r   r   r   r   r9   r   )Lrq   
__future__r   resysstructabcr   r   typingr   r   r   collections.abcr    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   r0   r/   r   r   r   r   Structpackr   r   unpack_fromr   r   r   r   r   compiler   
_dump_replr   r   items
_load_replr   r   r   r   r   r6   r6   r6   r7   <module>   s    
z-
I


	


