MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _flush

Method _flush

lib/sqlalchemy/orm/session.py:4538–4655  ·  view source on GitHub ↗
(self, objects: Optional[Sequence[object]] = None)

Source from the content-addressed store, hash-verified

4536 )
4537 )
4538 def _flush(self, objects: Optional[Sequence[object]] = None) -> None:
4539 dirty = self._dirty_states
4540 if not dirty and not self._deleted and not self._new:
4541 self.identity_map._modified.clear()
4542 return
4543
4544 flush_context = UOWTransaction(self)
4545
4546 if self.dispatch.before_flush:
4547 self.dispatch.before_flush(self, flush_context, objects)
4548 # re-establish "dirty states" in case the listeners
4549 # added
4550 dirty = self._dirty_states
4551
4552 deleted = set(self._deleted)
4553 new = set(self._new)
4554
4555 dirty = set(dirty).difference(deleted)
4556
4557 # create the set of all objects we want to operate upon
4558 if objects:
4559 # specific list passed in
4560 objset = set()
4561 for o in objects:
4562 try:
4563 state = attributes.instance_state(o)
4564
4565 except exc.NO_STATE as err:
4566 raise exc.UnmappedInstanceError(o) from err
4567 objset.add(state)
4568 else:
4569 objset = None
4570
4571 # store objects whose fate has been decided
4572 processed = set()
4573
4574 # put all saves/updates into the flush context. detect top-level
4575 # orphans and throw them into deleted.
4576 if objset:
4577 proc = new.union(dirty).intersection(objset).difference(deleted)
4578 else:
4579 proc = new.union(dirty).difference(deleted)
4580
4581 for state in proc:
4582 is_orphan = _state_mapper(state)._is_orphan(state)
4583
4584 is_persistent_orphan = is_orphan and state.has_identity
4585
4586 if (
4587 is_orphan
4588 and not is_persistent_orphan
4589 and state._orphaned_outside_of_session
4590 ):
4591 self._expunge_states([state])
4592 else:
4593 _reg = flush_context.register_object(
4594 state, isdelete=is_persistent_orphan
4595 )

Callers 1

flushMethod · 0.95

Calls 15

_expunge_statesMethod · 0.95
register_objectMethod · 0.95
_autobegin_tMethod · 0.95
executeMethod · 0.95
UOWTransactionClass · 0.85
_state_mapperFunction · 0.85
_is_orphanMethod · 0.80
_beginMethod · 0.80
after_flushMethod · 0.80
_commit_all_statesMethod · 0.80
after_flush_postexecMethod · 0.80

Tested by

no test coverage detected