Represents a :class:`_engine.Result` object in a "frozen" state suitable for caching. The :class:`_engine.FrozenResult` object is returned from the :meth:`_engine.Result.freeze` method of any :class:`_engine.Result` object. A new iterable :class:`_engine.Result` object is gener
| 1729 | |
| 1730 | |
| 1731 | class FrozenResult(Generic[Unpack[_Ts]]): |
| 1732 | """Represents a :class:`_engine.Result` object in a "frozen" state suitable |
| 1733 | for caching. |
| 1734 | |
| 1735 | The :class:`_engine.FrozenResult` object is returned from the |
| 1736 | :meth:`_engine.Result.freeze` method of any :class:`_engine.Result` |
| 1737 | object. |
| 1738 | |
| 1739 | A new iterable :class:`_engine.Result` object is generated from a fixed |
| 1740 | set of data each time the :class:`_engine.FrozenResult` is invoked as |
| 1741 | a callable:: |
| 1742 | |
| 1743 | |
| 1744 | result = connection.execute(query) |
| 1745 | |
| 1746 | frozen = result.freeze() |
| 1747 | |
| 1748 | unfrozen_result_one = frozen() |
| 1749 | |
| 1750 | for row in unfrozen_result_one: |
| 1751 | print(row) |
| 1752 | |
| 1753 | unfrozen_result_two = frozen() |
| 1754 | rows = unfrozen_result_two.all() |
| 1755 | |
| 1756 | # ... etc |
| 1757 | |
| 1758 | .. versionadded:: 1.4 |
| 1759 | |
| 1760 | .. seealso:: |
| 1761 | |
| 1762 | :ref:`do_orm_execute_re_executing` - example usage within the |
| 1763 | ORM to implement a result-set cache. |
| 1764 | |
| 1765 | :func:`_orm.loading.merge_frozen_result` - ORM function to merge |
| 1766 | a frozen result back into a :class:`_orm.Session`. |
| 1767 | |
| 1768 | """ |
| 1769 | |
| 1770 | data: Sequence[Any] |
| 1771 | |
| 1772 | def __init__(self, result: Result[Unpack[_Ts]]): |
| 1773 | self.metadata = result._metadata._for_freeze() |
| 1774 | self._source_supports_scalars = result._source_supports_scalars |
| 1775 | self._attributes = result._attributes |
| 1776 | |
| 1777 | if self._source_supports_scalars: |
| 1778 | self.data = list(result._raw_row_iterator()) |
| 1779 | else: |
| 1780 | self.data = result.fetchall() |
| 1781 | |
| 1782 | def _rewrite_rows(self) -> Sequence[Sequence[Any]]: |
| 1783 | # used only by the orm fn merge_frozen_result |
| 1784 | if self._source_supports_scalars: |
| 1785 | return [[elem] for elem in self.data] |
| 1786 | else: |
| 1787 | return [list(row) for row in self.data] |
| 1788 |