o
    ‹ågÅ,  ã                   @  sè   d dl mZ d dlZd dlZd dlZd dlmZ	 ddl
mZ ejr'ddlmZ G dd„ dƒZG dd	„ d	ƒZG d
d„ deƒZG dd„ dƒZG dd„ deƒZG dd„ dƒZd dd„Zd!dd„ZG dd„ deee	jƒZG dd„ dee	jƒZdS )"é    )ÚannotationsNé   )ÚQuery)Ú
SQLAlchemyc                   @  s   e Zd ZdZddd	„Zd
S )Ú_QueryPropertyzRA class property that creates a query object for a model.

    :meta private:
    ÚobjúModel | NoneÚclsútype[Model]Úreturnr   c                 C  s   |j ||j ¡ d�S )N)Úsession)Úquery_classÚ__fsa__r   )Úselfr   r	   © r   úR/var/www/html/flaskapp/venv/lib/python3.10/site-packages/flask_sqlalchemy/model.pyÚ__get__   s   
ÿz_QueryProperty.__get__N)r   r   r	   r
   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                   @  sD   e Zd ZU dZded< 	 eZded< 	 eƒ Zded< 	 dd
d„Z	dS )ÚModela€  The base class of the :attr:`.SQLAlchemy.Model` declarative model class.

    To define models, subclass :attr:`db.Model <.SQLAlchemy.Model>`, not this. To
    customize ``db.Model``, subclass this and pass it as ``model_class`` to
    :class:`.SQLAlchemy`. To customize ``db.Model`` at the metaclass level, pass an
    already created declarative model class as ``model_class``.
    zt.ClassVar[SQLAlchemy]r   zt.ClassVar[type[Query]]r   zt.ClassVar[Query]Úqueryr   Ústrc                 C  sp   t  | ¡}|d usJ ‚|jrdt| ƒ› d�}n|jr#dt| ƒ› d�}n	d tt|jƒ¡}dt	| ƒj
› d|› d�S )Nz(transient ú)z	(pending z, ú<ú ú>)ÚsaÚinspectÚ	transientÚidÚpendingÚjoinÚmapr   ÚidentityÚtyper   )r   ÚstateÚpkr   r   r   Ú__repr__8   s   
zModel.__repr__N)r   r   )
r   r   r   r   Ú__annotations__r   r   r   r   r)   r   r   r   r   r      s   
 r   c                      s4   e Zd ZU dZded< ded< d‡ fdd„Z‡  ZS )ÚBindMetaMixina  Metaclass mixin that sets a model's ``metadata`` based on its ``__bind_key__``.

    If the model sets ``metadata`` or ``__table__`` directly, ``__bind_key__`` is
    ignored. If the ``metadata`` is the same as the parent model, it will not be set
    directly on the child model.
    r   r   úsa.MetaDataÚmetadataÚnamer   Úbasesútuple[type, ...]Údúdict[str, t.Any]Úkwargsút.Anyr   ÚNonec                   sb   d| j v s#d| j v s#t| dd ƒ}t| dd ƒ}| j |¡}||ur#|| _tƒ j|||fi |¤Ž d S )Nr-   Ú	__table__Ú__bind_key__)Ú__dict__Úgetattrr   Ú_make_metadatar-   ÚsuperÚ__init__)r	   r.   r/   r1   r3   Úbind_keyÚparent_metadatar-   ©Ú	__class__r   r   r<   Q   s   zBindMetaMixin.__init__©
r.   r   r/   r0   r1   r2   r3   r4   r   r5   )r   r   r   r   r*   r<   Ú__classcell__r   r   r?   r   r+   F   s
   
 r+   c                      s8   e Zd ZU dZded< ded< ed‡ fdd„ƒZ‡  ZS )Ú	BindMixina  DeclarativeBase mixin to set a model's ``metadata`` based on ``__bind_key__``.

    If no ``__bind_key__`` is specified, the model will use the default metadata
    provided by ``DeclarativeBase`` or ``DeclarativeBaseNoMeta``.
    If the model doesn't set ``metadata`` or ``__table__`` directly
    and does set ``__bind_key__``, the model will use the metadata
    for the specified bind key.
    If the ``metadata`` is the same as the parent model, it will not be set
    directly on the child model.

    .. versionchanged:: 3.1.0
    r   r   r,   r-   r	   út.Type[BindMixin]r3   út.Dict[str, t.Any]r   r5   c                   sf   d| j v s(d| j v s(t| dƒr(t| dd ƒ}t| dd ƒ}| j |¡}||ur(|| _tƒ jdi |¤Ž d S )Nr-   r6   r7   r   )r8   Úhasattrr9   r   r:   r-   r;   Ú__init_subclass__)r	   r3   r=   r>   r-   r?   r   r   rG   p   s   ÿzBindMixin.__init_subclass__)r	   rD   r3   rE   r   r5   )r   r   r   r   r*   ÚclassmethodrG   rB   r   r   r?   r   rC   _   s   
 rC   c                      sF   e Zd ZU dZded< ded< ded< d‡ fdd„Zddd„Z‡  ZS )ÚNameMetaMixinaP  Metaclass mixin that sets a model's ``__tablename__`` by converting the
    ``CamelCase`` class name to ``snake_case``. A name is set for non-abstract models
    that do not otherwise define ``__tablename__``. If a model does not define a primary
    key, it will not generate a name or ``__table__``, for single-table inheritance.
    r,   r-   r   Ú__tablename__úsa.Tabler6   r.   r/   r0   r1   r2   r3   r4   r   r5   c                   sb   t | ƒr
t| jƒ| _tƒ j|||fi |¤Ž d| jvr+d| jv r-| jd d u r/| `d S d S d S d S )NrJ   r6   )Úshould_set_tablenameÚcamel_to_snake_caser   rJ   r;   r<   r8   r6   )r	   r.   r/   r1   r3   r?   r   r   r<   Š   s   

üzNameMetaMixin.__init__Úargsúsa.Table | Nonec                 O  óÊ   |  d¡}|du r|d }n	|› d|d › �}|| jjv r%tj|i |¤ŽS |D ]}t|tjƒr2|js8t|tjƒrBtj|i |¤Ž  S q'| j	dd… D ]	}d|j
v rS n	qJtj|i |¤ŽS d| j
v rc| `dS ©	a  This is called by SQLAlchemy during mapper setup. It determines the final
        table object that the model will use.

        If no primary key is found, that indicates single-table inheritance, so no table
        will be created and ``__tablename__`` will be unset.
        ÚschemaNr   Ú.r   éÿÿÿÿr6   rJ   ©Úgetr-   Útablesr   ÚTableÚ
isinstanceÚColumnÚprimary_keyÚPrimaryKeyConstraintÚ__mro__r8   rJ   ©r	   rN   r3   rR   ÚkeyÚargÚbaser   r   r   Ú__table_cls__š   s(   

ÿý
ÿ
zNameMetaMixin.__table_cls__rA   ©rN   r4   r3   r4   r   rO   )r   r   r   r   r*   r<   rb   rB   r   r   r?   r   rI      s   
 rI   c                      sN   e Zd ZU dZded< ded< ded< ed‡ fdd„ƒZeddd„ƒZ‡  ZS )Ú	NameMixinau  DeclarativeBase mixin that sets a model's ``__tablename__`` by converting the
    ``CamelCase`` class name to ``snake_case``. A name is set for non-abstract models
    that do not otherwise define ``__tablename__``. If a model does not define a primary
    key, it will not generate a name or ``__table__``, for single-table inheritance.

    .. versionchanged:: 3.1.0
    r,   r-   r   rJ   rK   r6   r	   út.Type[NameMixin]r3   rE   r   r5   c                   s\   t | ƒr
t| jƒ| _tƒ jdi |¤Ž d| jvr(d| jv r*| jd d u r,| `d S d S d S d S )NrJ   r6   r   )rL   rM   r   rJ   r;   rG   r8   r6   )r	   r3   r?   r   r   rG   Ñ   s   

üzNameMixin.__init_subclass__rN   r4   rO   c                 O  rP   rQ   rU   r^   r   r   r   rb   à   s(   

ÿý
ÿ
zNameMixin.__table_cls__)r	   re   r3   rE   r   r5   rc   )	r   r   r   r   r*   rH   rG   rb   rB   r   r   r?   r   rd   Ä   s   
 rd   r	   r&   r   Úboolc                 C  sÊ   | j  dd¡s(t| tjtjfƒstdd„ | jdd… D ƒƒr(tdd„ | jD ƒƒr*dS | jD ]5}d|j vr5q-t	|j d tj
ƒrA dS || u p_|j  dd¡p_t	|tjƒp^t	|tjjƒp^t|tjƒ    S d	S )
a  Determine whether ``__tablename__`` should be generated for a model.

    -   If no class in the MRO sets a name, one should be generated.
    -   If a declared attr is found, it should be used instead.
    -   If a name is found, it should be used if the class is a mixin, otherwise one
        should be generated.
    -   Abstract models should not have one generated.

    Later, ``__table_cls__`` will determine if the model looks like single or
    joined-table inheritance. If no primary key is found, the name will be unset.
    Ú__abstract__Fc                 s  s   � | ]	}t |tjƒV  qd S ©N)rY   Úsa_ormÚDeclarativeMeta©Ú.0Úbr   r   r   Ú	<genexpr>  s   € z'should_set_tablename.<locals>.<genexpr>r   Nc                 s  s$   � | ]}|t ju p|t ju V  qd S rh   )ri   ÚDeclarativeBaseÚDeclarativeBaseNoMetark   r   r   r   rn     s
   € ÿ
ÿrJ   T)r8   rV   Ú
issubclassri   ro   rp   Úanyr]   Ú	__bases__rY   Údeclared_attrrj   Údecl_apiÚDeclarativeAttributeIntercept)r	   ra   r   r   r   rL     s4   ÿýüþú

ÿþ
úýrL   r.   r   c                 C  s   t  dd| ¡} |  ¡  d¡S )z/Convert a ``CamelCase`` name to ``snake_case``.z(((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))z_\1Ú_)ÚreÚsubÚlowerÚlstrip)r.   r   r   r   rM   ;  s   rM   c                   @  ó   e Zd ZdZdS )ÚDefaultMetaúgSQLAlchemy declarative metaclass that provides ``__bind_key__`` and
    ``__tablename__`` support.
    N©r   r   r   r   r   r   r   r   r}   A  ó    r}   c                   @  r|   )ÚDefaultMetaNoNamer~   Nr   r   r   r   r   r�   G  r€   r�   )r	   r&   r   rf   )r.   r   r   r   )Ú
__future__r   rx   ÚtypingÚtÚ
sqlalchemyr   Úsqlalchemy.ormÚormri   r   r   ÚTYPE_CHECKINGÚ	extensionr   r   r   r&   r+   rC   rI   rd   rL   rM   rj   r}   r�   r   r   r   r   Ú<module>   s$    + E
G
0