class Engine(Connectable, log.Identified):
Known subclasses: sqlalchemy.engine.base.OptionEngine
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 |
Undocumented
Undocumented
Undocumented
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
.
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.
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
.
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.
See Also
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.
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:
_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._engine.Engine
inherits the events of the parent, and new events can be associated
with the new _engine.Engine
individually._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
Get the non-SQL options which will take effect during execution.
See Also
_engine.Engine.execution_options
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.
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.
See Also
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.
Executes and returns the first column of the first row.
The underlying result/cursor is closed after execution.
Parameters | |
schema | Optional, retrieve names from a non-default schema. |
connection | Optional, use a specified connection. |
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
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