o
    iQL                     @  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   OrderedIdentitySet)selfattrstatepassiveapply_to r9   X/var/www/html/LicenciasMIG/venv/lib/python3.10/site-packages/sqlalchemy/orm/writeonly.py__init__G   s   




zWriteOnlyHistory.__init__c                 C  s   t | j| jS N)listr0   unionr/   r4   r9   r9   r:   added_plus_unchanged\   s   z%WriteOnlyHistory.added_plus_unchangedc                 C  s   t | j| j| jS r<   )r=   r0   r>   r/   r1   r?   r9   r9   r:   	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   Historyr=   )r4   addeddeleted	unchangedr9   r9   r:   
as_historyh   s   
zWriteOnlyHistory.as_historyc                 C  s   t | j| S r<   )r=   r0   )r4   indexr9   r9   r:   indexedu   s   zWriteOnlyHistory.indexedc                 C  s   | j | d S r<   )r0   addr4   valuer9   r9   r:   	add_addedx   s   zWriteOnlyHistory.add_addedc                 C  s*   || j v r| j | d S | j| d S r<   )r0   remover1   rK   rL   r9   r9   r:   add_removed{   s   
zWriteOnlyHistory.add_removedr<   )__name__
__module____qualname____doc__r;   propertyr@   rA   rH   rJ   rN   rP   r9   r9   r9   r:   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WriteOnlyAttributeImplTFr9   c                   s<   t  j||||fi | || _t| _|rt|| _d S d S r<   )superr;   target_mapperWriteOnlyCollectionquery_classtupleorder_by)r4   class_keytypecallabledispatchrX   r\   kw	__class__r9   r:   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   rZ   )r4   r6   dict_r7   r9   r9   r:   get   s   
zWriteOnlyAttributeImpl.get.r6   InstanceState[Any]rf   r#   	user_dataLiteral[None]r7    Literal[PassiveFlag.PASSIVE_OFF]returnr&   c                 C     d S r<   r9   r4   r6   rf   ri   r7   r9   r9   r:   get_collection      z%WriteOnlyAttributeImpl.get_collectionr$   r   c                 C  rm   r<   r9   rn   r9   r9   r:   ro      rp   $Optional[_AdaptedCollectionProtocol]IUnion[Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter]c                 C  rm   r<   r9   rn   r9   r9   r:   ro      s   
Nc                 C  s:   |t j@ s| ||j}t|S | ||}|j}t|S r<   )r   r-   rd   r0   r@   DynamicCollectionAdapter)r4   r6   rf   ri   r7   datahistoryr9   r9   r:   ro      s   
	c                 C     t | t jS r<   )r   r%   	OP_APPENDr?   r9   r9   r:   _append_token      z$WriteOnlyAttributeImpl._append_tokenc                 C  rv   r<   )r   r%   	OP_REMOVEr?   r9   r9   r:   _remove_token   ry   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_eventrN   r`   appendrx   trackparentsethasparentr   instance_stater4   r6   rf   rM   	initiatorcollection_historyfnr9   r9   r:   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}   rP   r   r   r   r   r`   rO   r{   r   r9   r9   r:   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 r|   )r^   committed_statecollection_history_clsr   PASSIVE_NO_FETCHr}   r   	NEVER_SET)r4   r6   rf   r9   r9   r:   r}      s   
z&WriteOnlyAttributeImpl._modified_eventrM   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r7   )r   )parent_tokenr=   has_identity_supports_dynamic_iterationr   r.   r   IdentitySetrg   r}   r0   r>   rC   rB   r   r   )r4   r6   rf   rM   r   r7   r   r   r   iterable
new_valuesold_collectionr   	constants	additionsremovalsmemberr9   r9   r:   set  sT   


zWriteOnlyAttributeImpl.setc                 O  s   t  r<   NotImplementedError)r4   argskwargsr9   r9   r:   r   D  s   zWriteOnlyAttributeImpl.deletec                 C     t d)Nz7Dynamic attributes don't support collection population.r   )r4   r6   rf   rM   r9   r9   r:   set_committed_valueG     z*WriteOnlyAttributeImpl.set_committed_valuec                 C  s   |  ||}| S r<   )rd   rH   r4   r6   rf   r7   cr9   r9   r:   get_historyL  s   z"WriteOnlyAttributeImpl.get_historyc                 C  s   |  ||}dd |jD S )Nc                 S  s   g | ]	}t ||fqS r9   )r   r   ).0xr9   r9   r:   
<listcomp>T  s    z:WriteOnlyAttributeImpl.get_all_pending.<locals>.<listcomp>)rd   rA   r   r9   r9   r:   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)r8   )r^   r   r   r   r   r   r   INIT_OK)r4   r6   r7   r   r9   r9   r:   rd   V  s   z.WriteOnlyAttributeImpl._get_collection_historyc                 C      || ur|  |||| d S d S r<   )r   r4   r6   rf   rM   r   r7   r9   r9   r:   r~   e     zWriteOnlyAttributeImpl.appendc                 C  r   r<   )r   r   r9   r9   r:   rO   p  r   zWriteOnlyAttributeImpl.removec                 C  s   | j |||||d d S )Nr   )rO   r   r9   r9   r:   r   {  s   zWriteOnlyAttributeImpl.pop)..)
r6   rh   rf   r#   ri   rj   r7   rk   rl   r&   )
r6   rh   rf   r#   ri   r$   r7   r   rl   r&   )
r6   rh   rf   r#   ri   rq   r7   r   rl   rr   r<   )r6   rh   rf   r#   rM   r   r   r   r7   r   r   r   r   r   r   r   rl   r   )&rQ   rR   rS   uses_objectsdefault_accepts_scalar_loadersupports_populationr   
collectiondynamicr\   r,   r   r;   r   PASSIVE_OFFrg   r   ro   r   r   memoized_propertyrx   r{   r   r   r}   r   r   r   r   r   re   r   rd   r~   rO   r   __classcell__r9   r9   rb   r:   rV      sj    		



=


rV   
write_only)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.)	useobject
impl_classrX   r\   rZ   )is_class_leveluselistparent_property	directionr   	ONETOMANY
MANYTOMANYr   r.   r   _register_attributer   mapperr\   rZ   )r4   r   r9   r9   r:   init_class_attribute  s&   
z$WriteOnlyLoader.init_class_attributeN)rQ   rR   rS   rV   r   r   r9   r9   r9   r:   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 )rs   z9simplified CollectionAdapter for internal API consistencyc                 C  s
   || _ d S r<   )rt   )r4   rt   r9   r9   r:   r;        
z!DynamicCollectionAdapter.__init__c                 C  
   t | jS r<   )iterrt   r?   r9   r9   r:   __iter__  r   z!DynamicCollectionAdapter.__iter__c                 C  rm   r<   r9   r?   r9   r9   r:   _reset_empty     z%DynamicCollectionAdapter._reset_emptyc                 C  r   r<   )lenrt   r?   r9   r9   r:   __len__  r   z DynamicCollectionAdapter.__len__c                 C  s   dS r|   r9   r?   r9   r9   r:   __bool__  r   z!DynamicCollectionAdapter.__bool__N)	rQ   rR   rS   rT   r;   r   r   r   r   r9   r9   r9   r:   rs     s    rs   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.

    r9   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 )Nr9   F)alias_secondary)objinstancer5   r   _propsr^   	secondaryr   __clause_element__	_from_obj_with_parent_where_criteriar\   _order_by_clauses)r4   r5   r6   r   r   propr9   r9   r:   r;     s   


z!AbstractCollectionWriter.__init__iteratorIterable[_T]rl   r   c                 C  s0   |D ]}| j t| jt| j|d  qd S r<   )r5   r~   r   r   r   instance_dict)r4   r   itemr9   r9   r:   _add_all_impl  s   

z&AbstractCollectionWriter._add_all_implr   r*   c                 C  s&   | j t| jt| j|d  d S r<   )r5   rO   r   r   r   r   r4   r   r9   r9   r:   _remove_impl  s   

z%AbstractCollectionWriter._remove_implNr   r   rl   r   r   r*   rl   r   )	rQ   rR   rS   rT   r
   	__slots__r;   r   r   r9   r9   r9   r:   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 )&rY   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   r5   r   r   r   rl   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?   r9   r9   r:   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   r5   rX   wherer   r   select_fromr   r\   )r4   stmtr9   r9   r:   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)	callable_r9   )r   r   r   r   r5   r^   r   r   r   r   r.   synchronize_pairs_get_attr_w_warn_on_nonedictr   r   rX   values)r4   r6   r   r   rf   lrr   r9   r9   r:   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   r5   rX   r   r   r?   r9   r9   r:   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   r5   rX   r   r   r?   r9   r9   r:   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   )r4   r   r9   r9   r:   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   r9   r9   r:   rK   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   r9   r9   r:   rO   b  r   zWriteOnlyCollection.removeN)rl   r   )rl   r   )rl   r   )rl   r   )rl   r   r   r   )rQ   rR   rS   rT   r   r   r   r   r   r   r   rK   rO   r9   r9   r9   r:   rY     s    



(


		rY   N)@rT   
__future__r   typingr   r   r   r   r   r   r	   r
   r   r   sqlalchemy.sqlr    r   r   r   r   baser   r   r   r   r   r   r   sqlr   r   r   r   sql.dmlr   r    r!   util.typingr"   _typingr#   r$   r%   r&   r'   r6   r(   sql.selectabler)   r*   r,   HasCollectionAdapterAttributeImplrV   class_loggerRelationshipPropertystrategy_forAbstractRelationshipLoader
Identifiedr   rs   r   rY   r9   r9   r9   r:   <module>   sj   	
>  9