assembl.lib.read_write_session module¶
-
class
assembl.lib.read_write_session.
ReadWriteSession
(bind=None, autoflush=False, read_bind=None, readonly=False, **options)[source]¶ Bases:
sqlalchemy.orm.session.Session
A session that can divert read queries to a different engine Inspired by https://gist.github.com/adhorn/b84dc47175259992d406
-
get_bind
(mapper=None, clause=None)[source]¶ Return a “bind” to which this
Session
is bound.The “bind” is usually an instance of
_engine.Engine
, except in the case where theSession
has been explicitly bound directly to a_engine.Connection
.For a multiply-bound or unbound
Session
, themapper
orclause
arguments are used to determine the appropriate bind to return.Note that the “mapper” argument is usually present when
Session.get_bind()
is called via an ORM operation such as aSession.query()
, each individual INSERT/UPDATE/DELETE operation within aSession.flush()
, call, etc.The order of resolution is:
if mapper given and :paramref:`.Session.binds` is present, locate a bind based first on the mapper in use, then on the mapped class in use, then on any base classes that are present in the
__mro__
of the mapped class, from more specific superclasses to more general.if clause given and
Session.binds
is present, locate a bind based on_schema.Table
objects found in the given clause present inSession.binds
.if
Session.binds
is present, return that.if clause given, attempt to return a bind linked to the
_schema.MetaData
ultimately associated with the clause.if mapper given, attempt to return a bind linked to the
_schema.MetaData
ultimately associated with the_schema.Table
or other selectable to which the mapper is mapped.No bind can be found,
UnboundExecutionError
is raised.
Note that the
Session.get_bind()
method can be overridden on a user-defined subclass ofSession
to provide any kind of bind resolution scheme. See the example at session_custom_partitioning.- Parameters
mapper – Optional
mapper()
mapped class or instance of_orm.Mapper
. The bind can be derived from a_orm.Mapper
first by consulting the “binds” map associated with thisSession
, and secondly by consulting the_schema.MetaData
associated with the_schema.Table
to which the_orm.Mapper
is mapped for a bind.clause – A
_expression.ClauseElement
(i.e._expression.select()
,_expression.text()
, etc.). If themapper
argument is not present or could not produce a bind, the given expression construct will be searched for a bound element, typically a_schema.Table
associated with bound_schema.MetaData
.
-