o
    ޭh:U                     @  s@  d dl mZ d dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZmZmZ erXd dlmZ d d	lmZ d d
lmZm Z  edddZ!G dd dZ"e" Z#G dd de
e! Z$G dd de$e! Z%G dd dee! Z&G dd de&e! Z'G dd de&e! Z(G dd de'e! Z)G dd de(e! Z*G dd de&e! Z+eded fdGd/d0Z,eded d1fdHd4d0Z,ded d1fdId6d0Z,eded fdJd8d9Z-eded d1fdKd;d9Z-ded d1fdLd=d9Z-ddd>d>ed d fdMdEdFZ.ee)e!  Z/	 e)e! Z0	 ee'e!  Z1	 e'e! Z2dS )N    )annotationsN)AsyncGenerator	GeneratorIterator)TYPE_CHECKINGAnyGenericLiteralOptionalTypeVaroverload)Table)ConfigurationErrorNoValuesFetchedOperationalError)CASCADESET_NULLFieldOnDelete)BaseDBAsyncClient)Model)QQuerySetMODELr   )boundc                   @  s$   e Zd ZdZd
ddZdddZd	S )_NoneAwaitable returnGenerator[None, None, None]c                 c  s    d V  d S Nr   selfr   r   _/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/tortoise/fields/relational.py	__await__   s   
z_NoneAwaitable.__await__boolc                 C  s   dS NFr   r    r   r   r"   __bool__"      z_NoneAwaitable.__bool__N)r   r   r   r$   )__name__
__module____qualname__	__slots__r#   r&   r   r   r   r"   r      s    
r   c                   @  s   e Zd ZdZd?ddZed@ddZdAddZdBddZdCddZ	dDddZ
dEddZdFd!d"ZdGd$d%ZdHd)d*Zd@d+d,ZdId.d/ZdJd1d2ZdKd4d5ZdLdMd;d<ZdNd=d>Zd6S )OReverseRelationz:
    Relation container for :func:`.ForeignKeyField`.
    remote_modeltype[MODEL]relation_fieldstrinstancer   
from_fieldr   Nonec                 C  s.   || _ || _|| _|| _d| _d| _g | _d S r%   )r.   r0   r2   r3   _fetched_custom_queryrelated_objects)r!   r.   r0   r2   r3   r   r   r"   __init__.   s   
zReverseRelation.__init__QuerySet[MODEL]c                 C  s2   | j jstd| jjdi | jt| j | jiS )NzOThis objects hasn't been instanced, call .save() before calling related queriesr   )r2   _saved_in_dbr   r.   filterr0   getattrr3   r    r   r   r"   _query=   s   
zReverseRelation._queryitemr   r$   c                 C  s   |    || jv S r   _raise_if_not_fetchedr7   r!   r>   r   r   r"   __contains__G      
zReverseRelation.__contains__Iterator[MODEL]c                 C  s   |    | j S r   )r@   r7   __iter__r    r   r   r"   rE   K   rC   zReverseRelation.__iter__intc                 C     |    t| jS r   )r@   lenr7   r    r   r   r"   __len__O   rC   zReverseRelation.__len__c                 C  rG   r   )r@   r$   r7   r    r   r   r"   r&   S   rC   zReverseRelation.__bool__r   c                 C  s   |    | j| S r   r?   rA   r   r   r"   __getitem__W   rC   zReverseRelation.__getitem__!Generator[Any, None, list[MODEL]]c                 C  s
   | j  S r   )r=   r#   r    r   r   r"   r#   [   s   
zReverseRelation.__await__AsyncGenerator[MODEL, None]c                 C s.   | j s| | I d H  | jD ]}|V  qd S r   )r5   _set_result_for_queryr7   )r!   valr   r   r"   	__aiter__^   s   
zReverseRelation.__aiter__argsr   kwargsc                 O  s   | j j|i |S )zS
        Returns a QuerySet with related elements filtered by args/kwargs.
        )r=   r;   )r!   rP   rQ   r   r   r"   r;   d   s   zReverseRelation.filterc                 C  s   | j S )z?
        Returns a QuerySet with all related elements.
        )r=   r    r   r   r"   allj   s   zReverseRelation.all	orderingsc                 G  s   | j j| S )z?
        Returns a QuerySet related elements in order.
        )r=   order_by)r!   rS   r   r   r"   rT   p      zReverseRelation.order_bylimitc                 C     | j |S )uM   
        Returns a QuerySet with at most «limit» related elements.
        )r=   rV   )r!   rV   r   r   r"   rV   v   rU   zReverseRelation.limitoffsetc                 C  rW   )uT   
        Returns a QuerySet with all related elements offset by «offset».
        )r=   rX   )r!   rX   r   r   r"   rX   |   rU   zReverseRelation.offsetNsequencelist[MODEL]attr
str | Nonec                 C  s&   d| _ || _|rt| j|| d S d S NT)r5   r7   setattrr2   )r!   rY   r[   r   r   r"   rM      s
   z%ReverseRelation._set_result_for_queryc                 C  s   | j stdd S )NzDNo values were fetched for this relation, first use .fetch_related())r5   r   r    r   r   r"   r@      s
   z%ReverseRelation._raise_if_not_fetched)
r.   r/   r0   r1   r2   r   r3   r1   r   r4   )r   r9   )r>   r   r   r$   )r   rD   )r   rF   r(   )r>   rF   r   r   )r   rK   )r   rL   )rP   r   rQ   r   r   r9   )rS   r1   r   r9   )rV   rF   r   r9   )rX   rF   r   r9   r   )rY   rZ   r[   r\   r   r4   )r   r4   )r)   r*   r+   __doc__r8   propertyr=   rB   rE   rI   r&   rJ   r#   rO   r;   rR   rT   rV   rX   rM   r@   r   r   r   r"   r-   )   s&    

	










r-   c                      s^   e Zd ZdZd fdd	Zd
ddddZddddZd
ddddZ	
	
ddddZ  Z	S )ManyToManyRelationzG
    Many-to-many relation container for :func:`.ManyToManyField`.
    r2   r   	m2m_fieldManyToManyFieldInstance[MODEL]r   r4   c                   s&   t  |j|j|d || _|| _d S )Npk)superr8   related_modelrelated_namefieldr2   )r!   r2   rb   	__class__r   r"   r8      s   
zManyToManyRelation.__init__Nusing_db	instancesr   rl   BaseDBAsyncClient | Nonec                  s  |sdS j jstdj  |pjjj}tj jjj}t|d jjj|j jj }g }|D ]}|jsAtd| |j|}|	| q5t
jj}	jjjj}
 |	|
 |	  }}|j|	||k }t|dkr||d kn||}||}|j|  I dH \}} fdd|D }t||  }r|j|	||}|D ]}|||}q|j|  I dH  dS dS )z
        Adds one or more of ``instances`` to the relation.

        If it is already added, it will be silently ignored.

        :raises OperationalError: If Object to add is not saved.
        Nz!You should first call .save() on r      c                   s   h | ]
}|  j qS r   )r2   ).0rforward_keyrelated_pk_formatting_funcr!   r   r"   	<setcomp>   s    z)ManyToManyRelation.add.<locals>.<setcomp>)r2   r:   r   r.   _metadbtyperd   to_db_valueappendr   rh   throughbackward_keyrs   query_classfrom_whereselectrH   isinexecute_queryget_parameterized_sqlsetintocolumnsinsert)r!   rl   rm   rw   pk_formatting_funcpk_bpks_finstance_to_addpk_fthrough_tabler|   backward_fieldforward_fieldselect_query	criterion_already_existing_relations_rawalready_existing_forward_pkspks_f_to_insertqueryr   rr   r"   add   sD   "
zManyToManyRelation.addc                   s   | j |dI dH  dS )z'
        Clears ALL relations.
        rk   N)_remove_or_clear)r!   rl   r   r   r"   clear   s   zManyToManyRelation.clearc                  s$   |st d| ||I dH  dS )z
        Removes one or more of ``instances`` from the relation.

        :raises OperationalError: remove() was called with no instances.
        zremove() called on no instancesN)r   r   )r!   rl   rm   r   r   r"   remove   s   zManyToManyRelation.removetuple[MODEL, ...] | Nonec                   s   |p| j jj}t| jj}t| jjjj	}|| jj
 || jj| jk}|rZt|d jjj	 t|dkrH||| jj  |d j|d kM }n||| jj  fdd|D M }|j|| }|j|  I d H  d S )Nr   ro   c                   s   g | ]} |j |qS r   )rd   )rp   irt   r   r"   
<listcomp>   s    z7ManyToManyRelation._remove_or_clear.<locals>.<listcomp>)r.   rv   rw   r   rh   r{   rx   r2   rd   ry   r|   rH   rs   r   r}   r~   r   deleter   r   )r!   rm   rl   rw   r   r   	conditionr   r   r   r"   r      s$   

z#ManyToManyRelation._remove_or_clear)r2   r   rb   rc   r   r4   )rm   r   rl   rn   r   r4   r   )rl   rn   r   r4   )NN)rm   r   rl   rn   r   r4   )
r)   r*   r+   r_   r8   r   r   r   r   __classcell__r   r   ri   r"   ra      s    -ra   c                      sx   e Zd ZdZ		d'd( fddZer*ed)ddZed*ddZd+ddZd,ddZd- fd!d"Z	e
d.d%d&Z  ZS )/RelationalFieldFNTrf   r/   to_fieldr\   db_constraintr$   rQ   r   r   r4   c                   s.   t  jdi | || _|| _d | _|| _d S )Nr   )re   r8   rf   r   to_field_instancer   )r!   rf   r   r   rQ   ri   r   r"   r8      s
   
zRelationalField.__init__r2   ownertype[Model]RelationalField[MODEL]c                 C     d S r   r   r!   r2   r   r   r   r"   __get__  r'   zRelationalField.__get__r   r   c                 C  r   r   r   r   r   r   r"   r     r'   Model | NoneRelationalField[MODEL] | MODELc                 C  r   r   r   r   r   r   r"   r     s   valuec                 C  r   r   r   )r!   r2   r   r   r   r"   __set__  s    zRelationalField.__set__serializabledictc                   s    t  |}| j|d< |d= |S )Nr   	db_column)re   describer   r!   r   descri   r   r"   r     s   
zRelationalField.describe
model_namer1   c                 C  s2   t |ddkr| jdd}t| dd S )N.   Instance z) accepts model name in format "app.Model")rH   splitr)   replacer   )clsr   
field_typer   r   r"   validate_model_name  s   z#RelationalField.validate_model_namer]   )
rf   r/   r   r\   r   r$   rQ   r   r   r4   )r2   r4   r   r   r   r   )r2   r   r   r   r   r   )r2   r   r   r   r   r   )r2   r   r   r   r   r4   r   r$   r   r   )r   r1   r   r4   )r)   r*   r+   has_db_fieldr8   r   r   r   r   r   classmethodr   r   r   r   ri   r"   r      s    

r   c                      s2   e Zd Zdefd fddZd fddZ  ZS )ForeignKeyFieldInstanceNr   r1   rg   str | None | Literal[False]	on_deleter   rQ   r   r   r4   c                   sd   t  jdi | | | || _|| _|ttvrtd|tkr-t	|
ds-td|| _d S )NzKon_delete can only be CASCADE, RESTRICT, SET_NULL, SET_DEFAULT or NO_ACTIONnullz<If on_delete is SET_NULL, then field must have null=True setr   )re   r8   r   r   rg   r   r   r   r   r$   getr   r!   r   rg   r   rQ   ri   r   r"   r8     s   

z ForeignKeyFieldInstance.__init__r   r$   r   c                   s(   t  |}| j|d< t| j|d< |S )N	raw_fieldr   )re   r   source_fieldr1   r   r   ri   r   r"   r   /  s   
z ForeignKeyFieldInstance.describe
r   r1   rg   r   r   r   rQ   r   r   r4   r   )r)   r*   r+   r   r8   r   r   r   r   ri   r"   r     s
    r   c                      s   e Zd Zd fddZ  ZS )BackwardFKRelationr   r/   r0   r1   relation_source_fieldr   r$   descriptionr\   rQ   r   r   r4   c                   s.   t  j|fd|i| || _|| _|| _d S )Nr   )re   r8   r0   r   r   )r!   r   r0   r   r   r   rQ   ri   r   r"   r8   7  s   	
zBackwardFKRelation.__init__)r   r/   r0   r1   r   r1   r   r$   r   r\   rQ   r   r   r4   )r)   r*   r+   r8   r   r   r   ri   r"   r   6  s    r   c                      s$   e Zd Zdefd fddZ  ZS )OneToOneFieldInstanceNr   r1   rg   r   r   r   rQ   r   r   r4   c                   s*   |  | t j|||fddi| d S )NuniqueT)r   re   r8   r   ri   r   r"   r8   G  s   
 zOneToOneFieldInstance.__init__r   )r)   r*   r+   r   r8   r   r   r   ri   r"   r   F  s    r   c                   @  s   e Zd ZdS )BackwardOneToOneRelationN)r)   r*   r+   r   r   r   r"   r   R  s    r   c                      s@   e Zd ZeZddddeddfd fddZd fddZ  ZS )ManyToManyFieldInstanceNr   Tr   r1   r{   r\   rs   r|   rg   r   r   r   r/   r   r$   rQ   r   r   r4   c	           
        s   d|	v rt jdtdd |	d}t j|fd|i|	 | | || _|| _|p5|	dd 
  d| _|| _|| _d	| _|| _d S )
Ncreate_unique_indexzDParameter `create_unique_index` is deprecated! Use `unique` instead.r   )
stacklevelr   r   ro   _idF)warningswarnDeprecationWarningpopre   r8   r   r   rg   r   lowerrs   r|   r{   
_generatedr   )
r!   r   r{   rs   r|   rg   r   r   r   rQ   ri   r   r"   r8   Y  s    


z ManyToManyFieldInstance.__init__r   r   c                   sZ   t  |}| j|d< | j|d< | j|d< | j|d< | j|d< t| j|d< | j	|d< |S )Nr   rg   rs   r|   r{   r   r   )
re   r   r   rg   rs   r|   r{   r1   r   r   r   ri   r   r"   r   x  s   





z ManyToManyFieldInstance.describe)r   r1   r{   r\   rs   r\   r|   r1   rg   r1   r   r   r   r/   r   r$   rQ   r   r   r4   r   )	r)   r*   r+   ra   r   r   r8   r   r   r   r   ri   r"   r   V  s    r   Tr   r1   rg   r   r   r   r   r$   r   Literal[True]rQ   r   r   OneToOneNullableRelation[MODEL]c                K  r   r   r   r   rg   r   r   r   rQ   r   r   r"   OneToOneField     	r   FLiteral[False]OneToOneRelation[MODEL]c                 K  r   r   r   r   r   r   r"   r        9OneToOneRelation[MODEL] | OneToOneNullableRelation[MODEL]c                 K     t | ||f||d|S )u'  
    OneToOne relation field.

    This field represents a foreign key relation to another model.

    See :ref:`one_to_one` for usage information.

    You must provide the following:

    ``model_name``:
        The name of the related model in a :samp:`'{app}.{model}'` format.

    The following is optional:

    ``related_name``:
        The attribute name on the related model to reverse resolve the foreign key.
    ``on_delete``:
        One of:
            ``field.CASCADE``:
                Indicate that the model should be cascade deleted if related model gets deleted.
            ``field.RESTRICT``:
                Indicate that the related model delete will be restricted as long as a
                foreign key points to it.
            ``field.SET_NULL``:
                Resets the field to NULL in case the related model gets deleted.
                Can only be set if field has ``null=True`` set.
            ``field.SET_DEFAULT``:
                Resets the field to ``default`` value in case the related model gets deleted.
                Can only be set is field has a ``default`` set.
            ``field.NO_ACTION``:
                Take no action.
    ``to_field``:
        The attribute name on the related model to establish foreign key relationship.
        If not set, pk is used
    ``db_constraint``:
        Controls whether or not a constraint should be created in the database for this foreign key.
        The default is True, and that’s almost certainly what you want; setting this to False can be very bad for data integrity.
    r   r   )r   r   r   r   r"   r        /!ForeignKeyNullableRelation[MODEL]c                K  r   r   r   r   r   r   r"   ForeignKeyField  r   r   ForeignKeyRelation[MODEL]c                 K  r   r   r   r   r   r   r"   r     r   =ForeignKeyRelation[MODEL] | ForeignKeyNullableRelation[MODEL]c                 K  r   )u*  
    ForeignKey relation field.

    This field represents a foreign key relation to another model.

    See :ref:`foreign_key` for usage information.

    You must provide the following:

    ``model_name``:
        The name of the related model in a :samp:`'{app}.{model}'` format.

    The following is optional:

    ``related_name``:
        The attribute name on the related model to reverse resolve the foreign key.
    ``on_delete``:
        One of:
            ``field.CASCADE``:
                Indicate that the model should be cascade deleted if related model gets deleted.
            ``field.RESTRICT``:
                Indicate that the related model delete will be restricted as long as a
                foreign key points to it.
            ``field.SET_NULL``:
                Resets the field to NULL in case the related model gets deleted.
                Can only be set if field has ``null=True`` set.
            ``field.SET_DEFAULT``:
                Resets the field to ``default`` value in case the related model gets deleted.
                Can only be set is field has a ``default`` set.
            ``field.NO_ACTION``:
                Take no action.
    ``to_field``:
        The attribute name on the related model to establish foreign key relationship.
        If not set, pk is used
    ``db_constraint``:
        Controls whether or not a constraint should be created in the database for this foreign key.
        The default is True, and that’s almost certainly what you want; setting this to False can be very bad for data integrity.
    r   )r   r   r   r   r"   r     r   r   r{   r\   rs   r|   r   ManyToManyRelation[Any]c           	      K  s    t | ||||f|||d|S )u  
    ManyToMany relation field.

    This field represents a many-to-many between this model and another model.

    See :ref:`many_to_many` for usage information.

    You must provide the following:

    ``model_name``:
        The name of the related model in a :samp:`'{app}.{model}'` format.

    The following is optional:

    ``through``:
        The DB table that represents the through table.
        The default is normally safe.
    ``forward_key``:
        The forward lookup key on the through table.
        The default is normally safe.
    ``backward_key``:
        The backward lookup key on the through table.
        The default is normally safe.
    ``related_name``:
        The attribute name on the related model to reverse resolve the many to many.
    ``db_constraint``:
        Controls whether or not a constraint should be created in the database for this foreign key.
        The default is True, and that’s almost certainly what you want; setting this to False can be very bad for data integrity.
    ``on_delete``:
        One of:
            ``field.CASCADE``:
                Indicate that the model should be cascade deleted if related model gets deleted.
            ``field.RESTRICT``:
                Indicate that the related model delete will be restricted as long as a
                foreign key points to it.
            ``field.SET_NULL``:
                Resets the field to NULL in case the related model gets deleted.
                Can only be set if field has ``null=True`` set.
            ``field.SET_DEFAULT``:
                Resets the field to ``default`` value in case the related model gets deleted.
                Can only be set is field has a ``default`` set.
            ``field.NO_ACTION``:
                Take no action.
    ``unique``:
        Controls whether or not a unique index should be created in the database to speed up select queries.
        The default is True. If you want to allow repeat records, set this to False.
    )r   r   r   )r   )	r   r{   rs   r|   rg   r   r   r   rQ   r   r   r"   ManyToManyField  s   :	r   )r   r1   rg   r   r   r   r   r$   r   r   rQ   r   r   r   )r   r1   rg   r   r   r   r   r$   r   r   rQ   r   r   r   )r   r1   rg   r   r   r   r   r$   r   r$   rQ   r   r   r   )r   r1   rg   r   r   r   r   r$   r   r   rQ   r   r   r   )r   r1   rg   r   r   r   r   r$   r   r   rQ   r   r   r   )r   r1   rg   r   r   r   r   r$   r   r$   rQ   r   r   r   )r   r1   r{   r\   rs   r\   r|   r1   rg   r1   r   r   r   r$   r   r$   rQ   r   r   r   )3
__future__r   r   collections.abcr   r   r   typingr   r   r   r	   r
   r   r   pypika_tortoiser   tortoise.exceptionsr   r   r   tortoise.fields.baser   r   r   r   tortoise.backends.base.clientr   tortoise.modelsr   tortoise.querysetr   r   r   r   NoneAwaitabler-   ra   r   r   r   r   r   r   r   r   r   OneToOneNullableRelationOneToOneRelationForeignKeyNullableRelationForeignKeyRelationr   r   r   r"   <module>   s    $

fa+.46G