o
    XJ@hA0                     @  s   d 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 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 ddlmZ G dd dejZG dd dejZG dd dejZe Ze ZG dd dZdddZdS ) zEvaluation functions used **INTERNALLY** by ORM DML use cases.


This module is **private, for internal use by SQLAlchemy**.

.. versionchanged:: 2.0.4 renamed ``EvaluatorCompiler`` to
   ``_EvaluatorCompiler``.

    )annotations)Type   )exc)LoaderCallableStatus)PassiveFlag   )inspect)and_)	operators)Concatenable)Integer)Numeric)warn_deprecatedc                   @  s   e Zd ZdS )UnevaluatableErrorN)__name__
__module____qualname__ r   r   K/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/orm/evaluator.pyr   %   s    r   c                   @     e Zd Zdd Zdd ZdS )	_NoObjectc                 O     d S Nr   selfargkwr   r   r   operate*      z_NoObject.operatec                 O  r   r   r   r   r   r   r   reverse_operate-   r   z_NoObject.reverse_operateNr   r   r   r   r    r   r   r   r   r   )       r   c                   @  r   )_ExpiredObjectc                 O     | S r   r   r   r   r   r   r   2   r   z_ExpiredObject.operatec                 O  r$   r   r   r   r   r   r   r    5   r   z_ExpiredObject.reverse_operateNr!   r   r   r   r   r#   1   r"   r#   c                   @  s
  e Zd Zd6ddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZeZeZeZeZeZeZeZeZeZeZeZ d(d) Z!d*d+ Z"d,d- Z#d.d/ Z$d0d1 Z%d2d3 Z&d4d5 Z'dS )7_EvaluatorCompilerNc                 C  s
   || _ d S r   )
target_cls)r   r&   r   r   r   __init__>      
z_EvaluatorCompiler.__init__c                 G  sH   |r
t |g|R  }t| d|j d }|s tdt|j ||S )Nvisit_Cannot evaluate )r
   getattrZ__visit_name__r   typer   )r   clauseclausesmethr   r   r   processA   s   z_EvaluatorCompiler.processc                 C  s   |  |jS r   )r0   elementr   r-   r   r   r   visit_groupingL   s   z!_EvaluatorCompiler.visit_groupingc                 C     dd S )Nc                 S  r   r   r   objr   r   r   <lambda>P       z/_EvaluatorCompiler.visit_null.<locals>.<lambda>r   r2   r   r   r   
visit_nullO      z_EvaluatorCompiler.visit_nullc                 C  r4   )Nc                 S     dS )NFr   r5   r   r   r   r7   S   r8   z0_EvaluatorCompiler.visit_false.<locals>.<lambda>r   r2   r   r   r   visit_falseR   r:   z_EvaluatorCompiler.visit_falsec                 C  r4   )Nc                 S  r;   )NTr   r5   r   r   r   r7   V   r8   z/_EvaluatorCompiler.visit_true.<locals>.<lambda>r   r2   r   r   r   
visit_trueU   r:   z_EvaluatorCompiler.visit_truec              
     s   z|j d }W n ty } ztd| |d }~ww | jr.t| j|js.td|j |  z|j| j}W n t	j
yO } ztd| |d }~ww |j| j  fdd}|S )NparentmapperzCannot evaluate column: z0Can't evaluate criteria against alternate class zCannot evaluate expression: c                   s>   | d u rt S t| }|j} j||tjd}|tju rtS |S )N)Zpassive)	
_NO_OBJECTr	   dictgetr   ZPASSIVE_NO_FETCHr   ZPASSIVE_NO_RESULT_EXPIRED_OBJECT)r6   stateZdict_valueimplr   r   get_corresponding_attr   s   
z?_EvaluatorCompiler.visit_column.<locals>.get_corresponding_attr)Z_annotationsKeyErrorr   r&   
issubclassclass_Z_check_configureZ_columntopropertykeyorm_excZUnmappedColumnErrorZclass_managerrF   )r   r-   r>   ZkerK   errrG   r   rE   r   visit_columnX   s@   z_EvaluatorCompiler.visit_columnc                 C  
   |  |S r   visit_clauselistr2   r   r   r   visit_tuple   r(   z_EvaluatorCompiler.visit_tuplec                 C  rO   r   rP   r2   r   r   r   visit_expression_clauselist   r(   z._EvaluatorCompiler.visit_expression_clauselistc                   sX    fdd|j D }d|jjd d}t |d }|r$||j||S td|j )Nc                   s   g | ]}  |qS r   )r0   ).0r-   r   r   r   
<listcomp>   s    z7_EvaluatorCompiler.visit_clauselist.<locals>.<listcomp>r)   _Z_clauselist_opz)Cannot evaluate clauselist with operator )r.   operatorr   rstripr+   r   )r   r-   
evaluatorsdispatchr/   r   rU   r   rQ      s   
z#_EvaluatorCompiler.visit_clauselistc                 C  sj   |  |j}|  |j}d|jjd d}t| |d }|r'||j|||S tdt|j d|j )Nr)   rW   Z
_binary_opr*    with operator )	r0   leftrightrX   r   rY   r+   r   r,   )r   r-   	eval_left
eval_rightr[   r/   r   r   r   visit_binary   s   z_EvaluatorCompiler.visit_binaryc                       fdd}|S )Nc                   sH   d} D ]}|| }|t u rt   S |r dS |p|d u }q|r"d S dS NFTrB   )r6   Zhas_nullsub_evaluaterD   rZ   r   r   evaluate   s   z;_EvaluatorCompiler.visit_or_clauselist_op.<locals>.evaluater   r   rX   rZ   r-   rg   r   rf   r   visit_or_clauselist_op   s   z)_EvaluatorCompiler.visit_or_clauselist_opc                   rb   )Nc                   sF    D ]}|| }|t u rt   S |s |d u s|tu r d S  dS qdS rc   )rB   r?   )r6   re   rD   rf   r   r   rg      s   z<_EvaluatorCompiler.visit_and_clauselist_op.<locals>.evaluater   rh   r   rf   r   visit_and_clauselist_op   s   z*_EvaluatorCompiler.visit_and_clauselist_opc                   rb   )Nc                   sN   g } D ]}|| }|t u rt   S |d u s|tu r d S || qt|S r   )rB   r?   appendtuple)r6   valuesre   rD   rf   r   r   rg      s   zA_EvaluatorCompiler.visit_comma_op_clauselist_op.<locals>.evaluater   rh   r   rf   r   visit_comma_op_clauselist_op   s   z/_EvaluatorCompiler.visit_comma_op_clauselist_opc                 C  s(   |j r| ||||S td|jd)NzCustom operator zR can't be evaluated in Python unless it specifies a callable using `.python_impl`.)Zpython_impl_straight_evaluater   Zopstringr   rX   r_   r`   r-   r   r   r   visit_custom_op_binary_op   s   z,_EvaluatorCompiler.visit_custom_op_binary_opc                       fdd}|S )Nc                   s,    | }| }|t u s|t u rt S ||kS r   rd   r6   Zleft_valZ	right_valr_   r`   r   r   rg      
   z7_EvaluatorCompiler.visit_is_binary_op.<locals>.evaluater   r   rX   r_   r`   r-   rg   r   rt   r   visit_is_binary_op      z%_EvaluatorCompiler.visit_is_binary_opc                   rr   )Nc                   s,    | }| }|t u s|t u rt S ||kS r   rd   rs   rt   r   r   rg      ru   z;_EvaluatorCompiler.visit_is_not_binary_op.<locals>.evaluater   rv   r   rt   r   visit_is_not_binary_op   rx   z)_EvaluatorCompiler.visit_is_not_binary_opc                   s    fdd}|S )Nc                   sJ    | }| }|t u s|t u rt S |d u s|d u rd S  | | S r   rd   rs   r_   r`   rX   r   r   rg      s   z7_EvaluatorCompiler._straight_evaluate.<locals>.evaluater   rv   r   rz   r   ro      s   
z%_EvaluatorCompiler._straight_evaluatec                 C  sX   |j jjttfvs|jjjttfvr$td|j d|j j d|jj | ||||S )NzCannot evaluate math operator "" for datatypes , )	r]   r,   _type_affinityr   r   r^   r   r   ro   rp   r   r   r   _straight_evaluate_numeric_only  s   
z2_EvaluatorCompiler._straight_evaluate_numeric_onlyc                 C     |  dd |||S )Nc                 S  s   | t ur| |v S d S r   r?   abr   r   r   r7   (      z:_EvaluatorCompiler.visit_in_op_binary_op.<locals>.<lambda>ro   rp   r   r   r   visit_in_op_binary_op&  s   z(_EvaluatorCompiler.visit_in_op_binary_opc                 C  r   )Nc                 S  s   | t ur| |vS d S r   r   r   r   r   r   r7   2  r   z>_EvaluatorCompiler.visit_not_in_op_binary_op.<locals>.<lambda>r   rp   r   r   r   visit_not_in_op_binary_op.  s   z,_EvaluatorCompiler.visit_not_in_op_binary_opc                 C  sX   t |jjjtrt |jjjts"td|j d|jj d|jj | dd |||S )Nz&Cannot evaluate concatenate operator "r{   r|   c                 S  s   | | S r   r   r   r   r   r   r7   F  s    z>_EvaluatorCompiler.visit_concat_op_binary_op.<locals>.<lambda>)	rI   r]   r,   r}   r   r^   r   r   ro   rp   r   r   r   visit_concat_op_binary_op8  s"   
z,_EvaluatorCompiler.visit_concat_op_binary_opc                 C  r   )Nc                 S  rO   r   )
startswithr   r   r   r   r7   M     
 zB_EvaluatorCompiler.visit_startswith_op_binary_op.<locals>.<lambda>r   rp   r   r   r   visit_startswith_op_binary_opI     z0_EvaluatorCompiler.visit_startswith_op_binary_opc                 C  r   )Nc                 S  rO   r   )endswithr   r   r   r   r7   T  r   z@_EvaluatorCompiler.visit_endswith_op_binary_op.<locals>.<lambda>r   rp   r   r   r   visit_endswith_op_binary_opP  r   z._EvaluatorCompiler.visit_endswith_op_binary_opc                   sD   |  |j |jtju r fdd}|S tdt|j d|j )Nc                   s&    | }|t u r
t S |d u rd S | S r   rd   )r6   rD   Z
eval_innerr   r   rg   [  s   z0_EvaluatorCompiler.visit_unary.<locals>.evaluater*   r\   )r0   r1   rX   r   invr   r,   r   )r   r-   rg   r   r   r   visit_unaryW  s   z_EvaluatorCompiler.visit_unaryc                   s"   |j r|   n|j  fddS )Nc                   s    S r   r   r5   valr   r   r7   n  r8   z4_EvaluatorCompiler.visit_bindparam.<locals>.<lambda>)callablerD   r2   r   r   r   visit_bindparami  s   
z"_EvaluatorCompiler.visit_bindparamr   )(r   r   r   r'   r0   r3   r9   r<   r=   rN   rR   rS   rQ   ra   ri   rj   rn   rq   rw   ry   ro   r~   Zvisit_add_binary_opZvisit_mul_binary_opZvisit_sub_binary_opZvisit_mod_binary_opZvisit_truediv_binary_opZvisit_lt_binary_opZvisit_le_binary_opZvisit_ne_binary_opZvisit_gt_binary_opZvisit_ge_binary_opZvisit_eq_binary_opr   r   r   r   r   r   r   r   r   r   r   r%   =   sL    
6


r%   namestrreturnType[_EvaluatorCompiler]c                 C  s*   | dkrt dd tS tdtd| )NZEvaluatorCompilerzDirect use of 'EvaluatorCompiler' is not supported, and this name will be removed in a future release.  '_EvaluatorCompiler' is for internal use onlyz2.0zmodule z has no attribute )r   r%   AttributeErrorr   )r   r   r   r   __getattr__q  s   r   N)r   r   r   r   )__doc__
__future__r   typingr    r   rL   baser   r   r	   sqlr
   r   Zsql.sqltypesr   r   r   utilr   ZInvalidRequestErrorr   ZColumnOperatorsr   r#   r?   rB   r%   r   r   r   r   r   <module>   s.     6