
    gb                        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ej"                        Z	 	 	 	 	 	 d
dZdd	Zy)    )annotationsN)app_ctx   )
SQLAlchemyc                  J     e Zd ZdZd fdZ	 	 	 d	 	 	 	 	 	 	 	 	 d fdZ x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``.
    c                @    t        |   di | || _        i | _        y )N )super__init___db_model_changes)selfdbkwargs	__class__s      X/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/flask_sqlalchemy/session.pyr   zSession.__init__   s"    "6"?A    c                   ||S | j                   j                  }|0	 t        j                  |      }t        |j                  |      }||S |t        ||      }||S d|v r|d   S t        | 8  d|||d|S # t        j
                  $ r6}t        |t              r t        j                  j                  |      | d}~ww xY w)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)mapperclausebindr
   )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   s	           r   r%   zSession.get_bind   s    & K((""F+ 'v'9'97CF!&vw7F!7?4= wRvf4R6RR) // fd+ **77?QF	s   A< <C1C  C)r   r   r   t.AnyreturnNone)NNN)
r   t.Any | Noner   r+   r   z.sa.engine.Engine | sa.engine.Connection | Noner   r(   r)   z'sa.engine.Engine | sa.engine.Connection)__name__
__module____qualname____doc__r   r%   __classcell__)r   s   @r   r   r      sZ    	B  $#?C	/S/S /S =	/S
 /S 
1/S /Sr   r   c                   d}| gt        | t        j                        r| }nJt        | t        j                        r0t        | j                  t        j                        r| j                  }|Sd|j
                  j                  v r;|j
                  j                  d   }||vrt        j                  d| d      ||   S y)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   keys       r   r#   r#   Q   s     Efbhh'E.:fllBHH3ULLEZ5>>+>+>>nn!!*-g..SE!GH  s|r   c                 <    t        t        j                               S )zJGet the id of the current Flask application context for the session scope.)idr   _get_current_objectr
   r   r   _app_ctx_idr=   m   s    g))+,,r   )r   zsa.ClauseElement | Noner   z't.Mapping[str | None, sa.engine.Engine]r)   zsa.engine.Engine | None)r)   int)
__future__r   typingt
sqlalchemyr   sqlalchemy.excr!   r   sqlalchemy.ormormr    flask.globalsr   TYPE_CHECKING	extensionr   r   r#   r=   r
   r   r   <module>rI      sZ    "     !??%@Sfnn @SF#4 8-r   