o
    E6dN                     @  sd  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' erd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)l0m1Z1 ed*ed+Z2G d,d- d-Z3G d.d/ d/ej4ej5Z6ej7ej8j9d0d1G d2d3 d3ej:ej;Z<G d4d5 d5Z=G d6d7 d7ee2 Z>G d8d9 d9e>e2 Z?d:S );zWrite-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)Generic)Iterable)NoReturn)Optional)overload)Tuple)TYPE_CHECKING)TypeVar)Union)	bindparam   )
attributes)
interfaces)relationships)
strategies)object_mapper)PassiveFlag)RelationshipDirection   )exc)inspect)log)util)delete)insert)select)update)Delete)Insert)Update)Literal)_InstanceDict)_AdaptedCollectionProtocol)AttributeEventToken)CollectionAdapter)LoaderCallableStatus)InstanceState)Select_T)boundc                   @  sR   e Zd ZdZdddZedd Zedd Zd	d
 Zdd Z	dd Z
dd ZdS )WriteOnlyHistoryzDOverrides AttributeHistory to receive append/remove events directly.Nc                 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   InvalidRequestErrorunchanged_itemsadded_itemsdeleted_items_reconcile_collectionr   ZOrderedIdentitySet)selfattrstatepassiveapply_to r8   oC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\sqlalchemy/orm/writeonly.py__init__G   s   




zWriteOnlyHistory.__init__c                 C  s   t | j| jS N)listr0   unionr/   r3   r8   r8   r9   added_plus_unchanged\   s   z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j| j| jS r;   )r<   r0   r=   r/   r1   r>   r8   r8   r9   	all_items`   s
   zWriteOnlyHistory.all_itemsc                 C  s`   | j r| j| j}| j| j}| j|}n| j| j| j}}}tt|t|t|S r;   )	r2   r0   
differencer/   r1   intersectionr   ZHistoryr<   )r3   addedZdeletedZ	unchangedr8   r8   r9   
as_historyh   s   
zWriteOnlyHistory.as_historyc                 C  s   t | j| S r;   )r<   r0   )r3   indexr8   r8   r9   indexedu   s   zWriteOnlyHistory.indexedc                 C  s   | j | d S r;   )r0   addr3   valuer8   r8   r9   	add_addedx   s   zWriteOnlyHistory.add_addedc                 C  s*   || j v r| j | d S | j| d S r;   )r0   remover1   rG   rH   r8   r8   r9   add_removed{   s   
zWriteOnlyHistory.add_removedr;   )__name__
__module____qualname____doc__r:   propertyr?   r@   rD   rF   rJ   rL   r8   r8   r8   r9   r,   D   s    


r,   c                      sJ  e Zd ZdZdZdZdZdZdZdZ	e
Z fddZejfddZe		dBdCddZe		dBdDddZe		dBdEddZdejfdEddZejdd Zejdd  Z	dFd!d"Z	dFd#d$Zd%d& ZdejdddfdGd0d1Zd2d3 Zd4d5 Zejfd6d7Zejfd8d9Z d:d; Z!ejfd<d=Z"ejfd>d?Z#ejfd@dAZ$  Z%S )HWriteOnlyAttributeImplTFr8   c                   s<   t  j||||fi | || _t| _|rt|| _d S d S r;   )superr:   target_mapperWriteOnlyCollectionquery_classtupleorder_by)r3   class_keyZtypecallabledispatchrT   rX   kw	__class__r8   r9   r:      s   
zWriteOnlyAttributeImpl.__init__c                 C  s&   |t j@ s| |t jjS | | |S r;   )r   r-   _get_collection_historyPASSIVE_NO_INITIALIZEr0   rV   )r3   r5   dict_r6   r8   r8   r9   get   s   
zWriteOnlyAttributeImpl.get.r5   InstanceState[Any]ra   r#   	user_dataLiteral[None]r6    Literal[PassiveFlag.PASSIVE_OFF]returnr&   c                 C     d S r;   r8   r3   r5   ra   rd   r6   r8   r8   r9   get_collection      z%WriteOnlyAttributeImpl.get_collectionr$   r   c                 C  rh   r;   r8   ri   r8   r8   r9   rj      rk   $Optional[_AdaptedCollectionProtocol]IUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  rh   r;   r8   ri   r8   r8   r9   rj      s   
Nc                 C  s:   |t j@ s| ||j}t|S | ||}|j}t|S r;   )r   r-   r_   r0   r?   DynamicCollectionAdapter)r3   r5   ra   rd   r6   datahistoryr8   r8   r9   rj      s   
	c                 C     t | t jS r;   )r   r%   Z	OP_APPENDr>   r8   r8   r9   _append_token      z$WriteOnlyAttributeImpl._append_tokenc                 C  rq   r;   )r   r%   Z	OP_REMOVEr>   r8   r8   r9   _remove_token   rs   z$WriteOnlyAttributeImpl._remove_tokenc                 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_eventrJ   r[   appendrr   trackparentsethasparentr   instance_stater3   r5   ra   rI   	initiatorcollection_historyfnr8   r8   r9   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)	rv   rL   rx   ry   r   rz   r[   rK   rt   r{   r8   r8   r9   fire_remove_event   s   
z(WriteOnlyAttributeImpl.fire_remove_eventc                 C  sJ   | j |jvr| | |tj|j| j < ||| tj d|| j < |j| j  S ru   )rZ   committed_statecollection_history_clsr   PASSIVE_NO_FETCHrv   r   Z	NEVER_SET)r3   r5   ra   r8   r8   r9   rv      s   
z&WriteOnlyAttributeImpl._modified_eventrI   r   r|   Optional[AttributeEventToken]	check_oldpopbool_adaptNonec	                 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r6   )r}   )Zparent_tokenr<   has_identity_supports_dynamic_iterationr   r.   r   ZIdentitySetrb   rv   r0   r=   rB   rA   r   r   )r3   r5   ra   rI   r|   r6   r   r   r   iterable
new_valuesZold_collectionr}   	constants	additionsZremovalsmemberr8   r8   r9   set  sT   


zWriteOnlyAttributeImpl.setc                 O  s   t  r;   NotImplementedError)r3   argskwargsr8   r8   r9   r   D  s   zWriteOnlyAttributeImpl.deletec                 C     t d)Nz7Dynamic attributes don't support collection population.r   )r3   r5   ra   rI   r8   r8   r9   set_committed_valueG     z*WriteOnlyAttributeImpl.set_committed_valuec                 C  s   |  ||}| S r;   )r_   rD   r3   r5   ra   r6   cr8   r8   r9   get_historyL  s   z"WriteOnlyAttributeImpl.get_historyc                 C  s   |  ||}dd |jD S )Nc                 S  s   g | ]	}t ||fqS r8   )r   rz   ).0xr8   r8   r9   
<listcomp>T  s    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)r_   r@   r   r8   r8   r9   get_all_pendingP  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)r7   )rZ   r   r   r   r   r   r   ZINIT_OK)r3   r5   r6   r   r8   r8   r9   r_   V  s   z.WriteOnlyAttributeImpl._get_collection_historyc                 C      || ur|  |||| d S d S r;   )r   r3   r5   ra   rI   r|   r6   r8   r8   r9   rw   e     zWriteOnlyAttributeImpl.appendc                 C  r   r;   )r   r   r8   r8   r9   rK   p  r   zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d d S )Nr   )rK   r   r8   r8   r9   r   {  s   zWriteOnlyAttributeImpl.pop)..)
r5   rc   ra   r#   rd   re   r6   rf   rg   r&   )
r5   rc   ra   r#   rd   r$   r6   r   rg   r&   )
r5   rc   ra   r#   rd   rl   r6   r   rg   rm   r;   )r5   rc   ra   r#   rI   r   r|   r   r6   r   r   r   r   r   r   r   rg   r   )&rM   rN   rO   Zuses_objectsZdefault_accepts_scalar_loaderZsupports_populationr   Z
collectionZdynamicrX   r,   r   r:   r   ZPASSIVE_OFFrb   r   rj   r   r   Zmemoized_propertyrr   rt   r   r   rv   r   r   r   r   r   r`   r   r_   rw   rK   r   __classcell__r8   r8   r]   r9   rR      sj    		



=


rR   Z
write_only)Zlazyc                   @  s   e Zd ZeZdd ZdS )WriteOnlyLoaderc              	   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_classrT   rX   rV   )Zis_class_levelZuselistZparent_property	directionr   	ONETOMANYZ
MANYTOMANYr   r.   r   Z_register_attributer   mapperrX   rV   )r3   r   r8   r8   r9   init_class_attribute  s&   
z$WriteOnlyLoader.init_class_attributeN)rM   rN   rO   rR   r   r   r8   r8   r8   r9   r     s    r   c                   @  s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )rn   z9simplified CollectionAdapter for internal API consistencyc                 C  s
   || _ d S r;   )ro   )r3   ro   r8   r8   r9   r:        
z!DynamicCollectionAdapter.__init__c                 C  
   t | jS r;   )iterro   r>   r8   r8   r9   __iter__  r   z!DynamicCollectionAdapter.__iter__c                 C  rh   r;   r8   r>   r8   r8   r9   _reset_empty     z%DynamicCollectionAdapter._reset_emptyc                 C  r   r;   )lenro   r>   r8   r8   r9   __len__  r   z DynamicCollectionAdapter.__len__c                 C  s   dS ru   r8   r>   r8   r8   r9   __bool__  r   z!DynamicCollectionAdapter.__bool__N)	rM   rN   rO   rP   r:   r   r   r   r   r8   r8   r8   r9   rn     s    rn   c                   @  s4   e Zd ZdZes
dZ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.

    r8   c                 C  s   |   | _}|| _t|}|j| jj }|jd ur$|j |jf| _	nd| _	|j
|ddf| _| jjr;| jj| _d S d| _d S )Nr8   F)Zalias_secondary)objinstancer4   r   _propsrZ   Z	secondaryr   Z__clause_element__	_from_objZ_with_parent_where_criteriarX   _order_by_clauses)r3   r4   r5   r   r   propr8   r8   r9   r:     s   


z!AbstractCollectionWriter.__init__iteratorIterable[_T]rg   r   c                 C  s0   |D ]}| j t| jt| j|d  qd S r;   )r4   rw   r   rz   r   instance_dict)r3   r   itemr8   r8   r9   _add_all_impl  s   

z&AbstractCollectionWriter._add_all_implr   r*   c                 C  s&   | j t| jt| j|d  d S r;   )r4   rK   r   rz   r   r   r3   r   r8   r8   r9   _remove_impl  s   

z%AbstractCollectionWriter._remove_implNr   r   rg   r   r   r*   rg   r   )	rM   rN   rO   rP   r
   	__slots__r:   r   r   r8   r8   r8   r9   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 )&rU   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   r4   r   r   r   rg   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().)	TypeErrorr>   r8   r8   r9   r     r   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   r4   rT   wherer   r   select_fromr   rX   )r3   stmtr8   r8   r9   r     s   zWriteOnlyCollection.select
Insert[_T]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_r8   )r   r   r   r   r4   rZ   r   r   r   r   r.   Zsynchronize_pairsZ_get_attr_w_warn_on_nonedictr   r   rT   values)r3   r5   r   r   ra   lrr~   r8   r8   r9   r     s"   
zWriteOnlyCollection.insert
Update[_T]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   r4   rT   r   r   r>   r8   r8   r9   r   B     zWriteOnlyCollection.update
Delete[_T]c                 C  r   )zProduce a :class:`_dml.Delete` which will refer to rows in terms
        of this instance-local :class:`_orm.WriteOnlyCollection`.

        )r   r4   rT   r   r   r>   r8   r8   r9   r   I  r   zWriteOnlyCollection.deleter   r   r   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   )r3   r   r8   r8   r9   add_allP     zWriteOnlyCollection.add_allr   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   r8   r8   r9   rG   Y  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   r8   r8   r9   rK   b  r   zWriteOnlyCollection.removeN)rg   r   )rg   r   )rg   r   )rg   r   )rg   r   r   r   )rM   rN   rO   rP   r   r   r   r   r   r   r   rG   rK   r8   r8   r8   r9   rU     s    



(


		rU   N)@rP   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   Zsqlalchemy.sqlr    r   r   r   r   baser   r   r   r   r   r   r   sqlr   r   r   r   Zsql.dmlr   r    r!   Zutil.typingr"   Z_typingr#   r$   r%   r&   r'   r5   r(   Zsql.selectabler)   r*   r,   ZHasCollectionAdapterZAttributeImplrR   Zclass_loggerZRelationshipPropertyZstrategy_forZAbstractRelationshipLoaderZ
Identifiedr   rn   r   rU   r8   r8   r8   r9   <module>   sj   	
>  9