Merge a result into the given :class:`.Query` object's Session. See :meth:`_orm.Query.merge_result` for top-level documentation on this function.
(
query: Query[Any],
iterator: Union[FrozenResult, Iterable[Sequence[Any]], Iterable[object]],
load: bool = True,
)
| 372 | ) |
| 373 | @util.preload_module("sqlalchemy.orm.context") |
| 374 | def merge_result( |
| 375 | query: Query[Any], |
| 376 | iterator: Union[FrozenResult, Iterable[Sequence[Any]], Iterable[object]], |
| 377 | load: bool = True, |
| 378 | ) -> Union[FrozenResult, Iterable[Any]]: |
| 379 | """Merge a result into the given :class:`.Query` object's Session. |
| 380 | |
| 381 | See :meth:`_orm.Query.merge_result` for top-level documentation on this |
| 382 | function. |
| 383 | |
| 384 | """ |
| 385 | |
| 386 | querycontext = util.preloaded.orm_context |
| 387 | |
| 388 | session = query.session |
| 389 | if load: |
| 390 | # flush current contents if we expect to load data |
| 391 | session._autoflush() |
| 392 | |
| 393 | # TODO: need test coverage and documentation for the FrozenResult |
| 394 | # use case. |
| 395 | if isinstance(iterator, FrozenResult): |
| 396 | frozen_result = iterator |
| 397 | iterator = iter(frozen_result.data) |
| 398 | else: |
| 399 | frozen_result = None |
| 400 | |
| 401 | ctx = querycontext._ORMSelectCompileState._create_entities_collection( |
| 402 | query, legacy=True |
| 403 | ) |
| 404 | |
| 405 | autoflush = session.autoflush |
| 406 | try: |
| 407 | session.autoflush = False |
| 408 | single_entity = not frozen_result and len(ctx._entities) == 1 |
| 409 | |
| 410 | if single_entity: |
| 411 | if isinstance(ctx._entities[0], querycontext._MapperEntity): |
| 412 | result = [ |
| 413 | session._merge( |
| 414 | attributes.instance_state(instance), |
| 415 | attributes.instance_dict(instance), |
| 416 | load=load, |
| 417 | _recursive={}, |
| 418 | _resolve_conflict_map={}, |
| 419 | ) |
| 420 | for instance in iterator |
| 421 | ] |
| 422 | else: |
| 423 | result = list(iterator) |
| 424 | else: |
| 425 | mapped_entities = [ |
| 426 | i |
| 427 | for i, e in enumerate(ctx._entities) |
| 428 | if isinstance(e, querycontext._MapperEntity) |
| 429 | ] |
| 430 | result = [] |
| 431 | keys = [ent._label_name for ent in ctx._entities] |
nothing calls this directly
no test coverage detected