o
    )f.                     @  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 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)Integer)Numeric)warn_deprecatedc                   @  s   e Zd ZdS )UnevaluatableErrorN)__name__
__module____qualname__ r   r   U/var/www/html/flask-app/venv/lib/python3.12/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   1   r   z_ExpiredObject.operatec                 O  r#   r   r   r   r   r   r   r   4   r   z_ExpiredObject.reverse_operateNr    r   r   r   r   r"   0   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
   getattr__visit_name__r   typer   )r   clauseclausesmethr   r   r   process@   s   z_EvaluatorCompiler.processc                 C  s   |  |jS r   )r0   elementr   r-   r   r   r   visit_groupingK   s   z!_EvaluatorCompiler.visit_groupingc                 C     dd S )Nc                 S  r   r   r   objr   r   r   <lambda>O       z/_EvaluatorCompiler.visit_null.<locals>.<lambda>r   r2   r   r   r   
visit_nullN      z_EvaluatorCompiler.visit_nullc                 C  r4   )Nc                 S     dS )NFr   r5   r   r   r   r7   R   r8   z0_EvaluatorCompiler.visit_false.<locals>.<lambda>r   r2   r   r   r   visit_falseQ   r:   z_EvaluatorCompiler.visit_falsec                 C  r4   )Nc                 S  r;   )NTr   r5   r   r   r   r7   U   r8   z/_EvaluatorCompiler.visit_true.<locals>.<lambda>r   r2   r   r   r   
visit_trueT   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)passive)	
_NO_OBJECTr	   dictgetr   PASSIVE_NO_FETCHr   PASSIVE_NO_RESULT_EXPIRED_OBJECT)r6   statedict_valueimplr   r   get_corresponding_attr~   s   
z?_EvaluatorCompiler.visit_column.<locals>.get_corresponding_attr)_annotationsKeyErrorr   r%   
issubclassclass__check_configure_columntopropertykeyorm_excUnmappedColumnErrorclass_managerrJ   )r   r-   r>   kerR   errrK   r   rI   r   visit_columnW   s@   z_EvaluatorCompiler.visit_columnc                 C  
   |  |S r   visit_clauselistr2   r   r   r   visit_tuple   r'   z_EvaluatorCompiler.visit_tuplec                 C  rY   r   rZ   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(   __clauselist_opz)Cannot evaluate clauselist with operator )r.   operatorr   rstripr*   r   )r   r-   
evaluatorsdispatchr/   r   r_   r   r[      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(   ra   
_binary_opr)    with operator )	r0   leftrightrc   r   rd   r*   r   r,   )r   r-   	eval_left
eval_rightrf   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rE   )r6   has_nullsub_evaluaterH   re   r   r   evaluate   s   z;_EvaluatorCompiler.visit_or_clauselist_op.<locals>.evaluater   r   rc   re   r-   rt   r   rs   r   visit_or_clauselist_op   s   z)_EvaluatorCompiler.visit_or_clauselist_opc                   rn   )Nc                   sF    D ]}|| }|t u rt   S |s |d u s|tu r d S  dS qdS ro   )rE   r@   )r6   rr   rH   rs   r   r   rt      s   z<_EvaluatorCompiler.visit_and_clauselist_op.<locals>.evaluater   ru   r   rs   r   visit_and_clauselist_op   s   z*_EvaluatorCompiler.visit_and_clauselist_opc                   rn   )Nc                   sN   g } D ]}|| }|t u rt   S |d u s|tu r d S || qt|S r   )rE   r@   appendtuple)r6   valuesrr   rH   rs   r   r   rt      s   zA_EvaluatorCompiler.visit_comma_op_clauselist_op.<locals>.evaluater   ru   r   rs   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`.)python_impl_straight_evaluater   opstringr   rc   rk   rl   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   rp   r6   left_val	right_valrk   rl   r   r   rt      
   z7_EvaluatorCompiler.visit_is_binary_op.<locals>.evaluater   r   rc   rk   rl   r-   rt   r   r   r   visit_is_binary_op      z%_EvaluatorCompiler.visit_is_binary_opc                   r   )Nc                   s,    | }| }|t u s|t u rt S ||kS r   rp   r   r   r   r   rt      r   z;_EvaluatorCompiler.visit_is_not_binary_op.<locals>.evaluater   r   r   r   r   visit_is_not_binary_op   r   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   rp   r   rk   rl   rc   r   r   rt      s   z7_EvaluatorCompiler._straight_evaluate.<locals>.evaluater   r   r   r   r   r}      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 "z" for datatypes z, )	ri   r,   _type_affinityr   r   rj   r   r   r}   r   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>r}   r   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   1  r   z>_EvaluatorCompiler.visit_not_in_op_binary_op.<locals>.<lambda>r   r   r   r   r   visit_not_in_op_binary_op-  s   z,_EvaluatorCompiler.visit_not_in_op_binary_opc                 C  r   )Nc                 S  s   | | S r   r   r   r   r   r   r7   ;  s    z>_EvaluatorCompiler.visit_concat_op_binary_op.<locals>.<lambda>r   r   r   r   r   visit_concat_op_binary_op7     z,_EvaluatorCompiler.visit_concat_op_binary_opc                 C  r   )Nc                 S  rY   r   )
startswithr   r   r   r   r7   B     
 zB_EvaluatorCompiler.visit_startswith_op_binary_op.<locals>.<lambda>r   r   r   r   r   visit_startswith_op_binary_op>  r   z0_EvaluatorCompiler.visit_startswith_op_binary_opc                 C  r   )Nc                 S  rY   r   )endswithr   r   r   r   r7   I  r   z@_EvaluatorCompiler.visit_endswith_op_binary_op.<locals>.<lambda>r   r   r   r   r   visit_endswith_op_binary_opE  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   rp   )r6   rH   
eval_innerr   r   rt   P  s   z0_EvaluatorCompiler.visit_unary.<locals>.evaluater)   rh   )r0   r1   rc   r   invr   r,   r   )r   r-   rt   r   r   r   visit_unaryL  s   z_EvaluatorCompiler.visit_unaryc                   s"   |j r|   n|j  fddS )Nc                   s    S r   r   r5   valr   r   r7   c  r8   z4_EvaluatorCompiler.visit_bindparam.<locals>.<lambda>)callablerH   r2   r   r   r   visit_bindparam^  s   
z"_EvaluatorCompiler.visit_bindparamr   )(r   r   r   r&   r0   r3   r9   r<   r=   rX   r\   r]   r[   rm   rv   rw   r{   r   r   r   r}   r   visit_add_binary_opvisit_mul_binary_opvisit_sub_binary_opvisit_mod_binary_opvisit_truediv_binary_opvisit_lt_binary_opvisit_le_binary_opvisit_ne_binary_opvisit_gt_binary_opvisit_ge_binary_op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| )N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__f  s   r   N)r   r   r   r   )__doc__
__future__r   typingr    r   rS   baser   r   r	   sqlr
   r   sql.sqltypesr   r   utilr   InvalidRequestErrorr   ColumnOperatorsr   r"   r@   rE   r$   r   r   r   r   r   <module>   s,     ,