o
    "4g4                     @  sz  U d Z ddlmZ ddlZddlmZ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 ddlmZmZmZ ddlmZ ddlmZmZ ddlmZ eraddlmZ dZ dZ!G dd de	Z"G dd dZ#ee$e%gdf Z&de'd< d/ddZ(ee(Z)G d d! d!e#Z*ee$e%gd"f Z+de'd#< d0d$d%Z,ee,Z-e.d&Z/	(d1d2d,d-Z0ej1ej2ej3d.Z4dS )3z&
Utility module to manipulate queries
    )annotationsN)AnyCallableMappingMatch
NamedTuple)SequenceTYPE_CHECKING)	lru_cache   )pq)errors)
Composable)BufferQueryParams)PyFormat)	TypeAlias	TypeGuard)conn_encoding)Transformeri   2   c                   @  s&   e Zd ZU ded< ded< ded< dS )	QueryPartbytesprez	int | stritemr   formatN)__name__
__module____qualname____annotations__ r!   r!   Y/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_queries.pyr      s   
 r   c                   @  sR   e Zd ZdZd ZdddZdddZdddZe	d ddZ
e	d!ddZdS )"PostgresQueryzO
    Helper to convert a Python query and parameters into Postgres format.
    z^
        query params types formats
        _tx _want_formats _parts _encoding _order
        transformerr   c                 C  s>   || _ d | _d| _d | _d | _t|j| _|  d| _d | _	d S )Nr!       )
_txparamstypes_want_formatsformatsr   
connection	_encodingquery_order)selfr$   r!   r!   r"   __init__-   s   
zPostgresQuery.__init__r-   r   varsParams | NonereturnNonec                 C  s   t |tr|| j}nt |tr|| j}n|}|dur>t|tkr-t|t	kr-t
}nt}||| j\| _| _| _| _n	|| _d | _| _| | dS z
        Set up the query and parameters to convert.

        The results of this function can be obtained accessing the object
        attributes (`query`, `params`, `types`, `formats`).
        N)
isinstancestrencoder,   r   as_bytesr&   lenMAX_CACHED_STATEMENT_LENGTHMAX_CACHED_STATEMENT_PARAMS	_query2pg_query2pg_nocacher-   r)   r.   _partsdumpr/   r-   r1   bqueryfr!   r!   r"   convert=   s    

zPostgresQuery.convertc                 C  sl   |dur+|  | j|| j}| jdusJ | j|| j| _| jjp"d| _| jj| _dS d| _d| _d| _dS )
        Process a new set of variables on the query processed by `convert()`.

        This method updates `params` and `types`.
        Nr!   )	validate_and_reorder_paramsr?   r.   r)   r&   dump_sequencer'   r(   r*   r/   r1   r'   r!   r!   r"   r@   b   s   
zPostgresQuery.dumpr   TypeGuard[Sequence[Any]]c                 C  sv   t | }|tu s|tu rd}|S |tu rd}|S t| tr(t| ttfs(d}|S t| tr1d}|S t	dt | j
 )NTFz8query parameters should be a sequence or a mapping, got )typelisttupledictr6   r   r   r7   r   	TypeErrorr   )r1   tsequencer!   r!   r"   is_params_sequences   s$   

z PostgresQuery.is_params_sequencepartslist[QueryPart]orderlist[str] | NoneSequence[Any]c                   s   t  r1t t| d kr!tdt| d  dt  d r/t| d jts/td S  rFt| dkrFt| d d t	sFtdz|rS fdd	|D W S W d
S  t
yr   tddt fdd|pjd
D  w )zO
        Verify the compatibility between a query and a set of params.
        r   zthe query has z placeholders but z parameters were passedr   z2named placeholders require a mapping of parametersz=positional placeholders (%s) require a sequence of parametersc                   s   g | ]} | qS r!   r!   ).0r   r1   r!   r"   
<listcomp>   s    z=PostgresQuery.validate_and_reorder_params.<locals>.<listcomp>r!   zquery parameter missing: z, c                 3  s    | ]	}| vr|V  qd S )Nr!   )rW   irX   r!   r"   	<genexpr>   s    z<PostgresQuery.validate_and_reorder_params.<locals>.<genexpr>)r#   rQ   r:   eProgrammingErrorr6   r   intrN   r7   KeyErrorjoinsorted)rR   r1   rT   r!   rX   r"   rF      s2   
"z)PostgresQuery.validate_and_reorder_paramsN)r$   r   r-   r   r1   r2   r3   r4   r1   r2   r3   r4   )r1   r   r3   rI   )rR   rS   r1   r   rT   rU   r3   rV   )r   r   r   __doc__split	__slots__r0   rD   r@   staticmethodrQ   rF   r!   r!   r!   r"   r#   #   s    


%r#   ?tuple[bytes, list[PyFormat], list[str] | None, list[QueryPart]]r   	_Query2Pgr-   r   encodingr7   r3   c           	      C  sh  t | |}d}g }g }t|d jtr;|dd D ] }t|jts#J ||j |d|jd   ||j qnht|d jtri }g }|dd D ]U}t|jtsWJ ||j |j|vrdt|d  }||jf||j< ||j || ||j qM||j d |jkrt	
d|j d|||j d  qM||d j d||||fS )	a  
    Convert Python query and params into something Postgres understands.

    - Convert Python placeholders (``%s``, ``%(name)s``) into Postgres
      format (``$1``, ``$2``)
    - placeholders can be %s, %t, or %b (auto, text or binary)
    - return ``query`` (bytes), ``formats`` (list of formats) ``order``
      (sequence of names used in the query, in the position they appear)
      ``parts`` (splits of queries and placeholders).
    Nr   s   $%dr   zplaceholder 'z' cannot have different formatsr%   )_split_queryr6   r   r^   appendr   r   r7   r:   r\   r]   r`   )	r-   rj   rR   rT   chunksr*   partseenphr!   r!   r"   r>      s<   


r>   c                   @  s(   e Zd ZdZdZdd	d
ZdddZdS )PostgresClientQueryzI
    PostgresQuery subclass merging query and arguments client-side.
    )templater-   r   r1   r2   r3   r4   c                 C  s   t |tr|| j}nt |tr|| j}n|}|dur<t|tkr-t|t	kr-t
}nt}||| j\| _| _| _n|| _d| _| | dS r5   )r6   r7   r8   r,   r   r9   r&   r:   r;   r<   _query2pg_client_query2pg_client_nocachers   r.   r?   r-   r@   rA   r!   r!   r"   rD      s   

zPostgresClientQuery.convertc                   sN   |dur"   j| j}t fdd|D  _ j j  _dS d _dS )rE   Nc                 3  s(    | ]}|d ur j |ndV  qd S )Ns   NULL)r&   
as_literal)rW   pr/   r!   r"   r[     s    
z+PostgresClientQuery.dump.<locals>.<genexpr>)rF   r?   r.   rL   r'   rs   r-   rH   r!   rx   r"   r@     s   

zPostgresClientQuery.dumpNrb   rc   )r   r   r   rd   rf   rD   r@   r!   r!   r!   r"   rr      s
    
rr   /tuple[bytes, list[str] | None, list[QueryPart]]_Query2PgClientc                 C  s  t | |dd}d}g }t|d jtr0|dd D ]}t|jts#J ||j |d qnNt|d jtr~i }g }|dd D ];}t|jtsLJ ||j |j|vrmd}||jf||j< ||j || qB|||j d  ||j qB||d j d|||fS )zX
    Convert Python query and params into a template to perform client-side binding
    F)collapse_double_percentNr   rk   s   %sr%   )	rl   r6   r   r^   rm   r   r7   r   r`   )r-   rj   rR   rT   rn   ro   rp   rq   r!   r!   r"   ru   $  s0   
ru   s"  (?x)
        %                       # a literal %
        (?:
            (?:
                \( ([^)]+) \)   # or a name in (braces)
                .               # followed by a format
            )
            |
            (?:.)               # or any char, really
        )
        asciiTr{   boolrS   c                 C  s  g }d}d }t | D ]}| ||dd  }|||f |dd }q|r5|| |d  d f n|| d f g }d}d }	|t|k r|| \}}|d u r_|t|dtj 	 |S |d}
|
dkr|rld}
||d  \}}||
 | |f||d < ||= qB|
dkrt	
d| |dd d   d | d|
dkrt	
d	|
d
d  dvrt	
d|d| d|dr|d|n|}|	st|}	n|	t|urt	
dt|
d
d   }|t||| |d7 }|t|k sH|S )Nr   r   s   %%   %s   %(zincomplete placeholder: ''s   % zfincomplete placeholder: '%'; if you want to use '%' as an operator you can double it up, i.e. use '%%'rk   s   sbtz8only '%s', '%b', '%t' are allowed as placeholders, got 'z1positional and named placeholders cannot be mixed)_re_placeholderfinditerspanrm   r:   r   r   AUTOgroupr\   r]   re   decoderJ   
_ph_to_fmt)r-   rj   r{   rR   curmr   rvrZ   phtyperq   pre1m1r   r   r!   r!   r"   rl   [  sn   ,
"
1rl   )   s   t   b)r-   r   rj   r7   r3   rh   )r-   r   rj   r7   r3   ry   )r|   T)r-   r   rj   r7   r{   r}   r3   rS   )5rd   
__future__r   retypingr   r   r   r   r   r   r	   	functoolsr
    r   r   r\   sqlr   abcr   r   r   _enumsr   _compatr   r   
_encodingsr   r   r;   r<   r   r#   r   r7   ri   r    r>   r=   rr   rz   ru   rt   compiler   rl   r   TEXTBINARYr   r!   r!   r!   r"   <module>   sP     	

75

%K
