
    D6iV                         d dl mZ d dlZd dlmZ d dl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  G d dej.                        Z G d de      Zd Zy)    )IterableN)hybrid_property)
attributesclass_mapperColumnProperty)MapperPropertyPropComparator)_state_session)set_creation_order   )ImproperlyConfigured)identity)_get_class_registryc                   j     e Zd Z fdZej
                  fdZd Zd Zej
                  ddfdZ	 xZ
S )GenericAttributeImplc                     t        t        t        t        j                  j                  d                  }|dk\  rg |dd d|dd }t        |   |i | y)a  
        The constructor of attributes.AttributeImpl changed in SQLAlchemy 2.0.22,
        adding a 'default_function' required positional argument before 'dispatch'.
        This adjustment ensures compatibility across versions by inserting None for
        'default_function' in versions >= 2.0.22.

        Arguments received: (class, key, dispatch)
        Required by AttributeImpl: (class, key, default_function, dispatch)
        Setting None as default_function here.
        .)   r      Nr   )tuplemapintsa__version__splitsuper__init__)selfargskwargssqlalchemy_version	__class__s       [/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/sqlalchemy_utils/generic.pyr   zGenericAttributeImpl.__init__   s`     #3sBNN,@,@,E#FG+/T"1X/t/d12h/D$)&)    c                    | j                   |v r|| j                      S t        |      }|y | j                  |      }t        |j                        j                  |      }|y | j                  |      }|j                  ||      }|S N)keyr
   get_state_discriminatorr   class_getget_state_id)	r   statedict_passivesessiondiscriminatortarget_classidtargets	            r#   r*   zGenericAttributeImpl.get"   s    88u?" !'? 44U;*5<<8<<]Ku%\2. r$   c                     | j                   j                  }t        |t              r$t	        |j                         |j                        S |j                  |j                     j                  S r&   )
parent_tokenr0   
isinstancer   getattrobj__name__attrsr'   value)r   r,   r0   s      r#   r(   z,GenericAttributeImpl.get_state_discriminator=   sQ    ))77m_5599;(>(>??;;}001777r$   c                 T    t        fd| j                  j                  D              S )Nc              3   d   K   | ]'  }j                   |j                     j                   ) y wr&   )r:   r'   r;   ).0r2   r,   s     r#   	<genexpr>z4GenericAttributeImpl.get_state_id.<locals>.<genexpr>F   s$     N2U[[(..Ns   -0)r   r5   r2   )r   r,   s    `r#   r+   z!GenericAttributeImpl.get_state_idD   s     N9J9J9M9MNNNr$   NFc                    ||| j                   <   |N| j                  j                  D ]  }d ||j                   <    d || j                  j                  j                   <   y t	        |      }t        |      }	|	j                  |      d   }
|j                  }t        | j                  j                        D ]  \  }}|
|   ||j                   <    ||| j                  j                  j                   <   y )Nr   )	r'   r5   r2   r0   typer   identity_key_from_instancer9   	enumerate)r   r,   r-   	initiatorr.   	check_oldpopr2   r)   mapperpkr0   indexs                r#   setzGenericAttributeImpl.setH   s     $dhh''** % $bff%9=E$##11556 )_F!&)F229=a@B #OOM&t'8'8';';< *	r "5	bff*9FE$##11556r$   )r9   
__module____qualname__r   r   PASSIVE_OFFr*   r(   r+   rJ   __classcell__r"   s   @r#   r   r      s:    *$ )3(>(> 68O &&Gr$   r   c                   L     e Zd ZdZd fd	Zd Zd Z G d de      Zd Z	 xZ
S )	GenericRelationshipPropertyaS  A generic form of the relationship property.

    Creates a 1 to many relationship between the parent model
    and any other models using a discriminator (the table name).

    :param discriminator:
        Field to discriminate which model we are referring to.
    :param id:
        Field to point to the model we are referring to.
    c                 ~    t         |           || _        || _        d | _        d | _        || _        t        |        y r&   )r   r   _discriminator_col_id_cols_id_discriminatordocr   )r   r0   r2   rW   r"   s       r#   r   z$GenericRelationshipProperty.__init__u   s;    "/"4 r$   c                 p   t        |t              rB|j                  }| j                  j                  j                         D ]  \  }}||k(  s|c S  y | j                  j                  j                         D ]=  }t        |t              s|j                  d   j                  |j                  k(  s;|c S  y )Nr   )r6   r   r9   parentall_orm_descriptorsitemsr:   valuesr   columnsname)r   columnattr_keyr'   attrs        r#   _column_to_propertyz/GenericRelationshipProperty._column_to_property   s    fo.H![[<<BBD  	T(?K  ))002 $dN3||A++v{{:#$r$   c                      fd} | j                          _          | j                         _        t         j                  t              r%t	        t        | j                               _        n j                  g _         j                   j                          _         j                  t        d      t	        t         j                   j                               _	        y )Nc                 Z    t        | t              rj                  j                  |    S | S r&   )r6   strrY   r]   )r_   r   s    r#   convert_stringsz9GenericRelationshipProperty.init.<locals>.convert_strings   s'    &#&{{**622Mr$   z(Could not find discriminator descriptor.)
rS   rT   r6   r   listr   rb   r0   r   r2   )r   rf   s   ` r#   initz GenericRelationshipProperty.init   s    	
 #2$2I2I"J'6dmmX. _dmm!DEDM!]]ODM!55d6M6MN%&'QRRs433T]]CDr$   c                   $    e Zd Zd Zd Zd Zd Zy)&GenericRelationshipProperty.Comparatorc                      || _         || _        y r&   )property_parententity)r   propparentmappers      r#   r   z/GenericRelationshipProperty.Comparator.__init__   s     DM!-Dr$   c                     t        |      j                  }| j                  j                  |k(  }t	        |      }t        | j                  j                        D ]  \  }}||||   k(  z  } |S r&   )rA   r9   rl   rS   r   rC   rT   )r   otherr0   qother_idrI   r2   s          r#   __eq__z-GenericRelationshipProperty.Comparator.__eq__   sh     K00M00MAAH&t}}'='=> +	rR8E?**+Hr$   c                     | |k(   S r&    )r   rq   s     r#   __ne__z-GenericRelationshipProperty.Comparator.__ne__   s    U]##r$   c                    t        j                  |      }|j                  g}|j                  |j                  D cg c]  }|j
                  j                   c}       | j                  j                  j                  |      S c c}w r&   )	r   inspectr9   extend_inheriting_mappersr)   rl   rS   in_)r   rq   rG   class_names	submappers        r#   is_typez.GenericRelationshipProperty.Comparator.is_type   so    ZZ&F !>>*K<B<V<VWy!!**W ==3377DD Xs   BN)r9   rK   rL   r   rt   rw   r   rv   r$   r#   
Comparatorrj      s    	.		$		Er$   r   c           	          t        j                  |j                  | j                  | j	                  | |      || j
                  t        |        y )N)
comparatorparententityrW   
impl_classr5   )r   register_attributer)   r'   r   rW   r   )r   rG   s     r#   instrument_classz,GenericRelationshipProperty.instrument_class   s<    %%MMHHtV4+	
r$   r&   )r9   rK   rL   __doc__r   rb   rh   r	   r   r   rN   rO   s   @r#   rQ   rQ   i   s+    	!
$E*E^ E6	
r$   rQ   c                      t        | i |S r&   )rQ   )r   r    s     r#   generic_relationshipr      s    &777r$   )collections.abcr   
sqlalchemyr   sqlalchemy.ext.hybridr   sqlalchemy.ormr   r   r   sqlalchemy.orm.interfacesr   r	   sqlalchemy.orm.sessionr
   sqlalchemy.utilr   
exceptionsr   	functionsr   functions.ormr   ScalarAttributeImplr   rQ   r   rv   r$   r#   <module>r      sQ    $  1 C C D 1 . ,  .WG:99 WGt[
. [
|8r$   