o
    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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    OrderedIdentitySet)selfr>   r@   rB   rC    rK   Z/var/www/html/ecg_monitoring/venv/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   rJ   rK   rK   rL   added_plus_unchangedp   s   z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j| j| jS rO   )rP   r:   rQ   r9   r;   rR   rK   rK   rL   	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 rO   )	r=   r:   
differencer9   r;   intersectionr   HistoryrP   )rJ   addeddeleted	unchangedrK   rK   rL   
as_history|   s   
zWriteOnlyHistory.as_historyindexUnion[int, slice]Union[List[_T], _T]c                 C  s   t | j| S rO   )rP   r:   )rJ   r]   rK   rK   rL   indexed   s   zWriteOnlyHistory.indexedvaluer6   c                 C  s   | j | d S rO   )r:   addrJ   ra   rK   rK   rL   	add_added   s   zWriteOnlyHistory.add_addedc                 C  s*   || j v r| j | d S | j| d S rO   )r:   remover;   rb   rc   rK   rK   rL   add_removed   s   
zWriteOnlyHistory.add_removedrO   )
r>   r?   r@   rA   rB   r   rC   rD   rE   rF   )rE   rN   )rE   rU   )r]   r^   rE   r_   )ra   r6   rE   rF   )__name__
__module____qualname____doc____annotations__rM   propertyrS   rT   r\   r`   rd   rf   rK   rK   rK   rL   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dynamicrK   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 rO   )superrM   r|   WriteOnlyCollectionru   tuplers   )rJ   rv   rx   rz   r|   rs   r~   	__class__rK   rL   rM      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 rO   )r   rG   _get_collection_historyPASSIVE_NO_INITIALIZEr:   ru   )rJ   r@   r   rB   rK   rK   rL   get   s   
zWriteOnlyAttributeImpl.get.	user_dataLiteral[None] Literal[PassiveFlag.PASSIVE_OFF]r.   c                 C     d S rO   rK   rJ   r@   r   r   rB   rK   rK   rL   get_collection      z%WriteOnlyAttributeImpl.get_collectionr-   c                 C  r   rO   rK   r   rK   rK   rL   r      r   $Optional[_AdaptedCollectionProtocol]IUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  r   rO   rK   r   rK   rK   rL   r      s   	Nc                 C  s:   |t j@ s| ||j}t|S | ||}|j}t|S rO   )r   rG   r   r:   rS   DynamicCollectionAdapter)rJ   r@   r   r   rB   datahistoryrK   rK   rL   r      s   

attributes.AttributeEventTokenc                 C     t | t jS rO   )r   r+   	OP_APPENDrR   rK   rK   rL   _append_token      z$WriteOnlyAttributeImpl._append_tokenc                 C  r   rO   )r   r+   	OP_REMOVErR   rK   rK   rL   _remove_token   r   z$WriteOnlyAttributeImpl._remove_tokenra   	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_eventrd   rz   appendr   trackparentsethasparentr   instance_staterJ   r@   r   ra   r   r   fnrK   rK   rL   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   rf   r   r   r   r   rz   re   r   r   rK   rK   rL   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   )rx   committed_statert   r   PASSIVE_NO_FETCHr   r   )rJ   r@   r   rK   rK   rL   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   )parent_tokenrP   has_identityrp   r   rH   r    IdentitySetr   r   r:   rQ   rW   rV   r   r   )rJ   r@   r   ra   r   rB   r   r   r   iterable
new_valuesold_collectionr   	constants	additionsremovalsmemberrK   rK   rL   set/  sT   


zWriteOnlyAttributeImpl.setargskwargsr
   c                 O  s   t  rO   NotImplementedError)rJ   r   r   rK   rK   rL   r!   l  s   zWriteOnlyAttributeImpl.deletec                 C     t d)Nz7Dynamic attributes don't support collection population.r   )rJ   r@   r   ra   rK   rK   rL   set_committed_valueo  s   z*WriteOnlyAttributeImpl.set_committed_valuerU   c                 C  s   |  ||}| S rO   )r   r\   rJ   r@   r   rB   crK   rK   rL   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 rK   )r   r   ).0xrK   rK   rL   
<listcomp>  s    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r   rT   r   rK   rK   rL   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   )rx   r   rt   r   r   r   INIT_OK)rJ   r@   rB   r   rK   rK   rL   r     s   z.WriteOnlyAttributeImpl._get_collection_historyc                 C      || ur|  |||| d S d S rO   )r   rJ   r@   r   ra   r   rB   rK   rK   rL   r        zWriteOnlyAttributeImpl.appendc                 C  r   rO   )r   r   rK   rK   rL   re     r   zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d d S )Nr   )re   r   rK   rK   rL   r     s   zWriteOnlyAttributeImpl.pop)rv   rw   rx   ry   rz   r{   r|   r}   rs   r0   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.   )
r@   r   r   r*   r   r   rB   r   rE   r   )rE   r   rO   )r@   r   r   r*   ra   r   r   r   r   r   rE   rF   )r@   r   r   r*   rE   r   )r@   r   r   r*   ra   r   r   r   rB   r   r   r   r   r<   r   r<   rE   rF   )r   r   r   r   rE   r
   )r@   r   r   r*   ra   r   rE   r
   )r@   r   r   r*   rB   r   rE   rU   )r@   r   r   r*   rB   r   rE   r   )r@   r   rB   r   rE   r   )r@   r   r   r*   ra   r   r   r   rB   r   rE   rF   )&rg   rh   ri   rm   rk   rn   ro   rp   rq   rr   rs   r8   rt   rM   r   PASSIVE_OFFr   r   r   r    memoized_propertyr   r   r   r   r   r   r!   r   r   r   r   r   r   r   re   r   __classcell__rK   rK   r   rL   r?      sp   
 

=

	r?   
write_only)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.)	useobject
impl_classr|   rs   ru   )is_class_leveluselistparent_property	directionr   	ONETOMANY
MANYTOMANYr   rH   r   _register_attributer   r   rs   ru   )rJ   r   rK   rK   rL   init_class_attribute  s&   
z$WriteOnlyLoader.init_class_attributeN)r   r   rE   rF   )rg   rh   ri   r?   r   r   rK   rK   rK   rL   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 rO   )r   )rJ   r   rK   rK   rL   rM        
z!DynamicCollectionAdapter.__init__rE   Iterator[Any]c                 C  
   t | jS rO   )iterr   rR   rK   rK   rL   __iter__  r   z!DynamicCollectionAdapter.__iter__rF   c                 C  r   rO   rK   rR   rK   rK   rL   _reset_empty     z%DynamicCollectionAdapter._reset_emptyintc                 C  r   rO   )lenr   rR   rK   rK   rL   __len__  r   z DynamicCollectionAdapter.__len__r<   c                 C  s   dS r   rK   rR   rK   rK   rL   __bool__  r   z!DynamicCollectionAdapter.__bool__N)r   r   )rE   r   )rE   rF   )rE   r   )rE   r<   )
rg   rh   ri   rj   rk   rM   r   r   r   r   rK   rK   rK   rL   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.

    rK   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 )NrK   F)alias_secondary)objr   r   r>   r   _propsrx   	secondaryr   __clause_element__r   _with_parent_where_criteriars   _order_by_clauses)rJ   r>   r@   r   r   proprK   rK   rL   rM     s   


z!AbstractCollectionWriter.__init__iteratorIterable[_T]rE   rF   c                 C  s0   |D ]}| j t| jt| j|d  qd S rO   )r>   r   r   r   r   instance_dict)rJ   r   itemrK   rK   rL   _add_all_impl  s   

z&AbstractCollectionWriter._add_all_implr   c                 C  s&   | j t| jt| j|d  d S rO   )r>   re   r   r   r   r   rJ   r   rK   rK   rL   _remove_impl"  s   

z%AbstractCollectionWriter._remove_implN)r>   r?   r@   rA   r   r   rE   rF   r   r6   rE   rF   )
rg   rh   ri   rj   r   	__slots__rk   rM   r   r   rK   rK   rK   rL   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().)	TypeErrorrR   rK   rK   rL   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>   r|   wherer   r   select_fromr   rs   )rJ   stmtrK   rK   rL   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)	callable_rK   )r   r   r   r   r>   rx   r   r   r   r   rH   synchronize_pairs_get_attr_w_warn_on_nonedictr   r"   r|   values)rJ   r@   r   r   r   lrr   rK   rK   rL   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>   r|   r  r   rR   rK   rK   rL   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>   r|   r  r   rR   rK   rK   rL   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   )rJ   r   rK   rK   rL   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   rK   rK   rL   rb     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   rK   rK   rL   re     r  zWriteOnlyCollection.removeN)rE   r
   )rE   r  )rE   r&   )rE   r'   )rE   r%   r   r  )rg   rh   ri   rj   r  r   r#   r"   r$   r!   r  rb   re   rK   rK   rK   rL   r   +  s    



(


		r   N)Orj   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   sqlalchemy.sqlr    r   r   r   r   baser   r   r   r   r   r   r   r    sqlr!   r"   r#   r$   sql.dmlr%   r&   r'   util.typingr(   r)   _typingr*   r+   r,   collectionsr-   r.   r   r/   r0   r@   r1   r2   eventr3   sql.selectabler4   r5   r6   r8   HasCollectionAdapterAttributeImplr?   class_loggerRelationshipPropertystrategy_forAbstractRelationshipLoader
Identifiedr   r   r   r   rK   rK   rK   rL   <module>   s   
I  '?