
    +h]-                         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	  e	j                         Z G d d	e      Z G d
 de      Z G d de      Z G d de      Zi Z	 ddZddZd Zd Zd Zy)zThe :class:`.Annotated` class and related routines; creates hash-equivalent
copies of SQL constructs which contain context-specific markers and
associations.

   )	operators)HasCacheKey)anon_map)InternalTraversal   )utilc                   :    e Zd ZeZej                  d        Zd Zy)SupportsAnnotationsc                 8    t               }| j                  |      S N)r   _gen_annotations_cache_key)self	anon_map_s     L/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/sql/annotation.py_annotations_cache_keyz*SupportsAnnotations._annotations_cache_key   s    J	..y99    c           
          dt        fdt        | j                        D cg c]  }|| j                  |   f c}D              fS c c}w )N_annotationsc              3   p   K   | ]-  \  }}|t        |t              r|j                  g       n|f / y wr   )
isinstancer   _gen_cache_key).0keyvaluer   s      r   	<genexpr>zASupportsAnnotations._gen_annotations_cache_key.<locals>.<genexpr>#   sC       C !%5 ((26	s   36)tuplesortedr   )r   r   r   s    ` r   r   z.SupportsAnnotations._gen_annotations_cache_key    sZ       &d&7&78# $++C01# 
 	
#s   AN)	__name__
__module____qualname__EMPTY_ANNOTATIONSr   r   memoized_propertyr   r    r   r   r
   r
      s$    $L	: :

r   r
   c                   >    e Zd Zdej                  fgZd Zd ZddZy)SupportsCloneAnnotationsr   c                     | j                         }|j                  j                  |      |_        |j                  j	                  dd       |j                  j	                  dd       |S )greturn a copy of this ClauseElement with annotations
        updated by the given dictionary.

        r   N_generate_cache_key)_cloner   union__dict__popr   valuesnews      r   	_annotatez"SupportsCloneAnnotations._annotate8   sW    
 kkm++11&9148.5
r   c                     | j                         }t        j                  |      |_        |j                  j                  dd       |j                  j                  dd       |S )hreturn a copy of this ClauseElement with annotations
        replaced by the given dictionary.

        r   Nr(   )r)   r   immutabledictr   r+   r,   r-   s      r   _with_annotationsz*SupportsCloneAnnotations._with_annotationsC   sQ    
 kkm--f5148.5
r   Nc                     |s| j                   rG| j                         }t        j                         |_         |j                  j                  dd       |S | S )return a copy of this :class:`_expression.ClauseElement`
        with annotations
        removed.

        :param values: optional tuple of individual values
         to remove.

        r   N)r   r)   r   r3   r+   r,   )r   r.   cloner/   s       r   _deannotatez$SupportsCloneAnnotations._deannotateN   sL     D%% ++-C#113CLL5t<JKr   NF)	r   r   r    r   dp_annotations_key%_clone_annotations_traverse_internalsr0   r4   r8   r#   r   r   r%   r%   2   s*     
*==>-)		r   r%   c                        e Zd Zd Zd ZddZy)SupportsWrappingAnnotationsc                     t        | |      S )r'   	Annotatedr   r.   s     r   r0   z%SupportsWrappingAnnotations._annotatec       
 v&&r   c                     t        | |      S )r2   r?   rA   s     r   r4   z-SupportsWrappingAnnotations._with_annotationsj   rB   r   Nc                 .    |r| j                         }|S | S )r6   )r)   )r   r.   r7   ss       r   r8   z'SupportsWrappingAnnotations._deannotateq   s     AHKr   r9   )r   r   r    r0   r4   r8   r#   r   r   r=   r=   b   s    ''r   r=   c                   r    e Zd ZdZdZd Zd Zd Zd ZddZ	d	 Z
ed
        Zd Zd Zd Zd Zed        Zy)r@   aI  clones a SupportsAnnotated and applies an 'annotations' dictionary.

    Unlike regular clones, this clone also mimics __hash__() and
    __cmp__() of the original element so that it takes its place
    in hashed collections.

    A reference to the original element is maintained, for the important
    reason of keeping its hash value current.  When GC'ed, the
    hash value may be reused, causing conflicts.

    .. note::  The rationale for Annotated producing a brand new class,
       rather than placing the functionality directly within ClauseElement,
       is **performance**.  The __hash__() method is absent on plain
       ClauseElement which leads to significantly reduced function call
       overhead, as the use of sets and dictionaries against ClauseElement
       objects is prevalent, but most are not "annotated".

    Fc                     |st         j                  |       S |\  }}	 t        |j                     } t         j                  |       S # t        $ r t        |j                  |       } Y 6w xY wr   )object__new__annotated_classes	__class__KeyError_new_annotation_type)clsargselementr.   s       r   rI   zAnnotated.__new__   si    >>#&&"OGVC'(9(9: >>#&&  C*7+<+<cBCs   A A('A(c                    |j                   j                         | _         | j                   j                  dd        | j                   j                  dd        || _        t	        j
                  |      | _        t        |      | _        y Nr   r(   )	r+   copyr,   _Annotated__elementr   r3   r   hash_hash)r   rP   r.   s      r   __init__zAnnotated.__init__   sg    ((--/2D9/6  ..v6']
r   c                 Z    | j                   j                  |      }| j                  |      S r   )r   r*   r4   )r   r.   _valuess      r   r0   zAnnotated._annotate   s)    ##))&1%%g..r   c                    | j                   j                  | j                         }| j                  j                         |_        |j                  j	                  dd        |j                  j	                  dd        ||_        |S rR   )rK   rI   r+   rS   r,   r   )r   r.   r7   s      r   r4   zAnnotated._with_annotations   se    &&t~~6++-3T:0$7#r   Nc           
          || j                   S | j                  t        j                  | j                  j                         D ci c]  \  }}||vr|| c}}            S c c}}w r   )rT   r4   r   r3   r   items)r   r.   r7   r   r   s        r   r8   zAnnotated._deannotate   sn    >>>!))"" +/*;*;*A*A*C&Cf, U
 s   A#c                 R     | j                   j                  j                  | |fi |S r   )rT   rK   _compiler_dispatch)r   visitorkws      r   r^   zAnnotated._compiler_dispatch   s&    :t~~''::4OBOOr   c                 .    | j                   j                  S r   )rT   _constructorr   s    r   rb   zAnnotated._constructor   s    ~~***r   c                      | j                   j                  di |}|| j                   u r| S |j                  j                  | j                         | j	                  || j
                        S )Nr#   )rT   r)   r+   updaterK   r   )r   r`   r7   s      r   r)   zAnnotated._clone   s[    %%%++DNN"K NN!!$--0>>%):):;;r   c                 J    | j                   | j                  | j                  ffS r   )rK   rT   r   rc   s    r   
__reduce__zAnnotated.__reduce__   s     ~~0A0ABBBr   c                     | j                   S r   )rV   rc   s    r   __hash__zAnnotated.__hash__   s    zzr   c                     | j                   r&| j                  j                  j                  | |      S t	        |      t	        |       k(  S r   )_is_column_operatorsrT   rK   __eq__rU   )r   others     r   rl   zAnnotated.__eq__   s;    $$>>++224??;$t*,,r   c                 |    d| j                   v r| j                   d   j                  S | j                  j                  S )Nentity_namespace)r   ro   rT   rc   s    r   ro   zAnnotated.entity_namespace   s8    !2!22$$%78III>>222r   )NT)r   r   r    __doc__rk   rI   rW   r0   r4   r8   r^   propertyrb   r)   rg   ri   rl   ro   r#   r   r   r@   r@      sk    & !'#/P + +	<C- 3 3r   r@   Nc                 >    i fd|  |       } d| S )zDeep copy the given ClauseElement, annotating each element
    with the given annotations dictionary.

    Elements within the exclude collection will be cloned but not annotated.

    c                    |d<   t        |       }|v r|   S r<t        | d      r0| j                  j                        r | j                  ddi|}nU| j
                  k7  rDr0| j                  r$ | j                  ddi|j                        }n| j                        }n| }|j                         ||<   |S )Ndetect_subquery_cols	proxy_setr7   r7   r#   )	idhasattrru   intersectionr)   r   _is_immutabler0   _copy_internals)	elemr`   id_newelemannotationsr7   
cloned_idsrt   excludes	       r   r7   z_deep_annotate.<locals>.clone  s    %9!"h*c?" k*++G4!dkk444GD---#(:(:%$++8E8R8BB;O..5Ge,!
3r   Nr#   )rP   r   r   rt   r7   r   s    ```@@r   _deep_annotater      s/     J 0 .ENr   c                 6    i fd|  |       } d| S )z2Deep copy the given element, removing annotations.c                     rt        |       }n| }|vr,| j                  d      }|j                         ||<   |S |   S )NT)r.   r7   rv   )rw   r8   r{   )r|   r`   r   r~   r7   clonedr.   s       r   r7   z_deep_deannotate.<locals>.clone'  sZ    T(CCf&&fD&AG##%#0!F3KN#;r   Nr#   )rP   r.   r7   r   s    `@@r   _deep_deannotater   "  s*     F .ENr   c                 H    | j                  |      } | j                          | S )a  Annotate the given ClauseElement and copy its internals so that
    internal objects refer to the new annotated object.

    Basically used to apply a "don't traverse" annotation to a
    selectable, without digging throughout the whole
    structure wasting time.
    )r0   r{   )rP   r   s     r   _shallow_annotater   ;  s%     ,GNr   c                    t        | t              r| S | t        v r	t        |    S | j                  D ]  }|t        v st        |   } n t	        d| j
                  z  || fi       xt        | <   }|t               d| j
                  z  <   d| j                  v r/t        | j                        dt        j                  fgz   |_	        nJ| j                  j                  dd      r.t        | j                        dt        j                  fgz   |_	        | j                  j                  dd      rd|_        t        | t        j                        |_        |S )NzAnnotated%s_traverse_internalsr   inherit_cacheFT)
issubclassr@   rJ   __mro__typer   globalsr+   listr   r   r:   getr   r   ColumnOperatorsrk   )rN   base_clssuper_anno_clss       r   rM   rM   H  sH   #y!
	!	! %%++  &&(0H )-$xor) cX /7GImcll*+,'+C,C,C'D.AABH
 (
$ 
		/5	1'+C,C,C'D.AABH
 (
$ ||/!%$.sI4M4M$NH!Or   c                 P    t        j                  |       D ]  }t        ||        y r   )r   walk_subclassesrM   )target_hierarchyr   rN   s      r   _prepare_annotationsr   n  s'    ##$45 ,S(+,r   r9   r   )rp    r   baser   
traversalsr   visitorsr   r   r3   r!   rH   r
   r%   r=   r@   rJ   r   r   r   rM   r   r#   r   r   <module>r      s        ' &D&&( 
& 
6-2 -`"5 >i3 i3b   >C*Z2
#L,r   