U
    ^g%i,                     @  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rNd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ddZdd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dddZdS )	_QueryPropertyzRA class property that creates a query object for a model.

    :meta private:
    zModel | Noneztype[Model]r   )objclsreturnc                 C  s   |j ||j dS )N)session)query_class__fsa__r
   )selfr   r    r   U/var/www/html/api-medvista/venv/lib/python3.8/site-packages/flask_sqlalchemy/model.py__get__   s     z_QueryProperty.__get__N)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c                   @  sB   e Zd ZU dZded< eZded< e Zded< 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]querystr)r	   c                 C  sp   t | }|d k	st|jr.dt|  d}n*|jrFdt|  d}ndtt|j	}dt
| j d| dS )Nz(transient )z	(pending z, < >)sainspectAssertionError	transientidpendingjoinmapr   identitytyper   )r   statepkr   r   r   __repr__8   s    
zModel.__repr__N)
r   r   r   r   __annotations__r   r   r   r   r(   r   r   r   r   r      s
   
	r   c                      s@   e Zd ZU dZded< ded< dddd	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metadatar   tuple[type, ...]dict[str, t.Any]t.AnyNonenamebasesdkwargsr	   c                   s^   d| j ksFd| j ksFt| dd }t| dd }| j|}||k	rF|| _t j|||f| d S Nr,   	__table__Z__bind_key__)__dict__getattrr   _make_metadatar,   super__init__)r   r2   r3   r4   r5   bind_keyparent_metadatar,   	__class__r   r   r<   Q   s    zBindMetaMixin.__init__)r   r   r   r   r)   r<   __classcell__r   r   r?   r   r*   F   s   
r*   c                      s@   e Zd ZU dZded< ded< eddd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,   zt.Type[BindMixin]t.Dict[str, t.Any]r0   r   r5   r	   c                   sb   d| j ksPd| j ksPt| drPt| dd }t| dd }| j|}||k	rP|| _t jf | d S r6   )r8   hasattrr9   r   r:   r,   r;   __init_subclass__)r   r5   r=   r>   r,   r?   r   r   rF   p   s     zBindMixin.__init_subclass__)r   r   r   r   r)   classmethodrF   rA   r   r   r?   r   rB   _   s
   
rB   c                      sZ   e Zd ZU dZded< ded< ded< ddd	d
dd fddZd
d
d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.Tabler7   r-   r.   r/   r0   r1   c                   sR   t | rt| j| _t j|||f| d| jkrNd| jkrN| jd d krN| `d S NrI   r7   )should_set_tablenamecamel_to_snake_caser   rI   r;   r<   r8   r7   )r   r2   r3   r4   r5   r?   r   r   r<      s    zNameMetaMixin.__init__sa.Table | Noneargsr5   r	   c                 O  s   | d}|dkr|d }n| d|d  }|| jjkrFtj||S |D ]2}t|tjr`|jslt|tjrJtj||  S qJ| j	dd D ]}d|j
kr qqtj||S d| j
kr| `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   r7   rI   getr,   Ztablesr   Table
isinstanceColumnZprimary_keyZPrimaryKeyConstraint__mro__r8   rI   r   rP   r5   rR   keyargbaser   r   r   __table_cls__   s&    

 

zNameMetaMixin.__table_cls__)r   r   r   r   r)   r<   r_   rA   r   r   r?   r   rH      s   
rH   c                      s^   e Zd ZU dZded< ded< ded< edd	d
d fddZeddd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   rI   rJ   r7   zt.Type[NameMixin]rC   r0   rD   c                   sL   t | rt| j| _t jf | d| jkrHd| jkrH| jd d krH| `d S rK   )rL   rM   r   rI   r;   rF   r8   r7   )r   r5   r?   r   r   rF      s    zNameMixin.__init_subclass__r/   rN   rO   c                 O  s   | d}|dkr|d }n| d|d  }|| jjkrFtj||S |D ]2}t|tjr`|jslt|tjrJtj||  S qJ| j	dd D ]}d|j
kr qqtj||S d| j
kr| `dS rQ   rU   r[   r   r   r   r_      s&    

 

zNameMixin.__table_cls__)	r   r   r   r   r)   rG   rF   r_   rA   r   r   r?   r   r`      s   
r`   r%   bool)r   r	   c                 C  s   | j ddsPt| tjtjfs<tdd | jdd D rPtdd | jD rTdS | jD ]j}d|j krjqZt	|j d tj
r dS || kp|j ddpt	|tjpt	|tjjpt|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.
    Z__abstract__Fc                 s  s   | ]}t |tjV  qd S N)rX   sa_ormDeclarativeMeta.0br   r   r   	<genexpr>  s     z'should_set_tablename.<locals>.<genexpr>r   Nc                 s  s"   | ]}|t jkp|t jkV  qd S rb   )rc   DeclarativeBaseDeclarativeBaseNoMetare   r   r   r   rh     s   rI   T)r8   rV   
issubclassrc   ri   rj   anyrZ   	__bases__rX   Zdeclared_attrrd   Zdecl_apiZDeclarativeAttributeIntercept)r   r^   r   r   r   rL     s4    


rL   r   )r2   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)r2   r   r   r   rM   ;  s    rM   c                   @  s   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   rs   A  s   rs   c                   @  s   e Zd ZdZdS )DefaultMetaNoNamert   Nru   r   r   r   r   rv   G  s   rv   )
__future__r   ro   typingtZ
sqlalchemyr   Zsqlalchemy.ormZormrc   r   r   TYPE_CHECKING	extensionr   r   r   r%   r*   rB   rH   r`   rL   rM   rd   rs   rv   r   r   r   r   <module>   s"   + EG0