a
    9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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   s/var/www/vosh/data/www/fastworkle.ru/webshop-tgbot-v.1.0/venv/lib/python3.9/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 usJ |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inspectZ	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                   sb   d| j v sFd| j v sFt| dd }t| dd }| j|}||urF|| _t j|||fi | d S Nr*   	__table__Z__bind_key__)__dict__getattrr   _make_metadatar*   super__init__)r   r0   r1   r2   r3   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]r.   r   r3   r	   c                   sf   d| j v sPd| j v sPt| drPt| dd }t| dd }| j|}||urP|| _t jf i | d S r4   )r6   hasattrr7   r   r8   r*   r9   __init_subclass__)r   r3   r;   r<   r*   r=   r   r   rD   p   s    zBindMixin.__init_subclass__)r   r   r   r   r'   classmethodrD   r?   r   r   r=   r   r@   _   s
   
r@   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.Tabler5   r+   r,   r-   r.   r/   c                   sV   t | rt| j| _t j|||fi | d| jvrRd| jv rR| jd d u rR| `d S NrG   r5   )should_set_tablenamecamel_to_snake_caser   rG   r9   r:   r6   r5   )r   r0   r1   r2   r3   r=   r   r   r:      s    zNameMetaMixin.__init__sa.Table | Noneargsr3   r	   c                 O  s   | d}|du r|d }n| d|d  }|| jjv rJtj|i |S |D ]6}t|tjrd|jspt|tjrNtj|i |  S qN| j	dd D ]}d|j
v r qqtj|i |S d| j
v 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   r5   rG   getr*   Ztablesr   ZTable
isinstanceZColumnZprimary_keyZPrimaryKeyConstraint__mro__r6   rG   r   rN   r3   rP   keyargbaser   r   r   __table_cls__   s$    



zNameMetaMixin.__table_cls__)r   r   r   r   r'   r:   r[   r?   r   r   r=   r   rF      s   
rF   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   rG   rH   r5   zt.Type[NameMixin]rA   r.   rB   c                   sP   t | rt| j| _t jf i | d| jvrLd| jv rL| jd d u rL| `d S rI   )rJ   rK   r   rG   r9   rD   r6   r5   )r   r3   r=   r   r   rD      s    zNameMixin.__init_subclass__r-   rL   rM   c                 O  s   | d}|du r|d }n| d|d  }|| jjv rJtj|i |S |D ]6}t|tjrd|jspt|tjrNtj|i |  S qN| j	dd D ]}d|j
v r qqtj|i |S d| j
v r| `dS rO   rS   rW   r   r   r   r[      s$    



zNameMixin.__table_cls__)	r   r   r   r   r'   rE   rD   r[   r?   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 vrjqZt	|j d tj
r dS || u 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)rU   sa_ormDeclarativeMeta.0br   r   r   	<genexpr>      z'should_set_tablename.<locals>.<genexpr>r   Nc                 s  s"   | ]}|t ju p|t ju V  qd S r^   )r_   DeclarativeBaseDeclarativeBaseNoMetara   r   r   r   rd     s   rG   T)r6   rT   
issubclassr_   rf   rg   anyrV   	__bases__rU   Zdeclared_attrr`   Zdecl_apiZDeclarativeAttributeIntercept)r   rZ   r   r   r   rJ     s4    


rJ   r   )r0   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)r0   r   r   r   rK   ;  s    rK   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   rp   A  s   rp   c                   @  s   e Zd ZdZdS )DefaultMetaNoNamerq   Nrr   r   r   r   r   rs   G  s   rs   )
__future__r   rl   typingtZ
sqlalchemyr   Zsqlalchemy.ormZormr_   r   r   TYPE_CHECKING	extensionr   r   r   r#   r(   r@   rF   r\   rJ   rK   r`   rp   rs   r   r   r   r   <module>   s"   + EG0