
    [hb                        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dS )    )annotationsN)app_ctx   )
SQLAlchemyc                  8     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``.
    dbr   kwargst.AnyreturnNonec                V     t                      j        di | || _        i | _        d S )N )super__init___db_model_changes)selfr	   r
   	__class__s      g/var/www/api.easyaligner.net/htdocs/venv_linux/lib/python3.11/site-packages/flask_sqlalchemy/session.pyr   zSession.__init__   s5    ""6"""?A    Nmappert.Any | Noneclausebind.sa.engine.Engine | sa.engine.Connection | None'sa.engine.Engine | sa.engine.Connectionc                   ||S | j         j        }||	 t          j        |          }nM# t          j        $ r;}t          |t                    r t          j	        
                    |          | d}~ww xY wt          |j        |          }||S |t          ||          }||S d|v r|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   s	           r   r+   zSession.get_bind   s    & K("F++/   fd++ G *77??QF	 'v'97CCF!&vw77F!7??4= uwwRvf4RR6RRRs   * A46A//A4)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   s   @r   r   r      s        	 	B B B B B B  $#?C	/S /S /S /S /S /S /S /S /S /S /Sr   r   r   sa.ClauseElement | Noner   't.Mapping[str | None, sa.engine.Engine]r   sa.engine.Engine | Nonec                T   d}| ]t          | t          j                  r| }n@t          | t          j                  r&t          | j        t          j                  r| j        }|Dd|j        j        v r6|j        j        d         }||vrt          j        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   r:   keys       r   r)   r)   Q   s     Efbh'' 	!EE.. 	!:flBH3U3U 	!LEZ5>+>>>n!*-g.HSHHH   s|4r   intc                 B    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_idrC   m   s    g)++,,,r   )r   r3   r   r4   r   r5   )r   r?   )
__future__r   typingt
sqlalchemyr    sqlalchemy.excr'   r"   sqlalchemy.ormormr&   flask.globalsr   TYPE_CHECKING	extensionr   r   r)   rC   r   r   r   <module>rN      s    " " " " " "                     ! ! ! ! ! !? &%%%%%%@S @S @S @S @Sfn @S @S @SF   8- - - - - -r   