U a\ @s&ddlmZddlmZddlmZddlmZddlmZddlmZddl m Z ddl m Z dd lmZdd lmZdd lmZd d ZGdddZejeddggddddgdGdddeeeZejeddgdddgdddd d!d"d#gdGd$d%d%eeZGd&d'd'eeZd(d)Zd*S)+)exc)ProxyComparable)StartableContext) AsyncResult)util) create_engine)NestedTransaction) Connection)Engine)greenlet_spawncOs0|ddrtdd|d<t||}t|S)aECreate a new async engine instance. Arguments passed to :func:`_asyncio.create_async_engine` are mostly identical to those passed to the :func:`_sa.create_engine` function. The specified dialect must be an asyncio-compatible dialect such as :ref:`dialect-postgresql-asyncpg`. .. versionadded:: 1.4 Zserver_side_cursorsFz}Can't set server_side_cursors for async engine globally; use the connection.stream() method for an async streaming result setTfuture)get async_excAsyncMethodRequired_create_engine AsyncEngine)argkw sync_enginer^C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\ext\asyncio\engine.pycreate_async_engines  rc@seZdZdZdS)AsyncConnectable)Z_slots_dispatch __weakref__N)__name__ __module__ __qualname__ __slots__rrrrr+srz:class:`_future.Connection`z!:class:`_asyncio.AsyncConnection`closedZ invalidateddialectZdefault_isolation_level)Z classmethodsmethods attributesc@s*eZdZdZdZd=ddZeddZd>d d Ze d d Z d dZ e ddZ e ddZ ddZddZddZd?ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zdejfd/d0Zdejfd1d2Zdejfd3d4Z dejfd5d6Z!d7d8Z"d9d:Z#d;d<Z$dS)@AsyncConnectionaAn asyncio proxy for a :class:`_engine.Connection`. :class:`_asyncio.AsyncConnection` is acquired using the :meth:`_asyncio.AsyncEngine.connect` method of :class:`_asyncio.AsyncEngine`:: from sqlalchemy.ext.asyncio import create_async_engine engine = create_async_engine("postgresql+asyncpg://user:pass@host/dbname") async with engine.connect() as conn: result = await conn.execute(select(table)) .. versionadded:: 1.4 )rsync_connectionNcCs||_|j|_|||_dSN)enginer_assign_proxiedr$)self async_enginer$rrr__init__WszAsyncConnection.__init__cCstt|j|Sr%)r#r_retrieve_proxy_for_targetr&clstargetrrr_regenerate_proxy_for_target\s z,AsyncConnection._regenerate_proxy_for_targetFcs.|jrtd|t|jjIdH|_|S)zzStart this :class:`_asyncio.AsyncConnection` object's context outside of using a Python ``with:`` block. zconnection is already startedN)r$rInvalidRequestErrorr'r rconnectr( is_ctxmanagerrrrstartbs  zAsyncConnection.startcCstddS)zfNot implemented for async; call :meth:`_asyncio.AsyncConnection.get_raw_connection`. zAsyncConnection.connection accessor is not implemented as the attribute may need to reconnect on an invalidated connection. Use the get_raw_connection() method.N)rr0r(rrr connectionnszAsyncConnection.connectioncs|}tt|dIdHS)a"Return the pooled DBAPI-level connection in use by this :class:`_asyncio.AsyncConnection`. This is typically the SQLAlchemy connection-pool proxied connection which then has an attribute .connection that refers to the actual DBAPI-level connection. r6N)_sync_connectionr getattrr(connrrrget_raw_connectionzsz"AsyncConnection.get_raw_connectioncCs|jSr%)r$r5rrr_proxiedszAsyncConnection._proxiedcCs|jjS)aReturn the :attr:`_engine.Connection.info` dictionary of the underlying :class:`_engine.Connection`. This dictionary is freely writable for user-defined state to be associated with the database connection. This attribute is only available if the :class:`.AsyncConnection` is currently connected. If the :attr:`.AsyncConnection.closed` attribute is ``True``, then accessing this attribute will raise :class:`.ResourceClosedError`. .. versionadded:: 1.4.0b2 )r$infor5rrrr=szAsyncConnection.infocCs|js||jSr%)r$_raise_for_not_startedr5rrrr7sz AsyncConnection._sync_connectioncCs|t|S)z1Begin a transaction prior to autobegin occurring.r7AsyncTransactionr5rrrbeginszAsyncConnection.begincCs|t|ddS)z;Begin a nested transaction and return a transaction handle.T)nestedr?r5rrr begin_nestedszAsyncConnection.begin_nestedcs|}t|j|dIdHS)zInvalidate the underlying DBAPI connection associated with this :class:`_engine.Connection`. See the method :meth:`_engine.Connection.invalidate` for full detail on this method. ) exceptionN)r7r invalidate)r(rDr:rrrrEs zAsyncConnection.invalidatecs|}t|jIdHSr%r7r get_isolation_levelr9rrrrGsz#AsyncConnection.get_isolation_levelcs|}t|jIdHSr%rFr9rrrset_isolation_levelsz#AsyncConnection.set_isolation_levelcCs|}|SzYReturn True if a transaction is in progress. .. versionadded:: 1.4.0b2 )r7in_transactionr9rrrrJszAsyncConnection.in_transactioncCs|}|SrI)r7in_nested_transactionr9rrrrKsz%AsyncConnection.in_nested_transactioncCs*|}|}|dk r"t|SdSdS)aReturn an :class:`.AsyncTransaction` representing the current transaction, if any. This makes use of the underlying synchronous connection's :meth:`_engine.Connection.get_transaction` method to get the current :class:`_engine.Transaction`, which is then proxied in a new :class:`.AsyncTransaction` object. .. versionadded:: 1.4.0b2 N)r7get_transactionr@r+r(r:ZtransrrrrLs  zAsyncConnection.get_transactioncCs*|}|}|dk r"t|SdSdS)aReturn an :class:`.AsyncTransaction` representing the current nested (savepoint) transaction, if any. This makes use of the underlying synchronous connection's :meth:`_engine.Connection.get_nested_transaction` method to get the current :class:`_engine.Transaction`, which is then proxied in a new :class:`.AsyncTransaction` object. .. versionadded:: 1.4.0b2 N)r7get_nested_transactionr@r+rMrrrrNs  z&AsyncConnection.get_nested_transactioncs,|}t|jf|IdH}||ks(t|S)a'Set non-SQL options for the connection which take effect during execution. This returns this :class:`_asyncio.AsyncConnection` object with the new options added. See :meth:`_future.Connection.execution_options` for full details on this method. N)r7r execution_optionsAssertionError)r(optr:c2rrrrOs  z!AsyncConnection.execution_optionscs|}t|jIdHdS)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:`_future.Connection` automatically whenever a statement is first executed, or when the :meth:`_future.Connection.begin` method is called. N)r7r commitr9rrrrS s zAsyncConnection.commitcs|}t|jIdHdS)a&Roll 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:`_future.Connection` automatically whenever a statement is first executed, or when the :meth:`_future.Connection.begin` method is called. N)r7r rollbackr9rrrrTszAsyncConnection.rollbackcs|}t|jIdHdS)zClose this :class:`_asyncio.AsyncConnection`. This has the effect of also rolling back the transaction if one is in place. N)r7r closer9rrrrU-szAsyncConnection.closecs8|}t|j|||ddIdH}|jjr4td|S)zaExecutes a driver-level SQL string and return buffered :class:`_engine.Result`. TZ_require_awaitNzCan't use the connection.exec_driver_sql() method with a server-side cursor.Use the connection.stream() method for an async streaming result set.)r7r exec_driver_sqlcontext_is_server_siderrr( statement parametersrOr:resultrrrrW7s  zAsyncConnection.exec_driver_sqlc sL|}t|j||tj|ddiddIdH}|jjsDdsDtdt |S)zXExecute a statement and return a streaming :class:`_asyncio.AsyncResult` object.Zstream_resultsTrVNFzserver side result expected) r7r _execute_20r EMPTY_DICTZ merge_withrXrYrPrrZrrrstreamUs   zAsyncConnection.streamcs8|}t|j|||ddIdH}|jjr4td|S)aExecutes a SQL statement construct and return a buffered :class:`_engine.Result`. :param object: 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.DDLElement` :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:`_future.Connection.execution_options`. :return: a :class:`_engine.Result` object. TrVNzCan't use the connection.execute() method with a server-side cursor.Use the connection.stream() method for an async streaming result set.)r7r r^rXrYrrrZrrrexecutens% zAsyncConnection.executecs||||IdH}|S)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:`_future.Connection.execute` method. Parameters are equivalent. :return: a scalar Python value representing the first column of the first row returned. N)rascalar)r(r[r\rOr]rrrrbszAsyncConnection.scalarcs |}t||f||IdHS)aInvoke the given sync callable passing self as the first argument. This method maintains the asyncio event loop all the way through to the database connection by running the given callable in a specially instrumented greenlet. E.g.:: with async_engine.begin() as conn: await conn.run_sync(metadata.create_all) .. note:: The provided callable is invoked inline within the asyncio event loop, and will block on traditional IO calls. IO within this callable should only call into SQLAlchemy's asyncio database APIs which will be properly adapted to the greenlet context. .. seealso:: :ref:`session_run_sync` N)r7r )r(fnrrr:rrrrun_syncszAsyncConnection.run_synccCs |Sr%)r4 __await__r5rrrreszAsyncConnection.__await__cs|IdHdSr%)rUr(type_value tracebackrrr __aexit__szAsyncConnection.__aexit__)N)F)N)%rrr__doc__rr* classmethodr/r4propertyr6r;r<r=r7rArCrErGrHrJrKrLrNrOrSrTrUrr_rWr`rarbrdrerjrrrrr#/sT            !  : r#z:class:`_future.Engine`z:class:`_asyncio.AsyncEngine`Zclear_compiled_cacheZupdate_execution_optionsZget_execution_optionsurlpoolr&namedriverZechoc@sneZdZUdZdZeZeed<Gddde Z ddZ e dd Z d d Zd d ZddZddZddZdS)raPAn asyncio proxy for a :class:`_engine.Engine`. :class:`_asyncio.AsyncEngine` is acquired using the :func:`_asyncio.create_async_engine` function:: from sqlalchemy.ext.asyncio import create_async_engine engine = create_async_engine("postgresql+asyncpg://user:pass@host/dbname") .. versionadded:: 1.4 )rr<Z _option_clsc@s&eZdZddZd ddZddZdS) zAsyncEngine._trans_ctxcCs ||_dSr%)r:r9rrrr*szAsyncEngine._trans_ctx.__init__Fcs6|jj|dIdH|j|_|jIdH|jS)N)r3)r:r4rA transaction __aenter__r2rrrr4s zAsyncEngine._trans_ctx.startcs*|j|||IdH|jIdHdSr%)rrrjr:rUrfrrrrj sz AsyncEngine._trans_ctx.__aexit__N)F)rrrr*r4rjrrrr _trans_ctxs rtcCs4|jjstd|jjd|||_|_dS)NzFThe asyncio extension requires an async driver to be used. The loaded z is not async.)r Zis_asyncrr0rqr'rr<)r(rrrrr*s zAsyncEngine.__init__cCst|Sr%)rr,rrrr/sz(AsyncEngine._regenerate_proxy_for_targetcCs|}||S)aReturn a context manager which when entered will deliver an :class:`_asyncio.AsyncConnection` with an :class:`_asyncio.AsyncTransaction` established. E.g.:: async with async_engine.begin() as conn: await conn.execute( text("insert into table (x, y, z) values (1, 2, 3)") ) await conn.execute(text("my_special_procedure(5)")) )r1rtr9rrrrAszAsyncEngine.begincCs ||S)aReturn an :class:`_asyncio.AsyncConnection` object. The :class:`_asyncio.AsyncConnection` will procure a database connection from the underlying connection pool when it is entered as an async context manager:: async with async_engine.connect() as conn: result = await conn.execute(select(user_table)) The :class:`_asyncio.AsyncConnection` may also be started outside of a context manager by invoking its :meth:`_asyncio.AsyncConnection.start` method. )_connection_clsr5rrrr1,szAsyncEngine.connectcst|jjIdHS)zReturn a "raw" DBAPI connection from the connection pool. .. seealso:: :ref:`dbapi_connections` N)r rraw_connectionr5rrrrv>szAsyncEngine.raw_connectioncKst|jjf|S)aReturn a new :class:`_asyncio.AsyncEngine` that will provide :class:`_asyncio.AsyncConnection` objects with the given execution options. Proxied from :meth:`_future.Engine.execution_options`. See that method for details. )rrrO)r(rQrrrrOHs zAsyncEngine.execution_optionscst|jjIdHS)amDispose of the connection pool used by this :class:`_asyncio.AsyncEngine`. This will close all connection pool connections that are **currently checked in**. See the documentation for the underlying :meth:`_future.Engine.dispose` method for further notes. .. seealso:: :meth:`_future.Engine.dispose` N)r rdisposer5rrrrwTszAsyncEngine.disposeN)rrrrkrr#rutype__annotations__rrtr*rlr/rAr1rvrOrwrrrrrs    rc@seZdZdZdZdddZeddZdd Ze d d Z e d d Z e ddZ ddZ ddZddZdddZddZdS)r@z4An asyncio proxy for a :class:`_engine.Transaction`.r6sync_transactionrBFcCs||_d|_||_dSr%rz)r(r6rBrrrr*jszAsyncTransaction.__init__cCsP|j}|}t|t}t|}|dk s*t||}||_|||_||_ |Sr%) r6 isinstancer r#r+rP__new__r'r{rB)r-r.r$r{rBZasync_connectionobjrrrr/os    z-AsyncTransaction._regenerate_proxy_for_targetcCs|js||jSr%)r{r>r5rrr_sync_transactionsz"AsyncTransaction._sync_transactioncCs|jSr%)r{r5rrrr<szAsyncTransaction._proxiedcCs |jSr%)ris_validr5rrrrszAsyncTransaction.is_validcCs |jSr%)r is_activer5rrrrszAsyncTransaction.is_activecst|jIdHdS)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)r rrUr5rrrrUs zAsyncTransaction.closecst|jIdHdS)z%Roll back this :class:`.Transaction`.N)r rrTr5rrrrTszAsyncTransaction.rollbackcst|jIdHdS)z"Commit this :class:`.Transaction`.N)r rrSr5rrrrSszAsyncTransaction.commitcsB|t|jr|jjn |jjIdH|_|r>|j|S)z{Start this :class:`_asyncio.AsyncTransaction` object's context outside of using a Python ``with:`` block. N) r'r rBr6r7rCrAr{ __enter__r2rrrr4s  zAsyncTransaction.startcst|j|||IdHdSr%)r r__exit__rfrrrrjs zAsyncTransaction.__aexit__N)F)F)rrrrkrr*rlr/rrmr<rrrUrTrSr4rjrrrrr@es"       r@c CsPt|tr|jSz|jWStk rJ}ztd||W5d}~XYnXdS)NzAsyncEngine expected, got %r)r|r#r$rAttributeErrorr ArgumentError)r)errr_get_sync_engine_or_connections rN)rrbaserrr]rrr&rrZ engine.baser r r r Zutil.concurrencyr rrZcreate_proxy_methodsr#rr@rrrrrsT            " ~Y