o
    Rc@
                     @  sl   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
r%ddlmZ G dd dejj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   :D:\Flask\env\Lib\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 |dur1zt |}W n t jjy- } zt|tr(t jj|| d}~ww |j}| j	j
}t|t jrYd|jjv rY|jjd }||vrUt jd| d|| S d|v ra|d S t jd|||d|S )az  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
            The implementation more closely matches the base SQLAlchemy implementation.

        .. versionchanged:: 2.1
            Support joining an external transaction.
        NZbind_keyz
Bind key 'z&' is not in 'SQLALCHEMY_BINDS' config.)r   r   r   r   )sainspectexcZNoInspectionAvailable
isinstancetypeormZUnmappedClassErrorZpersist_selectabler   enginesTablemetadatainfoZUnboundExecutionErrorr   get_bind)r   r   r   r   r   er    keyr   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
   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_idO   s   r.   )r
   r,   )
__future__r   typingtZ
sqlalchemyr   Zsqlalchemy.excZsqlalchemy.ormZflask.globalsr   TYPE_CHECKING	extensionr   r   r   r.   r   r   r   r   <module>   s    A