o
    Rc                     @  s   d dl mZ d dlZd dlZd dlZd dl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eZdddZdddZG dd deeejjZdS )    )annotationsN   )Query)
SQLAlchemyc                   @  s:   e Zd ZdZejddd	Zejddd	Zddd	ZdS )_QueryPropertyzRA class property that creates a query object for a model.

    :meta private:
    objNoneclst.Type[Model]returnr   c                 C     d S N selfr   r	   r   r   8D:\Flask\env\Lib\site-packages\flask_sqlalchemy/model.py__get__      z_QueryProperty.__get__Modelc                 C  r   r   r   r   r   r   r   r      r   Model | Nonec                 C  s   |j ||j dS )N)session)query_class__fsa__r   r   r   r   r   r      s   
N)r   r   r	   r
   r   r   )r   r   r	   r
   r   r   )r   r   r	   r
   r   r   )__name__
__module____qualname____doc__toverloadr   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 )r   a  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[t.Type[Query]]r   zt.ClassVar[Query]queryr   strc                 C  sd   t | }|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__@   s   
zModel.__repr__N)r   r    )
r   r   r   r   __annotations__r   r   r   r   r0   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   r   c                   sb   d| j v s#d| j v s#t| dd }t| dd }| j|}||ur#|| _t j|||fi | d S )Nr4   	__table__Z__bind_key__)__dict__getattrr   Z_make_metadatar4   super__init__)r	   r5   r6   r8   r:   Zbind_keyZparent_metadatar4   	__class__r   r   r@   X   s   zBindMetaMixin.__init__
r5   r    r6   r7   r8   r9   r:   r;   r   r   )r   r   r   r   r1   r@   __classcell__r   r   rA   r   r2   M   s
   
 r2   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.
    r3   r4   r    __tablename__zsa.Tabler<   r5   r6   r7   r8   r9   r:   r;   r   r   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 )NrF   r<   )should_set_tablenamecamel_to_snake_caser   rF   r?   r@   r=   r<   )r	   r5   r6   r8   r:   rA   r   r   r@   q   s   zNameMetaMixin.__init__argssa.Table | Nonec                 O  s   | 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   r<   rF   )getr4   Ztablesr%   Table
isinstanceColumnZprimary_keyZPrimaryKeyConstraint__mro__r=   rF   )r	   rI   r:   rK   keyargbaser   r   r   __table_cls__   s(   



zNameMetaMixin.__table_cls__rC   )rI   r;   r:   r;   r   rJ   )r   r   r   r   r1   r@   rV   rD   r   r   rA   r   rE   f   s   
 rE   r	   r-   r   boolc                 C  s   | j ddstdd | jdd D sdS | jD ]*}d|j vr"qt|j d tjjr/ dS || u pA|j ddpAt|tj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jV  qd S r   )rP   r%   ormDeclarativeMeta).0br   r   r   	<genexpr>   s    
z'should_set_tablename.<locals>.<genexpr>r   NrF   T)	r=   rN   anyrR   rP   r%   rX   Zdeclared_attrrY   )r	   rU   r   r   r   rG      s   

rG   r5   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)r5   r   r   r   rH      s   rH   c                   @  s   e Zd ZdZdS )DefaultMetazgSQLAlchemy declarative metaclass that provides ``__bind_key__`` and
    ``__tablename__`` support.
    N)r   r   r   r   r   r   r   r   rc      s    rc   )r	   r-   r   rW   )r5   r    r   r    )
__future__r   r_   typingr   Z
sqlalchemyr%   Zsqlalchemy.ormr   r   TYPE_CHECKING	extensionr   r   r   r-   r2   rE   rG   rH   rX   rY   rc   r   r   r   r   <module>   s    *
E
!