U kfN@sUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"dd l m#Z#dd!l mZdd"l$m%Z%dd!l$mZ&ej'rdd#l m(Z(dd$l m)Z)dd%lm*Z*dd&lm+Z+dd'lm,Z,dd(lm-Z-dd)lm.Z.dd*lm/Z/dd+lm0Z0dd,lm1Z1dd-lm2Z2dd.lm3Z3dd/lm4Z4dd0lm5Z5dd1l6m7Z7dd2l8m9Z9dd3l:m;Z;dd4l#mZ>dd6l=m?Z?dd7l=m@Z@dd8l$mAZAdd9lBmCZCdd:lDmEZEdd;lFmGZGddlJmKZKdd?lLmMZMdd@lLmNZNddAlLmOZOddBlPmQZQedCedDZRejSZTdEeUdF<ejSZVdGeUdH<GdIdJdJee"jWdKZXGdLdMdMeZYGdNdOdOeZZGdPdQdQeZZ[GdRdSdSeZZ\GdTdUdUe[Z]GdVdWdWee#j^e"jWdKZ_GdXdYdYe#j^Z`GdZd[d[e`e_Zaeae__bdS)\zBDefines :class:`_engine.Connection` and :class:`_engine.Engine`. ) annotationsN)Any)Callable)cast)Iterable)Iterator)List)Mapping)NoReturn)Optional)overload)Tuple)Type)TypeVar)Union) BindTyping)ConnectionEventsTarget) DBAPICursor)ExceptionContext) ExecuteStyle)ExecutionContext)IsolationLevel)_distill_params_20)_distill_raw_params)TransactionalContext)exc) inspection)log)util)compiler) CursorResult) ScalarResult)_AnyExecuteParams)_AnyMultiExecuteParams)_CoreAnyExecuteParams)_CoreMultiExecuteParams)_CoreSingleExecuteParams)_DBAPIAnyExecuteParams)_DBAPISingleExecuteParams)_ExecuteOptions)CompiledCacheType)CoreExecuteOptionsParameter)Dialect)SchemaTranslateMapType) Inspector)URL) dispatcher) _EchoFlagType)_ConnectionFairy)Pool)PoolProxiedConnection) Executable) _InfoType)Compiled)ExecutableDDLElement) SchemaDropper)SchemaGenerator)FunctionElement)DefaultGenerator) HasSchemaAttr) SchemaItem)TypedReturnsRows_T)boundr+_EMPTY_EXECUTION_OPTSzMapping[str, Any] NO_OPTIONSc@s&eZdZUdZded<ded<dZdZded <d Zd ed <d ed<ded<ded<ddd ddddddZdZ ded<ddddddd Z dddddd!d"Z e d#d$d%d&Z d'd(d)d*d+Zdd$d,d-Zddddd.d/d0Zed1d1d1d d d1d1d1d1d d2 d3dd4ddd5d5d5d#dddd6 d7d8Zeddd9d:d8Zddd9d;d8Zd d$dd?Ze dd$d@dAZe dd$dBdCZe dDd$dEdFZd4d$dGdHZe dId$dJdKZdLd$dMdNZdDd$dOdPZe dQd$dRdSZddTddUdVdWZdd$dXdYZdd$dZd[Zd\d$d]d^Z d_d$d`daZ!ddbdcdddedfZ"dd$dgdhZ#dd$didjZ$dkd$dldmZ%dddddndodpZ&dddddndqdrZ'dd$dsdtZ(dd$dudvZ)dd$dwdxZ*d\d$dydzZ+d_d$d{d|Z,dd$d}d~Z-dd$ddZ.d\ddddZ/dd$ddZ0dd$ddZ1dd(ddddZ2dddddZ3dddddZ4dcddddZ5ddddddZ6ddddddZ7ddddddZ8dd$ddZ9eddddddddddZ:eddddddddddZ:ddddddddddZ:eddddddddddZ;eddddddddddZ;ddddddddddZ;eddddddddddZdddddddZ?ddd ddœddĄZ@dddddŜddDŽZAeBfdddddɜdd˄ZCdddddddd΄ZDddddd ddddҜddԄZEdddddd֜dd؄ZFddddٜddۄZGdddddddߜddZHdddddZId ZJd ZKddd(dddddLdddZLeMddddddddLdddZNdddddddZOdS( Connectiona:Provides high-level functionality for a wrapped DB-API connection. The :class:`_engine.Connection` object is procured by calling the :meth:`_engine.Engine.connect` method of the :class:`_engine.Engine` object, and provides services for execution of SQL statements as well as transaction control. The Connection object is **not** thread-safe. While a Connection can be shared among threads using properly synchronized access, it is still possible that the underlying DBAPI connection may not support shared access between threads. Check the DBAPI documentation for details. The Connection object represents a single DBAPI connection checked out from the connection pool. In this state, the connection pool has no affect upon the connection, including its expiration or timeout state. For the connection pool to properly manage connections, connections should be returned to the connection pool (i.e. ``connection.close()``) whenever the connection is not in use. .. index:: single: thread safety; Connection r.dialect"dispatcher[ConnectionEventsTarget]dispatchzsqlalchemy.engine.ConnectionNzOptional[TransactionalContext]_trans_context_managerFzOptional[PoolProxiedConnection]_dbapi_connectionr+_execution_optionszOptional[RootTransaction] _transactionzOptional[NestedTransaction]_nested_transactionTEnginezOptional[bool]bool)engine connection _has_events_allow_revalidate_allow_autobeginc Cs||_|j|_}|dkrbz||_Wqh|jjk r^}zt|||W5d}~XYqhXn||_d|_|_ d|_ d|_ ||_ ||_ |j|_|dkr|j|j|_|p|dko|j|_|j|_|js|jjr|j|dS)zConstruct a new Connection.NrF)rQrGraw_connectionrK loaded_dbapiErrorrF$_handle_dbapi_exception_noconnectionrMrN_Connection__savepoint_seq_Connection__in_begin_Connection__can_reconnectrU_should_log_info_echorI_joinrSrLZengine_connect)selfrQrRrSrTrUrGerrrbF/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/engine/base.py__init__s6     zConnection.__init__r_message_formatterstrNone)messageargkwreturncOs@|j}|r||}tjr&dtj|d<|jjj|f||dSNr stacklevel)rer STACKLEVELSTACKLEVEL_OFFSETrQloggerinfor`rhrirjfmtrbrbrc _log_infos zConnection._log_infocOs@|j}|r||}tjr&dtj|d<|jjj|f||dSrl)rerrnrorQrpdebugrrrbrbrc _log_debugs zConnection._log_debug Optional[SchemaTranslateMapType]rkcCs|jdd}|S)Nschema_translate_map)rLget)r`ryrbrbrc_schema_translate_maps z Connection._schema_translate_mapr? Optional[str])objrkcCs6|j}|jdd}|r.||kr.|jr.||S|SdS)ztReturn the schema name for the given schema item taking into account current schema translate map. ryN)ZschemarLrzZ_use_schema_map)r`r}nameryrbrbrcschema_for_objects zConnection.schema_for_objectcCs|SNrbr`rbrbrc __enter__szConnection.__enter__)type_value tracebackrkcCs |dSr)close)r`rrrrbrbrc__exit__szConnection.__exit__.) compiled_cache logging_tokenisolation_level no_parametersstream_resultsmax_row_buffer yield_perinsertmanyvalues_page_sizerypreserve_rowcountOptional[CompiledCacheType]rint) rrrrrrrrryroptrkc KsdSrrb) r`rrrrrrrrryrrrbrbrcexecution_optionsszConnection.execution_optionsrrkcKsdSrrbr`rrbrbrcrscKs<|js|jjr|j|||j||_|j|||S)a0Set non-SQL options for the connection which take effect during execution. This method modifies this :class:`_engine.Connection` **in-place**; the return value is the same :class:`_engine.Connection` object upon which the method is called. Note that this is in contrast to the behavior of the ``execution_options`` methods on other objects such as :meth:`_engine.Engine.execution_options` and :meth:`_sql.Executable.execution_options`. The rationale is that many such execution options necessarily modify the state of the base DBAPI connection in any case so there is no feasible means of keeping the effect of such an option localized to a "sub" connection. .. versionchanged:: 2.0 The :meth:`_engine.Connection.execution_options` method, in contrast to other objects with this method, modifies the connection in-place without creating copy of it. As discussed elsewhere, the :meth:`_engine.Connection.execution_options` method accepts any arbitrary parameters including user defined names. All parameters given are consumable in a number of ways including by using the :meth:`_engine.Connection.get_execution_options` method. See the examples at :meth:`_sql.Executable.execution_options` and :meth:`_engine.Engine.execution_options`. The keywords that are currently recognized by SQLAlchemy itself include all those listed under :meth:`.Executable.execution_options`, as well as others that are specific to :class:`_engine.Connection`. :param compiled_cache: Available on: :class:`_engine.Connection`, :class:`_engine.Engine`. A dictionary where :class:`.Compiled` objects will be cached when the :class:`_engine.Connection` compiles a clause expression into a :class:`.Compiled` object. This dictionary will supersede the statement cache that may be configured on the :class:`_engine.Engine` itself. If set to None, caching is disabled, even if the engine has a configured cache size. Note that the ORM makes use of its own "compiled" caches for some operations, including flush operations. The caching used by the ORM internally supersedes a cache dictionary specified here. :param logging_token: Available on: :class:`_engine.Connection`, :class:`_engine.Engine`, :class:`_sql.Executable`. Adds the specified string token surrounded by brackets in log messages logged by the connection, i.e. the logging that's enabled either via the :paramref:`_sa.create_engine.echo` flag or via the ``logging.getLogger("sqlalchemy.engine")`` logger. This allows a per-connection or per-sub-engine token to be available which is useful for debugging concurrent connection scenarios. .. versionadded:: 1.4.0b2 .. seealso:: :ref:`dbengine_logging_tokens` - usage example :paramref:`_sa.create_engine.logging_name` - adds a name to the name used by the Python logger object itself. :param isolation_level: Available on: :class:`_engine.Connection`, :class:`_engine.Engine`. Set the transaction isolation level for the lifespan of this :class:`_engine.Connection` object. Valid values include those string values accepted by the :paramref:`_sa.create_engine.isolation_level` parameter passed to :func:`_sa.create_engine`. These levels are semi-database specific; see individual dialect documentation for valid levels. The isolation level option applies the isolation level by emitting statements on the DBAPI connection, and **necessarily affects the original Connection object overall**. The isolation level will remain at the given setting until explicitly changed, or when the DBAPI connection itself is :term:`released` to the connection pool, i.e. the :meth:`_engine.Connection.close` method is called, at which time an event handler will emit additional statements on the DBAPI connection in order to revert the isolation level change. .. note:: The ``isolation_level`` execution option may only be established before the :meth:`_engine.Connection.begin` method is called, as well as before any SQL statements are emitted which would otherwise trigger "autobegin", or directly after a call to :meth:`_engine.Connection.commit` or :meth:`_engine.Connection.rollback`. A database cannot change the isolation level on a transaction in progress. .. note:: The ``isolation_level`` execution option is implicitly reset if the :class:`_engine.Connection` is invalidated, e.g. via the :meth:`_engine.Connection.invalidate` method, or if a disconnection error occurs. The new connection produced after the invalidation will **not** have the selected isolation level re-applied to it automatically. .. seealso:: :ref:`dbapi_autocommit` :meth:`_engine.Connection.get_isolation_level` - view current actual level :param no_parameters: Available on: :class:`_engine.Connection`, :class:`_sql.Executable`. When ``True``, if the final parameter list or dictionary is totally empty, will invoke the statement on the cursor as ``cursor.execute(statement)``, not passing the parameter collection at all. Some DBAPIs such as psycopg2 and mysql-python consider percent signs as significant only when parameters are present; this option allows code to generate SQL containing percent signs (and possibly other characters) that is neutral regarding whether it's executed by the DBAPI or piped into a script that's later invoked by command line tools. :param stream_results: Available on: :class:`_engine.Connection`, :class:`_sql.Executable`. Indicate to the dialect that results should be "streamed" and not pre-buffered, if possible. For backends such as PostgreSQL, MySQL and MariaDB, this indicates the use of a "server side cursor" as opposed to a client side cursor. Other backends such as that of Oracle may already use server side cursors by default. The usage of :paramref:`_engine.Connection.execution_options.stream_results` is usually combined with setting a fixed number of rows to to be fetched in batches, to allow for efficient iteration of database rows while at the same time not loading all result rows into memory at once; this can be configured on a :class:`_engine.Result` object using the :meth:`_engine.Result.yield_per` method, after execution has returned a new :class:`_engine.Result`. If :meth:`_engine.Result.yield_per` is not used, the :paramref:`_engine.Connection.execution_options.stream_results` mode of operation will instead use a dynamically sized buffer which buffers sets of rows at a time, growing on each batch based on a fixed growth size up until a limit which may be configured using the :paramref:`_engine.Connection.execution_options.max_row_buffer` parameter. When using the ORM to fetch ORM mapped objects from a result, :meth:`_engine.Result.yield_per` should always be used with :paramref:`_engine.Connection.execution_options.stream_results`, so that the ORM does not fetch all rows into new ORM objects at once. For typical use, the :paramref:`_engine.Connection.execution_options.yield_per` execution option should be preferred, which sets up both :paramref:`_engine.Connection.execution_options.stream_results` and :meth:`_engine.Result.yield_per` at once. This option is supported both at a core level by :class:`_engine.Connection` as well as by the ORM :class:`_engine.Session`; the latter is described at :ref:`orm_queryguide_yield_per`. .. seealso:: :ref:`engine_stream_results` - background on :paramref:`_engine.Connection.execution_options.stream_results` :paramref:`_engine.Connection.execution_options.max_row_buffer` :paramref:`_engine.Connection.execution_options.yield_per` :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel` describing the ORM version of ``yield_per`` :param max_row_buffer: Available on: :class:`_engine.Connection`, :class:`_sql.Executable`. Sets a maximum buffer size to use when the :paramref:`_engine.Connection.execution_options.stream_results` execution option is used on a backend that supports server side cursors. The default value if not specified is 1000. .. seealso:: :paramref:`_engine.Connection.execution_options.stream_results` :ref:`engine_stream_results` :param yield_per: Available on: :class:`_engine.Connection`, :class:`_sql.Executable`. Integer value applied which will set the :paramref:`_engine.Connection.execution_options.stream_results` execution option and invoke :meth:`_engine.Result.yield_per` automatically at once. Allows equivalent functionality as is present when using this parameter with the ORM. .. versionadded:: 1.4.40 .. seealso:: :ref:`engine_stream_results` - background and examples on using server side cursors with Core. :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel` describing the ORM version of ``yield_per`` :param insertmanyvalues_page_size: Available on: :class:`_engine.Connection`, :class:`_engine.Engine`. Number of rows to format into an INSERT statement when the statement uses "insertmanyvalues" mode, which is a paged form of bulk insert that is used for many backends when using :term:`executemany` execution typically in conjunction with RETURNING. Defaults to 1000. May also be modified on a per-engine basis using the :paramref:`_sa.create_engine.insertmanyvalues_page_size` parameter. .. versionadded:: 2.0 .. seealso:: :ref:`engine_insertmanyvalues` :param schema_translate_map: Available on: :class:`_engine.Connection`, :class:`_engine.Engine`, :class:`_sql.Executable`. A dictionary mapping schema names to schema names, that will be applied to the :paramref:`_schema.Table.schema` element of each :class:`_schema.Table` encountered when SQL or DDL expression elements are compiled into strings; the resulting schema name will be converted based on presence in the map of the original name. .. seealso:: :ref:`schema_translating` :param preserve_rowcount: Boolean; when True, the ``cursor.rowcount`` attribute will be unconditionally memoized within the result and made available via the :attr:`.CursorResult.rowcount` attribute. Normally, this attribute is only preserved for UPDATE and DELETE statements. Using this option, the DBAPIs rowcount value can be accessed for other kinds of statements such as INSERT and SELECT, to the degree that the DBAPI supports these statements. See :attr:`.CursorResult.rowcount` for notes regarding the behavior of this attribute. .. versionadded:: 2.0.28 .. seealso:: :meth:`_engine.Engine.execution_options` :meth:`.Executable.execution_options` :meth:`_engine.Connection.get_execution_options` :ref:`orm_queryguide_execution_options` - documentation on all ORM-specific execution options )rSrQrIZ set_connection_execution_optionsrLunionrGrrbrbrcrscCs|jS)zGet the non-SQL options which will take effect during execution. .. versionadded:: 1.3 .. seealso:: :meth:`_engine.Connection.execution_options` rLrrbrbrcget_execution_options s z Connection.get_execution_optionscCs|j}|dk o|jSr)rKis_validr`pool_proxied_connectionrbrbrc)_still_open_and_dbapi_connection_is_validsz4Connection._still_open_and_dbapi_connection_is_validcCs|jdko|j S)z)Return True if this connection is closed.NrKr\rrbrbrcclosedszConnection.closedcCs|j}|dko|jS)zReturn True if this connection was invalidated. This does not indicate whether or not the connection was invalidated at the pool level, however Nrrrbrbrc invalidated"szConnection.invalidatedr6c Csr|jdkrhz |WStjtjfk r2Yqntk rd}z||ddddW5d}~XYqnXn|jSdS)aVThe underlying DB-API connection managed by this Connection. This is a SQLAlchemy connection-pool proxied connection which then has the attribute :attr:`_pool._ConnectionFairy.dbapi_connection` that refers to the actual driver connection. .. seealso:: :ref:`dbapi_connections` N)rK_revalidate_connectionrPendingRollbackErrorResourceClosedError BaseException_handle_dbapi_exceptionr`erbrbrcrR7s  &zConnection.connectionc Cs\|jj}|dk stz|j|WStk rV}z||ddddW5d}~XYnXdS)aReturn the current **actual** isolation level that's present on the database within the scope of this connection. This attribute will perform a live SQL operation against the database in order to procure the current isolation level, so the value returned is the actual level on the underlying DBAPI connection regardless of how this state was set. This will be one of the four actual isolation modes ``READ UNCOMMITTED``, ``READ COMMITTED``, ``REPEATABLE READ``, ``SERIALIZABLE``. It will **not** include the ``AUTOCOMMIT`` isolation level setting. Third party dialects may also feature additional isolation level settings. .. note:: This method **will not report** on the ``AUTOCOMMIT`` isolation level, which is a separate :term:`dbapi` setting that's independent of **actual** isolation level. When ``AUTOCOMMIT`` is in use, the database connection still has a "traditional" isolation mode in effect, that is typically one of the four values ``READ UNCOMMITTED``, ``READ COMMITTED``, ``REPEATABLE READ``, ``SERIALIZABLE``. Compare to the :attr:`_engine.Connection.default_isolation_level` accessor which returns the isolation level that is present on the database at initial connection time. .. seealso:: :attr:`_engine.Connection.default_isolation_level` - view default level :paramref:`_sa.create_engine.isolation_level` - set per :class:`_engine.Engine` isolation level :paramref:`.Connection.execution_options.isolation_level` - set per :class:`_engine.Connection` isolation level N)rRdbapi_connectionAssertionErrorrGget_isolation_levelrr)r`rrrbrbrcrQs % zConnection.get_isolation_levelzOptional[IsolationLevel]cCs|jjS)aThe initial-connection time isolation level associated with the :class:`_engine.Dialect` in use. This value is independent of the :paramref:`.Connection.execution_options.isolation_level` and :paramref:`.Engine.execution_options.isolation_level` execution options, and is determined by the :class:`_engine.Dialect` when the first connection is created, by performing a SQL query against the database for the current isolation level before any additional commands have been emitted. Calling this accessor does not invoke any new SQL queries. .. seealso:: :meth:`_engine.Connection.get_isolation_level` - view current actual isolation level :paramref:`_sa.create_engine.isolation_level` - set per :class:`_engine.Engine` isolation level :paramref:`.Connection.execution_options.isolation_level` - set per :class:`_engine.Connection` isolation level )rGdefault_isolation_levelrrbrbrcr}sz"Connection.default_isolation_levelr cCs$tjd|jdk rdnddddS)NzfCan't reconnect until invalid %stransaction is rolled back. Please rollback() fully before proceedingz savepoint Z8s2b)code)rrrNrrbrbrc_invalid_transactions zConnection._invalid_transactioncCs>|jr0|jr0|jdk r||j|_|jStddS)NThis Connection is closed) r\rrMrrQrVrKrrrrbrbrcrs    z!Connection._revalidate_connectionr8cCs|jjS)aInfo dictionary associated with the underlying DBAPI connection referred to by this :class:`_engine.Connection`, allowing user-defined data to be associated with the connection. The data here will follow along with the DBAPI connection including after it is returned to the connection pool and used again in subsequent instances of :class:`_engine.Connection`. )rRrqrrbrbrcrqs zConnection.infozOptional[BaseException]) exceptionrkcCsF|jr dS|jrtd|jr<|j}|dk s2t||d|_dS)aInvalidate the underlying DBAPI connection associated with this :class:`_engine.Connection`. An attempt will be made to close the underlying DBAPI connection immediately; however if this operation fails, the error is logged but not raised. The connection is then discarded whether or not close() succeeded. Upon the next use (where "use" typically means using the :meth:`_engine.Connection.execute` method or similar), this :class:`_engine.Connection` will attempt to procure a new DBAPI connection using the services of the :class:`_pool.Pool` as a source of connectivity (e.g. a "reconnection"). If a transaction was in progress (e.g. the :meth:`_engine.Connection.begin` method has been called) when :meth:`_engine.Connection.invalidate` method is called, at the DBAPI level all state associated with this transaction is lost, as the DBAPI connection is closed. The :class:`_engine.Connection` will not allow a reconnection to proceed until the :class:`.Transaction` object is ended, by calling the :meth:`.Transaction.rollback` method; until that point, any attempt at continuing to use the :class:`_engine.Connection` will raise an :class:`~sqlalchemy.exc.InvalidRequestError`. This is to prevent applications from accidentally continuing an ongoing transactional operations despite the fact that the transaction has been lost due to an invalidation. The :meth:`_engine.Connection.invalidate` method, just like auto-invalidation, will at the connection pool level invoke the :meth:`_events.PoolEvents.invalidate` event. :param exception: an optional ``Exception`` instance that's the reason for the invalidation. is passed along to event handlers and logging functions. .. seealso:: :ref:`pool_connection_invalidation` Nr)rrrrrrKr invalidate)r`rrrbrbrcrs.   zConnection.invalidatecCs4|jrtd|j}|dkr(td|dS)a2Detach the underlying DB-API connection from its connection pool. E.g.:: with engine.connect() as conn: conn.detach() conn.execute(text("SET search_path TO schema1, schema2")) # work with connection # connection is fully closed (since we used "with:", can # also call .close()) This :class:`_engine.Connection` instance will remain usable. When closed (or exited from a context manager context as above), the DB-API connection will be literally closed and not returned to its originating pool. This method can be used to insulate the rest of an application from a modified state on a connection (such as a transaction isolation level or similar). rNz&Can't detach an invalidated Connection)rrrrKInvalidRequestErrordetachrrbrbrcrs zConnection.detachcCs|jr|js|dSr)rUr[beginrrbrbrc _autobegins zConnection._autobeginRootTransactioncCs(|jdkrt||_|jStddS)a Begin a transaction prior to autobegin occurring. E.g.:: with engine.connect() as conn: with conn.begin() as trans: conn.execute(table.insert(), {"username": "sandy"}) The returned object is an instance of :class:`_engine.RootTransaction`. This object represents the "scope" of the transaction, which completes when either the :meth:`_engine.Transaction.rollback` or :meth:`_engine.Transaction.commit` method is called; the object also works as a context manager as illustrated above. The :meth:`_engine.Connection.begin` method begins a transaction that normally will be begun in any case when the connection is first used to execute a statement. The reason this method might be used would be to invoke the :meth:`_events.ConnectionEvents.begin` event at a specific time, or to organize code within the scope of a connection checkout in terms of context managed blocks, such as:: with engine.connect() as conn: with conn.begin(): conn.execute(...) conn.execute(...) with conn.begin(): conn.execute(...) conn.execute(...) The above code is not fundamentally any different in its behavior than the following code which does not use :meth:`_engine.Connection.begin`; the below style is known as "commit as you go" style:: with engine.connect() as conn: conn.execute(...) conn.execute(...) conn.commit() conn.execute(...) conn.execute(...) conn.commit() From a database point of view, the :meth:`_engine.Connection.begin` method does not emit any SQL or change the state of the underlying DBAPI connection in any way; the Python DBAPI does not have any concept of explicit transaction begin. .. seealso:: :ref:`tutorial_working_with_transactions` - in the :ref:`unified_tutorial` :meth:`_engine.Connection.begin_nested` - use a SAVEPOINT :meth:`_engine.Connection.begin_twophase` - use a two phase /XID transaction :meth:`_engine.Engine.begin` - context manager available from :class:`_engine.Engine` NzThis connection has already initialized a SQLAlchemy Transaction() object via begin() or autobegin; can't call begin() here unless rollback() or commit() is called first.)rMrrrrrbrbrcrs A  zConnection.beginNestedTransactioncCs|jdkr|t|S)a Begin a nested transaction (i.e. SAVEPOINT) and return a transaction handle that controls the scope of the SAVEPOINT. E.g.:: with engine.begin() as connection: with connection.begin_nested(): connection.execute(table.insert(), {"username": "sandy"}) The returned object is an instance of :class:`_engine.NestedTransaction`, which includes transactional methods :meth:`_engine.NestedTransaction.commit` and :meth:`_engine.NestedTransaction.rollback`; for a nested transaction, these methods correspond to the operations "RELEASE SAVEPOINT " and "ROLLBACK TO SAVEPOINT ". The name of the savepoint is local to the :class:`_engine.NestedTransaction` object and is generated automatically. Like any other :class:`_engine.Transaction`, the :class:`_engine.NestedTransaction` may be used as a context manager as illustrated above which will "release" or "rollback" corresponding to if the operation within the block were successful or raised an exception. Nested transactions require SAVEPOINT support in the underlying database, else the behavior is undefined. SAVEPOINT is commonly used to run operations within a transaction that may fail, while continuing the outer transaction. E.g.:: from sqlalchemy import exc with engine.begin() as connection: trans = connection.begin_nested() try: connection.execute(table.insert(), {"username": "sandy"}) trans.commit() except exc.IntegrityError: # catch for duplicate username trans.rollback() # rollback to savepoint # outer transaction continues connection.execute( ... ) If :meth:`_engine.Connection.begin_nested` is called without first calling :meth:`_engine.Connection.begin` or :meth:`_engine.Engine.begin`, the :class:`_engine.Connection` object will "autobegin" the outer transaction first. This outer transaction may be committed using "commit-as-you-go" style, e.g.:: with engine.connect() as connection: # begin() wasn't called with connection.begin_nested(): will auto-"begin()" first connection.execute( ... ) # savepoint is released connection.execute( ... ) # explicitly commit outer transaction connection.commit() # can continue working with connection here .. versionchanged:: 2.0 :meth:`_engine.Connection.begin_nested` will now participate in the connection "autobegin" behavior that is new as of 2.0 / "future" style connections in 1.4. .. seealso:: :meth:`_engine.Connection.begin` :ref:`session_begin_nested` - ORM support for SAVEPOINT N)rMrrrrbrbrc begin_nestedgsI zConnection.begin_nestedz Optional[Any]TwoPhaseTransaction)xidrkcCs2|jdk rtd|dkr(|jj}t||S)a'Begin a two-phase or XA transaction and return a transaction handle. The returned object is an instance of :class:`.TwoPhaseTransaction`, which in addition to the methods provided by :class:`.Transaction`, also provides a :meth:`~.TwoPhaseTransaction.prepare` method. :param xid: the two phase transaction id. If not supplied, a random id will be generated. .. seealso:: :meth:`_engine.Connection.begin` :meth:`_engine.Connection.begin_twophase` NzOCannot start a two phase transaction when a transaction is already in progress.)rMrrrQrGZ create_xidr)r`rrbrbrcbegin_twophases  zConnection.begin_twophasecCs|jr|jdS)aCommit the transaction that is currently in progress. This method commits the current transaction if one has been started. If no transaction was started, the method has no effect, assuming the connection is in a non-invalidated state. A transaction is begun on a :class:`_engine.Connection` automatically whenever a statement is first executed, or when the :meth:`_engine.Connection.begin` method is called. .. note:: The :meth:`_engine.Connection.commit` method only acts upon the primary database transaction that is linked to the :class:`_engine.Connection` object. It does not operate upon a SAVEPOINT that would have been invoked from the :meth:`_engine.Connection.begin_nested` method; for control of a SAVEPOINT, call :meth:`_engine.NestedTransaction.commit` on the :class:`_engine.NestedTransaction` that is returned by the :meth:`_engine.Connection.begin_nested` method itself. N)rMcommitrrbrbrcrszConnection.commitcCs|jr|jdS)aYRoll back the transaction that is currently in progress. This method rolls back the current transaction if one has been started. If no transaction was started, the method has no effect. If a transaction was started and the connection is in an invalidated state, the transaction is cleared using this method. A transaction is begun on a :class:`_engine.Connection` automatically whenever a statement is first executed, or when the :meth:`_engine.Connection.begin` method is called. .. note:: The :meth:`_engine.Connection.rollback` method only acts upon the primary database transaction that is linked to the :class:`_engine.Connection` object. It does not operate upon a SAVEPOINT that would have been invoked from the :meth:`_engine.Connection.begin_nested` method; for control of a SAVEPOINT, call :meth:`_engine.NestedTransaction.rollback` on the :class:`_engine.NestedTransaction` that is returned by the :meth:`_engine.Connection.begin_nested` method itself. N)rMrollbackrrbrbrcrszConnection.rollbackz List[Any]cCs|jj|Sr)rQrGZdo_recover_twophaserrbrbrcrecover_twophaseszConnection.recover_twophase)rrecoverrkcCs|jjj|||ddSN)r)rQrGdo_rollback_twophaser`rrrbrbrcrollback_preparedszConnection.rollback_preparedcCs|jjj|||ddSr)rQrGdo_commit_twophaserrbrbrccommit_prepared szConnection.commit_preparedcCs|jdk o|jjSz,Return True if a transaction is in progress.N)rM is_activerrbrbrcin_transactionszConnection.in_transactioncCs|jdk o|jjSr)rNrrrbrbrcin_nested_transactions z Connection.in_nested_transactioncCs0|jdd}t|dkp,|dko,|jjjdkS)NrZ AUTOCOMMIT)rLrzrPrQrGZ_on_connect_isolation_level)r`Zopt_isorbrbrc_is_autocommit_isolationsz#Connection._is_autocommit_isolationcCs|j}|dkrtd|S)Nz"connection is not in a transaction)rMrrr`Ztransrbrbrc_get_required_transaction$s z$Connection._get_required_transactioncCs|j}|dkrtd|S)Nz)connection is not in a nested transaction)rNrrrrbrbrc _get_required_nested_transaction*s z+Connection._get_required_nested_transactioncCs|jS)zaReturn the current root transaction in progress, if any. .. versionadded:: 1.4 )rMrrbrbrcget_transaction2szConnection.get_transactioncCs|jS)zcReturn the current nested transaction in progress, if any. .. versionadded:: 1.4 )rNrrbrbrcget_nested_transaction;sz!Connection.get_nested_transaction) transactionrkc Cs|jr$|r|dn |dd|_|js8|jjrD|j|zNz|jj |j Wn4t k r}z| |ddddW5d}~XYnXW5d|_XdS)Nz?BEGIN (implicit; DBAPI should not BEGIN due to autocommit mode)zBEGIN (implicit)TF) r^rrtr[rSrQrIrrGZdo_beginrRrrr`rrrbrbrc _begin_implCs  (zConnection._begin_implc Cs|js|jjr|j||jr|jrD|r:|dn |dz|jj |j Wn4t k r}z| |ddddW5d}~XYnXdS)NzVROLLBACK using DBAPI connection.rollback(), DBAPI should ignore due to autocommit modeZROLLBACK) rSrQrIrrr^rrtrGZ do_rollbackrRrrrrbrbrc_rollback_implYs  zConnection._rollback_implc Cs|js|jjr|j||jr>|r4|dn |dz|jj|j Wn4t k r}z| |ddddW5d}~XYnXdS)NzRCOMMIT using DBAPI connection.commit(), DBAPI should ignore due to autocommit modeZCOMMIT) rSrQrIrr^rrtrGZ do_commitrRrrrrbrbrc _commit_implks  zConnection._commit_impl)r~rkcCsP|js|jjr|j|||dkr<|jd7_d|j}|jj|||S)Nrzsa_savepoint_%s)rSrQrIZ savepointrZrGZ do_savepointr`r~rbrbrc_savepoint_impl|s zConnection._savepoint_implcCs8|js|jjr|j||d|jr4|jj||dSr)rSrQrIZrollback_savepointrrGZdo_rollback_to_savepointrrbrbrc_rollback_to_savepoint_implsz&Connection._rollback_to_savepoint_implcCs2|js|jjr|j||d|jj||dSr)rSrQrIZrelease_savepointrGZdo_release_savepointrrbrbrc_release_savepoint_implsz"Connection._release_savepoint_implc Cs|jr|d|js|jjr.|j||jd|_zPz|jj ||jWn4t k r}z| |ddddW5d}~XYnXW5d|_XdS)NzBEGIN TWOPHASE (implicit)TF) r^rtrSrQrIrrr[rGZdo_begin_twophaserrrrbrbrc_begin_twophase_impls (zConnection._begin_twophase_implc Csz|js|jjr|j||t|jts,tz|jj ||Wn4t k rt}z| |ddddW5d}~XYnXdSr) rSrQrIZprepare_twophase isinstancerMrrrGZdo_prepare_twophaserr)r`rrrbrbrc_prepare_twophase_implsz!Connection._prepare_twophase_impl)r is_preparedrkc Cs|js|jjr|j||||jrt|jts4tz|jj |||Wn4t k r~}z| |ddddW5d}~XYnXdSr) rSrQrIZrollback_twophaserrrMrrrGrrrr`rrrrbrbrc_rollback_twophase_implsz"Connection._rollback_twophase_implc Cs~|js|jjr|j|||t|jts.tz|jj |||Wn4t k rx}z| |ddddW5d}~XYnXdSr) rSrQrIZcommit_twophaserrMrrrGrrrrrbrbrc_commit_twophase_implsz Connection._commit_twophase_implcCsZ|jr|jd}nd}|jdk rP|j}|rBtd|jddn|d|_d|_dS)aClose this :class:`_engine.Connection`. This results in a release of the underlying database resources, that is, the DBAPI connection referenced internally. The DBAPI connection is typically restored back to the connection-holding :class:`_pool.Pool` referenced by the :class:`_engine.Engine` that produced this :class:`_engine.Connection`. Any transactional state present on the DBAPI connection is also unconditionally released via the DBAPI connection's ``rollback()`` method, regardless of any :class:`.Transaction` object that may be outstanding with regards to this :class:`_engine.Connection`. This has the effect of also calling :meth:`_engine.Connection.rollback` if any transaction is in place. After :meth:`_engine.Connection.close` is called, the :class:`_engine.Connection` is permanently in a closed state, and will allow no further operations. TFNr4)Ztransaction_reset)rMrrKrZ_close_specialr\)r`Z skip_resetconnrbrbrcrs   zConnection.closerzTypedReturnsRows[Tuple[_T]]z"Optional[_CoreSingleExecuteParams]z%Optional[CoreExecuteOptionsParameter]z Optional[_T]) statement parametersrrkcCsdSrrbr`rrrrbrbrcscalarszConnection.scalarr7cCsdSrrbrrbrbrcrsc CsVt|}z |j}Wn.tk r@}zt||W5d}~XYnX||||pNtSdS)auExecutes a SQL statement construct and returns a scalar object. This method is shorthand for invoking the :meth:`_engine.Result.scalar` method after invoking the :meth:`_engine.Connection.execute` method. Parameters are equivalent. :return: a scalar Python value representing the first column of the first row returned. N)rZ_execute_on_scalarAttributeErrorrObjectNotExecutableErrorrEr`rrrdistilled_parametersmethrarbrbrcrs zOptional[_CoreAnyExecuteParams]zScalarResult[_T]cCsdSrrbrrbrbrcscalars!szConnection.scalarszScalarResult[Any]cCsdSrrbrrbrbrcr*scCs|j|||dS)aExecutes and returns a scalar result set, which yields scalar values from the first column of each row. This method is equivalent to calling :meth:`_engine.Connection.execute` to receive a :class:`_result.Result` object, then invoking the :meth:`_result.Result.scalars` method to produce a :class:`_result.ScalarResult` instance. :return: a :class:`_result.ScalarResult` .. versionadded:: 1.4.24 r)executerrrbrbrcr3s zTypedReturnsRows[_T]zCursorResult[_T]cCsdSrrbrrbrbrcrLszConnection.executezCursorResult[Any]cCsdSrrbrrbrbrcrUsc CsVt|}z |j}Wn.tk r@}zt||W5d}~XYnX||||pNtSdS)aExecutes a SQL statement construct and returns a :class:`_engine.CursorResult`. :param statement: The statement to be executed. This is always an object that is in both the :class:`_expression.ClauseElement` and :class:`_expression.Executable` hierarchies, including: * :class:`_expression.Select` * :class:`_expression.Insert`, :class:`_expression.Update`, :class:`_expression.Delete` * :class:`_expression.TextClause` and :class:`_expression.TextualSelect` * :class:`_schema.DDL` and objects which inherit from :class:`_schema.ExecutableDDLElement` :param parameters: parameters which will be bound into the statement. This may be either a dictionary of parameter names to values, or a mutable sequence (e.g. a list) of dictionaries. When a list of dictionaries is passed, the underlying statement execution will make use of the DBAPI ``cursor.executemany()`` method. When a single dictionary is passed, the DBAPI ``cursor.execute()`` method will be used. :param execution_options: optional dictionary of execution options, which will be associated with the statement execution. This dictionary can provide a subset of the options that are accepted by :meth:`_engine.Connection.execution_options`. :return: a :class:`_engine.Result` object. N)rZ_execute_on_connectionrrrrErrbrbrcr^s& zFunctionElement[Any]r'r-)funcrrrkcCs||||S)z%Execute a sql.FunctionElement object.)_execute_clauseelementselect)r`rrrrbrbrc_execute_functions zConnection._execute_functionr>)defaultrrrkc Cs|j|}|js|jjr2||||\}}}}nd}}z2|j}|dkrR|}|j}|j ||||}WnPt j t j fk rYn4t k r} z|| ddddW5d} ~ XYnX|d|d} |js|jjr|j|||||| | S)z&Execute a schema.ColumnDefault object.N)rL merge_withrSrQ_invoke_before_exec_eventrKrrGexecution_ctx_clsZ _init_defaultrrrrrZ _exec_defaultrI after_execute) r`rrrevent_multiparams event_paramsrrGctxrretrbrbrc_execute_defaultsT  $ zConnection._execute_defaultr:)ddlrrrkc Cs|j|j|}|js|jjr6||||\}}}}nd}}|dd}|j}|j||d} |||j j | d|| } |js|jjr|j |||||| | S)zExecute a schema.DDL object.Nry)rGry) rLrrSrQrrzrGcompile_execute_contextrZ _init_ddlrIr) r`rrrZ exec_optsrrryrGcompiledrrbrbrc _execute_ddlsR  zConnection._execute_ddlzVTuple[Any, _CoreMultiExecuteParams, _CoreMultiExecuteParams, _CoreSingleExecuteParams])elemdistilled_paramsrrkcCst|dkrg|d}}n |i}}|jjD]}||||||\}}}q.|rft|}|rvtdn|rr|g}ng}||||fS)NrrzLEvent handler can't return non-empty multiparams and params at the same time)lenrIZbefore_executelistrr)r`rrrrrfnrbrbrcr s*    z$Connection._invoke_before_exec_event)rrrrkc Cs|j|j|}|jp|jj}|r8||||\}}}}|rVt|d}t|dk}ng}d}|j} |dd} |d|jj } |j | | ||| |jj t j Bd\} } }|j| | jj| ||| ||| |d }|r|j|||||||S) z#Execute a sql.ClauseElement object.rrFryNr)rGrZ column_keysfor_executemanyryZlinting) cache_hit)rLrrSrQrsortedrrGrz_compiled_cacheZ_compile_w_cacheZcompiler_lintingr!Z WARN_LINTINGrr_init_compiledrIr)r`rrrZ has_eventsrrkeysrrGryrZ compiled_sqlZextracted_paramsrrrbrbrcr6sx   z!Connection._execute_clauseelementr9)rrrrkc Cs|j|j|}|js|jjr4||||\}}}}|j}|||jj |||||dd }|jsf|jjr||j |||||||S)ziExecute a sql.Compiled object. TODO: why do we have this? likely deprecate or remove N) rrrLrSrQrrGrrr rIr)r`rrrrrrGrrbrbrc_execute_compiledsJ  zConnection._execute_compiled Optional[_DBAPIAnyExecuteParams]c Cs8t|}|j|}|j}|||jj|d|||}|S)aWExecutes a string SQL statement on the DBAPI cursor directly, without any SQL compilation steps. This can be used to pass any string directly to the ``cursor.execute()`` method of the DBAPI in use. :param statement: The statement str to be executed. Bound parameters must use the underlying DBAPI's paramstyle, such as "qmark", "pyformat", "format", etc. :param parameters: represent bound parameter values to be used in the execution. The format is one of: a dictionary of named parameters, a tuple of positional parameters, or a list containing either dictionaries or tuples for multiple-execute support. :return: a :class:`_engine.CursorResult`. E.g. multiple dictionaries:: conn.exec_driver_sql( "INSERT INTO table (id, value) VALUES (%(id)s, %(value)s)", [{"id":1, "value":"v1"}, {"id":2, "value":"v2"}] ) Single dictionary:: conn.exec_driver_sql( "INSERT INTO table (id, value) VALUES (%(id)s, %(value)s)", dict(id=1, value="v1") ) Single tuple:: conn.exec_driver_sql( "INSERT INTO table (id, value) VALUES (?, ?)", (1, 'v1') ) .. note:: The :meth:`_engine.Connection.exec_driver_sql` method does not participate in the :meth:`_events.ConnectionEvents.before_execute` and :meth:`_events.ConnectionEvents.after_execute` events. To intercept calls to :meth:`_engine.Connection.exec_driver_sql`, use :meth:`_events.ConnectionEvents.before_cursor_execute` and :meth:`_events.ConnectionEvents.after_cursor_execute`. .. seealso:: :pep:`249` N)rrLrrGrrZ_init_statement)r`rrrrrGrrbrbrcexec_driver_sqls; zConnection.exec_driver_sqlzCallable[..., ExecutionContext]zUnion[str, Compiled]z Optional[_AnyMultiExecuteParams])rG constructorrrrargsrjrkc Os(|r$|dd}|r$|d|d}z0|j} | dkr<|} |||| |f||} WnTtjtjfk rrYn8tk r} z|| t ||ddW5d} ~ XYnX|j r|j j r|j r|j j s| n|jrt||j dkr|| | jtjkr||| S||| ||SdS)zdCreate an :class:`.ExecutionContext` and execute, returning a :class:`_engine.CursorResult`.rNT)rr)rzrrKrrrrrrrfrMrrNrrJr_trans_ctx_checkrZpre_exec execute_stylerZINSERTMANYVALUES_exec_insertmany_context_exec_single_context) r`rGrrrrrrjZyprcontextrrbrbrcrsh      zConnection._execute_contextr)rGrrrrkc Cs|jtjkrf|}|rfz||j||Wn8tk rd}z||t||d|W5d}~XYnX|j|j |j }}}|j s|d} n|} |j s|j j r|jjD]} | |||| ||j \}} q|jr|||} |j js|d| tj| d|j dn |d| d} zX|jtjkrtd| } |jj rh|jjjD] } | ||| |rFd } qhqF| s4|j||| |n| s|jr|jj r|jjjD]} | |||rd } qĐq| s4|j|||nXtd | } |jj r|jjjD] } | ||| |rd } qq| s4|j||| ||j sF|j j r^|j|||| ||j | |!} Wn6tk r}z|||| ||W5d}~XYnX| S) zzcontinue the _execute_context() method for a single DBAPI cursor.execute() or cursor.executemany() call. Nr[%s] %r Zbatchesismulti8[%s] [SQL parameters hidden due to hide_parameters=True]Fr'Tr()" bind_typingr SETINPUTSIZES_prepare_set_input_sizesdo_set_input_sizescursorrrrfrr executemanyrSrQrIbefore_cursor_executer^rt_get_cache_statshide_parameterssql_util _repr_paramsrrZ EXECUTEMANYrrGZdo_executemanyrZdo_execute_no_params do_executeafter_cursor_execute post_exec_setup_result_proxy)r`rGrrrgeneric_setinputsizesrr str_statementeffective_parametersrstatsZ evt_handledresultrbrbrcr:s                zConnection._exec_single_context)rGrrkc Cs|jtjkr|}nd}|j|j|j}}}|}|jp@|jj}|j jrV|j j j } nd} |j rl| d} |jdd} d} |||||||D]} | jrz||j| j|WnBtk r}z$|j|t| j| jd|ddW5d}~XYnX| j}| j}|r0|j jD]}||||||d\}}q|j r|t|d | jd | jd | jrfd nd | jrvdndd}| jdkr| |7} n d|} |jjs|d| tj |dddn |d| z6| D]}|||||rqq| ||||Wn@tk rH}z |j|t||||ddW5d}~XYnX|rh|j !||||||j"| r| | j#7} qz |$| r| |_%|&}Wn6tk r}z||||||W5d}~XYnX|S)zcontinue the _execute_context() method for an "insertmanyvalues" operation, which will invoke DBAPI cursor.execute() one or more times with individual log and event hook calls. Nrbz (insertmanyvalues)rFrT) is_sub_exec /z (ordered unorderedz; batch not supportedr)rZinsertmanyvaluesrrrr)'rrrrrrrrSrQrGrIr%r^r!rrzZ!_deliver_insertmanyvalues_batchesZprocessed_setinputsizesrrrr#Z_long_statementZreplaced_statementZreplaced_parametersr rtZbatchnumZ total_batchesZ rows_sortedZ is_downgradedr"r$r&rZcurrent_batch_sizer'Z _rowcountr()r`rGrr)rr*rr+Z engine_eventsZdo_execute_dispatchr,rZrowcountZ imv_batchrZsub_stmtZ sub_paramsrZ imv_statsr-rbrbrcrs                    z#Connection._exec_insertmany_contextrr*Optional[ExecutionContext])rrrrrkc Cs|js|jjr2|jjD]}||||||d\}}q|jrN|||d|zD|jjs\dn|jjjD]}|||||rfqqf|j||||Wn4tk r}z| |||||W5d}~XYnX|js|jjr|j |||||ddS)a_Execute a statement + params on the given cursor. Adds appropriate logging and exception handling. This method is used by DefaultDialect for special-case executions, such as for sequences and column defaults. The path of statement execution in the majority of cases terminates at _execute_context(). Fz [raw sql] %rrbN) rSrQrIr r^rtrGr%rrr&)r`rrrrrrrbrbrc_cursor_executessL    zConnection._cursor_execute)rrkcCs:z |Wn(tk r4|jjjjdddYnXdS)z\Close the given cursor, catching exceptions and turning into log warnings. zError closing cursorT)exc_infoN)r ExceptionrQpoolrperror)r`rrbrbrc_safe_close_cursors  zConnection._safe_close_cursorrzOptional[_AnyExecuteParams]Optional[DBAPICursor])rrrrrr.rkc Cst}t|}|jsZt||jjjrL|j rL|j ||j sD|j nd|pV|oV|j |_| } |dk rt| ov|j nd} |jrtjj||||jjj|jj|j| d|d|d|_zt||jjjp|dk o|dko| } | rtjj||tt||jjj|jj|j|j| d} nd} d}|jjr|jddst|| |j|j||||||j| d }|jjj D]\}z ||}|dk r||_!}Wn4tk r}z|}WY qW5d}~XYnXqh|j|j kr|j |_| r|j | _"|j#} | r|r|$||js0|r|%||&s0|'|rH||d|nJ| rn| dk s\t| |d|n$|ddk st|d|dW5|`|jr|`|j s|j } | dk st| r|jj| |||XdS)NF)r"rGrrT)r"connection_invalidatedrGrZskip_user_error_eventsr)(sysr6r is_exit_exception_is_disconnectrrGrWrXr is_disconnectrrKr_reentrant_errorr DBAPIErrorinstancerQr"with_tracebackrr8Z _invalidaterrr7rSrLrzExceptionContextImplrI handle_errorchained_exceptionr<invalidate_pool_on_disconnectZhandle_dbapi_exceptionr:rr)r`rrrrrr.r6r>rHrZdbapi_conn_wrapper should_wrapsqlalchemy_exceptionnewraiserrper_fn_raisedrbrbrcrs       "    z"Connection._handle_dbapi_exceptionOptional[Engine])rrGrQr@rH is_pre_pingrkc Cst}|dkr,t||jjo*||dd}t||jj}|rrtjjddt t ||jj|dk rd|j nd||d} nd} d} |j rt || ||ddddd||| } |jjD]T} z| | } | dk r| | _} Wqt k r}z|} WYqW5d}~XYqXq| r|| jkr| j| _}| r4| |d|nJ|rZ| dk sHt| |d|n$|ddk slt|d|ddS)NF)r"r<rGrr)r=r6rrWrXr@rrBrCrr7r"rSrErIrFrGr<rDr)clsrrGrQr@rHrOr6rIrJrKrrrLrMrbrbrcrYB sl    z/Connection._handle_dbapi_exception_noconnection+Type[Union[SchemaGenerator, SchemaDropper]]r@visitorcallableelementkwargsrkcKs||j|f||dS)zrun a DDL visitor. This method is only here so that the MockConnection can change the options given to the visitor so that "checkfirst" is skipped. N)rGZtraverse_single)r`rSrTrUrbrbrc_run_ddl_visitor s zConnection._run_ddl_visitor)NNTT)N)N)F)F)N)N)N)N)N)N)N)N)N)N)NN)N)F)NNTF)P__name__ __module__ __qualname____doc____annotations___sqla_logger_namespacerJZshould_close_with_resultrdrertrvpropertyr{rrrr rrrrrrRrrrrrqrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrDr r rrrr5r:rAr?r classmethodrYrVrbrbrbrcrFWs:  /   *  , ;$LN      0   2 ;5*M5N;1.  LrFr0c @s8eZdZdZdZddddddd d d d d d d ddZdS)rEz3Implement the :class:`.ExceptionContext` interface.) rRrQrGrrroriginal_exceptionrJrGexecution_contextr@rHrOrzOptional[exc.StatementError]rNr.Optional[Connection]r;r|r r4rP) rrJrQrGrRrrrrr@rHrOc CsF||_||_||_||_||_| |_||_||_| |_| |_ | |_ dSr) rQrGrRrJr_r`rrr@rHrO) r`rrJrQrGrRrrrrr@rHrOrbrbrcrd szExceptionContextImpl.__init__N)rWrXrYrZ __slots__rdrbrbrbrcrE srEc@seZdZUdZdZdZded<ded<ded<dd d d Zedd d dZ dd ddZ dd ddZ dd ddZ edd ddZ dd ddZdd ddZdd ddZdd ddZdd d d!Zdd d"d#Zdd d$d%Zd&S)' Transactiona}Represent a database transaction in progress. The :class:`.Transaction` object is procured by calling the :meth:`_engine.Connection.begin` method of :class:`_engine.Connection`:: from sqlalchemy import create_engine engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test") connection = engine.connect() trans = connection.begin() connection.execute(text("insert into x (a, b) values (1, 2)")) trans.commit() The object provides :meth:`.rollback` and :meth:`.commit` methods in order to control transaction boundaries. It also implements a context manager interface so that the Python ``with`` statement can be used with the :meth:`_engine.Connection.begin` method:: with connection.begin(): connection.execute(text("insert into x (a, b) values (1, 2)")) The Transaction object is **not** threadsafe. .. seealso:: :meth:`_engine.Connection.begin` :meth:`_engine.Connection.begin_twophase` :meth:`_engine.Connection.begin_nested` .. index:: single: thread safety; Transaction rbFrP_is_rootrrFrRrRcCs tdSrNotImplementedErrorr`rRrbrbrcrd szTransaction.__init__rxcCs tdS)zTrue if this transaction is totally deactivated from the connection and therefore can no longer affect its state. Nrfrrbrbrc_deactivated_from_connection sz(Transaction._deactivated_from_connectionrgcCs tdSrrfrrbrbrc _do_close szTransaction._do_closecCs tdSrrfrrbrbrc _do_rollback szTransaction._do_rollbackcCs tdSrrfrrbrbrc _do_commit szTransaction._do_commitcCs|jo|jj Sr)rrRrrrbrbrcr szTransaction.is_validcCsz |W5|jrtXdS)a;Close this :class:`.Transaction`. If this transaction is the base transaction in a begin/commit nesting, the transaction will rollback(). Otherwise, the method returns. This is used to cancel a Transaction without affecting the scope of an enclosing transaction. N)rrrjrrbrbrcr s  zTransaction.closecCsz |W5|jrtXdS)aRoll back this :class:`.Transaction`. The implementation of this may vary based on the type of transaction in use: * For a simple database transaction (e.g. :class:`.RootTransaction`), it corresponds to a ROLLBACK. * For a :class:`.NestedTransaction`, it corresponds to a "ROLLBACK TO SAVEPOINT" operation. * For a :class:`.TwoPhaseTransaction`, DBAPI-specific methods for two phase transactions may be used. N)rrrkrrbrbrcr s zTransaction.rollbackcCsz |W5|jrtXdS)aCommit this :class:`.Transaction`. The implementation of this may vary based on the type of transaction in use: * For a simple database transaction (e.g. :class:`.RootTransaction`), it corresponds to a COMMIT. * For a :class:`.NestedTransaction`, it corresponds to a "RELEASE SAVEPOINT" operation. * For a :class:`.TwoPhaseTransaction`, DBAPI-specific methods for two phase transactions may be used. N)rrrlrrbrbrcr6 s zTransaction.commitcCs|jSrrerrbrbrc _get_subjectK szTransaction._get_subjectcCs|jSr)rrrbrbrc_transaction_is_activeN sz"Transaction._transaction_is_activecCs|j Sr)rirrbrbrc_transaction_is_closedQ sz"Transaction._transaction_is_closedcCsdSNTrbrrbrbrc_rollback_can_be_calledT sz#Transaction._rollback_can_be_calledN)rWrXrYrZrbrdr[rdr]rirjrkrlrrrrrmrnrorqrbrbrbrcrc s( $ rcc@seZdZdZdZdZddddZdd d d Zed d d dZ dd ddZ dd ddZ dd ddZ d d ddddZ dd ddZdd ddZdd ddZdS)!raRepresent the "root" transaction on a :class:`_engine.Connection`. This corresponds to the current "BEGIN/COMMIT/ROLLBACK" that's occurring for the :class:`_engine.Connection`. The :class:`_engine.RootTransaction` is created by calling upon the :meth:`_engine.Connection.begin` method, and remains associated with the :class:`_engine.Connection` throughout its active span. The current :class:`_engine.RootTransaction` in use is accessible via the :attr:`_engine.Connection.get_transaction` method of :class:`_engine.Connection`. In :term:`2.0 style` use, the :class:`_engine.Connection` also employs "autobegin" behavior that will create a new :class:`_engine.RootTransaction` whenever a connection in a non-transactional state is used to emit commands on the DBAPI connection. The scope of the :class:`_engine.RootTransaction` in 2.0 style use can be controlled using the :meth:`_engine.Connection.commit` and :meth:`_engine.Connection.rollback` methods. T)rRrrFrecCs<|jdkst|jrt|||_|||_d|_dSrp)rMrrJrrrR_connection_begin_implrrhrbrbrcrdv s zRootTransaction.__init__rgrxcCs8|jr|jj|kstd|_n|jj|k r4tddS)NFz0transaction already deassociated from connection)rrRrMrr warnrrbrbrc_deactivate_from_connection s  z+RootTransaction._deactivate_from_connectionrPcCs |jj|k Sr)rRrMrrbrbrcri sz,RootTransaction._deactivated_from_connectioncCs|j|dSr)rRrrrbrbrcrr sz&RootTransaction._connection_begin_implcCs|jdSr)rRrrrbrbrc_connection_rollback_impl sz)RootTransaction._connection_rollback_implcCs|jdSr)rRrrrbrbrc_connection_commit_impl sz'RootTransaction._connection_commit_implF)try_deactivaterkcCsnz&|jr||jjr$|jjW5|js2|r:||jj|krNd|j_X|jrZt|jj|k sjtdSr)rrtrRrMrurN_cancelr)r`rwrbrbrc _close_impl s    zRootTransaction._close_implcCs |dSrryrrbrbrcrj szRootTransaction._do_closecCs|jdddS)NT)rwrzrrbrbrcrk szRootTransaction._do_rollbackcCs|jrL|jj|kstz |W5|jjr8|jj|Xd|j_n"|jj|krd|jn t d|jrxt|jj|k stdS)NThis transaction is inactive) rrRrMrrNrxrtrvrrrrrbrbrcrl s        zRootTransaction._do_commitN)F)rWrXrYrZrdrbrdrtr]rirrrurvryrjrkrlrbrbrbrcr\ s rc@seZdZUdZdZded<ddddZdd d d d dZed dddZ d dddZ d d d dddZ d dddZ d dddZ d dddZdS)raRepresent a 'nested', or SAVEPOINT transaction. The :class:`.NestedTransaction` object is created by calling the :meth:`_engine.Connection.begin_nested` method of :class:`_engine.Connection`. When using :class:`.NestedTransaction`, the semantics of "begin" / "commit" / "rollback" are as follows: * the "begin" operation corresponds to the "BEGIN SAVEPOINT" command, where the savepoint is given an explicit name that is part of the state of this object. * The :meth:`.NestedTransaction.commit` method corresponds to a "RELEASE SAVEPOINT" operation, using the savepoint identifier associated with this :class:`.NestedTransaction`. * The :meth:`.NestedTransaction.rollback` method corresponds to a "ROLLBACK TO SAVEPOINT" operation, using the savepoint identifier associated with this :class:`.NestedTransaction`. The rationale for mimicking the semantics of an outer transaction in terms of savepoints so that code may deal with a "savepoint" transaction and an "outer" transaction in an agnostic way. .. seealso:: :ref:`session_begin_nested` - ORM version of the SAVEPOINT API. )rRr _savepoint_previous_nestedrfr|rFrecCsH|jdk st|jrt|||_|j|_d|_|j |_ ||_ dSrp) rMrrJrrrRrr|rrNr}rhrbrbrcrd s  zNestedTransaction.__init__TrPrg)rsrkcCs*|jj|kr|j|j_n|r&tddS)Nz7nested transaction already deassociated from connection)rRrNr}r rs)r`rsrbrbrcrt s   z-NestedTransaction._deactivate_from_connectionrxcCs |jj|k Sr)rRrNrrbrbrcri sz.NestedTransaction._deactivated_from_connectioncCs"d|_||jr|jdSNF)rrtr}rxrrbrbrcrx szNestedTransaction._cancel)deactivate_from_connectionwarn_already_deactiverkc Csfz*|jr(|jjr(|jjjr(|j|jW5d|_|rB|j|dX|jrNt|rb|jj|k sbtdS)NF)rs)rrtrRrMrr|rrN)r`rrrbrbrcry s zNestedTransaction._close_implcCs|dddS)NTFrzrrbrbrcrj szNestedTransaction._do_closecCs|dddSrprzrrbrbrcrk" szNestedTransaction._do_rollbackcCsR|jr,z|j|jW5d|_X|n"|jj|krD|jn tddS)NFz#This nested transaction is inactive) rrRrr|rtrNrrrrrbrbrcrl% s   zNestedTransaction._do_commitN)T)rWrXrYrZrbr[rdrtr]rirxryrjrkrlrbrbrbrcr s   rcsneZdZUdZdZded<dddfdd Zd d d d Zd d d dZd d ddZ d d ddZ Z S)raRepresent a two-phase transaction. A new :class:`.TwoPhaseTransaction` object may be procured using the :meth:`_engine.Connection.begin_twophase` method. The interface is the same as that of :class:`.Transaction` with the addition of the :meth:`prepare` method. )r _is_preparedrrrF)rRrcsd|_||_t|dSr~)rrsuperrd)r`rRr __class__rbrcrdI szTwoPhaseTransaction.__init__rgrxcCs(|jstd|j|jd|_dS)zqPrepare this :class:`.TwoPhaseTransaction`. After a PREPARE, the transaction can be committed. r{TN)rrrrRrrrrrbrbrcprepareN s zTwoPhaseTransaction.preparecCs|j|dSr)rRrrrbrbrcrrY sz*TwoPhaseTransaction._connection_begin_implcCs|j|j|jdSr)rRrrrrrbrbrcru\ sz-TwoPhaseTransaction._connection_rollback_implcCs|j|j|jdSr)rRrrrrrbrbrcrv_ sz+TwoPhaseTransaction._connection_commit_impl) rWrXrYrZrbr[rdrrrrurv __classcell__rbrbrrcr: s   rc @seZdZUdZded<ded<eZded<dZd ed <eZ d ed <d Z ded<dZ d ed<dZ ded<ded<ded<ded<ded<d ed<dZdddddd d!d d"d#d$Z d%d&d'd(d)Zedd*d+d,Zd&d*d-d.Zd/d&d0d1d2Zed3d3d3d3d3d4ddd5d dd/d6d7d8d9Zed/d6d0d:d9Zd/d6d0d;d9Zdd*dd?Zedd*d@dAZeZdd*dBdCZd[d d&dEdFdGZejd\dHdIdJdKdLZejdId*dMdNZ dOdPd/d&dQdRdSZ!dTd*dUdVZ"dWd*dXdYZ#dS)]rOa Connects a :class:`~sqlalchemy.pool.Pool` and :class:`~sqlalchemy.engine.interfaces.Dialect` together to provide a source of database connectivity and behavior. An :class:`_engine.Engine` object is instantiated publicly using the :func:`~sqlalchemy.create_engine` function. .. seealso:: :doc:`/core/engines` :ref:`connections_toplevel` rHrIrrr+rLFrPrSzType[Connection]_connection_clszsqlalchemy.engine.Enginerfr\ _is_futureNrwr{zType[OptionEngine] _option_clsr.rGr5r8r1urlr"r|zOptional[_EchoFlagType]rzOptional[Mapping[str, Any]])r8rGr logging_nameechoquery_cache_sizerr"c Csl||_||_||_|r||_||_||_|dkrDtj||jd|_ nd|_ t j ||d|rh|j f|dS)Nr)Z size_alertZechoflag) r8rrGrrr"r ZLRUCache_lru_size_alertrrinstance_loggerupdate_execution_options) r`r8rGrrrrrr"rbrbrcrd s  zEngine.__init__zutil.LRUCache[Any, Any]rg)cacherkcCs"|r|jdt||jdS)NziCompiled cache size pruning from %d items to %d. Increase cache size to reduce the frequency of pruning.)r]rprqrcapacity)r`rrbrbrcr s zEngine._lru_size_alertrxcCs|S)zReturns this :class:`.Engine`. Used for legacy schemes that accept :class:`.Connection` / :class:`.Engine` objects within the same variable. rbrrbrbrcrQ sz Engine.enginecCs|jr|jdS)aClear the compiled cache associated with the dialect. This applies **only** to the built-in cache that is established via the :paramref:`_engine.create_engine.query_cache_size` parameter. It will not impact any dictionary caches that were passed via the :paramref:`.Connection.execution_options.compiled_cache` parameter. .. versionadded:: 1.4 N)rclearrrbrbrcclear_compiled_cache s zEngine.clear_compiled_cacherrcKs.|j|||j||_|j||dS)aUpdate the default execution_options dictionary of this :class:`_engine.Engine`. The given keys/values in \**opt are added to the default execution options that will be used for all connections. The initial contents of this dictionary can be sent via the ``execution_options`` parameter to :func:`_sa.create_engine`. .. seealso:: :meth:`_engine.Connection.execution_options` :meth:`_engine.Engine.execution_options` N)rIZset_engine_execution_optionsrLrrGrrbrbrcr szEngine.update_execution_options.)rrrrryr OptionEngine)rrrrryrrkcKsdSrrb)r`rrrrryrrbrbrcr s zEngine.execution_optionscKsdSrrbrrbrbrcr scKs |||S)a*Return a new :class:`_engine.Engine` that will provide :class:`_engine.Connection` objects with the given execution options. The returned :class:`_engine.Engine` remains related to the original :class:`_engine.Engine` in that it shares the same connection pool and other state: * The :class:`_pool.Pool` used by the new :class:`_engine.Engine` is the same instance. The :meth:`_engine.Engine.dispose` method will replace the connection pool instance for the parent engine as well as this one. * Event listeners are "cascaded" - meaning, the new :class:`_engine.Engine` inherits the events of the parent, and new events can be associated with the new :class:`_engine.Engine` individually. * The logging configuration and logging_name is copied from the parent :class:`_engine.Engine`. The intent of the :meth:`_engine.Engine.execution_options` method is to implement schemes where multiple :class:`_engine.Engine` objects refer to the same connection pool, but are differentiated by options that affect some execution-level behavior for each engine. One such example is breaking into separate "reader" and "writer" :class:`_engine.Engine` instances, where one :class:`_engine.Engine` has a lower :term:`isolation level` setting configured or is even transaction-disabled using "autocommit". An example of this configuration is at :ref:`dbapi_autocommit_multiple`. Another example is one that uses a custom option ``shard_id`` which is consumed by an event to change the current schema on a database connection:: from sqlalchemy import event from sqlalchemy.engine import Engine primary_engine = create_engine("mysql+mysqldb://") shard1 = primary_engine.execution_options(shard_id="shard1") shard2 = primary_engine.execution_options(shard_id="shard2") shards = {"default": "base", "shard_1": "db1", "shard_2": "db2"} @event.listens_for(Engine, "before_cursor_execute") def _switch_shard(conn, cursor, stmt, params, context, executemany): shard_id = conn.get_execution_options().get('shard_id', "default") current_shard = conn.info.get("current_shard", None) if current_shard != shard_id: cursor.execute("use %s" % shards[shard_id]) conn.info["current_shard"] = shard_id The above recipe illustrates two :class:`_engine.Engine` objects that will each serve as factories for :class:`_engine.Connection` objects that have pre-established "shard_id" execution options present. A :meth:`_events.ConnectionEvents.before_cursor_execute` event handler then interprets this execution option to emit a MySQL ``use`` statement to switch databases before a statement execution, while at the same time keeping track of which database we've established using the :attr:`_engine.Connection.info` dictionary. .. seealso:: :meth:`_engine.Connection.execution_options` - update execution options on a :class:`_engine.Connection` object. :meth:`_engine.Engine.update_execution_options` - update the execution options for a given :class:`_engine.Engine` in place. :meth:`_engine.Engine.get_execution_options` )rrrbrbrcr sNcCs|jS)zGet the non-SQL options which will take effect during execution. .. versionadded: 1.3 .. seealso:: :meth:`_engine.Engine.execution_options` rrrbrbrcr9 s zEngine.get_execution_optionscCs|jjS)zsString name of the :class:`~sqlalchemy.engine.interfaces.Dialect` in use by this :class:`Engine`. )rGr~rrbrbrcr~D sz Engine.namecCs|jjS)zsDriver name of the :class:`~sqlalchemy.engine.interfaces.Dialect` in use by this :class:`Engine`. )rGdriverrrbrbrcrM sz Engine.drivercCs d|jfS)Nz Engine(%r))rrrbrbrc__repr__X szEngine.__repr__T)rrkcCs*|r|j|j|_|j|dS)aDispose of the connection pool used by this :class:`_engine.Engine`. A new connection pool is created immediately after the old one has been disposed. The previous connection pool is disposed either actively, by closing out all currently checked-in connections in that pool, or passively, by losing references to it but otherwise not closing any connections. The latter strategy is more appropriate for an initializer in a forked Python process. :param close: if left at its default of ``True``, has the effect of fully closing all **currently checked in** database connections. Connections that are still checked out will **not** be closed, however they will no longer be associated with this :class:`_engine.Engine`, so when they are closed individually, eventually the :class:`_pool.Pool` which they are associated with will be garbage collected and they will be closed out fully, if not already closed on checkin. If set to ``False``, the previous connection pool is de-referenced, and otherwise not touched in any way. .. versionadded:: 1.4.33 Added the :paramref:`.Engine.dispose.close` parameter to allow the replacement of a connection pool in a child process without interfering with the connections used by the parent process. .. seealso:: :ref:`engine_disposal` :ref:`pooling_multiprocessing` N)r8disposeZrecreaterIZengine_disposed)r`rrbrbrcr[ s%  zEngine.disposerazIterator[Connection])rRrkc cs.|dkr$| }|VW5QRXn|VdSr)connect)r`rRrrbrbrc_optional_conn_ctx_manager s z!Engine._optional_conn_ctx_managerc cs2| }| |VW5QRXW5QRXdS)a7Return a context manager delivering a :class:`_engine.Connection` with a :class:`.Transaction` established. E.g.:: with engine.begin() as conn: conn.execute( text("insert into table (x, y, z) values (1, 2, 3)") ) conn.execute(text("my_special_procedure(5)")) Upon successful operation, the :class:`.Transaction` is committed. If an error is raised, the :class:`.Transaction` is rolled back. .. seealso:: :meth:`_engine.Engine.connect` - procure a :class:`_engine.Connection` from an :class:`_engine.Engine`. :meth:`_engine.Connection.begin` - start a :class:`.Transaction` for a particular :class:`_engine.Connection`. N)rr)r`rrbrbrcr s  z Engine.beginrQr@rRc Ks(|}|j||f|W5QRXdSr)rrV)r`rSrTrUrrbrbrcrV s zEngine._run_ddl_visitorrFcCs ||S)a<Return a new :class:`_engine.Connection` object. The :class:`_engine.Connection` acts as a Python context manager, so the typical use of this method looks like:: with engine.connect() as connection: connection.execute(text("insert into table values ('foo')")) connection.commit() Where above, after the block is completed, the connection is "closed" and its underlying DBAPI resources are returned to the connection pool. This also has the effect of rolling back any transaction that was explicitly begun or was begun via autobegin, and will emit the :meth:`_events.ConnectionEvents.rollback` event if one was started and is still in progress. .. seealso:: :meth:`_engine.Engine.begin` )rrrbrbrcr szEngine.connectr6cCs |jS)aCReturn a "raw" DBAPI connection from the connection pool. The returned object is a proxied version of the DBAPI connection object used by the underlying driver in use. The object will have all the same behavior as the real DBAPI connection, except that its ``close()`` method will result in the connection being returned to the pool, rather than being closed for real. This method provides direct DBAPI connection access for special situations when the API provided by :class:`_engine.Connection` is not needed. When a :class:`_engine.Connection` object is already present, the DBAPI connection is available using the :attr:`_engine.Connection.connection` accessor. .. seealso:: :ref:`dbapi_connections` )r8rrrbrbrcrV szEngine.raw_connection)NNrNF)T)N)$rWrXrYrZr[rDrLrSrFrr\rr{rdrr]rQrrr rrr~rrZ echo_propertyrrr contextlibcontextmanagerrrrVrrVrbrbrbrcrOc sf          P *  rOc@seZdZUdZded<ded<ded<ded <d ed <d ed <ded<dddddZdddddZejse ddddZ e j dddddZ e d ddd Z e j d dd!d"d Z d#S)$OptionEngineMixinFrHrIrrr.rGr5r8r1rrPr"zlog.echo_propertyrrOr-)proxiedrcCsn||_|j|_|j|_|j|_|j|_|j|_|j|_tj||jd|j |j |_ |j |_ |j f|dS)Nr) _proxiedrrGrrrr"rrrIr_rLr)r`rrrbrbrcrd szOptionEngineMixin.__init__rrgrcKs tdSrrfrrbrbrcr sz*OptionEngineMixin.update_execution_optionsrxcCs|jjSrrr8rrbrbrcr8 szOptionEngineMixin.pool)r8rkcCs ||j_dSrr)r`r8rbrbrcr8 scCs|jjp|jddS)NrSF)rrS__dict__rzrrbrbrcrS szOptionEngineMixin._has_events)rrkcCs||jd<dS)NrS)r)r`rrbrbrcrS% sN) rWrXrYZ_sa_propagate_class_eventsr[rdrtyping TYPE_CHECKINGr]r8setterrSrbrbrbrcr s& rc@seZdZdddddZdS)rrrgrcKstj|f|dSr)rOrrrbrbrcr+ sz%OptionEngine.update_execution_optionsN)rWrXrYrrbrbrbrcr* sr)crZ __future__rrr=rrrrrrrr r r r r rrrZ interfacesrrrrrrrr rrrrrrrsqlr!r#rr"r#r$r%r&r'r(r)r*r+r,r-r.r/Z reflectionr0rr1eventr2r3r8r4r5r6r7Z sql._typingr8Z sql.compilerr9Zsql.ddlr:r;r<Z sql.functionsr=Z sql.schemar>r?r@Zsql.selectablerArB EMPTY_DICTrDr[rEZ InspectablerFrErcrrrZ IdentifiedrOrrrrbrbrbrcs                                                                Y/lr)  A