a
    ~g!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rbd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dddddZeddddZeddddZddddZ	dddddZ
ddd d!d"Zdd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WriteOnlyAttributeImplInstanceState[_T]r   zOptional[WriteOnlyHistory[_T]]None)attrstatepassiveapply_toreturnc                 C  sp   |rH|t j@ r&td| d| d|j| _|j| _|j| _|j| _n$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    OrderedIdentitySet)selfrA   rB   rC   rD    rJ   ]/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-packages/sqlalchemy/orm/writeonly.py__init__U   s    




zWriteOnlyHistory.__init__zList[_T]rE   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 r0| j| j}| j| j}| j|}n| j| j| j  }}}tt|t|t|S rN   )	r=   r:   
differencer9   r;   intersectionr   HistoryrO   )rI   addeddeleted	unchangedrJ   rJ   rK   
as_history|   s    
zWriteOnlyHistory.as_historyzUnion[int, slice]zUnion[List[_T], _T])indexrE   c                 C  s   t | j| S rN   )rO   r:   )rI   r\   rJ   rJ   rK   indexed   s    zWriteOnlyHistory.indexedr6   )valuerE   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 | n| j| d S rN   )r:   remover;   r_   r`   rJ   rJ   rK   add_removed   s    
zWriteOnlyHistory.add_removed)N)__name__
__module____qualname____doc____annotations__rL   propertyrR   rS   r[   r]   ra   rc   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< ddddddd fddZejfdddddddZedZddd!d"d#d$d%d&Zed[ddd'dd#d$d(d&Zed\ddd)dd*d$d+d&Zd,ejfddd)dd*d$d-d&Zejd.d/d0d1Zejd.d/d2d3Zd]dddd4d5d6d7d8d9Zd^dddd4d5d6d7d:d;Zddd<d=d>d?Zd,ejd,ddfdddd4ddddd6d@	dAdBZdddCdDdEdFZddddCdGdHdIZejfddddJddKdLZejfddddMddNdOZ ddd<dPdQdRZ!ejfdddd4dd6dSdTdUZ"ejfdddd4dd6dSdVdWZ#ejfdddd4dd6dSdXdYZ$  Z%S )_r>   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_classz#Union[Type[Any], AliasedClass[Any]]strz"_Dispatch[QueryableAttribute[Any]]z
Mapper[_T]r   )class_keydispatchtarget_mapperrp   kwc                   s8   t  j||d |fi | || _t| _|r4t|| _d S rN   )superrL   rw   WriteOnlyCollectionrr   tuplerp   )rI   rt   ru   rv   rw   rp   rx   	__class__rJ   rK   rL      s
    	zWriteOnlyAttributeImpl.__init__zInstanceState[Any]r*   r   z8Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]])rB   dict_rC   rE   c                 C  s*   |t j@ s| |t jjS | | |S d S rN   )r   rF   _get_collection_historyPASSIVE_NO_INITIALIZEr:   rr   )rI   rB   r~   rC   rJ   rJ   rK   get   s
    
zWriteOnlyAttributeImpl.get.zLiteral[None]z Literal[PassiveFlag.PASSIVE_OFF]r.   )rB   r~   	user_datarC   rE   c                 C  s   d S rN   rJ   rI   rB   r~   r   rC   rJ   rJ   rK   get_collection   s    z%WriteOnlyAttributeImpl.get_collectionr-   c                 C  s   d S rN   rJ   r   rJ   rJ   rK   r      s    z$Optional[_AdaptedCollectionProtocol]zIUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  s   d S rN   rJ   r   rJ   rJ   rK   r      s    	Nc                 C  s4   |t j@ s| ||j}n| ||}|j}t|S rN   )r   rF   r   r:   rR   DynamicCollectionAdapter)rI   rB   r~   r   rC   datahistoryrJ   rJ   rK   r      s
    

zattributes.AttributeEventTokenrM   c                 C  s   t | t jS rN   )r   r+   	OP_APPENDrQ   rJ   rJ   rK   _append_token   s    z$WriteOnlyAttributeImpl._append_tokenc                 C  s   t | t jS rN   )r   r+   	OP_REMOVErQ   rJ   rJ   rK   _remove_token   s    z$WriteOnlyAttributeImpl._remove_tokenzOptional[AttributeEventToken]zOptional[WriteOnlyHistory[Any]]r@   )rB   r~   r^   	initiatorcollection_historyrE   c                 C  sd   |d u r|  ||}|| | jjD ]}||||p8| j}q&| jr`|d ur`| t||d d S NT)	_modified_eventra   rv   appendr   trackparentsethasparentr   instance_staterI   rB   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 ]}||||pZ| j qHd S )NF)	r   rc   r   r   r   r   rv   rb   r   r   rJ   rJ   rK   fire_remove_event  s    
z(WriteOnlyAttributeImpl.fire_remove_eventzWriteOnlyHistory[Any])rB   r~   rE   c                 C  sH   | j |jvr$| | |tj|j| j < ||| t d|| j < |j| j  S r   )ru   committed_staterq   r   PASSIVE_NO_FETCHr   r   )rI   rB   r~   rJ   rJ   rK   r      s    
z&WriteOnlyAttributeImpl._modified_event)	rB   r~   r^   r   rC   	check_oldpop_adaptrE   c	                 C  s   |r|j | j u rd S |r$|d u r$d S |}	t|	}
|jrd| jsNtd|  dt| j|||d}| 	||}|js~|j
}n||j
}||
}t|
|}||}|
D ] }||v r| j|||d |d q|D ]}| j|||d |d qd S )NzCollection "zV" does not support implicit iteration; collection replacement operations can't be usedrC   )r   )parent_tokenrO   has_identityrm   r   rG   r    IdentitySetr   r   r:   rP   rV   rU   r   r   )rI   rB   r~   r^   r   rC   r   r   r   iterable
new_valuesold_collectionr   	constants	additionsremovalsmemberrJ   rJ   rK   set/  sP    


zWriteOnlyAttributeImpl.setr
   )argskwargsrE   c                 O  s
   t  d S rN   NotImplementedError)rI   r   r   rJ   rJ   rK   r!   l  s    zWriteOnlyAttributeImpl.delete)rB   r~   r^   rE   c                 C  s   t dd S )Nz7Dynamic attributes don't support collection population.r   )rI   rB   r~   r^   rJ   rJ   rK   set_committed_valueo  s    z*WriteOnlyAttributeImpl.set_committed_valuerT   c                 C  s   |  ||}| S rN   )r   r[   rI   rB   r~   rC   crJ   rJ   rK   get_historyv  s    z"WriteOnlyAttributeImpl.get_historyz$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>      z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r   rS   r   rJ   rJ   rK   get_all_pending  s    z&WriteOnlyAttributeImpl.get_all_pending)rB   rC   rE   c                 C  sT   | j |jv r|j| j  }n| | |tj}|jrL|tj@ rL| j| |||dS |S d S )N)rD   )ru   r   rq   r   r   r   INIT_OK)rI   rB   rC   r   rJ   rJ   rK   r     s    z.WriteOnlyAttributeImpl._get_collection_history)rB   r~   r^   r   rC   rE   c                 C  s   || ur|  |||| d S rN   )r   rI   rB   r~   r^   r   rC   rJ   rJ   rK   r     s    zWriteOnlyAttributeImpl.appendc                 C  s   || ur|  |||| d S rN   )r   r   rJ   rJ   rK   rb     s    zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d d S )Nr   )rb   r   rJ   rJ   rK   r     s    zWriteOnlyAttributeImpl.pop)..)..)..)N)N)&rd   re   rf   rj   rh   rk   rl   rm   rn   ro   rp   r8   rq   rL   r   PASSIVE_OFFr   r   r   r    memoized_propertyr   r   r   r   r   r   r!   r   r   r   r   r   r   r   rb   r   __classcell__rJ   rJ   r|   rK   r>      sn   
         =	r>   
write_only)lazyc                   @  s    e Zd ZeZdddddZdS )WriteOnlyLoaderzMapper[Any]r@   )mapperrE   c              	   C  s\   d| _ | jr | jjtjtjfvr0t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.)	useobject
impl_classrw   rp   rr   )is_class_leveluselistparent_property	directionr   	ONETOMANY
MANYTOMANYr   rG   r   Z_register_attributer   r   rp   rr   )rI   r   rJ   rJ   rK   init_class_attribute  s&    z$WriteOnlyLoader.init_class_attributeN)rd   re   rf   r>   r   r   rJ   rJ   rJ   rK   r     s   r   c                   @  s`   e Zd ZU dZded< ddddZddd	d
ZddddZddddZddddZ	dS )r   z9simplified CollectionAdapter for internal API consistencyzCollection[Any]r   r   c                 C  s
   || _ d S rN   r   )rI   r   rJ   rJ   rK   rL     s    z!DynamicCollectionAdapter.__init__zIterator[Any]rM   c                 C  s
   t | jS rN   )iterr   rQ   rJ   rJ   rK   __iter__  s    z!DynamicCollectionAdapter.__iter__r@   c                 C  s   d S rN   rJ   rQ   rJ   rJ   rK   _reset_empty  s    z%DynamicCollectionAdapter._reset_emptyintc                 C  s
   t | jS rN   )lenr   rQ   rJ   rJ   rK   __len__  s    z DynamicCollectionAdapter.__len__r<   c                 C  s   dS r   rJ   rQ   rJ   rJ   rK   __bool__  s    z!DynamicCollectionAdapter.__bool__N)
rd   re   rf   rg   rh   rL   r   r   r   r   rJ   rJ   rJ   rK   r     s   
r   c                   @  sZ   e Zd ZU dZesdZded< ded< ddd	d
dZdddddZdd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?   )rA   rB   c                 C  s   |  }tr|sJ || _|| _t|}|j| jj }|jd urT|j	 |jf| _
nd| _
|j|ddf| _| jjr| jj| _nd| _d S )NrJ   F)alias_secondary)objr   r   rA   r   _propsru   	secondaryr   __clause_element__r   _with_parent_where_criteriarp   _order_by_clauses)rI   rA   rB   r   r   proprJ   rJ   rK   rL     s    

z!AbstractCollectionWriter.__init__Iterable[_T]r@   iteratorrE   c                 C  s0   |D ]&}| j t| jt| j|d  qd S rN   )rA   r   r   r   r   instance_dict)rI   r   itemrJ   rJ   rK   _add_all_impl  s    

z&AbstractCollectionWriter._add_all_implr   rE   c                 C  s&   | j t| jt| j|d  d S rN   )rA   rb   r   r   r   r   rI   r   rJ   rJ   rK   _remove_impl"  s    

z%AbstractCollectionWriter._remove_implN)
rd   re   rf   rg   r   	__slots__rh   rL   r   r   rJ   rJ   rJ   rK   r     s   
!	r   c                   @  s   e Zd ZdZdZddddZdddd	Zd
dddZddddZddddZ	dddddZ
dddddZdddddZdS )rz   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   rA   r   r   r   r
   rM   c                 C  s   t dd S )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__zSelect[Tuple[_T]]c                 C  s<   t | jjj| j }| jr&|j| j }| jr8|j| j }|S )zProduce a :class:`_sql.Select` construct that represents the
        rows within this instance-local :class:`_orm.WriteOnlyCollection`.

        )	r#   rA   rw   wherer   r   select_fromr   rp   )rI   stmtrJ   rJ   rK   r#   K  s    zWriteOnlyCollection.selectr&   c                 C  s   t | j}|j}|j| jj }|jtjur4t	
di }|jD ],\}}||||j|}td|d||j< q>t| jjjf 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)	callable_)r   r   r   r   rA   ru   r   r   r   r   rG   synchronize_pairs_get_attr_w_warn_on_nonedictr   r"   rw   values)rI   rB   r   r   r~   lrr   rJ   rJ   rK   r"   W  s"    
zWriteOnlyCollection.insertr'   c                 C  s   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$   rA   rw   r   r   rQ   rJ   rJ   rK   r$     s    zWriteOnlyCollection.updater%   c                 C  s   t | jjj| j S )zProduce a :class:`_dml.Delete` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r!   rA   rw   r   r   rQ   rJ   rJ   rK   r!     s    zWriteOnlyCollection.deleter   r@   r   c                 C  s   |  | 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  s    zWriteOnlyCollection.add_allr6   r   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  s   |  | dS )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   rb     s    zWriteOnlyCollection.removeN)rd   re   rf   rg   r   r   r#   r"   r$   r!   r   r_   rb   rJ   rJ   rJ   rK   rz   +  s   (		rz   N)Org   
__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)   _typingr*   r+   r,   collectionsr-   r.   r   r/   r0   rB   r1   r2   eventr3   Zsql.selectabler4   r5   r6   r8   HasCollectionAdapterAttributeImplr>   class_loggerRelationshipPropertystrategy_forZAbstractRelationshipLoader
Identifiedr   r   r   rz   rJ   rJ   rJ   rK   <module>   s   I
  '?