o
    XJ@hb                     @  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___dbZ_model_changes)selfr   r   	__class__r   K/var/www/html/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_excZNoInspectionAvailable
isinstancetypesa_ormexcZUnmappedClassError_clause_to_engineZlocal_tabler   get_bind)r   r   r   r   r   r   eZ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.
    NZbind_keyz
Bind key 'z&' is not in 'SQLALCHEMY_BINDS' config.)	r   r   TableZ
UpdateBasetablemetadatainfor   ZUnboundExecutionError)r   r   r.   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   Z_get_current_objectr   r   r   r   _app_ctx_idm   s   r4   )r   r*   r   r+   r
   r,   )r
   r2   )
__future__r   typingtZ
sqlalchemyr   Zsqlalchemy.excr!   r   Zsqlalchemy.ormZormr    Zflask.globalsr   TYPE_CHECKING	extensionr   r   r"   r4   r   r   r   r   <module>   s    
C