o
    6	f3                     @   s<  U d Z ddl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 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 ddlmZmZmZ ddlmZ ddlm Z  ercddlm!Z! dZ"dZ#G dd de	Z$G dd dZ%ee&e'gee&ee e
ee'  ee$ f f Z(ee)d< de&de'dee&ee e
ee'  ee$ f fddZ*e e*Z+G dd de%Z,ee&e'gee&e
ee'  ee$ f f Z-ee)d< de&de'dee&e
ee'  ee$ f fddZ.e e.Z/d ee$ d!ed"e
ee'  dee fd#d$Z0e1d%Z2	'd,de&de'd(e3dee$ fd)d*Z4ej5ej6ej7d+Z8dS )-z&
Utility module to manipulate queries
    N)AnyCallableDictListMappingMatch
NamedTupleOptional)SequenceTupleUnionTYPE_CHECKING)	lru_cache)	TypeAlias   )pq)errors)
Composable)BufferQueryParams)PyFormat)conn_encoding)Transformeri   2   c                   @   s.   e Zd ZU eed< eeef ed< eed< dS )	QueryPartpreitemformatN)	__name__
__module____qualname__bytes__annotations__r   intstrr    r&   r&   I/home/ertert/spirit/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edee	 d	d
fddZ
dee	 d	d
f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__r2   varsreturnNc                 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)
isinstancer%   encoder1   r   as_bytesr+   lenMAX_CACHED_STATEMENT_LENGTHMAX_CACHED_STATEMENT_PARAMS	_query2pg_query2pg_nocacher2   r.   r3   _partsdumpr4   r2   r6   bqueryfr&   r&   r'   convert;   s    

zPostgresQuery.convertc                 C   sj   |dur*t | 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_paramsrA   r3   r.   r+   dump_sequencer,   r-   r/   r4   r6   r,   r&   r&   r'   rB   `   s   
zPostgresQuery.dump)r)   r   )r   r    r!   __doc__split	__slots__r5   r   r	   r   rF   rB   r&   r&   r&   r'   r(   !   s    
%r(   	_Query2Pgr2   encodingr7   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_queryr9   r   r$   appendr   r   r%   r<   eProgrammingErrorjoin)	r2   rO   partsorderchunksr/   partseenphr&   r&   r'   r@   x   s<   


r@   c                   @   sD   e Zd ZdZdZdedee ddfddZdee ddfd	d
Z	dS )PostgresClientQueryzI
    PostgresQuery subclass merging query and arguments client-side.
    )templater2   r6   r7   Nc                 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 r8   )r9   r%   r:   r1   r   r;   r+   r<   r=   r>   _query2pg_client_query2pg_client_nocacher]   r3   rA   r2   rB   rC   r&   r&   r'   rF      s   

zPostgresClientQuery.convertc                    sL   |dur!t  j| j}t fdd|D  _ j j  _dS d _dS )rG   Nc                 3   s(    | ]}|d ur j |ndV  qd S )Ns   NULL)r+   
as_literal).0pr4   r&   r'   	<genexpr>   s    
z+PostgresClientQuery.dump.<locals>.<genexpr>)rH   rA   r3   tupler,   r]   r2   rJ   r&   rc   r'   rB      s   

zPostgresClientQuery.dump)
r   r    r!   rK   rM   r   r	   r   rF   rB   r&   r&   r&   r'   r\      s
    r\   _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   rP   s   %sr*   )	rQ   r9   r   r$   rR   r   r%   r   rU   )r2   rO   rV   rW   rX   rY   rZ   r[   r&   r&   r'   r_      s0   
r_   rV   r6   rW   c                    sH  t  }|tu s|tu rd}n(|tu rd}n!t tr%t ttfs%d}nt tr-d}n
t	dt  j
 |ret t| d krUtdt| d  dt  d rct| d jtsct	d	 S  rzt| dkrzt| d d tszt	d
z fdd|pdD W S  ty   tddt fdd|pdD  w )zG
    Verify the compatibility between a query and a set of params.
    TFz8query parameters should be a sequence or a mapping, got 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&   )ra   r   r6   r&   r'   
<listcomp>:  s    z0_validate_and_reorder_params.<locals>.<listcomp>r&   zquery parameter missing: z, c                 3   s    | ]	}| vr|V  qd S )Nr&   )ra   irh   r&   r'   rd   >  s    z/_validate_and_reorder_params.<locals>.<genexpr>)typelistre   dictr9   r
   r"   r%   r   	TypeErrorr   r<   rS   rT   r   r$   KeyErrorrU   sorted)rV   r6   rW   tsequencer&   rh   r'   rH     sJ   
"rH   s"  (?x)
        %                       # a literal %
        (?:
            (?:
                \( ([^)]+) \)   # or a name in (braces)
                .               # followed by a format
            )
            |
            (?:.)               # or any char, really
        )
        asciiTrg   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 '%%'rP   s   sbtz8only '%s', '%b', '%t' are allowed as placeholders, got 'z1positional and named placeholders cannot be mixed)_re_placeholderfinditerspanrR   r<   r   r   AUTOgrouprS   rT   rL   decoderk   
_ph_to_fmt)r2   rO   rg   rV   curmr   rvrj   phtyper[   pre1m1r   r   r&   r&   r'   rQ   Q  sn   ,
"
1rQ   )   s   t   b)rs   T)9rK   retypingr   r   r   r   r   r   r   r	   r
   r   r   r   	functoolsr   typing_extensionsr    r   r   rS   sqlr   abcr   r   r   _enumsr   
_encodingsr   r   r=   r>   r   r(   r"   r%   rN   r#   r@   r?   r\   rf   r_   r^   rH   compilerv   boolrQ   ry   TEXTBINARYr|   r&   r&   r&   r'   <module>   s    (R&

75 

%

.
K
