o
    i                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ ddl	m
Z
 dd Ze d	Zd
d ZG dd deZG dd deZG dd deeeZG dd deZdS )    N)inspect)DeclarativeMetadeclared_attr)_get_table_key   to_strc                 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r- dS || u p=|j ddp=t|t    S dS )	a@  Determine whether ``__tablename__`` should be automatically 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, :meth:`._BoundDeclarativeMeta.__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                 s   s    | ]}t |tV  qd S N)
isinstancer   ).0b r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/flask_sqlalchemy/model.py	<genexpr>   s    z'should_set_tablename.<locals>.<genexpr>r   N__tablename__T)__dict__getany__mro__r   r   r   )clsbaser   r   r   should_set_tablename   s    


r   z([A-Z]+)(?=[a-z0-9])c                 C   s   dd }t || dS )Nc                 S   s<   |   }t|dkrd|d d |d f  S d|  S )Nr   z_%s_%s_)grouplenlower)matchwordr   r   r   _join3   s   z"camel_to_snake_case.<locals>._joinr   )camelcase_resublstrip)namer    r   r   r   camel_to_snake_case2   s   r%   c                       s$   e Zd Z fddZdd Z  ZS )NameMetaMixinc                    s^   t | r
t| j| _tt| ||| d| jvr)d| jv r+| jd d u r-| `d S d S d S d S )Nr   	__table__)	r   r%   __name__r   superr&   __init__r   r'   )r   r$   basesd	__class__r   r   r*   ?   s   

zNameMetaMixin.__init__c                 O   s   t |d |d}|| jjv rtj|i |S |D ]}t|tjr%|js+t|tj	r5tj|i |  S q| j
dd D ]	}d|jv rF n	q=tj|i |S d| jv rX| `dS 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.
        r   schemar   r   r'   r   N)r   r   metadatatablessaTabler   Columnprimary_keyPrimaryKeyConstraintr   r   r   )r   argskwargskeyargr   r   r   r   __table_cls__N   s(   	



zNameMetaMixin.__table_cls__)r(   
__module____qualname__r*   r;   __classcell__r   r   r-   r   r&   >   s    r&   c                       s   e Zd Z fddZ  ZS )BindMetaMixinc                    s\   | dd pt| dd }tt| ||| |d ur*t| dd d ur,|| jjd< d S d S d S )N__bind_key__r'   bind_key)popgetattrr)   r?   r*   r'   info)r   r$   r+   r,   rA   r-   r   r   r*   s   s   
zBindMetaMixin.__init__)r(   r<   r=   r*   r>   r   r   r-   r   r?   r   s    r?   c                   @   s   e Zd ZdS )DefaultMetaN)r(   r<   r=   r   r   r   r   rE      s    rE   c                   @   s    e Zd ZdZdZdZdd ZdS )ModelzBase class for SQLAlchemy declarative base model.

    To define models, subclass :attr:`db.Model <SQLAlchemy.Model>`, not this
    class. To customize ``db.Model``, subclass this and pass it as
    ``model_class`` to :class:`SQLAlchemy`.
    Nc                 C   sH   t | j}|d u rdt| }n
ddd |D }dt| j|S )Nz(transient {0})z, c                 s   s    | ]}t |V  qd S r
   r   )r   valuer   r   r   r      s    z!Model.__repr__.<locals>.<genexpr>z	<{0} {1}>)r   identityformatidjointyper(   )selfrH   pkr   r   r   __repr__   s
   
zModel.__repr__)r(   r<   r=   __doc__query_classqueryrO   r   r   r   r   rF      s
    	rF   )re
sqlalchemyr2   r   sqlalchemy.ext.declarativer   r   sqlalchemy.schemar   _compatr   r   compiler!   r%   objectr&   r?   rE   rF   r   r   r   r   <module>   s    
$4