class documentation

class Engine(Connectable, log.Identified):

Known subclasses: sqlalchemy.engine.base.OptionEngine

View In Hierarchy

Connects a ~sqlalchemy.pool.Pool and ~sqlalchemy.engine.interfaces.Dialect together to provide a source of database connectivity and behavior.

This is the SQLAlchemy 1.x version of _engine.Engine. For the :term:`2.0 style` version, which includes some API differences, see _future.Engine.

An _engine.Engine object is instantiated publicly using the ~sqlalchemy.create_engine function.

Class ​_trans​_ctx Undocumented
Method __init__ Undocumented
Method __repr__ Undocumented
Method ​_execute​_clauseelement Undocumented
Method ​_execute​_compiled Undocumented
Method ​_execute​_default Undocumented
Method ​_lru​_size​_alert Undocumented
Method ​_optional​_conn​_ctx​_manager Undocumented
Method ​_run​_ddl​_visitor Undocumented
Method ​_wrap​_pool​_connect Undocumented
Method begin Return a context manager delivering a _engine.Connection with a .Transaction established.
Method clear​_compiled​_cache Clear the compiled cache associated with the dialect.
Method connect Return a new _engine.Connection object.
Method dispose Dispose of the connection pool used by this _engine.Engine.
Method execute Executes the given construct and returns a _engine.CursorResult.
Method execution​_options Return a new _engine.Engine that will provide _engine.Connection objects with the given execution options.
Method get​_execution​_options Get the non-SQL options which will take effect during execution.
Method has​_table Return True if the given backend has a table of the given name.
Method raw​_connection Return a "raw" DBAPI connection from the connection pool.
Method run​_callable Given a callable object or function, execute it, passing a _engine.Connection as the first argument.
Method scalar Executes and returns the first column of the first row.
Method table​_names Return a list of all table names available in the database.
Method transaction Execute the given function within a transaction boundary.
Method update​_execution​_options Update the default execution_options dictionary of this _engine.Engine.
Class Variable ​_has​_events Undocumented
Class Variable ​_is​_future Undocumented
Class Variable ​_schema​_translate​_map Undocumented
Class Variable ​_sqla​_logger​_namespace Undocumented
Instance Variable ​_compiled​_cache Undocumented
Instance Variable ​_execution​_options Undocumented
Instance Variable dialect Undocumented
Instance Variable echo Undocumented
Instance Variable hide​_parameters Undocumented
Instance Variable logging​_name Undocumented
Instance Variable pool Undocumented
Instance Variable url Undocumented
Property driver Driver name of the ~sqlalchemy.engine.interfaces.Dialect in use by this Engine.
Property engine The _engine.Engine instance referred to by this .Connectable.
Property name String name of the ~sqlalchemy.engine.interfaces.Dialect in use by this Engine.

Inherited from Connectable:

Method ​_run​_visitor Undocumented

Inherited from Identified:

Method ​_should​_log​_debug Undocumented
Method ​_should​_log​_info Undocumented
def __init__(self, pool, dialect, url, logging_name=None, echo=None, query_cache_size=500, execution_options=None, hide_parameters=False):

Undocumented

def __repr__(self):

Undocumented

def _execute_clauseelement(self, elem, multiparams=None, params=None, execution_options=_EMPTY_EXECUTION_OPTS):
def _execute_compiled(self, compiled, multiparams, params, execution_options=_EMPTY_EXECUTION_OPTS):

Undocumented

def _execute_default(self, default, multiparams=(), params=util.EMPTY_DICT):

Undocumented

def _lru_size_alert(self, cache):

Undocumented

@contextlib.contextmanager
def _optional_conn_ctx_manager(self, connection=None):

Undocumented

def _run_ddl_visitor(self, visitorcallable, element, **kwargs):

Undocumented

def _wrap_pool_connect(self, fn, connection):

Undocumented

def begin(self, close_with_result=False):

Return a context manager delivering a _engine.Connection with a .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 .Transaction is committed. If an error is raised, the .Transaction is rolled back.

Legacy use only: the close_with_result flag is normally False, and indicates that the _engine.Connection will be closed when the operation is complete. When set to True, it indicates the _engine.Connection is in "single use" mode, where the _engine.CursorResult returned by the first call to _engine.Connection.execute will close the _engine.Connection when that _engine.CursorResult has exhausted all result rows.

See Also

_engine.Engine.connect - procure a _engine.Connection from an _engine.Engine.

_engine.Connection.begin - start a .Transaction for a particular _engine.Connection.

def clear_compiled_cache(self):

Clear 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.query_cache` parameter.

New in version 1.4.
def connect(self, close_with_result=False):

Return a new _engine.Connection object.

The _engine.Connection object is a facade that uses a DBAPI connection internally in order to communicate with the database. This connection is procured from the connection-holding _pool.Pool referenced by this _engine.Engine. When the _engine.Connection.close method of the _engine.Connection object is called, the underlying DBAPI connection is then returned to the connection pool, where it may be used again in a subsequent call to _engine.Engine.connect.

def dispose(self):

Dispose of the connection pool used by this _engine.Engine.

This 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 _engine.Engine, so when they are closed individually, eventually the _pool.Pool which they are associated with will be garbage collected and they will be closed out fully, if not already closed on checkin.

A new connection pool is created immediately after the old one has been disposed. This new pool, like all SQLAlchemy connection pools, does not make any actual connections to the database until one is first requested, so as long as the _engine.Engine isn't used again, no new connections will be made.

@util.deprecated_20(':meth:`_engine.Engine.execute`', alternative='All statement execution in SQLAlchemy 2.0 is performed by the :meth:`_engine.Connection.execute` method of :class:`_engine.Connection`, or in the ORM by the :meth:`.Session.execute` method of :class:`.Session`.')
def execute(self, statement, *multiparams, **params):

Executes the given construct and returns a _engine.CursorResult.

The arguments are the same as those used by _engine.Connection.execute.

Here, a _engine.Connection is acquired using the _engine.Engine.connect method, and the statement executed with that connection. The returned _engine.CursorResult is flagged such that when the _engine.CursorResult is exhausted and its underlying cursor is closed, the _engine.Connection created here will also be closed, which allows its associated DBAPI connection resource to be returned to the connection pool.

def execution_options(self, **opt):

Return a new _engine.Engine that will provide _engine.Connection objects with the given execution options.

The returned _engine.Engine remains related to the original _engine.Engine in that it shares the same connection pool and other state:

  • The _pool.Pool used by the new _engine.Engine is the same instance. The _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 _engine.Engine inherits the events of the parent, and new events can be associated with the new _engine.Engine individually.
  • The logging configuration and logging_name is copied from the parent _engine.Engine.

The intent of the _engine.Engine.execution_options method is to implement "sharding" schemes where multiple _engine.Engine objects refer to the same connection pool, but are differentiated by options that would be consumed by a custom event:

primary_engine = create_engine("mysql://")
shard1 = primary_engine.execution_options(shard_id="shard1")
shard2 = primary_engine.execution_options(shard_id="shard2")

Above, the shard1 engine serves as a factory for _engine.Connection objects that will contain the execution option shard_id=shard1, and shard2 will produce _engine.Connection objects that contain the execution option shard_id=shard2.

An event handler can consume the above execution option to perform a schema switch or other operation, given a connection. Below we emit a MySQL use statement to switch databases, at the same time keeping track of which database we've established using the _engine.Connection.info dictionary, which gives us a persistent storage space that follows the DBAPI connection:

from sqlalchemy import event
from sqlalchemy.engine import Engine

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._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

See Also

_engine.Connection.execution_options - update execution options on a _engine.Connection object.

_engine.Engine.update_execution_options - update the execution options for a given _engine.Engine in place.

_engine.Engine.get_execution_options

def get_execution_options(self):

Get the non-SQL options which will take effect during execution.

See Also

_engine.Engine.execution_options

@util.deprecated('1.4', 'The :meth:`_engine.Engine.has_table` method is deprecated and will be removed in a future release. Please refer to :meth:`_reflection.Inspector.has_table`.')
def has_table(self, table_name, schema=None):

Return True if the given backend has a table of the given name.

See Also

:ref:`metadata_reflection_inspector` - detailed schema inspection using the _reflection.Inspector interface.

.quoted_name - used to pass quoting information along with a schema identifier.

def raw_connection(self, _connection=None):

Return 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 _engine.Connection is not needed. When a _engine.Connection object is already present, the DBAPI connection is available using the _engine.Connection.connection accessor.

@util.deprecated('1.4', 'The :meth:`_engine.Engine.run_callable` method is deprecated and will be removed in a future release. Use the :meth:`_engine.Engine.begin` context manager instead.')
def run_callable(self, callable_, *args, **kwargs):

Given a callable object or function, execute it, passing a _engine.Connection as the first argument.

The given *args and **kwargs are passed subsequent to the _engine.Connection argument.

This function, along with _engine.Connection.run_callable, allows a function to be run with a _engine.Connection or _engine.Engine object without the need to know which one is being dealt with.

@util.deprecated_20(':meth:`_engine.Engine.scalar`', alternative='All statement execution in SQLAlchemy 2.0 is performed by the :meth:`_engine.Connection.execute` method of :class:`_engine.Connection`, or in the ORM by the :meth:`.Session.execute` method of :class:`.Session`; the :meth:`_future.Result.scalar` method can then be used to return a scalar result.')
def scalar(self, statement, *multiparams, **params):

Executes and returns the first column of the first row.

The underlying result/cursor is closed after execution.

@util.deprecated('1.4', 'The :meth:`_engine.Engine.table_names` method is deprecated and will be removed in a future release. Please refer to :meth:`_reflection.Inspector.get_table_names`.')
def table_names(self, schema=None, connection=None):
Return a list of all table names available in the database.
Parameters
schemaOptional, retrieve names from a non-default schema.
connectionOptional, use a specified connection.
@util.deprecated('1.4', 'The :meth:`_engine.Engine.transaction` method is deprecated and will be removed in a future release. Use the :meth:`_engine.Engine.begin` context manager instead.')
def transaction(self, callable_, *args, **kwargs):

Execute the given function within a transaction boundary.

The function is passed a _engine.Connection newly procured from _engine.Engine.connect as the first argument, followed by the given *args and **kwargs.

e.g.:

def do_something(conn, x, y):
    conn.execute(text("some statement"), {'x':x, 'y':y})

engine.transaction(do_something, 5, 10)

The operations inside the function are all invoked within the context of a single .Transaction. Upon success, the transaction is committed. If an exception is raised, the transaction is rolled back before propagating the exception.

Note

The .transaction method is superseded by the usage of the Python with: statement, which can be used with _engine.Engine.begin:

with engine.begin() as conn:
    conn.execute(text("some statement"), {'x':5, 'y':10})

See Also

_engine.Engine.begin - engine-level transactional context

_engine.Connection.transaction - connection-level version of _engine.Engine.transaction

def update_execution_options(self, **opt):

Update the default execution_options dictionary of this _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 _sa.create_engine.

See Also

_engine.Connection.execution_options

_engine.Engine.execution_options

_has_events: bool =

Undocumented

_is_future: bool =

Undocumented

_schema_translate_map =

Undocumented

_sqla_logger_namespace: str =

Undocumented

_compiled_cache =

Undocumented

_execution_options =

Undocumented

dialect =

Undocumented

echo =

Undocumented

hide_parameters =

Undocumented

logging_name =
pool =

Undocumented

url =

Undocumented

@property
driver =
Driver name of the ~sqlalchemy.engine.interfaces.Dialect in use by this Engine.
@property
engine =

The _engine.Engine instance referred to by this .Connectable.

May be self if this is already an _engine.Engine.

@property
name =
String name of the ~sqlalchemy.engine.interfaces.Dialect in use by this Engine.