a
    `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rRddlmZ G dd dejZdd	d
ddd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 | || _i | _d S )N)super__init___dbZ_model_changes)selfr   r	   	__class__ f/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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ur|S | j j}|durzt|}W n@ tjyj } z&t|trTtj	
|| W Y d}~n
d}~0 0 t|j|}|dur|S |durt||}|dur|S d|v r|d S t jf |||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   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)NNN)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s      r   zsa.ClauseElement | Nonez't.Mapping[str | None, sa.engine.Engine]zsa.engine.Engine | None)r   r   r
   c                 C  s   d}| dur>t | tjr| }n t | tjr>t | jtjr>| j}|durd|jjv r|jjd }||vrxt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   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_idm   s    r-   )
__future__r   typingtZ
sqlalchemyr   Zsqlalchemy.excr   r   Zsqlalchemy.ormZormr   Zflask.globalsr   TYPE_CHECKING	extensionr   r   r   r-   r   r   r   r   <module>   s   C