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   Z/var/www/html/ecg_monitoring/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