o
    !Zh!W                     @  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 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 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- er1dd)lm.Z. dd*l/m0Z0 dd+lm1Z1 dd,lm2Z2 dd-l3m4Z4 dd.l3m5Z5 dd/l6m7Z7 dd0lm8Z8 dd1l9m:Z: dd2l"m;Z; dd3l<m=Z= dd4l>m?Z? dd5l>m@Z@ ed6ed7ZAG d8d9 d9eeA ZBG d:d; d;ejCejDZEe!jFejGjHd<d=G d>d? d?ejIe!jJZKG d@dA dAZLG dBdC dCeeA ZMG dDdE dEeMeA ZNdFS )GzWrite-only collection API.

This is an alternate mapped attribute style that only supports single-item
collection mutation operations.   To read the collection, a select()
object must be executed each time.

.. versionadded:: 2.0


    )annotations)Any)
Collection)Dict)Generic)Iterable)Iterator)List)NoReturn)Optional)overload)Tuple)Type)TYPE_CHECKING)TypeVar)Union)	bindparam   )
attributes)
interfaces)relationships)
strategies)	NEVER_SET)object_mapper)PassiveFlag)RelationshipDirection   )exc)inspect)log)util)delete)insert)select)update)Delete)Insert)Update)Literal)QueryableAttribute)_InstanceDict)AttributeEventToken)LoaderCallableStatus)_AdaptedCollectionProtocol)CollectionAdapter)Mapper)_RelationshipOrderByArg)InstanceState)AliasedClass)	_Dispatch)
FromClause)Select_T)boundc                   @  s   e Zd ZU dZded< ded< ded< ded< 	d(d)ddZed*ddZed*ddZd+ddZ	d,d d!Z
d-d$d%Zd-d&d'ZdS ).WriteOnlyHistoryzDOverrides AttributeHistory to receive append/remove events directly.zutil.OrderedIdentitySetunchanged_itemsadded_itemsdeleted_itemsbool_reconcile_collectionNattrWriteOnlyAttributeImplstateInstanceState[_T]passiver   apply_toOptional[WriteOnlyHistory[_T]]returnNonec                 C  sr   |r%|t j@ rtd| d| d|j| _|j| _|j| _|j| _d S t	 | _t	 | _t	 | _d| _d S )Nz
Attribute z can't load the existing state from the database for this operation; full iteration is not permitted.  If this is a delete operation, configure passive_deletes=True on the z- relationship in order to resolve this error.F)
r   SQL_OKr   InvalidRequestErrorr9   r:   r;   r=   r    ZOrderedIdentitySet)selfr>   r@   rB   rC    rJ   O/var/www/html/lang_env/lib/python3.10/site-packages/sqlalchemy/orm/writeonly.py__init__U   s   




zWriteOnlyHistory.__init__List[_T]c                 C  s   t | j| jS N)listr:   unionr9   rI   rJ   rJ   rK   added_plus_unchangedp   s   z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j| j| jS rN   )rO   r:   rP   r9   r;   rQ   rJ   rJ   rK   	all_itemst   s
   zWriteOnlyHistory.all_itemsattributes.Historyc                 C  s`   | j r| j| j}| j| j}| j|}n| j| j| j}}}tt|t|t|S rN   )	r=   r:   
differencer9   r;   intersectionr   ZHistoryrO   )rI   addedZdeletedZ	unchangedrJ   rJ   rK   
as_history|   s   
zWriteOnlyHistory.as_historyindexUnion[int, slice]Union[List[_T], _T]c                 C  s   t | j| S rN   )rO   r:   )rI   rY   rJ   rJ   rK   indexed   s   zWriteOnlyHistory.indexedvaluer6   c                 C  s   | j | d S rN   )r:   addrI   r]   rJ   rJ   rK   	add_added   s   zWriteOnlyHistory.add_addedc                 C  s*   || j v r| j | d S | j| d S rN   )r:   remover;   r^   r_   rJ   rJ   rK   add_removed   s   
zWriteOnlyHistory.add_removedrN   )
r>   r?   r@   rA   rB   r   rC   rD   rE   rF   )rE   rM   )rE   rT   )rY   rZ   rE   r[   )r]   r6   rE   rF   )__name__
__module____qualname____doc____annotations__rL   propertyrR   rS   rX   r\   r`   rb   rJ   rJ   rJ   rK   r8   M   s    
 


r8   c                      s  e Zd ZU dZded< dZded< dZded< dZded< dZded< dZ	ded	< d
Z
ded< eZded< ded< da fddZejfdbd%d&Ze	'	'dcddd,d-Ze	'	'dcded/d-Ze	'	'dcdfd2d-Zd3ejfdfd4d-Zejdgd6d7Zejdgd8d9Z	3dhdid@dAZ	3dhdidBdCZdjdEdFZd3ejd3ddfdkdJdKZdldOdPZdmdQdRZejfdndTdUZejfdodWdXZ dpdYdZZ!ejfdqd[d\Z"ejfdqd]d^Z#ejfdqd_d`Z$  Z%S )rr?   Tr<   uses_objectsFdefault_accepts_scalar_loadersupports_population_supports_dynamic_iteration
collectiondynamicrJ   r0   order_byzType[WriteOnlyHistory[Any]]collection_history_clszType[WriteOnlyCollection[Any]]query_classclass_#Union[Type[Any], AliasedClass[Any]]keystrdispatch"_Dispatch[QueryableAttribute[Any]]target_mapper
Mapper[_T]kwr   c                   s<   t  j||d |fi | || _t| _|rt|| _d S d S rN   )superrL   rx   WriteOnlyCollectionrq   tuplero   )rI   rr   rt   rv   rx   ro   rz   	__class__rJ   rK   rL      s   	zWriteOnlyAttributeImpl.__init__r@   InstanceState[Any]dict_r*   rB   r   rE   8Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]]c                 C  s&   |t j@ s| |t jjS | | |S rN   )r   rG   _get_collection_historyPASSIVE_NO_INITIALIZEr:   rq   )rI   r@   r   rB   rJ   rJ   rK   get   s   
zWriteOnlyAttributeImpl.get.	user_dataLiteral[None] Literal[PassiveFlag.PASSIVE_OFF]r.   c                 C     d S rN   rJ   rI   r@   r   r   rB   rJ   rJ   rK   get_collection      z%WriteOnlyAttributeImpl.get_collectionr-   c                 C  r   rN   rJ   r   rJ   rJ   rK   r      r   $Optional[_AdaptedCollectionProtocol]IUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  r   rN   rJ   r   rJ   rJ   rK   r      s   	Nc                 C  s:   |t j@ s| ||j}t|S | ||}|j}t|S rN   )r   rG   r   r:   rR   DynamicCollectionAdapter)rI   r@   r   r   rB   datahistoryrJ   rJ   rK   r      s   

attributes.AttributeEventTokenc                 C     t | t jS rN   )r   r+   Z	OP_APPENDrQ   rJ   rJ   rK   _append_token      z$WriteOnlyAttributeImpl._append_tokenc                 C  r   rN   )r   r+   Z	OP_REMOVErQ   rJ   rJ   rK   _remove_token   r   z$WriteOnlyAttributeImpl._remove_tokenr]   	initiatorOptional[AttributeEventToken]collection_historyOptional[WriteOnlyHistory[Any]]rF   c                 C  sl   |d u r
|  ||}|| | jjD ]}||||p| j}q| jr2|d ur4| t||d d S d S d S NT)	_modified_eventr`   rv   appendr   trackparentsethasparentr   instance_staterI   r@   r   r]   r   r   fnrJ   rJ   rK   fire_append_event   s   
z(WriteOnlyAttributeImpl.fire_append_eventc                 C  sd   |d u r
|  ||}|| | jr |d ur | t||d | jjD ]}||||p-| j q$d S )NF)	r   rb   r   r   r   r   rv   ra   r   r   rJ   rJ   rK   fire_remove_event  s   
z(WriteOnlyAttributeImpl.fire_remove_eventWriteOnlyHistory[Any]c                 C  sH   | j |jvr| | |tj|j| j < ||| t d|| j < |j| j  S r   )rt   committed_staterp   r   PASSIVE_NO_FETCHr   r   )rI   r@   r   rJ   rJ   rK   r      s   
z&WriteOnlyAttributeImpl._modified_event	check_oldpop_adaptc	                 C  s   |r
|j | j u r
d S |r|d u rd S |}	t|	}
|jr2| js'td|  dt| j|||d}| 	||}|js?|j
}n||j
}||
}t|
|}||}|
D ]}||v ri| j|||d |d qY|D ]}| j|||d |d qld S )NzCollection "zV" does not support implicit iteration; collection replacement operations can't be usedrB   )r   )Zparent_tokenrO   has_identityrl   r   rH   r    ZIdentitySetr   r   r:   rP   rV   rU   r   r   )rI   r@   r   r]   r   rB   r   r   r   iterable
new_valuesZold_collectionr   	constants	additionsZremovalsmemberrJ   rJ   rK   set/  sT   


zWriteOnlyAttributeImpl.setargskwargsr
   c                 O  s   t  rN   NotImplementedError)rI   r   r   rJ   rJ   rK   r!   l  s   zWriteOnlyAttributeImpl.deletec                 C     t d)Nz7Dynamic attributes don't support collection population.r   )rI   r@   r   r]   rJ   rJ   rK   set_committed_valueo  s   z*WriteOnlyAttributeImpl.set_committed_valuerT   c                 C  s   |  ||}| S rN   )r   rX   rI   r@   r   rB   crJ   rJ   rK   get_historyv  s   z"WriteOnlyAttributeImpl.get_history$List[Tuple[InstanceState[Any], Any]]c                 C  s   |  ||}dd |jD S )Nc                 S  s   g | ]	}t ||fqS rJ   )r   r   ).0xrJ   rJ   rK   
<listcomp>  s    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r   rS   r   rJ   rJ   rK   get_all_pending  s   z&WriteOnlyAttributeImpl.get_all_pendingc                 C  sP   | j |jv r|j| j  }n| | |tj}|jr&|tj@ r&| j| |||dS |S )N)rC   )rt   r   rp   r   r   r   ZINIT_OK)rI   r@   rB   r   rJ   rJ   rK   r     s   z.WriteOnlyAttributeImpl._get_collection_historyc                 C      || ur|  |||| d S d S rN   )r   rI   r@   r   r]   r   rB   rJ   rJ   rK   r        zWriteOnlyAttributeImpl.appendc                 C  r   rN   )r   r   rJ   rJ   rK   ra     r   zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d d S )Nr   )ra   r   rJ   rJ   rK   r     s   zWriteOnlyAttributeImpl.pop)rr   rs   rt   ru   rv   rw   rx   ry   ro   r0   rz   r   )r@   r   r   r*   rB   r   rE   r   )..)
r@   r   r   r*   r   r   rB   r   rE   r.   )
r@   r   r   r*   r   r-   rB   r   rE   r.   )
r@   r   r   r*   r   r   rB   r   rE   r   )rE   r   rN   )r@   r   r   r*   r]   r   r   r   r   r   rE   rF   )r@   r   r   r*   rE   r   )r@   r   r   r*   r]   r   r   r   rB   r   r   r   r   r<   r   r<   rE   rF   )r   r   r   r   rE   r
   )r@   r   r   r*   r]   r   rE   r
   )r@   r   r   r*   rB   r   rE   rT   )r@   r   r   r*   rB   r   rE   r   )r@   r   rB   r   rE   r   )r@   r   r   r*   r]   r   r   r   rB   r   rE   rF   )&rc   rd   re   ri   rg   rj   rk   rl   rm   rn   ro   r8   rp   rL   r   ZPASSIVE_OFFr   r   r   r    Zmemoized_propertyr   r   r   r   r   r   r!   r   r   r   r   r   r   r   ra   r   __classcell__rJ   rJ   r~   rK   r?      sp   
 

=

	r?   Z
write_only)Zlazyc                   @  s   e Zd ZeZdddZdS )	WriteOnlyLoadermapperMapper[Any]rE   rF   c              	   C  s\   d| _ | jr| jjtjtjfvrtd| j t	j
| j|d| j| jj| jj| jjd d S )NTztOn relationship %s, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.)Z	useobject
impl_classrx   ro   rq   )Zis_class_levelZuselistZparent_property	directionr   	ONETOMANYZ
MANYTOMANYr   rH   r   Z_register_attributer   r   ro   rq   )rI   r   rJ   rJ   rK   init_class_attribute  s&   
z$WriteOnlyLoader.init_class_attributeN)r   r   rE   rF   )rc   rd   re   r?   r   r   rJ   rJ   rJ   rK   r     s    r   c                   @  sL   e Zd ZU dZded< dddZddd	ZdddZdddZdddZ	dS )r   z9simplified CollectionAdapter for internal API consistencyCollection[Any]r   c                 C  s
   || _ d S rN   )r   )rI   r   rJ   rJ   rK   rL        
z!DynamicCollectionAdapter.__init__rE   Iterator[Any]c                 C  
   t | jS rN   )iterr   rQ   rJ   rJ   rK   __iter__  r   z!DynamicCollectionAdapter.__iter__rF   c                 C  r   rN   rJ   rQ   rJ   rJ   rK   _reset_empty     z%DynamicCollectionAdapter._reset_emptyintc                 C  r   rN   )lenr   rQ   rJ   rJ   rK   __len__  r   z DynamicCollectionAdapter.__len__r<   c                 C  s   dS r   rJ   rQ   rJ   rJ   rK   __bool__  r   z!DynamicCollectionAdapter.__bool__N)r   r   )rE   r   )rE   rF   )rE   r   )rE   r<   )
rc   rd   re   rf   rg   rL   r   r   r   r   rJ   rJ   rJ   rK   r     s   
 



r   c                   @  sH   e Zd ZU dZesdZded< ded< dddZdddZdddZ	dS )AbstractCollectionWriterzsVirtual collection which includes append/remove methods that synchronize
    into the attribute event system.

    rJ   r6   instancezTuple[FromClause, ...]	_from_objr>   r?   r@   rA   c                 C  s   |  }tr
|s
J || _|| _t|}|j| jj }|jd ur*|j	 |jf| _
nd| _
|j|ddf| _| jjrA| jj| _d S d| _d S )NrJ   F)Zalias_secondary)objr   r   r>   r   _propsrt   Z	secondaryr   Z__clause_element__r   Z_with_parent_where_criteriaro   _order_by_clauses)rI   r>   r@   r   r   proprJ   rJ   rK   rL     s   


z!AbstractCollectionWriter.__init__iteratorIterable[_T]rE   rF   c                 C  s0   |D ]}| j t| jt| j|d  qd S rN   )r>   r   r   r   r   instance_dict)rI   r   itemrJ   rJ   rK   _add_all_impl  s   

z&AbstractCollectionWriter._add_all_implr   c                 C  s&   | j t| jt| j|d  d S rN   )r>   ra   r   r   r   r   rI   r   rJ   rJ   rK   _remove_impl"  s   

z%AbstractCollectionWriter._remove_implN)r>   r?   r@   rA   r   r   rE   rF   r   r6   rE   rF   )
rc   rd   re   rf   r   	__slots__rg   rL   r   r   rJ   rJ   rJ   rK   r     s   
 

!	r   c                   @  sd   e Zd ZdZdZdddZd dd	Zd!ddZd"ddZd#ddZ	d$ddZ
d%ddZd%ddZdS )&r|   a  Write-only collection which can synchronize changes into the
    attribute event system.

    The :class:`.WriteOnlyCollection` is used in a mapping by
    using the ``"write_only"`` lazy loading strategy with
    :func:`_orm.relationship`.     For background on this configuration,
    see :ref:`write_only_relationship`.

    .. versionadded:: 2.0

    .. seealso::

        :ref:`write_only_relationship`

    )r   r>   r   r   r   rE   r
   c                 C  r   )NzWriteOnly collections don't support iteration in-place; to query for collection items, use the select() method to produce a SQL statement and execute it with session.scalars().)	TypeErrorrQ   rJ   rJ   rK   r   D  s   zWriteOnlyCollection.__iter__Select[Tuple[_T]]c                 C  s<   t | jjj| j }| jr|j| j }| jr|j| j }|S )zProduce a :class:`_sql.Select` construct that represents the
        rows within this instance-local :class:`_orm.WriteOnlyCollection`.

        )	r#   r>   rx   wherer   r   select_fromr   ro   )rI   stmtrJ   rJ   rK   r#   K  s   zWriteOnlyCollection.selectr&   c                 C  s   t | j}|j}|j| jj }|jtjurt	
di }|jD ]\}}||||j|}td|d||j< qt| jjjdi |S )aN  For one-to-many collections, produce a :class:`_dml.Insert` which
        will insert new rows in terms of this this instance-local
        :class:`_orm.WriteOnlyCollection`.

        This construct is only supported for a :class:`_orm.Relationship`
        that does **not** include the :paramref:`_orm.relationship.secondary`
        parameter.  For relationships that refer to a many-to-many table,
        use ordinary bulk insert techniques to produce new objects, then
        use :meth:`_orm.AbstractCollectionWriter.add_all` to associate them
        with the collection.


        zWrite only bulk INSERT only supported for one-to-many collections; for many-to-many, use a separate bulk INSERT along with add_all().N)Z	callable_rJ   )r   r   r   r   r>   rt   r   r   r   r   rH   Zsynchronize_pairsZ_get_attr_w_warn_on_nonedictr   r"   rx   values)rI   r@   r   r   r   lrr   rJ   rJ   rK   r"   W  s"   
zWriteOnlyCollection.insertr'   c                 C     t | jjj| j S )zProduce a :class:`_dml.Update` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r$   r>   rx   r   r   rQ   rJ   rJ   rK   r$        zWriteOnlyCollection.updater%   c                 C  r   )zProduce a :class:`_dml.Delete` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r!   r>   rx   r   r   rQ   rJ   rJ   rK   r!     r   zWriteOnlyCollection.deleter   r   rF   c                 C     |  | dS )zAdd an iterable of items to this :class:`_orm.WriteOnlyCollection`.

        The given items will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        Nr   )rI   r   rJ   rJ   rK   add_all     zWriteOnlyCollection.add_allr   r6   c                 C  s   |  |g dS )zAdd an item to this :class:`_orm.WriteOnlyCollection`.

        The given item will be persisted to the database in terms of
        the parent instance's collection on the next flush.

        Nr   r   rJ   rJ   rK   r^     s   zWriteOnlyCollection.addc                 C  r   )zRemove an item from this :class:`_orm.WriteOnlyCollection`.

        The given item will be removed from the parent instance's collection on
        the next flush.

        N)r   r   rJ   rJ   rK   ra     r   zWriteOnlyCollection.removeN)rE   r
   )rE   r   )rE   r&   )rE   r'   )rE   r%   r   r   )rc   rd   re   rf   r   r   r#   r"   r$   r!   r   r^   ra   rJ   rJ   rJ   rK   r|   +  s    



(


		r|   N)Orf   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Zsqlalchemy.sqlr    r   r   r   r   baser   r   r   r   r   r   r   r    sqlr!   r"   r#   r$   Zsql.dmlr%   r&   r'   Zutil.typingr(   r)   Z_typingr*   r+   r,   collectionsr-   r.   r   r/   r0   r@   r1   r2   eventr3   Zsql.selectabler4   r5   r6   r8   ZHasCollectionAdapterZAttributeImplr?   Zclass_loggerZRelationshipPropertyZstrategy_forZAbstractRelationshipLoaderZ
Identifiedr   r   r   r|   rJ   rJ   rJ   rK   <module>   s   
I  '?