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   X/var/www/html/ecg_monitoring/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 )DefaultMetagSQLAlchemy 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