self.transaction.prepare()
- def connection(self, mapper=None, clause=None, _state=None):
+ def connection(self, mapper=None, clause=None):
"""Return the active Connection.
Retrieves the ``Connection`` managing the current transaction. Any
Optional, any ``ClauseElement``
"""
- return self.__connection(self.get_bind(mapper, clause, _state))
+ return self.__connection(self.get_bind(mapper, clause))
def __connection(self, engine, **kwargs):
if self.transaction is not None:
else:
return engine.contextual_connect(**kwargs)
- def execute(self, clause, params=None, mapper=None, _state=None):
+ def execute(self, clause, params=None, mapper=None, **kw):
"""Execute a clause within the current transaction.
Returns a ``ResultProxy`` of execution results. `autocommit` Sessions
mapper
Optional, a ``mapper`` or mapped class
- _state
- Optional, an instance of a mapped class
-
+ \**kw
+ Additional keyword arguments are sent to :method:`get_bind()`
+ which locates a connectable to use for the execution.
+ Subclasses of :class:`Session` may override this.
+
"""
clause = expression._literal_as_text(clause)
- engine = self.get_bind(mapper, clause=clause, _state=_state)
+ engine = self.get_bind(mapper, clause=clause, **kw)
return self.__connection(engine, close_with_result=True).execute(
clause, params or {})
- def scalar(self, clause, params=None, mapper=None, _state=None):
+ def scalar(self, clause, params=None, mapper=None):
"""Like execute() but return a scalar result."""
- engine = self.get_bind(mapper, clause=clause, _state=_state)
+ engine = self.get_bind(mapper, clause=clause)
return self.__connection(engine, close_with_result=True).scalar(
clause, params or {})
"""
self.__binds[table] = bind
- def get_bind(self, mapper, clause=None, _state=None):
+ def get_bind(self, mapper, clause=None):
"""Return an engine corresponding to the given arguments.
All arguments are optional.
clause
Optional, A ClauseElement (i.e. select(), text(), etc.)
- _state
- Optional, SA internal representation of a mapped instance
-
"""
- if mapper is clause is _state is None:
+ if mapper is clause is None:
if self.bind:
return self.bind
else:
"Connection, and no context was provided to locate "
"a binding.")
- s_mapper = _state is not None and _state_mapper(_state) or None
c_mapper = mapper is not None and _class_to_mapper(mapper) or None
# manually bound?
if self.__binds:
- if s_mapper:
- if s_mapper.base_mapper in self.__binds:
- return self.__binds[s_mapper.base_mapper]
- elif s_mapper.mapped_table in self.__binds:
- return self.__binds[s_mapper.mapped_table]
if c_mapper:
if c_mapper.base_mapper in self.__binds:
return self.__binds[c_mapper.base_mapper]
if isinstance(clause, sql.expression.ClauseElement) and clause.bind:
return clause.bind
- if s_mapper and s_mapper.mapped_table.bind:
- return s_mapper.mapped_table.bind
if c_mapper and c_mapper.mapped_table.bind:
return c_mapper.mapped_table.bind
context.append('mapper %s' % c_mapper)
if clause is not None:
context.append('SQL expression')
- if _state is not None:
- context.append('state %r' % _state)
raise sa_exc.UnboundExecutionError(
"Could not locate a bind configured on %s or this Session" % (
from sqlalchemy.orm.shard import ShardedSession
from sqlalchemy.sql import operators
from testlib import *
+from testlib.testing import eq_
# TODO: ShardTest can be turned into a base for further subclasses
tokyo.city # reload 'city' attribute on tokyo
sess.clear()
- assert db2.execute(weather_locations.select()).fetchall() == [(1, 'Asia', 'Tokyo')]
- assert db1.execute(weather_locations.select()).fetchall() == [(2, 'North America', 'New York'), (3, 'North America', 'Toronto')]
-
+ eq_(db2.execute(weather_locations.select()).fetchall(), [(1, 'Asia', 'Tokyo')])
+ eq_(db1.execute(weather_locations.select()).fetchall(), [(2, 'North America', 'New York'), (3, 'North America', 'Toronto')])
+ eq_(sess.execute(weather_locations.select(), shard_id='asia').fetchall(), [(1, 'Asia', 'Tokyo')])
+
t = sess.query(WeatherLocation).get(tokyo.id)
- assert t.city == tokyo.city
- assert t.reports[0].temperature == 80.0
+ eq_(t.city, tokyo.city)
+ eq_(t.reports[0].temperature, 80.0)
north_american_cities = sess.query(WeatherLocation).filter(WeatherLocation.continent == 'North America')
- assert set([c.city for c in north_american_cities]) == set(['New York', 'Toronto'])
+ eq_(set([c.city for c in north_american_cities]), set(['New York', 'Toronto']))
asia_and_europe = sess.query(WeatherLocation).filter(WeatherLocation.continent.in_(['Europe', 'Asia']))
- assert set([c.city for c in asia_and_europe]) == set(['Tokyo', 'London', 'Dublin'])
+ eq_(set([c.city for c in asia_and_europe]), set(['Tokyo', 'London', 'Dublin']))