[SQLAlchemy] synchronize_session參數詳解

來源:互聯網
上載者:User

標籤:

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 True13 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 match23         self.matched_objects = [24             obj for (cls, pk), obj in25             query.session.identity_map.items()26             if issubclass(cls, target_cls) and27             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])

[SQLAlchemy] synchronize_session參數詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.