Horizontal sharding support.
Defines a rudimental ‘horizontal sharding’ system which allows a Session to distribute queries and persistence operations across multiple databases.
For a usage example, see the Horizontal Sharding example included in the source distribution.
sqlalchemy.ext.horizontal_shard.ShardedSession(shard_chooser, id_chooser, query_chooser, shards=None, query_cls=<class 'sqlalchemy.ext.horizontal_shard.ShardedQuery'>, **kwargs)¶Bases: sqlalchemy.orm.session.Session
__init__(shard_chooser, id_chooser, query_chooser, shards=None, query_cls=<class 'sqlalchemy.ext.horizontal_shard.ShardedQuery'>, **kwargs)¶Construct a ShardedSession.
| Parameters: |
|
|---|
connection(mapper=None, instance=None, shard_id=None, **kwargs)¶Return a Connection object corresponding to this
Session object’s transactional state.
If this Session is configured with autocommit=False,
either the Connection corresponding to the current
transaction is returned, or if no transaction is in progress, a new
one is begun and the Connection returned (note that no
transactional state is established with the DBAPI until the first
SQL statement is emitted).
Alternatively, if this Session is configured with
autocommit=True, an ad-hoc Connection is returned
using Engine.contextual_connect() on the underlying
Engine.
Ambiguity in multi-bind or unbound Session objects can be
resolved through any of the optional keyword arguments. This
ultimately makes usage of the get_bind() method for resolution.
| Parameters: |
|
|---|
get_bind(mapper, shard_id=None, instance=None, clause=None, **kw)¶Return a “bind” to which this Session is bound.
The “bind” is usually an instance of Engine,
except in the case where the Session has been
explicitly bound directly to a Connection.
For a multiply-bound or unbound Session, the
mapper or clause 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 a Session.query(), each
individual INSERT/UPDATE/DELETE operation within a
Session.flush(), call, etc.
The order of resolution is:
Table objects
found in the given clause present in session.binds.MetaData ultimately
associated with the clause.MetaData ultimately
associated with the Table or other
selectable to which the mapper is mapped.UnboundExecutionError
is raised.| Parameters: |
|
|---|
sqlalchemy.ext.horizontal_shard.ShardedQuery(*args, **kwargs)¶Bases: sqlalchemy.orm.query.Query
set_shard(shard_id)¶return a new query, limited to a single shard ID.
all subsequent operations with the returned query will be against the single shard regardless of other state.