本文整理汇总了Python中sqlalchemy.orm.object_session方法的典型用法代码示例。如果您正苦于以下问题:Python orm.object_session方法的具体用法?Python orm.object_session怎么用?Python orm.object_session使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.object_session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tracked_operation
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def tracked_operation(func):
@wraps(func)
def wrapper(self, mapper, connection, target):
if not is_versioned(target):
return
session = object_session(target)
conn = session.connection()
try:
uow = self.units_of_work[conn]
except KeyError:
try:
uow = self.units_of_work[conn.engine]
except KeyError:
for connection in self.units_of_work.keys():
if not connection.closed and connection.connection is conn.connection:
uow = self.unit_of_work(session)
break # The ConnectionFairy is the same, this connection is a clone
else:
raise
return func(self, uow, target)
return wrapper
示例2: move_before
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def move_before(self, node_id):
""" Moving one node of tree before another
For example see:
* :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_before_function`
* :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_other_tree`
* :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_top_level`
""" # noqa
session = Session.object_session(self)
table = _get_tree_table(self.__mapper__)
pk = getattr(table.c, self.get_pk_column().name)
node = session.query(table).filter(pk == node_id).one()
self.parent_id = node.parent_id
self.mptt_move_before = node_id
session.add(self)
示例3: leftsibling_in_level
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def leftsibling_in_level(self):
""" Node to the left of the current node at the same level
For example see
:mod:`sqlalchemy_mptt.tests.cases.get_tree.test_leftsibling_in_level`
""" # noqa
table = _get_tree_table(self.__mapper__)
session = Session.object_session(self)
current_lvl_nodes = (
session.query(table)
.filter_by(level=self.level)
.filter_by(tree_id=self.tree_id)
.filter(table.c.lft < self.left)
.order_by(table.c.lft)
.all()
)
if current_lvl_nodes:
return current_lvl_nodes[-1]
return None
示例4: routes
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def routes(self):
"""
return list of routes servicing this stop
@todo: rewrite the cache to use timeout checking in Base.py
"""
try:
self._routes
except AttributeError:
from gtfsdb.model.route import Route
from gtfsdb.model.trip import Trip
from gtfsdb.model.stop_time import StopTime
session = object_session(self)
q = session.query(Route)
f = ((StopTime.stop_id == self.stop_id) & (StopTime.departure_time != ''))
q = q.filter(Route.trips.any(Trip.stop_times.any(f)))
q = q.order_by(Route.route_sort_order)
self._routes = q.all()
return self._routes
示例5: headsigns
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def headsigns(self):
"""
Returns a dictionary of all unique (route_id, headsign) tuples used
at the stop and the number of trips the head sign is used
"""
if not hasattr(self, '_headsigns'):
from gtfsdb.model.stop_time import StopTime
self._headsigns = defaultdict(int)
session = object_session(self)
log.info("QUERY StopTime")
q = session.query(StopTime)
q = q.options(joinedload_all('trip.route'))
q = q.filter_by(stop_id=self.stop_id)
for r in q:
headsign = r.stop_headsign or r.trip.trip_headsign
self._headsigns[(r.trip.route, headsign)] += 1
return self._headsigns
示例6: _answer_stat
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def _answer_stat(survey_node: AnswerableSurveyNode,
allowable_types: set,
func: Function) -> object:
type_constraint = survey_node.the_type_constraint
if type_constraint not in allowable_types:
raise InvalidTypeForOperation(
(type_constraint, func._FunctionGenerator__names[0])
)
answer_cls = ANSWER_TYPES[survey_node.the_type_constraint]
return (
object_session(survey_node)
.scalar(
sa.select([func(answer_cls.main_answer)])
.select_from(Answer.__table__.join(
answer_cls.__table__, Answer.id == answer_cls.id
))
.where(Answer.survey_node_id == survey_node.id)
)
)
示例7: _test_extra_dirty_state
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def _test_extra_dirty_state(self):
users, User = self.tables.users, self.classes.User
m = mapper(User, users)
s = Session()
@event.listens_for(m, "after_update")
def e(mapper, conn, target):
sess = object_session(target)
for entry in list(sess.identity_map.values()):
entry.name = "5"
a1, a2 = User(name="1"), User(name="2")
s.add_all([a1, a2])
s.commit()
a1.name = "3"
return s, a1, a2
示例8: test_deleted_auto_expunged
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def test_deleted_auto_expunged(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = Session()
sess.add(User(name="x"))
sess.commit()
u1 = sess.query(User).first()
sess.delete(u1)
assert not was_deleted(u1)
sess.flush()
assert was_deleted(u1)
assert u1 not in sess
assert object_session(u1) is sess
sess.commit()
assert object_session(u1) is None
示例9: test_explicit_expunge_pending
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def test_explicit_expunge_pending(self):
users, User = self.tables.users, self.classes.User
mapper(User, users)
sess = Session()
u1 = User(name="x")
sess.add(u1)
sess.flush()
sess.expunge(u1)
assert u1 not in sess
assert object_session(u1) is None
sess.rollback()
assert u1 not in sess
assert object_session(u1) is None
示例10: _update_many_to_many
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def _update_many_to_many(self, table, parent_id_name, child_id_name, parent_id_val, new_list, old_list=None):
"""Perfomr upsert on a many to many association table
Does NOT commit changes, parent should optimize when it needs to commit
raises exception if ids don't exist in the DB
"""
session = object_session(self)
old_set = {x for x in old_list} if old_list else set()
new_set = {x for x in new_list} if new_list else set()
# Update many-to-many relations
# Remove old relations and apply the new ones
if old_set != new_set:
to_add = new_set - old_set
to_del = old_set - new_set
if to_del:
session.execute(
table.delete().where(
and_(table.c[parent_id_name] == parent_id_val, table.c[child_id_name].in_(to_del))
)
)
if to_add:
session.execute(table.insert().values([(parent_id_val, my_id) for my_id in to_add]))
示例11: move_inside
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def move_inside(self, parent_id):
""" Moving one node of tree inside another
For example see:
* :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_inside_function`
* :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_inside_to_the_same_parent_function`
""" # noqa
session = Session.object_session(self)
self.parent_id = parent_id
self.mptt_move_inside = parent_id
session.add(self)
示例12: move_after
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def move_after(self, node_id):
""" Moving one node of tree after another
For example see :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_after_function`
""" # noqa
session = Session.object_session(self)
self.parent_id = self.parent_id
self.mptt_move_after = node_id
session.add(self)
示例13: _base_query_obj
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def _base_query_obj(self, session=None):
if not session:
session = object_session(self)
return self._base_query(session)
示例14: before_insert
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def before_insert(self, mapper, connection, instance):
session = object_session(instance)
self.instances[session].add(instance)
mptt_before_insert(mapper, connection, instance)
示例15: before_update
# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import object_session [as 别名]
def before_update(self, mapper, connection, instance):
session = object_session(instance)
self.instances[session].add(instance)
mptt_before_update(mapper, connection, instance)