U
    ÓZj¥  ã                   @  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
rJd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œdd„Zdddœdd„ZG dd„ deeejjƒZdS )é    )ÚannotationsNé   )ÚQuery)Ú
SQLAlchemyc                   @  sR   e Zd ZdZejddddœdd„ƒZejddddœd	d„ƒZd
dddœdd„ZdS )Ú_QueryPropertyzRA class property that creates a query object for a model.

    :meta private:
    ÚNonezt.Type[Model]r   )ÚobjÚclsÚreturnc                 C  s   d S ©N© ©Úselfr   r	   r   r   úY/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/flask_sqlalchemy/model.pyÚ__get__   s    z_QueryProperty.__get__ÚModelc                 C  s   d S r   r   r   r   r   r   r      s    zModel | Nonec                 C  s   |j ||j ¡ d�S )N)Úsession)Úquery_classÚ__fsa__r   r   r   r   r   r      s     ÿN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtÚoverloadr   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 )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]ÚqueryÚstr)r
   c                 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   Ú__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.Anyr   ©Ú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 )Nr0   Ú	__table__Z__bind_key__)Ú__dict__Úgetattrr   Z_make_metadatar0   ÚsuperÚ__init__)r	   r5   r6   r7   r8   Zbind_keyZparent_metadatar0   ©Ú	__class__r   r   r=   X   s    zBindMetaMixin.__init__)r   r   r   r   r-   r=   Ú__classcell__r   r   r>   r   r.   M   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/   r0   r   Ú__tablename__zsa.Tabler9   r1   r2   r3   r   r4   c                   sR   t | ƒrt| jƒ| _tƒ j|||f|Ž d| jkrNd| jkrN| jd d krN| `d S )NrB   r9   )Úshould_set_tablenameÚcamel_to_snake_caser   rB   r<   r=   r:   r9   )r	   r5   r6   r7   r8   r>   r   r   r=   q   s    ÿþýzNameMetaMixin.__init__zsa.Table | None)Úargsr8   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Œ q¬qŒt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   éÿÿÿÿr9   rB   )Úgetr0   Ztablesr!   ÚTableÚ
isinstanceÚColumnZprimary_keyZPrimaryKeyConstraintÚ__mro__r:   rB   )r	   rE   r8   rF   ÚkeyÚargÚbaser   r   r   Ú__table_cls__�   s&    

 ÿ

zNameMetaMixin.__table_cls__)r   r   r   r   r-   r=   rQ   r@   r   r   r>   r   rA   f   s   
rA   r)   Úbool)r	   r
   c                 C  sŽ   | j  dd¡s*tdd„ | jdd… D ƒƒs.dS | jD ]T}d|j krDq4t|j d tjjƒr^ dS || kp‚|j  dd¡p‚t|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   )rK   r!   ÚormÚDeclarativeMeta)Ú.0Úbr   r   r   Ú	<genexpr>·   s    z'should_set_tablename.<locals>.<genexpr>r   NrB   T)	r:   rI   ÚanyrM   rK   r!   rS   Zdeclared_attrrT   )r	   rP   r   r   r   rC   «   s    ÿ

ÿýrC   r   )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   rD   Ì   s    rD   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   r^   Ò   s   r^   )Ú
__future__r   rZ   Útypingr   Z
sqlalchemyr!   Zsqlalchemy.ormr   r   ÚTYPE_CHECKINGÚ	extensionr   r   r   r)   r.   rA   rC   rD   rS   rT   r^   r   r   r   r   Ú<module>   s   *E!