o
    ‹ågb  ã                   @  s|   d dl mZ d dlZd dlZd dlmZ d dl	m
Z d dlmZ ejr)ddlmZ G dd„ dejƒZddd„Zddd„ZdS )é    )ÚannotationsN)Úapp_ctxé   )Ú
SQLAlchemyc                      s8   e Zd ZdZd‡ fdd	„Z	
	
	
dd‡ fdd„Z‡  ZS )ÚSessiona�  A SQLAlchemy :class:`~sqlalchemy.orm.Session` class that chooses what engine to
    use based on the bind key associated with the metadata associated with the thing
    being queried.

    To customize ``db.session``, subclass this and pass it as the ``class_`` key in the
    ``session_options`` to :class:`.SQLAlchemy`.

    .. versionchanged:: 3.0
        Renamed from ``SignallingSession``.
    Údbr   Úkwargsút.AnyÚreturnÚNonec                   s"   t ƒ jdi |¤Ž || _i | _d S )N© )ÚsuperÚ__init__Ú_dbÚ_model_changes)Úselfr   r   ©Ú	__class__r   úT/var/www/html/flaskapp/venv/lib/python3.10/site-packages/flask_sqlalchemy/session.pyr      s   
zSession.__init__NÚmapperút.Any | NoneÚclauseÚbindú.sa.engine.Engine | sa.engine.Connection | Noneú'sa.engine.Engine | sa.engine.Connectionc              
     sÀ   |dur|S | j j}|dur<zt |¡}W n tjy/ } zt|tƒr*tj	 
|¡|‚‚ d}~ww t|j|ƒ}|dur<|S |durKt||ƒ}|durK|S d|v rS|d S tƒ jd|||dœ|¤ŽS )aÞ  Select an engine based on the ``bind_key`` of the metadata associated with
        the model or table being queried. If no bind key is set, uses the default bind.

        .. versionchanged:: 3.0.3
            Fix finding the bind for a joined inheritance model.

        .. versionchanged:: 3.0
            The implementation more closely matches the base SQLAlchemy implementation.

        .. versionchanged:: 2.1
            Support joining an external transaction.
        N)r   r   r   r   )r   ÚenginesÚsaÚinspectÚsa_excÚNoInspectionAvailableÚ
isinstanceÚtypeÚsa_ormÚexcÚUnmappedClassErrorÚ_clause_to_engineÚlocal_tabler   Úget_bind)r   r   r   r   r   r   ÚeÚenginer   r   r   r'      s,   
€ü
zSession.get_bind)r   r   r   r	   r
   r   )NNN)
r   r   r   r   r   r   r   r	   r
   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   Ú__classcell__r   r   r   r   r      s    ür   r   úsa.ClauseElement | Noner   ú't.Mapping[str | None, sa.engine.Engine]r
   úsa.engine.Engine | Nonec                 C  s„   d}| durt | tjƒr| }nt | tjƒrt | jtjƒr| j}|dur@d|jjv r@|jjd }||vr<t d|› d�¡‚|| S dS )zeIf the clause is a table, return the engine associated with the table's
    metadata's bind key.
    NÚbind_keyz
Bind key 'z&' is not in 'SQLALCHEMY_BINDS' config.)	r    r   ÚTableÚ
UpdateBaseÚtableÚmetadataÚinfor   ÚUnboundExecutionError)r   r   r5   Úkeyr   r   r   r%   Q   s   
ÿr%   Úintc                   C  s   t t ¡ ƒS )zJGet the id of the current Flask application context for the session scope.)Úidr   Ú_get_current_objectr   r   r   r   Ú_app_ctx_idm   s   r=   )r   r/   r   r0   r
   r1   )r
   r:   )Ú
__future__r   ÚtypingÚtÚ
sqlalchemyr   Úsqlalchemy.excr#   r   Úsqlalchemy.ormÚormr"   Úflask.globalsr   ÚTYPE_CHECKINGÚ	extensionr   r   r%   r=   r   r   r   r   Ú<module>   s    
C