
    ]h                        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      Z G d d	      Z G d
 de      Z G d de      ZddZddZ G d deee	j,                        Zy)    )annotationsN   )Query)
SQLAlchemyc                  d    e Zd ZdZej
                  dd       Zej
                  dd       ZddZy)	_QueryPropertyzRA class property that creates a query object for a model.

    :meta private:
    c                     y N selfobjclss      I/var/www/html/venv/lib/python3.12/site-packages/flask_sqlalchemy/model.py__get__z_QueryProperty.__get__           c                     y r
   r   r   s      r   r   z_QueryProperty.__get__   r   r   c                X    |j                  ||j                  j                               S )N)session)query_class__fsa__r   r   s      r   r   z_QueryProperty.__get__   s*    ,,.  
 	
r   N)r   Noner   type[Model]returnr   )r   Modelr   r   r   r   )r   zModel | Noner   r   r   r   )__name__
__module____qualname____doc__toverloadr   r   r   r   r   r      s<    
 ZZ  ZZ 
r   r   c                  P    e Zd ZU dZded<   	 eZded<   	  e       Zded<   	 d
dZ	y	)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[type[Query]]r   zt.ClassVar[Query]queryc                .   t        j                  |       }|J |j                  rdt        |        d}nE|j                  rdt        |        d}n)dj                  t        t        |j                              }dt        |       j                   d| dS )Nz(transient )z	(pending z, < >)sainspect	transientidpendingjoinmapstridentitytyper   )r   statepks      r   __repr__zModel.__repr__@   s    

4    ??r$xj*B]]RXJa(B3sENN34B4:&&'qA..r   N)r   r1   )
r   r   r   r    __annotations__r   r   r   r$   r6   r   r   r   r   r   #   s@     $#
 ,1K(0  ./E//r   r   c                  N     e Zd ZU dZded<   ded<   	 	 	 	 	 	 	 	 	 	 d fdZ x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metadatac                    d| j                   v sNd| j                   v s@t        | dd       }t        | dd       }| j                  j                  |      }||ur|| _        t        |   |||fi | y )Nr;   	__table____bind_key__)__dict__getattrr   _make_metadatar;   super__init__)	r   namebasesdkwargsbind_keyparent_metadatar;   	__class__s	           r   rC   zBindMetaMixin.__init__Y   sq     cll*kS\\.IsND9H%c:t<O{{11(;H.'ua262r   
rD   r1   rE   ztuple[type, ...]rF   zdict[str, t.Any]rG   t.Anyr   r   )r   r   r   r    r7   rC   __classcell__rJ   s   @r   r9   r9   N   sH     33/34D3PU3	3 3r   r9   c                  `     e Zd ZU dZded<   ded<   ded<   	 	 	 	 	 	 	 	 	 	 d
 fdZdd	Z x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;   r1   __tablename__zsa.Tabler=   c                    t        |       rt        | j                        | _        t	        |   |||fi | d| j                  vr"d| j                  v r| j                  d   | `y y y y )NrQ   r=   )should_set_tablenamecamel_to_snake_caser   rQ   rB   rC   r?   r=   )r   rD   rE   rF   rG   rJ   s        r   rC   zNameMetaMixin.__init__r   sq      $ 3CLL ACua262 3<</s||+[)1 2 , 0r   c                   |j                  d      }||d   }n
| d|d    }|| j                  j                  v rt        j                  |i |S |D ]Y  }t        |t        j                        r|j                  st        |t        j                        sDt        j                  |i |c S  | j                  dd D ]  }d|j                  v s n t        j                  |i |S d| j                  v r| `y)	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=   rQ   )getr;   tablesr*   Table
isinstanceColumnprimary_keyPrimaryKeyConstraint__mro__r?   rQ   )r   argsrG   rV   keyargbases          r   __table_cls__zNameMetaMixin.__table_cls__   s    H%>q'CHAd1gY'C #,,%%%88T,V,,  	1C3		*s:R,,D xx000		1 KK"% 	-Ddmm+	- 88T,V,, cll*!r   rK   )ra   rL   rG   rL   r   zsa.Table | None)r   r   r   r    r7   rC   re   rM   rN   s   @r   rP   rP   g   sN     /4DPU	 'r   rP   c                   | j                   j                  dd      st        d | j                  dd D              sy| j                  D ]{  }d|j                   vrt	        |j                   d   t
        j                        r y|| u xs9 |j                   j                  dd      xs t	        |t
        j                          c S  y)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              3  P   K   | ]  }t        |t        j                           y wr
   )r\   sa_ormDeclarativeMeta).0bs     r   	<genexpr>z'should_set_tablename.<locals>.<genexpr>   s"      :23
1f,,-:s   $&r   NrQ   T)r?   rY   anyr`   r\   ri   declared_attrrj   )r   rd   s     r   rS   rS      s     ||.c :7:{{12: 7  
$--/dmmO4f6J6JK CK <}}  7<dF$:$:;;
 	

 r   c                n    t        j                  dd|       } | j                         j                  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)rD   s    r   rT   rT      s,    66=vtLD::<s##r   c                      e Zd ZdZy)DefaultMetazgSQLAlchemy declarative metaclass that provides ``__bind_key__`` and
    ``__tablename__`` support.
    N)r   r   r   r    r   r   r   rw   rw      s    r   rw   )r   r3   r   bool)rD   r1   r   r1   )
__future__r   rr   typingr!   
sqlalchemyr*   sqlalchemy.ormormri   r$   r   TYPE_CHECKING	extensionr   r   r   r3   r9   rP   rS   rT   rj   rw   r   r   r   <module>r      sr    " 	    ??%
 
((/ (/V3D 32BD BJB$-0F0F r   