U
    cc@
                     @  sp   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
rJddlmZ G dd dejjZdd	d
dZdS )    )annotationsN)app_ctx   )
SQLAlchemyc                      sF   e Zd ZdZdddd fddZdd	d	d
d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``.
    r   zt.AnyNone)dbkwargsreturnc                   s   t  jf | || _i | _d S )N)super__init___dbZ_model_changes)selfr   r	   	__class__ </tmp/pip-unpacked-wheel-q5ixi8hf/flask_sqlalchemy/session.pyr      s    zSession.__init__Nzt.Any | Nonez.sa.engine.Engine | sa.engine.Connection | Nonez'sa.engine.Engine | sa.engine.Connection)mapperclausebindr	   r
   c              
     s   |dk	r|S |dk	rlzt |}W nB t jjk
rd } z t|trRt jj|| W 5 d}~X Y nX |j}| j	j
}t|t jrd|jjkr|jjd }||krt jd| d|| S d|kr|d S t jf |||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   )sainspectexcZNoInspectionAvailable
isinstancetypeormZUnmappedClassErrorZpersist_selectabler   enginesZTablemetadatainfoZUnboundExecutionErrorr   get_bind)r   r   r   r   r	   er   keyr   r   r   r      s*    

zSession.get_bind)NNN)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s      r   int)r
   c                   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)   )
__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