MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / merge_frozen_result

Function merge_frozen_result

lib/sqlalchemy/orm/loading.py:301–357  ·  view source on GitHub ↗

Merge a :class:`_engine.FrozenResult` back into a :class:`_orm.Session`, returning a new :class:`_engine.Result` object with :term:`persistent` objects. See the section :ref:`do_orm_execute_re_executing` for an example. .. seealso:: :ref:`do_orm_execute_re_executing`

(session, statement, frozen_result, load=True)

Source from the content-addressed store, hash-verified

299
300@util.preload_module("sqlalchemy.orm.context")
301def merge_frozen_result(session, statement, frozen_result, load=True):
302 """Merge a :class:`_engine.FrozenResult` back into a :class:`_orm.Session`,
303 returning a new :class:`_engine.Result` object with :term:`persistent`
304 objects.
305
306 See the section :ref:`do_orm_execute_re_executing` for an example.
307
308 .. seealso::
309
310 :ref:`do_orm_execute_re_executing`
311
312 :meth:`_engine.Result.freeze`
313
314 :class:`_engine.FrozenResult`
315
316 """
317 querycontext = util.preloaded.orm_context
318
319 if load:
320 # flush current contents if we expect to load data
321 session._autoflush()
322
323 ctx = querycontext.ORMSelectCompileState._create_entities_collection(
324 statement, legacy=False
325 )
326
327 autoflush = session.autoflush
328 try:
329 session.autoflush = False
330 mapped_entities = [
331 i
332 for i, e in enumerate(ctx._entities)
333 if isinstance(e, querycontext._MapperEntity)
334 ]
335 keys = [ent._label_name for ent in ctx._entities]
336
337 keyed_tuple = result_tuple(
338 keys, [ent._extra_entities for ent in ctx._entities]
339 )
340
341 result = []
342 for newrow in frozen_result.rewrite_rows():
343 for i in mapped_entities:
344 if newrow[i] is not None:
345 newrow[i] = session._merge(
346 attributes.instance_state(newrow[i]),
347 attributes.instance_dict(newrow[i]),
348 load=load,
349 _recursive={},
350 _resolve_conflict_map={},
351 )
352
353 result.append(keyed_tuple(newrow))
354
355 return frozen_result.with_new_rows(result)
356 finally:
357 session.autoflush = autoflush
358

Callers

nothing calls this directly

Calls 7

result_tupleFunction · 0.85
_autoflushMethod · 0.80
rewrite_rowsMethod · 0.80
with_new_rowsMethod · 0.80
_mergeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected