o
    "4g6                     @  sJ  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rbd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/Z1	dHdId#d$Z2	dHdId%d&Z3dJd*d+Z4dJd,d-Z5e6d.j7Z8e6d/j7Z9e6d.j:Z;e6d/j:Z<d0Z=d1Z>d2Z?e@d3ZAd4d5d6d7d8d9d:d;ZBeBfdKdAdBZCe@dCZDdDdE eBE D ZFeFfdKdFdGZGerejHZHejIZIejJZJejKZKdS e2ZHe3ZIe4ZJe5ZKdS )Lz
psycopg copy support
    )annotationsN)ABCabstractmethod)AnyGenericMatchSequenceTYPE_CHECKING   )pq)adapt)errors)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 r7   [/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_copy_base.py__init__K   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)r4   clsinfor7   r7   r8   __repr__k   s   
zBaseCopy.__repr__Nonec                 C  s   | j rtdd S )Nz!copy blocks can be used only once)r3   	TypeErrorr4   r7   r7   r8   _enterp   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 r7   )
isinstanceintget_oid).0tregistryr7   r8   
<listcomp>      $ z&BaseCopy.set_types.<locals>.<listcomp>N)
r   adaptersrL   r&   r'   r   transformerset_dumper_typesformatset_loader_types)r4   rL   oidsr7   rS   r8   	set_typest   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)r3   
memoryviewr   r#   rN   command_tuplesr   	_rowcount)r4   resnrowsr7   r7   r8   	_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)rf   r   	parse_rowr3   )r4   datarowr7   r7   r8   _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)rf   r)   QueryCanceledrJ   r7   r7   r8   _end_copy_out_gen   s   zBaseCopy._end_copy_out_gen)r   r   r   r   )r:   r;   )r:   rH   )rL   rM   r:   rH   )r:   r^   )r:   rg   )r:   rm   )__name__rB   rC   __doc____annotations__r9   rG   rK   r]   rf   rl   rp   r7   r7   r7   r8   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.FormatrZ   rX   r   c                 C  s   || _ t | _d| _d S NF)rX   	bytearray_write_buffer	_row_moder4   rX   r7   r7   r8   r9      s   
zFormatter.__init__rj   r   r:   tuple[Any, ...] | Nonec                 C     d S rn   r7   r4   rj   r7   r7   r8   ri         zFormatter.parse_rowbufferBuffer | strc                 C  rz   rn   r7   r4   r}   r7   r7   r8   write   r|   zFormatter.writerk   Sequence[Any]c                 C  rz   rn   r7   )r4   rk   r7   r7   r8   	write_row   r|   zFormatter.write_rowc                 C  rz   rn   r7   rJ   r7   r7   r8   end   r|   zFormatter.endNrX   r   rj   r   r:   ry   r}   r~   r:   r   rk   r   r:   r   r:   r   )rq   rB   rC   rr   rs   r9   r   ri   r   r   r   r7   r7   r7   r8   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 )!r1   utf-8rX   r   r    r;   c                   s   t  | || _d S rn   )superr9   r2   )r4   rX   r    rA   r7   r8   r9         
zTextFormatter.__init__rj   r   r:   ry   c                 C  s   |rt || jS d S rn   )parse_row_textrX   r{   r7   r7   r8   ri      s   zTextFormatter.parse_rowr}   r~   c                 C     |  |}d| _|S rh   _ensure_bytes_signature_sentr4   r}   rj   r7   r7   r8   r         
zTextFormatter.writerk   r   c                 C  s<   d| _ t|| j| j t| jtkr| jt }| _|S dS NTr_   )rw   format_row_textrX   rv   lenBUFFER_SIZEru   r4   rk   r}   r7   r7   r8   r      s   zTextFormatter.write_rowc                 C  s   | j t }| _ |S rn   )rv   ru   r   r7   r7   r8   r      s   zTextFormatter.endc                 C  s   t |tr|| jS |S rn   )rN   r;   encoder2   r{   r7   r7   r8   r      s   
zTextFormatter._ensure_bytes)r   )rX   r   r    r;   r   r   r   r   rj   r~   r:   r   )rq   rB   rC   TEXTrZ   r9   ri   r   r   r   r   __classcell__r7   r7   r   r8   r1      s    



r1   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 )r0   rX   r   c                   s   t  | d| _d S rt   )r   r9   r   rx   r   r7   r8   r9     r   zBinaryFormatter.__init__rj   r   r:   ry   c                 C  sT   | j s|d tt tkrtdd| _ |ttd  }n|tkr$d S t|| jS )Nz5binary copy doesn't start with the expected signatureT)r   r   _binary_signaturer)   	DataError_binary_trailerparse_row_binaryrX   r{   r7   r7   r8   ri     s   zBinaryFormatter.parse_rowr}   r~   c                 C  r   rh   r   r   r7   r7   r8   r     r   zBinaryFormatter.writerk   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   )	rw   r   rv   r   format_row_binaryrX   r   r   ru   r   r7   r7   r8   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 rn   )r   rv   r   r   rw   ru   r   r7   r7   r8   r   )  s   zBinaryFormatter.endc                 C  s   t |tr	td|S )Nz6cannot copy str data in binary mode: use bytes instead)rN   r;   rI   r{   r7   r7   r8   r   <  s   
zBinaryFormatter._ensure_bytesr   r   r   r   r   r   )rq   rB   rC   BINARYrZ   r9   ri   r   r   r   r   r   r7   r7   r   r8   r0      s    



r0   rk   r   r6   r   outbytearray | Noner:   ru   c                 C  sr   |du rt  }| s|d7 }|S || tgt|  }|D ]}||dur)tt|nd7 }|d7 }qd|dd< |S )z6Convert a row of objects to the data to send for copy.N   
   \N   	r`   )ru   dump_sequencePY_TEXTr   _dump_resub	_dump_subrk   r6   r   adaptedbr7   r7   r8   _format_row_textF  s   
r   c                 C  sl   |du rt  }|tt| 7 }|| tgt|  }|D ]}|dur/|tt|7 }||7 }q|t7 }q|S )z=Convert a row of objects to the data to send for binary copy.N)ru   
_pack_int2r   r   	PY_BINARY
_pack_int4_binary_nullr   r7   r7   r8   _format_row_binaryZ  s   

r   rj   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)rQ   fr7   r7   r8   rU   r  rV   z#_parse_row_text.<locals>.<listcomp>)rN   bytessplitload_sequence)rj   r6   fieldsrk   r7   r7   r8   _parse_row_textm  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   )rj   r6   rk   nfieldsposilengthr7   r7   r8   _parse_row_binaryv  s   

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                 C     ||  d S Nr   groupr   r   r7   r7   r8   r        r   s   \\[btnvfr\\]c                 C  s   i | ]\}}||qS r7   r7   )rQ   kvr7   r7   r8   
<dictcomp>  s    r   c                 C  r   r   r   r   r7   r7   r8   r     r   r   rn   )rk   r   r6   r   r   r   r:   ru   )rj   r   r6   r   r:   r   )r   r   r   r   r:   r   )Lrr   
__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   r1   r0   r   r   r   r   Structpackr   r   unpack_fromr   r   r   r   r   compiler   
_dump_replr   r   items
_load_replr   r   r   r   r   r7   r7   r7   r8   <module>   s    
 ,H

	


