[SQLAlchemy] synchronize_session参数详解

synchronize_session用于query在进行delete or update操作时,对session的同步策略。

  • False - 不对session进行同步,直接进行delete or update操作。
  • 'fetch'

   在delete or update操作之前,先发一条sql到数据库获取符合条件的记录。

 1 def _do_pre_synchronize(self):
 2         query = self.query
 3         session = query.session
 4         context = query._compile_context()
 5         select_stmt = context.statement.with_only_columns(
 6             self.primary_table.primary_key)
 7         self.matched_rows = session.execute(
 8             select_stmt,
 9             mapper=self.mapper,
10             params=query._params).fetchall()
  在delete or update操作之后,将session的identity_map与前一步获取到的记录进行match,符合条件的就从session中删掉或更新。
 1 def _do_post_synchronize(self):
 2         session = self.query.session
 3         target_mapper = self.query._mapper_zero()
 4         for primary_key in self.matched_rows:
 5             # TODO: inline this and call remove_newly_deleted
 6             # once
 7             identity_key = target_mapper.identity_key_from_primary_key(
 8                 list(primary_key))
 9             if identity_key in session.identity_map:
10                 session._remove_newly_deleted(
11                     [attributes.instance_state(
12                         session.identity_map[identity_key]
13                     )]
14                 )
  • 'evaluate'

   在delete or update操作之前,用query中的条件直接对session的identity_map中的objects进行eval操作,将符合条件的记录下来。

 1 def _do_pre_synchronize(self):
 2         query = self.query
 3         target_cls = query._mapper_zero().class_
 4 
 5         try:
 6             evaluator_compiler = evaluator.EvaluatorCompiler(target_cls)
 7             if query.whereclause is not None:
 8                 eval_condition = evaluator_compiler.process(
 9                     query.whereclause)
10             else:
11                 def eval_condition(obj):
12                     return True
13 
14             self._additional_evaluators(evaluator_compiler)
15 
16         except evaluator.UnevaluatableError:
17             raise sa_exc.InvalidRequestError(
18                 "Could not evaluate current criteria in Python. "
19                 "Specify 'fetch' or False for the "
20                 "synchronize_session parameter.")
21 
22         # TODO: detect when the where clause is a trivial primary key match
23         self.matched_objects = [
24             obj for (cls, pk), obj in
25             query.session.identity_map.items()
26             if issubclass(cls, target_cls) and
27             eval_condition(obj)]

   在delete or update操作之后,将符合条件的记录删除或更新。

1 def _do_post_synchronize(self):
2         self.query.session._remove_newly_deleted(
3             [attributes.instance_state(obj)
4              for obj in self.matched_objects])

转载于:https://www.cnblogs.com/rednada/p/5520896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值