oz > d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm Z dd lm Z dd lmZ ddl mZ ddl mZ dd l mZ ddl mZ ddl mZ ddl mZ ddl mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ g dZ! ej" Z# d Z$ G d de% Z& ej' d Z( ej' d Z) ej' d Z* ej' d! Z+ ej' d" Z, G d# d$e% Z- G d% d&e& Z. G d' d(e& Z/d) Z0d* Z1d+ Z2d, Z3 ej4 Z5dS )-z1Provides the Session class and related utilities. N ) attributes)exc)identity)loading)persistence)querystate)_class_to_mapper) _none_set) _state_mapper)instance_str) object_mapper)object_state) state_str)SessionExtension)UOWTransaction )engine)sql)util)inspect) expression)SessionSessionTransactionr sessionmakerc Z | j r# t | j S # t $ r Y nw xY wdS )z\Given an :class:`.InstanceState`, return the :class:`.Session` associated, if any. N) session_id _sessionsKeyErrorr s j/builddir/build/BUILD/cloudlinux-venv-1.0.10/venv/lib64/python3.11/site-packages/sqlalchemy/orm/session.py_state_sessionr# - sG U-.. D 4s ((c e Zd ZdZe ej dd d Ze ej d d Z ed Z dS ) _SessionClassMethodszBClass-level methods for :class:`.Session`, :class:`.sessionmaker`.z1.3zThe :meth:`.Session.close_all` method is deprecated and will be removed in a future release. Please refer to :func:`.session.close_all_sessions`.c " t dS )zClose *all* sessions in memory.N)close_all_sessions)clss r" close_allz_SessionClassMethods.close_all= s zsqlalchemy.orm.utilc |j |i |S )zZReturn an identity key. This is an alias of :func:`.util.identity_key`. )identity_key)r( orm_utilargskwargss r" r, z!_SessionClassMethods.identity_keyI s %x$d5f555r* c t | S )zxReturn the :class:`.Session` to which an object belongs. This is an alias of :func:`.object_session`. )object_session)r( instances r" r1 z#_SessionClassMethods.object_sessionS s h'''r* N)__name__ __module____qualname____doc__classmethodr deprecatedr) dependenciesr, r1 r* r" r% r% : s LLT_ / [ T,--6 6 .- [6 ( ( [( ( (r* r% ACTIVEPREPARED COMMITTEDDEACTIVECLOSEDc e Zd ZdZdZddZed ZdZ ed Z ddZ ed Zdd ZddZ ddZd ZddZd Zd Zd Zd Zd ZddZddZd Zd ZdS )r a A :class:`.Session`-level transaction. :class:`.SessionTransaction` is a mostly behind-the-scenes object not normally referenced directly by application code. It coordinates among multiple :class:`_engine.Connection` objects, maintaining a database transaction for each one individually, committing or rolling them back all at once. It also provides optional two-phase commit behavior which can augment this coordination operation. The :attr:`.Session.transaction` attribute of :class:`.Session` refers to the current :class:`.SessionTransaction` object in use, if any. The :attr:`.SessionTransaction.parent` attribute refers to the parent :class:`.SessionTransaction` in the stack of :class:`.SessionTransaction` objects. If this attribute is ``None``, then this is the top of the stack. If non-``None``, then this :class:`.SessionTransaction` refers either to a so-called "subtransaction" or a "nested" transaction. A "subtransaction" is a scoping concept that demarcates an inner portion of the outermost "real" transaction. A nested transaction, which is indicated when the :attr:`.SessionTransaction.nested` attribute is also True, indicates that this :class:`.SessionTransaction` corresponds to a SAVEPOINT. **Life Cycle** A :class:`.SessionTransaction` is associated with a :class:`.Session` in its default mode of ``autocommit=False`` immediately, associated with no database connections. As the :class:`.Session` is called upon to emit SQL on behalf of various :class:`_engine.Engine` or :class:`_engine.Connection` objects, a corresponding :class:`_engine.Connection` and associated :class:`.Transaction` is added to a collection within the :class:`.SessionTransaction` object, becoming one of the connection/transaction pairs maintained by the :class:`.SessionTransaction`. The start of a :class:`.SessionTransaction` can be tracked using the :meth:`.SessionEvents.after_transaction_create` event. The lifespan of the :class:`.SessionTransaction` ends when the :meth:`.Session.commit`, :meth:`.Session.rollback` or :meth:`.Session.close` methods are called. At this point, the :class:`.SessionTransaction` removes its association with its parent :class:`.Session`. A :class:`.Session` that is in ``autocommit=False`` mode will create a new :class:`.SessionTransaction` to replace it immediately, whereas a :class:`.Session` that's in ``autocommit=True`` mode will remain without a :class:`.SessionTransaction` until the :meth:`.Session.begin` method is called. The end of a :class:`.SessionTransaction` can be tracked using the :meth:`.SessionEvents.after_transaction_end` event. **Nesting and Subtransactions** Another detail of :class:`.SessionTransaction` behavior is that it is capable of "nesting". This means that the :meth:`.Session.begin` method can be called while an existing :class:`.SessionTransaction` is already present, producing a new :class:`.SessionTransaction` that temporarily replaces the parent :class:`.SessionTransaction`. When a :class:`.SessionTransaction` is produced as nested, it assigns itself to the :attr:`.Session.transaction` attribute, and it additionally will assign the previous :class:`.SessionTransaction` to its :attr:`.Session.parent` attribute. The behavior is effectively a stack, where :attr:`.Session.transaction` refers to the current head of the stack, and the :attr:`.SessionTransaction.parent` attribute allows traversal up the stack until :attr:`.SessionTransaction.parent` is ``None``, indicating the top of the stack. When the scope of :class:`.SessionTransaction` is ended via :meth:`.Session.commit` or :meth:`.Session.rollback`, it restores its parent :class:`.SessionTransaction` back onto the :attr:`.Session.transaction` attribute. The purpose of this stack is to allow nesting of :meth:`.Session.rollback` or :meth:`.Session.commit` calls in context with various flavors of :meth:`.Session.begin`. This nesting behavior applies to when :meth:`.Session.begin_nested` is used to emit a SAVEPOINT transaction, and is also used to produce a so-called "subtransaction" which allows a block of code to use a begin/rollback/commit sequence regardless of whether or not its enclosing code block has begun a transaction. The :meth:`.flush` method, whether called explicitly or via autoflush, is the primary consumer of the "subtransaction" feature, in that it wishes to guarantee that it works within in a transaction block regardless of whether or not the :class:`.Session` is in transactional mode when the method is called. Note that the flush process that occurs within the "autoflush" feature as well as when the :meth:`.Session.flush` method is used **always** creates a :class:`.SessionTransaction` object. This object is normally a subtransaction, unless the :class:`.Session` is in autocommit mode and no transaction exists at all, in which case it's the outermost transaction. Any event-handling logic or other inspection logic needs to take into account whether a :class:`.SessionTransaction` is the outermost transaction, a subtransaction, or a "nested" / SAVEPOINT transaction. .. seealso:: :meth:`.Session.rollback` :meth:`.Session.commit` :meth:`.Session.begin` :meth:`.Session.begin_nested` :attr:`.Session.is_active` :meth:`.SessionEvents.after_transaction_create` :meth:`.SessionEvents.after_transaction_end` :meth:`.SessionEvents.after_commit` :meth:`.SessionEvents.after_rollback` :meth:`.SessionEvents.after_soft_rollback` NFc || _ i | _ || _ || _ t | _ |s|rt j d | j j r| | j j | j | d S )NzOCan't start a SAVEPOINT transaction when no existing transaction is in progress)session_connections_parentnestedr; _statesa_excInvalidRequestError_enable_transaction_accounting_take_snapshotdispatchafter_transaction_create)selfrB parentrE s r" __init__zSessionTransaction.__init__ s &