SQLAlchemy 2.1.0b1__cp313-cp313-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. sqlalchemy/__init__.py +295 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +161 -0
  4. sqlalchemy/connectors/asyncio.py +476 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +30 -0
  8. sqlalchemy/dialects/mssql/__init__.py +88 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4110 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  12. sqlalchemy/dialects/mssql/json.py +129 -0
  13. sqlalchemy/dialects/mssql/provision.py +185 -0
  14. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  15. sqlalchemy/dialects/mssql/pyodbc.py +758 -0
  16. sqlalchemy/dialects/mysql/__init__.py +106 -0
  17. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  18. sqlalchemy/dialects/mysql/aiomysql.py +226 -0
  19. sqlalchemy/dialects/mysql/asyncmy.py +214 -0
  20. sqlalchemy/dialects/mysql/base.py +3870 -0
  21. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  22. sqlalchemy/dialects/mysql/dml.py +279 -0
  23. sqlalchemy/dialects/mysql/enumerated.py +277 -0
  24. sqlalchemy/dialects/mysql/expression.py +146 -0
  25. sqlalchemy/dialects/mysql/json.py +91 -0
  26. sqlalchemy/dialects/mysql/mariadb.py +67 -0
  27. sqlalchemy/dialects/mysql/mariadbconnector.py +330 -0
  28. sqlalchemy/dialects/mysql/mysqlconnector.py +296 -0
  29. sqlalchemy/dialects/mysql/mysqldb.py +312 -0
  30. sqlalchemy/dialects/mysql/provision.py +147 -0
  31. sqlalchemy/dialects/mysql/pymysql.py +157 -0
  32. sqlalchemy/dialects/mysql/pyodbc.py +156 -0
  33. sqlalchemy/dialects/mysql/reflection.py +724 -0
  34. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  35. sqlalchemy/dialects/mysql/types.py +845 -0
  36. sqlalchemy/dialects/oracle/__init__.py +83 -0
  37. sqlalchemy/dialects/oracle/base.py +3871 -0
  38. sqlalchemy/dialects/oracle/cx_oracle.py +1522 -0
  39. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  40. sqlalchemy/dialects/oracle/oracledb.py +894 -0
  41. sqlalchemy/dialects/oracle/provision.py +288 -0
  42. sqlalchemy/dialects/oracle/types.py +350 -0
  43. sqlalchemy/dialects/oracle/vector.py +368 -0
  44. sqlalchemy/dialects/postgresql/__init__.py +171 -0
  45. sqlalchemy/dialects/postgresql/_psycopg_common.py +193 -0
  46. sqlalchemy/dialects/postgresql/array.py +534 -0
  47. sqlalchemy/dialects/postgresql/asyncpg.py +1331 -0
  48. sqlalchemy/dialects/postgresql/base.py +5729 -0
  49. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  50. sqlalchemy/dialects/postgresql/dml.py +360 -0
  51. sqlalchemy/dialects/postgresql/ext.py +593 -0
  52. sqlalchemy/dialects/postgresql/hstore.py +413 -0
  53. sqlalchemy/dialects/postgresql/json.py +407 -0
  54. sqlalchemy/dialects/postgresql/named_types.py +521 -0
  55. sqlalchemy/dialects/postgresql/operators.py +130 -0
  56. sqlalchemy/dialects/postgresql/pg8000.py +672 -0
  57. sqlalchemy/dialects/postgresql/pg_catalog.py +344 -0
  58. sqlalchemy/dialects/postgresql/provision.py +175 -0
  59. sqlalchemy/dialects/postgresql/psycopg.py +815 -0
  60. sqlalchemy/dialects/postgresql/psycopg2.py +887 -0
  61. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  62. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  63. sqlalchemy/dialects/postgresql/types.py +388 -0
  64. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  65. sqlalchemy/dialects/sqlite/aiosqlite.py +321 -0
  66. sqlalchemy/dialects/sqlite/base.py +3050 -0
  67. sqlalchemy/dialects/sqlite/dml.py +279 -0
  68. sqlalchemy/dialects/sqlite/json.py +89 -0
  69. sqlalchemy/dialects/sqlite/provision.py +223 -0
  70. sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
  71. sqlalchemy/dialects/sqlite/pysqlite.py +754 -0
  72. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  73. sqlalchemy/engine/__init__.py +62 -0
  74. sqlalchemy/engine/_processors_cy.cp313-win_arm64.pyd +0 -0
  75. sqlalchemy/engine/_processors_cy.py +92 -0
  76. sqlalchemy/engine/_result_cy.cp313-win_arm64.pyd +0 -0
  77. sqlalchemy/engine/_result_cy.py +633 -0
  78. sqlalchemy/engine/_row_cy.cp313-win_arm64.pyd +0 -0
  79. sqlalchemy/engine/_row_cy.py +232 -0
  80. sqlalchemy/engine/_util_cy.cp313-win_arm64.pyd +0 -0
  81. sqlalchemy/engine/_util_cy.py +136 -0
  82. sqlalchemy/engine/base.py +3334 -0
  83. sqlalchemy/engine/characteristics.py +155 -0
  84. sqlalchemy/engine/create.py +869 -0
  85. sqlalchemy/engine/cursor.py +2416 -0
  86. sqlalchemy/engine/default.py +2393 -0
  87. sqlalchemy/engine/events.py +965 -0
  88. sqlalchemy/engine/interfaces.py +3465 -0
  89. sqlalchemy/engine/mock.py +134 -0
  90. sqlalchemy/engine/processors.py +82 -0
  91. sqlalchemy/engine/reflection.py +2100 -0
  92. sqlalchemy/engine/result.py +1932 -0
  93. sqlalchemy/engine/row.py +397 -0
  94. sqlalchemy/engine/strategies.py +16 -0
  95. sqlalchemy/engine/url.py +922 -0
  96. sqlalchemy/engine/util.py +156 -0
  97. sqlalchemy/event/__init__.py +26 -0
  98. sqlalchemy/event/api.py +220 -0
  99. sqlalchemy/event/attr.py +674 -0
  100. sqlalchemy/event/base.py +472 -0
  101. sqlalchemy/event/legacy.py +258 -0
  102. sqlalchemy/event/registry.py +390 -0
  103. sqlalchemy/events.py +17 -0
  104. sqlalchemy/exc.py +922 -0
  105. sqlalchemy/ext/__init__.py +11 -0
  106. sqlalchemy/ext/associationproxy.py +2072 -0
  107. sqlalchemy/ext/asyncio/__init__.py +29 -0
  108. sqlalchemy/ext/asyncio/base.py +281 -0
  109. sqlalchemy/ext/asyncio/engine.py +1475 -0
  110. sqlalchemy/ext/asyncio/exc.py +21 -0
  111. sqlalchemy/ext/asyncio/result.py +994 -0
  112. sqlalchemy/ext/asyncio/scoping.py +1667 -0
  113. sqlalchemy/ext/asyncio/session.py +1993 -0
  114. sqlalchemy/ext/automap.py +1701 -0
  115. sqlalchemy/ext/baked.py +559 -0
  116. sqlalchemy/ext/compiler.py +600 -0
  117. sqlalchemy/ext/declarative/__init__.py +65 -0
  118. sqlalchemy/ext/declarative/extensions.py +560 -0
  119. sqlalchemy/ext/horizontal_shard.py +481 -0
  120. sqlalchemy/ext/hybrid.py +1877 -0
  121. sqlalchemy/ext/indexable.py +364 -0
  122. sqlalchemy/ext/instrumentation.py +450 -0
  123. sqlalchemy/ext/mutable.py +1081 -0
  124. sqlalchemy/ext/orderinglist.py +439 -0
  125. sqlalchemy/ext/serializer.py +185 -0
  126. sqlalchemy/future/__init__.py +16 -0
  127. sqlalchemy/future/engine.py +15 -0
  128. sqlalchemy/inspection.py +174 -0
  129. sqlalchemy/log.py +283 -0
  130. sqlalchemy/orm/__init__.py +175 -0
  131. sqlalchemy/orm/_orm_constructors.py +2694 -0
  132. sqlalchemy/orm/_typing.py +179 -0
  133. sqlalchemy/orm/attributes.py +2868 -0
  134. sqlalchemy/orm/base.py +970 -0
  135. sqlalchemy/orm/bulk_persistence.py +2152 -0
  136. sqlalchemy/orm/clsregistry.py +582 -0
  137. sqlalchemy/orm/collections.py +1568 -0
  138. sqlalchemy/orm/context.py +3471 -0
  139. sqlalchemy/orm/decl_api.py +2257 -0
  140. sqlalchemy/orm/decl_base.py +2304 -0
  141. sqlalchemy/orm/dependency.py +1306 -0
  142. sqlalchemy/orm/descriptor_props.py +1183 -0
  143. sqlalchemy/orm/dynamic.py +300 -0
  144. sqlalchemy/orm/evaluator.py +379 -0
  145. sqlalchemy/orm/events.py +3386 -0
  146. sqlalchemy/orm/exc.py +237 -0
  147. sqlalchemy/orm/identity.py +302 -0
  148. sqlalchemy/orm/instrumentation.py +746 -0
  149. sqlalchemy/orm/interfaces.py +1589 -0
  150. sqlalchemy/orm/loading.py +1684 -0
  151. sqlalchemy/orm/mapped_collection.py +557 -0
  152. sqlalchemy/orm/mapper.py +4406 -0
  153. sqlalchemy/orm/path_registry.py +814 -0
  154. sqlalchemy/orm/persistence.py +1789 -0
  155. sqlalchemy/orm/properties.py +973 -0
  156. sqlalchemy/orm/query.py +3521 -0
  157. sqlalchemy/orm/relationships.py +3570 -0
  158. sqlalchemy/orm/scoping.py +2220 -0
  159. sqlalchemy/orm/session.py +5389 -0
  160. sqlalchemy/orm/state.py +1175 -0
  161. sqlalchemy/orm/state_changes.py +196 -0
  162. sqlalchemy/orm/strategies.py +3480 -0
  163. sqlalchemy/orm/strategy_options.py +2544 -0
  164. sqlalchemy/orm/sync.py +164 -0
  165. sqlalchemy/orm/unitofwork.py +798 -0
  166. sqlalchemy/orm/util.py +2435 -0
  167. sqlalchemy/orm/writeonly.py +694 -0
  168. sqlalchemy/pool/__init__.py +41 -0
  169. sqlalchemy/pool/base.py +1514 -0
  170. sqlalchemy/pool/events.py +372 -0
  171. sqlalchemy/pool/impl.py +582 -0
  172. sqlalchemy/py.typed +0 -0
  173. sqlalchemy/schema.py +72 -0
  174. sqlalchemy/sql/__init__.py +153 -0
  175. sqlalchemy/sql/_dml_constructors.py +132 -0
  176. sqlalchemy/sql/_elements_constructors.py +2147 -0
  177. sqlalchemy/sql/_orm_types.py +20 -0
  178. sqlalchemy/sql/_selectable_constructors.py +773 -0
  179. sqlalchemy/sql/_typing.py +486 -0
  180. sqlalchemy/sql/_util_cy.cp313-win_arm64.pyd +0 -0
  181. sqlalchemy/sql/_util_cy.py +127 -0
  182. sqlalchemy/sql/annotation.py +590 -0
  183. sqlalchemy/sql/base.py +2602 -0
  184. sqlalchemy/sql/cache_key.py +1066 -0
  185. sqlalchemy/sql/coercions.py +1373 -0
  186. sqlalchemy/sql/compiler.py +8259 -0
  187. sqlalchemy/sql/crud.py +1807 -0
  188. sqlalchemy/sql/ddl.py +1928 -0
  189. sqlalchemy/sql/default_comparator.py +654 -0
  190. sqlalchemy/sql/dml.py +1974 -0
  191. sqlalchemy/sql/elements.py +6016 -0
  192. sqlalchemy/sql/events.py +458 -0
  193. sqlalchemy/sql/expression.py +170 -0
  194. sqlalchemy/sql/functions.py +2257 -0
  195. sqlalchemy/sql/lambdas.py +1443 -0
  196. sqlalchemy/sql/naming.py +209 -0
  197. sqlalchemy/sql/operators.py +2897 -0
  198. sqlalchemy/sql/roles.py +332 -0
  199. sqlalchemy/sql/schema.py +6560 -0
  200. sqlalchemy/sql/selectable.py +7497 -0
  201. sqlalchemy/sql/sqltypes.py +4050 -0
  202. sqlalchemy/sql/traversals.py +1042 -0
  203. sqlalchemy/sql/type_api.py +2425 -0
  204. sqlalchemy/sql/util.py +1495 -0
  205. sqlalchemy/sql/visitors.py +1157 -0
  206. sqlalchemy/testing/__init__.py +96 -0
  207. sqlalchemy/testing/assertions.py +1007 -0
  208. sqlalchemy/testing/assertsql.py +519 -0
  209. sqlalchemy/testing/asyncio.py +128 -0
  210. sqlalchemy/testing/config.py +440 -0
  211. sqlalchemy/testing/engines.py +478 -0
  212. sqlalchemy/testing/entities.py +117 -0
  213. sqlalchemy/testing/exclusions.py +476 -0
  214. sqlalchemy/testing/fixtures/__init__.py +30 -0
  215. sqlalchemy/testing/fixtures/base.py +366 -0
  216. sqlalchemy/testing/fixtures/mypy.py +247 -0
  217. sqlalchemy/testing/fixtures/orm.py +227 -0
  218. sqlalchemy/testing/fixtures/sql.py +538 -0
  219. sqlalchemy/testing/pickleable.py +155 -0
  220. sqlalchemy/testing/plugin/__init__.py +6 -0
  221. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  222. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  223. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  224. sqlalchemy/testing/profiling.py +329 -0
  225. sqlalchemy/testing/provision.py +596 -0
  226. sqlalchemy/testing/requirements.py +1973 -0
  227. sqlalchemy/testing/schema.py +198 -0
  228. sqlalchemy/testing/suite/__init__.py +19 -0
  229. sqlalchemy/testing/suite/test_cte.py +237 -0
  230. sqlalchemy/testing/suite/test_ddl.py +420 -0
  231. sqlalchemy/testing/suite/test_dialect.py +776 -0
  232. sqlalchemy/testing/suite/test_insert.py +630 -0
  233. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  234. sqlalchemy/testing/suite/test_results.py +660 -0
  235. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  236. sqlalchemy/testing/suite/test_select.py +2112 -0
  237. sqlalchemy/testing/suite/test_sequence.py +317 -0
  238. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  239. sqlalchemy/testing/suite/test_types.py +2253 -0
  240. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  241. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  242. sqlalchemy/testing/util.py +535 -0
  243. sqlalchemy/testing/warnings.py +52 -0
  244. sqlalchemy/types.py +76 -0
  245. sqlalchemy/util/__init__.py +157 -0
  246. sqlalchemy/util/_collections.py +693 -0
  247. sqlalchemy/util/_collections_cy.cp313-win_arm64.pyd +0 -0
  248. sqlalchemy/util/_collections_cy.pxd +8 -0
  249. sqlalchemy/util/_collections_cy.py +516 -0
  250. sqlalchemy/util/_has_cython.py +46 -0
  251. sqlalchemy/util/_immutabledict_cy.cp313-win_arm64.pyd +0 -0
  252. sqlalchemy/util/_immutabledict_cy.py +240 -0
  253. sqlalchemy/util/compat.py +287 -0
  254. sqlalchemy/util/concurrency.py +322 -0
  255. sqlalchemy/util/cython.py +79 -0
  256. sqlalchemy/util/deprecations.py +401 -0
  257. sqlalchemy/util/langhelpers.py +2256 -0
  258. sqlalchemy/util/preloaded.py +152 -0
  259. sqlalchemy/util/queue.py +304 -0
  260. sqlalchemy/util/tool_support.py +201 -0
  261. sqlalchemy/util/topological.py +120 -0
  262. sqlalchemy/util/typing.py +711 -0
  263. sqlalchemy-2.1.0b1.dist-info/METADATA +267 -0
  264. sqlalchemy-2.1.0b1.dist-info/RECORD +267 -0
  265. sqlalchemy-2.1.0b1.dist-info/WHEEL +5 -0
  266. sqlalchemy-2.1.0b1.dist-info/licenses/LICENSE +19 -0
  267. sqlalchemy-2.1.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1932 @@
1
+ # engine/result.py
2
+ # Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
3
+ # <see AUTHORS file>
4
+ #
5
+ # This module is part of SQLAlchemy and is released under
6
+ # the MIT License: https://www.opensource.org/licenses/mit-license.php
7
+
8
+ """Define generic result set constructs."""
9
+
10
+ from __future__ import annotations
11
+
12
+ import functools
13
+ import itertools
14
+ import operator
15
+ import typing
16
+ from typing import Any
17
+ from typing import Callable
18
+ from typing import cast
19
+ from typing import Dict
20
+ from typing import Generic
21
+ from typing import Iterable
22
+ from typing import Iterator
23
+ from typing import List
24
+ from typing import Literal
25
+ from typing import Mapping
26
+ from typing import NoReturn
27
+ from typing import Optional
28
+ from typing import overload
29
+ from typing import Sequence
30
+ from typing import Tuple
31
+ from typing import TYPE_CHECKING
32
+ from typing import Union
33
+
34
+ from ._result_cy import _InterimRowType
35
+ from ._result_cy import _NO_ROW as _NO_ROW
36
+ from ._result_cy import _R as _R
37
+ from ._result_cy import _RowData
38
+ from ._result_cy import _T
39
+ from ._result_cy import _UniqueFilterType as _UniqueFilterType
40
+ from ._result_cy import BaseResultInternal
41
+ from ._util_cy import tuplegetter as tuplegetter
42
+ from .row import Row
43
+ from .row import RowMapping
44
+ from .. import exc
45
+ from .. import util
46
+ from ..sql.base import _generative
47
+ from ..sql.base import InPlaceGenerative
48
+ from ..util import deprecated
49
+ from ..util import NONE_SET
50
+ from ..util.typing import Self
51
+ from ..util.typing import TupleAny
52
+ from ..util.typing import TypeVarTuple
53
+ from ..util.typing import Unpack
54
+
55
+ if typing.TYPE_CHECKING:
56
+ from typing import Type
57
+
58
+ from .. import inspection
59
+ from ..sql import roles
60
+ from ..sql._typing import _HasClauseElement
61
+ from ..sql.elements import SQLCoreOperations
62
+ from ..sql.type_api import _ResultProcessorType
63
+
64
+ _KeyType = Union[
65
+ str,
66
+ "SQLCoreOperations[Any]",
67
+ "roles.TypedColumnsClauseRole[Any]",
68
+ "roles.ColumnsClauseRole",
69
+ "Type[Any]",
70
+ "inspection.Inspectable[_HasClauseElement[Any]]",
71
+ ]
72
+ _KeyIndexType = Union[_KeyType, int]
73
+
74
+ # is overridden in cursor using _CursorKeyMapRecType
75
+ _KeyMapRecType = Any
76
+
77
+ _KeyMapType = Mapping[_KeyType, _KeyMapRecType]
78
+
79
+
80
+ _Ts = TypeVarTuple("_Ts")
81
+
82
+
83
+ _InterimSupportsScalarsRowType = Union[Row[Unpack[TupleAny]], Any]
84
+
85
+ _ProcessorsType = Sequence[Optional["_ResultProcessorType[Any]"]]
86
+ _TupleGetterType = Callable[[Sequence[Any]], Sequence[Any]]
87
+
88
+
89
+ class ResultMetaData:
90
+ """Base for metadata about result rows."""
91
+
92
+ __slots__ = ()
93
+
94
+ _tuplefilter: Optional[_TupleGetterType] = None
95
+ _translated_indexes: Optional[Sequence[int]] = None
96
+ _unique_filters: Optional[Sequence[Callable[[Any], Any]]] = None
97
+ _keymap: _KeyMapType
98
+ _keys: Sequence[str]
99
+ _processors: Optional[_ProcessorsType]
100
+ _key_to_index: Dict[_KeyType, int]
101
+
102
+ @property
103
+ def keys(self) -> RMKeyView:
104
+ return RMKeyView(self)
105
+
106
+ def _has_key(self, key: object) -> bool:
107
+ raise NotImplementedError()
108
+
109
+ def _for_freeze(self) -> ResultMetaData:
110
+ raise NotImplementedError()
111
+
112
+ @overload
113
+ def _key_fallback(
114
+ self, key: Any, err: Optional[Exception], raiseerr: Literal[True] = ...
115
+ ) -> NoReturn: ...
116
+
117
+ @overload
118
+ def _key_fallback(
119
+ self,
120
+ key: Any,
121
+ err: Optional[Exception],
122
+ raiseerr: Literal[False] = ...,
123
+ ) -> None: ...
124
+
125
+ @overload
126
+ def _key_fallback(
127
+ self, key: Any, err: Optional[Exception], raiseerr: bool = ...
128
+ ) -> Optional[NoReturn]: ...
129
+
130
+ def _key_fallback(
131
+ self, key: Any, err: Optional[Exception], raiseerr: bool = True
132
+ ) -> Optional[NoReturn]:
133
+ assert raiseerr
134
+ raise KeyError(key) from err
135
+
136
+ def _raise_for_ambiguous_column_name(
137
+ self, rec: _KeyMapRecType
138
+ ) -> NoReturn:
139
+ raise NotImplementedError(
140
+ "ambiguous column name logic is implemented for "
141
+ "CursorResultMetaData"
142
+ )
143
+
144
+ def _index_for_key(
145
+ self, key: _KeyIndexType, raiseerr: bool
146
+ ) -> Optional[int]:
147
+ raise NotImplementedError()
148
+
149
+ def _indexes_for_keys(
150
+ self, keys: Sequence[_KeyIndexType]
151
+ ) -> Sequence[int]:
152
+ raise NotImplementedError()
153
+
154
+ def _metadata_for_keys(
155
+ self, keys: Sequence[_KeyIndexType]
156
+ ) -> Iterator[_KeyMapRecType]:
157
+ raise NotImplementedError()
158
+
159
+ def _reduce(self, keys: Sequence[_KeyIndexType]) -> ResultMetaData:
160
+ raise NotImplementedError()
161
+
162
+ def _getter(
163
+ self, key: Any, raiseerr: bool = True
164
+ ) -> Optional[Callable[[Row[Unpack[TupleAny]]], Any]]:
165
+ index = self._index_for_key(key, raiseerr)
166
+
167
+ if index is not None:
168
+ return operator.itemgetter(index)
169
+ else:
170
+ return None
171
+
172
+ def _row_as_tuple_getter(
173
+ self, keys: Sequence[_KeyIndexType]
174
+ ) -> _TupleGetterType:
175
+ indexes = self._indexes_for_keys(keys)
176
+ return tuplegetter(*indexes)
177
+
178
+ def _make_key_to_index(
179
+ self, keymap: Mapping[_KeyType, Sequence[Any]], index: int
180
+ ) -> Dict[_KeyType, int]:
181
+ return {
182
+ key: rec[index]
183
+ for key, rec in keymap.items()
184
+ if rec[index] is not None
185
+ }
186
+
187
+ def _key_not_found(self, key: Any, attr_error: bool) -> NoReturn:
188
+ if key in self._keymap:
189
+ # the index must be none in this case
190
+ self._raise_for_ambiguous_column_name(self._keymap[key])
191
+ else:
192
+ # unknown key
193
+ if attr_error:
194
+ try:
195
+ self._key_fallback(key, None)
196
+ except KeyError as ke:
197
+ raise AttributeError(ke.args[0]) from ke
198
+ else:
199
+ self._key_fallback(key, None)
200
+
201
+ @property
202
+ def _effective_processors(self) -> Optional[_ProcessorsType]:
203
+ if not self._processors or NONE_SET.issuperset(self._processors):
204
+ return None
205
+ else:
206
+ return self._processors
207
+
208
+
209
+ class RMKeyView(typing.KeysView[Any]):
210
+ __slots__ = ("_parent", "_keys")
211
+
212
+ _parent: ResultMetaData
213
+ _keys: Sequence[str]
214
+
215
+ def __init__(self, parent: ResultMetaData):
216
+ self._parent = parent
217
+ self._keys = [k for k in parent._keys if k is not None]
218
+
219
+ def __len__(self) -> int:
220
+ return len(self._keys)
221
+
222
+ def __repr__(self) -> str:
223
+ return "{0.__class__.__name__}({0._keys!r})".format(self)
224
+
225
+ def __iter__(self) -> Iterator[str]:
226
+ return iter(self._keys)
227
+
228
+ def __contains__(self, item: Any) -> bool:
229
+ if isinstance(item, int):
230
+ return False
231
+
232
+ # note this also includes special key fallback behaviors
233
+ # which also don't seem to be tested in test_resultset right now
234
+ return self._parent._has_key(item)
235
+
236
+ def __eq__(self, other: Any) -> bool:
237
+ return list(other) == list(self)
238
+
239
+ def __ne__(self, other: Any) -> bool:
240
+ return list(other) != list(self)
241
+
242
+
243
+ class SimpleResultMetaData(ResultMetaData):
244
+ """result metadata for in-memory collections."""
245
+
246
+ __slots__ = (
247
+ "_keys",
248
+ "_keymap",
249
+ "_processors",
250
+ "_tuplefilter",
251
+ "_translated_indexes",
252
+ "_unique_filters",
253
+ "_key_to_index",
254
+ )
255
+
256
+ _keys: Sequence[str]
257
+
258
+ def __init__(
259
+ self,
260
+ keys: Sequence[str],
261
+ extra: Optional[Sequence[Any]] = None,
262
+ _processors: Optional[_ProcessorsType] = None,
263
+ _tuplefilter: Optional[_TupleGetterType] = None,
264
+ _translated_indexes: Optional[Sequence[int]] = None,
265
+ _unique_filters: Optional[Sequence[Callable[[Any], Any]]] = None,
266
+ ):
267
+ self._keys = list(keys)
268
+ self._tuplefilter = _tuplefilter
269
+ self._translated_indexes = _translated_indexes
270
+ self._unique_filters = _unique_filters
271
+ if extra:
272
+ assert len(self._keys) == len(extra)
273
+ recs_names = [
274
+ (
275
+ (name,) + (extras if extras else ()),
276
+ (index, name, extras),
277
+ )
278
+ for index, (name, extras) in enumerate(zip(self._keys, extra))
279
+ ]
280
+ else:
281
+ recs_names = [
282
+ ((name,), (index, name, ()))
283
+ for index, name in enumerate(self._keys)
284
+ ]
285
+
286
+ self._keymap = {key: rec for keys, rec in recs_names for key in keys}
287
+
288
+ self._processors = _processors
289
+
290
+ self._key_to_index = self._make_key_to_index(self._keymap, 0)
291
+
292
+ def _has_key(self, key: object) -> bool:
293
+ return key in self._keymap
294
+
295
+ def _for_freeze(self) -> ResultMetaData:
296
+ unique_filters = self._unique_filters
297
+ if unique_filters and self._tuplefilter:
298
+ unique_filters = self._tuplefilter(unique_filters)
299
+
300
+ # TODO: are we freezing the result with or without uniqueness
301
+ # applied?
302
+ return SimpleResultMetaData(
303
+ self._keys,
304
+ extra=[self._keymap[key][2] for key in self._keys],
305
+ _unique_filters=unique_filters,
306
+ )
307
+
308
+ def __getstate__(self) -> Dict[str, Any]:
309
+ return {
310
+ "_keys": self._keys,
311
+ "_translated_indexes": self._translated_indexes,
312
+ }
313
+
314
+ def __setstate__(self, state: Dict[str, Any]) -> None:
315
+ if state["_translated_indexes"]:
316
+ _translated_indexes = state["_translated_indexes"]
317
+ _tuplefilter = tuplegetter(*_translated_indexes)
318
+ else:
319
+ _translated_indexes = _tuplefilter = None
320
+ self.__init__( # type: ignore
321
+ state["_keys"],
322
+ _translated_indexes=_translated_indexes,
323
+ _tuplefilter=_tuplefilter,
324
+ )
325
+
326
+ def _index_for_key(self, key: Any, raiseerr: bool = True) -> int:
327
+ if isinstance(key, int):
328
+ key = self._keys[key]
329
+ try:
330
+ rec = self._keymap[key]
331
+ except KeyError as ke:
332
+ rec = self._key_fallback(key, ke, raiseerr)
333
+
334
+ return rec[0] # type: ignore[no-any-return]
335
+
336
+ def _indexes_for_keys(self, keys: Sequence[Any]) -> Sequence[int]:
337
+ return [self._keymap[key][0] for key in keys]
338
+
339
+ def _metadata_for_keys(
340
+ self, keys: Sequence[Any]
341
+ ) -> Iterator[_KeyMapRecType]:
342
+ for key in keys:
343
+ if isinstance(key, int):
344
+ key = self._keys[key]
345
+
346
+ try:
347
+ rec = self._keymap[key]
348
+ except KeyError as ke:
349
+ rec = self._key_fallback(key, ke, True)
350
+
351
+ yield rec
352
+
353
+ def _reduce(self, keys: Sequence[Any]) -> ResultMetaData:
354
+ try:
355
+ metadata_for_keys = [
356
+ self._keymap[self._keys[key] if isinstance(key, int) else key]
357
+ for key in keys
358
+ ]
359
+ except KeyError as ke:
360
+ self._key_fallback(ke.args[0], ke, True)
361
+
362
+ indexes: Sequence[int]
363
+ new_keys: Sequence[str]
364
+ extra: Sequence[Any]
365
+ indexes, new_keys, extra = zip(*metadata_for_keys)
366
+
367
+ if self._translated_indexes:
368
+ indexes = [self._translated_indexes[idx] for idx in indexes]
369
+
370
+ tup = tuplegetter(*indexes)
371
+
372
+ new_metadata = SimpleResultMetaData(
373
+ new_keys,
374
+ extra=extra,
375
+ _tuplefilter=tup,
376
+ _translated_indexes=indexes,
377
+ _processors=self._processors,
378
+ _unique_filters=self._unique_filters,
379
+ )
380
+
381
+ return new_metadata
382
+
383
+
384
+ def result_tuple(
385
+ fields: Sequence[str], extra: Optional[Any] = None
386
+ ) -> Callable[[Iterable[Any]], Row[Unpack[TupleAny]]]:
387
+ parent = SimpleResultMetaData(fields, extra)
388
+ return functools.partial(
389
+ Row, parent, parent._effective_processors, parent._key_to_index
390
+ )
391
+
392
+
393
+ class ResultInternal(InPlaceGenerative, BaseResultInternal[_R]):
394
+ __slots__ = ()
395
+ _is_cursor = False
396
+
397
+ @_generative
398
+ def _column_slices(self, indexes: Sequence[_KeyIndexType]) -> Self:
399
+ real_result = (
400
+ self._real_result
401
+ if self._real_result
402
+ else cast("Result[Any]", self)
403
+ )
404
+
405
+ if not real_result._source_supports_scalars or len(indexes) != 1:
406
+ self._metadata = self._metadata._reduce(indexes)
407
+
408
+ assert self._generate_rows
409
+
410
+ return self
411
+
412
+
413
+ class _WithKeys:
414
+ __slots__ = ()
415
+
416
+ _metadata: ResultMetaData
417
+
418
+ # used mainly to share documentation on the keys method.
419
+ def keys(self) -> RMKeyView:
420
+ """Return an iterable view which yields the string keys that would
421
+ be represented by each :class:`_engine.Row`.
422
+
423
+ The keys can represent the labels of the columns returned by a core
424
+ statement or the names of the orm classes returned by an orm
425
+ execution.
426
+
427
+ The view also can be tested for key containment using the Python
428
+ ``in`` operator, which will test both for the string keys represented
429
+ in the view, as well as for alternate keys such as column objects.
430
+
431
+ .. versionchanged:: 1.4 a key view object is returned rather than a
432
+ plain list.
433
+
434
+
435
+ """
436
+ return self._metadata.keys
437
+
438
+
439
+ class Result(_WithKeys, ResultInternal[Row[Unpack[_Ts]]]):
440
+ """Represent a set of database results.
441
+
442
+ .. versionadded:: 1.4 The :class:`_engine.Result` object provides a
443
+ completely updated usage model and calling facade for SQLAlchemy
444
+ Core and SQLAlchemy ORM. In Core, it forms the basis of the
445
+ :class:`_engine.CursorResult` object which replaces the previous
446
+ :class:`_engine.ResultProxy` interface. When using the ORM, a
447
+ higher level object called :class:`_engine.ChunkedIteratorResult`
448
+ is normally used.
449
+
450
+ .. note:: In SQLAlchemy 1.4 and above, this object is
451
+ used for ORM results returned by :meth:`_orm.Session.execute`, which can
452
+ yield instances of ORM mapped objects either individually or within
453
+ tuple-like rows. Note that the :class:`_engine.Result` object does not
454
+ deduplicate instances or rows automatically as is the case with the
455
+ legacy :class:`_orm.Query` object. For in-Python de-duplication of
456
+ instances or rows, use the :meth:`_engine.Result.unique` modifier
457
+ method.
458
+
459
+ .. seealso::
460
+
461
+ :ref:`tutorial_fetching_rows` - in the :doc:`/tutorial/index`
462
+
463
+ """
464
+
465
+ __slots__ = ("_metadata", "__dict__")
466
+
467
+ _row_logging_fn: Optional[
468
+ Callable[[Row[Unpack[TupleAny]]], Row[Unpack[TupleAny]]]
469
+ ] = None
470
+
471
+ _source_supports_scalars: bool = False
472
+
473
+ _yield_per: Optional[int] = None
474
+
475
+ _attributes: util.immutabledict[Any, Any] = util.immutabledict()
476
+
477
+ def __init__(self, cursor_metadata: ResultMetaData):
478
+ self._metadata = cursor_metadata
479
+
480
+ def __enter__(self) -> Self:
481
+ return self
482
+
483
+ def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
484
+ self.close()
485
+
486
+ def close(self) -> None:
487
+ """close this :class:`_engine.Result`.
488
+
489
+ The behavior of this method is implementation specific, and is
490
+ not implemented by default. The method should generally end
491
+ the resources in use by the result object and also cause any
492
+ subsequent iteration or row fetching to raise
493
+ :class:`.ResourceClosedError`.
494
+
495
+ .. versionadded:: 1.4.27 - ``.close()`` was previously not generally
496
+ available for all :class:`_engine.Result` classes, instead only
497
+ being available on the :class:`_engine.CursorResult` returned for
498
+ Core statement executions. As most other result objects, namely the
499
+ ones used by the ORM, are proxying a :class:`_engine.CursorResult`
500
+ in any case, this allows the underlying cursor result to be closed
501
+ from the outside facade for the case when the ORM query is using
502
+ the ``yield_per`` execution option where it does not immediately
503
+ exhaust and autoclose the database cursor.
504
+
505
+ """
506
+ self._soft_close(hard=True)
507
+
508
+ @property
509
+ def _soft_closed(self) -> bool:
510
+ raise NotImplementedError()
511
+
512
+ @property
513
+ def closed(self) -> bool:
514
+ """return ``True`` if this :class:`_engine.Result` reports .closed
515
+
516
+ .. versionadded:: 1.4.43
517
+
518
+ """
519
+ raise NotImplementedError()
520
+
521
+ @_generative
522
+ def yield_per(self, num: int) -> Self:
523
+ """Configure the row-fetching strategy to fetch ``num`` rows at a time.
524
+
525
+ This impacts the underlying behavior of the result when iterating over
526
+ the result object, or otherwise making use of methods such as
527
+ :meth:`_engine.Result.fetchone` that return one row at a time. Data
528
+ from the underlying cursor or other data source will be buffered up to
529
+ this many rows in memory, and the buffered collection will then be
530
+ yielded out one row at a time or as many rows are requested. Each time
531
+ the buffer clears, it will be refreshed to this many rows or as many
532
+ rows remain if fewer remain.
533
+
534
+ The :meth:`_engine.Result.yield_per` method is generally used in
535
+ conjunction with the
536
+ :paramref:`_engine.Connection.execution_options.stream_results`
537
+ execution option, which will allow the database dialect in use to make
538
+ use of a server side cursor, if the DBAPI supports a specific "server
539
+ side cursor" mode separate from its default mode of operation.
540
+
541
+ .. tip::
542
+
543
+ Consider using the
544
+ :paramref:`_engine.Connection.execution_options.yield_per`
545
+ execution option, which will simultaneously set
546
+ :paramref:`_engine.Connection.execution_options.stream_results`
547
+ to ensure the use of server side cursors, as well as automatically
548
+ invoke the :meth:`_engine.Result.yield_per` method to establish
549
+ a fixed row buffer size at once.
550
+
551
+ The :paramref:`_engine.Connection.execution_options.yield_per`
552
+ execution option is available for ORM operations, with
553
+ :class:`_orm.Session`-oriented use described at
554
+ :ref:`orm_queryguide_yield_per`. The Core-only version which works
555
+ with :class:`_engine.Connection` is new as of SQLAlchemy 1.4.40.
556
+
557
+ .. versionadded:: 1.4
558
+
559
+ :param num: number of rows to fetch each time the buffer is refilled.
560
+ If set to a value below 1, fetches all rows for the next buffer.
561
+
562
+ .. seealso::
563
+
564
+ :ref:`engine_stream_results` - describes Core behavior for
565
+ :meth:`_engine.Result.yield_per`
566
+
567
+ :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel`
568
+
569
+ """
570
+ self._yield_per = num
571
+ return self
572
+
573
+ @_generative
574
+ def unique(self, strategy: Optional[_UniqueFilterType] = None) -> Self:
575
+ """Apply unique filtering to the objects returned by this
576
+ :class:`_engine.Result`.
577
+
578
+ When this filter is applied with no arguments, the rows or objects
579
+ returned will filtered such that each row is returned uniquely. The
580
+ algorithm used to determine this uniqueness is by default the Python
581
+ hashing identity of the whole tuple. In some cases a specialized
582
+ per-entity hashing scheme may be used, such as when using the ORM, a
583
+ scheme is applied which works against the primary key identity of
584
+ returned objects.
585
+
586
+ The unique filter is applied **after all other filters**, which means
587
+ if the columns returned have been refined using a method such as the
588
+ :meth:`_engine.Result.columns` or :meth:`_engine.Result.scalars`
589
+ method, the uniquing is applied to **only the column or columns
590
+ returned**. This occurs regardless of the order in which these
591
+ methods have been called upon the :class:`_engine.Result` object.
592
+
593
+ The unique filter also changes the calculus used for methods like
594
+ :meth:`_engine.Result.fetchmany` and :meth:`_engine.Result.partitions`.
595
+ When using :meth:`_engine.Result.unique`, these methods will continue
596
+ to yield the number of rows or objects requested, after uniquing
597
+ has been applied. However, this necessarily impacts the buffering
598
+ behavior of the underlying cursor or datasource, such that multiple
599
+ underlying calls to ``cursor.fetchmany()`` may be necessary in order
600
+ to accumulate enough objects in order to provide a unique collection
601
+ of the requested size.
602
+
603
+ :param strategy: a callable that will be applied to rows or objects
604
+ being iterated, which should return an object that represents the
605
+ unique value of the row. A Python ``set()`` is used to store
606
+ these identities. If not passed, a default uniqueness strategy
607
+ is used which may have been assembled by the source of this
608
+ :class:`_engine.Result` object.
609
+
610
+ """
611
+ self._unique_filter_state = (set(), strategy)
612
+ return self
613
+
614
+ def columns(self, *col_expressions: _KeyIndexType) -> Self:
615
+ r"""Establish the columns that should be returned in each row.
616
+
617
+ This method may be used to limit the columns returned as well
618
+ as to reorder them. The given list of expressions are normally
619
+ a series of integers or string key names. They may also be
620
+ appropriate :class:`.ColumnElement` objects which correspond to
621
+ a given statement construct.
622
+
623
+ .. versionchanged:: 2.0 Due to a bug in 1.4, the
624
+ :meth:`_engine.Result.columns` method had an incorrect behavior
625
+ where calling upon the method with just one index would cause the
626
+ :class:`_engine.Result` object to yield scalar values rather than
627
+ :class:`_engine.Row` objects. In version 2.0, this behavior
628
+ has been corrected such that calling upon
629
+ :meth:`_engine.Result.columns` with a single index will
630
+ produce a :class:`_engine.Result` object that continues
631
+ to yield :class:`_engine.Row` objects, which include
632
+ only a single column.
633
+
634
+ E.g.::
635
+
636
+ statement = select(table.c.x, table.c.y, table.c.z)
637
+ result = connection.execute(statement)
638
+
639
+ for z, y in result.columns("z", "y"):
640
+ ...
641
+
642
+ Example of using the column objects from the statement itself::
643
+
644
+ for z, y in result.columns(
645
+ statement.selected_columns.c.z, statement.selected_columns.c.y
646
+ ):
647
+ ...
648
+
649
+ .. versionadded:: 1.4
650
+
651
+ :param \*col_expressions: indicates columns to be returned. Elements
652
+ may be integer row indexes, string column names, or appropriate
653
+ :class:`.ColumnElement` objects corresponding to a select construct.
654
+
655
+ :return: this :class:`_engine.Result` object with the modifications
656
+ given.
657
+
658
+ """
659
+ return self._column_slices(col_expressions)
660
+
661
+ @overload
662
+ def scalars(self: Result[_T, Unpack[TupleAny]]) -> ScalarResult[_T]: ...
663
+
664
+ @overload
665
+ def scalars(
666
+ self: Result[_T, Unpack[TupleAny]], index: Literal[0]
667
+ ) -> ScalarResult[_T]: ...
668
+
669
+ @overload
670
+ def scalars(self, index: _KeyIndexType = 0) -> ScalarResult[Any]: ...
671
+
672
+ def scalars(self, index: _KeyIndexType = 0) -> ScalarResult[Any]:
673
+ """Return a :class:`_engine.ScalarResult` filtering object which
674
+ will return single elements rather than :class:`_row.Row` objects.
675
+
676
+ E.g.::
677
+
678
+ >>> result = conn.execute(text("select int_id from table"))
679
+ >>> result.scalars().all()
680
+ [1, 2, 3]
681
+
682
+ When results are fetched from the :class:`_engine.ScalarResult`
683
+ filtering object, the single column-row that would be returned by the
684
+ :class:`_engine.Result` is instead returned as the column's value.
685
+
686
+ .. versionadded:: 1.4
687
+
688
+ :param index: integer or row key indicating the column to be fetched
689
+ from each row, defaults to ``0`` indicating the first column.
690
+
691
+ :return: a new :class:`_engine.ScalarResult` filtering object referring
692
+ to this :class:`_engine.Result` object.
693
+
694
+ """
695
+ return ScalarResult(self, index)
696
+
697
+ def _getter(
698
+ self, key: _KeyIndexType, raiseerr: bool = True
699
+ ) -> Optional[Callable[[Row[Unpack[TupleAny]]], Any]]:
700
+ """return a callable that will retrieve the given key from a
701
+ :class:`_engine.Row`.
702
+
703
+ """
704
+ if self._source_supports_scalars:
705
+ raise NotImplementedError(
706
+ "can't use this function in 'only scalars' mode"
707
+ )
708
+ return self._metadata._getter(key, raiseerr)
709
+
710
+ def _tuple_getter(self, keys: Sequence[_KeyIndexType]) -> _TupleGetterType:
711
+ """return a callable that will retrieve the given keys from a
712
+ :class:`_engine.Row`.
713
+
714
+ """
715
+ if self._source_supports_scalars:
716
+ raise NotImplementedError(
717
+ "can't use this function in 'only scalars' mode"
718
+ )
719
+ return self._metadata._row_as_tuple_getter(keys)
720
+
721
+ def mappings(self) -> MappingResult:
722
+ """Apply a mappings filter to returned rows, returning an instance of
723
+ :class:`_engine.MappingResult`.
724
+
725
+ When this filter is applied, fetching rows will return
726
+ :class:`_engine.RowMapping` objects instead of :class:`_engine.Row`
727
+ objects.
728
+
729
+ .. versionadded:: 1.4
730
+
731
+ :return: a new :class:`_engine.MappingResult` filtering object
732
+ referring to this :class:`_engine.Result` object.
733
+
734
+ """
735
+
736
+ return MappingResult(self)
737
+
738
+ @property
739
+ @deprecated(
740
+ "2.1.0",
741
+ "The :attr:`.Result.t` method is deprecated, :class:`.Row` "
742
+ "now behaves like a tuple and can unpack types directly.",
743
+ )
744
+ def t(self) -> TupleResult[Tuple[Unpack[_Ts]]]:
745
+ """Apply a "typed tuple" typing filter to returned rows.
746
+
747
+ The :attr:`_engine.Result.t` attribute is a synonym for
748
+ calling the :meth:`_engine.Result.tuples` method.
749
+
750
+ .. versionadded:: 2.0
751
+
752
+ .. seealso::
753
+
754
+ :ref:`change_10635` - describes a migration path from this
755
+ workaround for SQLAlchemy 2.1.
756
+
757
+ """
758
+ return self # type: ignore
759
+
760
+ @deprecated(
761
+ "2.1.0",
762
+ "The :meth:`.Result.tuples` method is deprecated, :class:`.Row` "
763
+ "now behaves like a tuple and can unpack types directly.",
764
+ )
765
+ def tuples(self) -> TupleResult[Tuple[Unpack[_Ts]]]:
766
+ """Apply a "typed tuple" typing filter to returned rows.
767
+
768
+ This method returns the same :class:`_engine.Result` object
769
+ at runtime,
770
+ however annotates as returning a :class:`_engine.TupleResult` object
771
+ that will indicate to :pep:`484` typing tools that plain typed
772
+ ``Tuple`` instances are returned rather than rows. This allows
773
+ tuple unpacking and ``__getitem__`` access of :class:`_engine.Row`
774
+ objects to by typed, for those cases where the statement invoked
775
+ itself included typing information.
776
+
777
+ .. versionadded:: 2.0
778
+
779
+ :return: the :class:`_engine.TupleResult` type at typing time.
780
+
781
+ .. seealso::
782
+
783
+ :ref:`change_10635` - describes a migration path from this
784
+ workaround for SQLAlchemy 2.1.
785
+
786
+ :attr:`_engine.Result.t` - shorter synonym
787
+
788
+ :attr:`_engine.Row._t` - :class:`_engine.Row` version
789
+
790
+ """
791
+
792
+ return self # type: ignore
793
+
794
+ def _raw_row_iterator(self) -> Iterator[_RowData]:
795
+ """Return a safe iterator that yields raw row data.
796
+
797
+ This is used by the :meth:`_engine.Result.merge` method
798
+ to merge multiple compatible results together.
799
+
800
+ """
801
+ raise NotImplementedError()
802
+
803
+ def __iter__(self) -> Iterator[Row[Unpack[_Ts]]]:
804
+ return self._iter_impl()
805
+
806
+ def __next__(self) -> Row[Unpack[_Ts]]:
807
+ return self._next_impl()
808
+
809
+ def partitions(
810
+ self, size: Optional[int] = None
811
+ ) -> Iterator[Sequence[Row[Unpack[_Ts]]]]:
812
+ """Iterate through sub-lists of rows of the size given.
813
+
814
+ Each list will be of the size given, excluding the last list to
815
+ be yielded, which may have a small number of rows. No empty
816
+ lists will be yielded.
817
+
818
+ The result object is automatically closed when the iterator
819
+ is fully consumed.
820
+
821
+ Note that the backend driver will usually buffer the entire result
822
+ ahead of time unless the
823
+ :paramref:`.Connection.execution_options.stream_results` execution
824
+ option is used indicating that the driver should not pre-buffer
825
+ results, if possible. Not all drivers support this option and
826
+ the option is silently ignored for those who do not.
827
+
828
+ When using the ORM, the :meth:`_engine.Result.partitions` method
829
+ is typically more effective from a memory perspective when it is
830
+ combined with use of the
831
+ :ref:`yield_per execution option <orm_queryguide_yield_per>`,
832
+ which instructs both the DBAPI driver to use server side cursors,
833
+ if available, as well as instructs the ORM loading internals to only
834
+ build a certain amount of ORM objects from a result at a time before
835
+ yielding them out.
836
+
837
+ .. versionadded:: 1.4
838
+
839
+ :param size: indicate the maximum number of rows to be present
840
+ in each list yielded. If None, makes use of the value set by
841
+ the :meth:`_engine.Result.yield_per`, method, if it were called,
842
+ or the :paramref:`_engine.Connection.execution_options.yield_per`
843
+ execution option, which is equivalent in this regard. If
844
+ yield_per weren't set, it makes use of the
845
+ :meth:`_engine.Result.fetchmany` default, which may be backend
846
+ specific and not well defined.
847
+
848
+ :return: iterator of lists
849
+
850
+ .. seealso::
851
+
852
+ :ref:`engine_stream_results`
853
+
854
+ :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel`
855
+
856
+ """
857
+
858
+ getter = self._manyrow_getter
859
+
860
+ while True:
861
+ partition = getter(self, size)
862
+ if partition:
863
+ yield partition
864
+ else:
865
+ break
866
+
867
+ def fetchall(self) -> Sequence[Row[Unpack[_Ts]]]:
868
+ """A synonym for the :meth:`_engine.Result.all` method."""
869
+
870
+ return self._allrows()
871
+
872
+ def fetchone(self) -> Optional[Row[Unpack[_Ts]]]:
873
+ """Fetch one row.
874
+
875
+ When all rows are exhausted, returns None.
876
+
877
+ This method is provided for backwards compatibility with
878
+ SQLAlchemy 1.x.x.
879
+
880
+ To fetch the first row of a result only, use the
881
+ :meth:`_engine.Result.first` method. To iterate through all
882
+ rows, iterate the :class:`_engine.Result` object directly.
883
+
884
+ :return: a :class:`_engine.Row` object if no filters are applied,
885
+ or ``None`` if no rows remain.
886
+
887
+ """
888
+ row = self._onerow_getter(self)
889
+ if row is _NO_ROW:
890
+ return None
891
+ else:
892
+ return row
893
+
894
+ def fetchmany(
895
+ self, size: Optional[int] = None
896
+ ) -> Sequence[Row[Unpack[_Ts]]]:
897
+ """Fetch many rows.
898
+
899
+ When all rows are exhausted, returns an empty sequence.
900
+
901
+ This method is provided for backwards compatibility with
902
+ SQLAlchemy 1.x.x.
903
+
904
+ To fetch rows in groups, use the :meth:`_engine.Result.partitions`
905
+ method.
906
+
907
+ :return: a sequence of :class:`_engine.Row` objects.
908
+
909
+ .. seealso::
910
+
911
+ :meth:`_engine.Result.partitions`
912
+
913
+ """
914
+
915
+ return self._manyrow_getter(self, size)
916
+
917
+ def all(self) -> Sequence[Row[Unpack[_Ts]]]:
918
+ """Return all rows in a sequence.
919
+
920
+ Closes the result set after invocation. Subsequent invocations
921
+ will return an empty sequence.
922
+
923
+ .. versionadded:: 1.4
924
+
925
+ :return: a sequence of :class:`_engine.Row` objects.
926
+
927
+ .. seealso::
928
+
929
+ :ref:`engine_stream_results` - How to stream a large result set
930
+ without loading it completely in python.
931
+
932
+ """
933
+
934
+ return self._allrows()
935
+
936
+ def first(self) -> Optional[Row[Unpack[_Ts]]]:
937
+ """Fetch the first row or ``None`` if no row is present.
938
+
939
+ Closes the result set and discards remaining rows.
940
+
941
+ .. note:: This method returns one **row**, e.g. tuple, by default.
942
+ To return exactly one single scalar value, that is, the first
943
+ column of the first row, use the
944
+ :meth:`_engine.Result.scalar` method,
945
+ or combine :meth:`_engine.Result.scalars` and
946
+ :meth:`_engine.Result.first`.
947
+
948
+ Additionally, in contrast to the behavior of the legacy ORM
949
+ :meth:`_orm.Query.first` method, **no limit is applied** to the
950
+ SQL query which was invoked to produce this
951
+ :class:`_engine.Result`;
952
+ for a DBAPI driver that buffers results in memory before yielding
953
+ rows, all rows will be sent to the Python process and all but
954
+ the first row will be discarded.
955
+
956
+ .. seealso::
957
+
958
+ :ref:`migration_20_unify_select`
959
+
960
+ :return: a :class:`_engine.Row` object, or None
961
+ if no rows remain.
962
+
963
+ .. seealso::
964
+
965
+ :meth:`_engine.Result.scalar`
966
+
967
+ :meth:`_engine.Result.one`
968
+
969
+ """
970
+
971
+ return self._only_one_row(
972
+ raise_for_second_row=False, raise_for_none=False, scalar=False
973
+ )
974
+
975
+ def one_or_none(self) -> Optional[Row[Unpack[_Ts]]]:
976
+ """Return at most one result or raise an exception.
977
+
978
+ Returns ``None`` if the result has no rows.
979
+ Raises :class:`.MultipleResultsFound`
980
+ if multiple rows are returned.
981
+
982
+ .. versionadded:: 1.4
983
+
984
+ :return: The first :class:`_engine.Row` or ``None`` if no row
985
+ is available.
986
+
987
+ :raises: :class:`.MultipleResultsFound`
988
+
989
+ .. seealso::
990
+
991
+ :meth:`_engine.Result.first`
992
+
993
+ :meth:`_engine.Result.one`
994
+
995
+ """
996
+ return self._only_one_row(
997
+ raise_for_second_row=True, raise_for_none=False, scalar=False
998
+ )
999
+
1000
+ def scalar_one(self: Result[_T, Unpack[TupleAny]]) -> _T:
1001
+ """Return exactly one scalar result or raise an exception.
1002
+
1003
+ This is equivalent to calling :meth:`_engine.Result.scalars` and
1004
+ then :meth:`_engine.ScalarResult.one`.
1005
+
1006
+ .. seealso::
1007
+
1008
+ :meth:`_engine.ScalarResult.one`
1009
+
1010
+ :meth:`_engine.Result.scalars`
1011
+
1012
+ """
1013
+ return self._only_one_row(
1014
+ raise_for_second_row=True, raise_for_none=True, scalar=True
1015
+ )
1016
+
1017
+ def scalar_one_or_none(self: Result[_T, Unpack[TupleAny]]) -> Optional[_T]:
1018
+ """Return exactly one scalar result or ``None``.
1019
+
1020
+ This is equivalent to calling :meth:`_engine.Result.scalars` and
1021
+ then :meth:`_engine.ScalarResult.one_or_none`.
1022
+
1023
+ .. seealso::
1024
+
1025
+ :meth:`_engine.ScalarResult.one_or_none`
1026
+
1027
+ :meth:`_engine.Result.scalars`
1028
+
1029
+ """
1030
+ return self._only_one_row(
1031
+ raise_for_second_row=True, raise_for_none=False, scalar=True
1032
+ )
1033
+
1034
+ def one(self) -> Row[Unpack[_Ts]]:
1035
+ """Return exactly one row or raise an exception.
1036
+
1037
+ Raises :class:`_exc.NoResultFound` if the result returns no
1038
+ rows, or :class:`_exc.MultipleResultsFound` if multiple rows
1039
+ would be returned.
1040
+
1041
+ .. note:: This method returns one **row**, e.g. tuple, by default.
1042
+ To return exactly one single scalar value, that is, the first
1043
+ column of the first row, use the
1044
+ :meth:`_engine.Result.scalar_one` method, or combine
1045
+ :meth:`_engine.Result.scalars` and
1046
+ :meth:`_engine.Result.one`.
1047
+
1048
+ .. versionadded:: 1.4
1049
+
1050
+ :return: The first :class:`_engine.Row`.
1051
+
1052
+ :raises: :class:`.MultipleResultsFound`, :class:`.NoResultFound`
1053
+
1054
+ .. seealso::
1055
+
1056
+ :meth:`_engine.Result.first`
1057
+
1058
+ :meth:`_engine.Result.one_or_none`
1059
+
1060
+ :meth:`_engine.Result.scalar_one`
1061
+
1062
+ """
1063
+ return self._only_one_row(
1064
+ raise_for_second_row=True, raise_for_none=True, scalar=False
1065
+ )
1066
+
1067
+ def scalar(self: Result[_T, Unpack[TupleAny]]) -> Optional[_T]:
1068
+ """Fetch the first column of the first row, and close the result set.
1069
+
1070
+ Returns ``None`` if there are no rows to fetch.
1071
+
1072
+ No validation is performed to test if additional rows remain.
1073
+
1074
+ After calling this method, the object is fully closed,
1075
+ e.g. the :meth:`_engine.CursorResult.close`
1076
+ method will have been called.
1077
+
1078
+ :return: a Python scalar value, or ``None`` if no rows remain.
1079
+
1080
+ """
1081
+ return self._only_one_row(
1082
+ raise_for_second_row=False, raise_for_none=False, scalar=True
1083
+ )
1084
+
1085
+ def freeze(self) -> FrozenResult[Unpack[_Ts]]:
1086
+ """Return a callable object that will produce copies of this
1087
+ :class:`_engine.Result` when invoked.
1088
+
1089
+ The callable object returned is an instance of
1090
+ :class:`_engine.FrozenResult`.
1091
+
1092
+ This is used for result set caching. The method must be called
1093
+ on the result when it has been unconsumed, and calling the method
1094
+ will consume the result fully. When the :class:`_engine.FrozenResult`
1095
+ is retrieved from a cache, it can be called any number of times where
1096
+ it will produce a new :class:`_engine.Result` object each time
1097
+ against its stored set of rows.
1098
+
1099
+ .. seealso::
1100
+
1101
+ :ref:`do_orm_execute_re_executing` - example usage within the
1102
+ ORM to implement a result-set cache.
1103
+
1104
+ """
1105
+
1106
+ return FrozenResult(self)
1107
+
1108
+ def merge(
1109
+ self, *others: Result[Unpack[TupleAny]]
1110
+ ) -> MergedResult[Unpack[TupleAny]]:
1111
+ """Merge this :class:`_engine.Result` with other compatible result
1112
+ objects.
1113
+
1114
+ The object returned is an instance of :class:`_engine.MergedResult`,
1115
+ which will be composed of iterators from the given result
1116
+ objects.
1117
+
1118
+ The new result will use the metadata from this result object.
1119
+ The subsequent result objects must be against an identical
1120
+ set of result / cursor metadata, otherwise the behavior is
1121
+ undefined.
1122
+
1123
+ """
1124
+ return MergedResult(self._metadata, (self,) + others)
1125
+
1126
+
1127
+ class FilterResult(ResultInternal[_R]):
1128
+ """A wrapper for a :class:`_engine.Result` that returns objects other than
1129
+ :class:`_engine.Row` objects, such as dictionaries or scalar objects.
1130
+
1131
+ :class:`_engine.FilterResult` is the common base for additional result
1132
+ APIs including :class:`_engine.MappingResult`,
1133
+ :class:`_engine.ScalarResult` and :class:`_engine.AsyncResult`.
1134
+
1135
+ """
1136
+
1137
+ __slots__ = (
1138
+ "_real_result",
1139
+ "_post_creational_filter",
1140
+ "_metadata",
1141
+ "_unique_filter_state",
1142
+ "__dict__",
1143
+ )
1144
+
1145
+ _post_creational_filter: Optional[Callable[[Any], Any]]
1146
+
1147
+ _real_result: Result[Unpack[TupleAny]]
1148
+
1149
+ def __enter__(self) -> Self:
1150
+ return self
1151
+
1152
+ def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
1153
+ self._real_result.__exit__(type_, value, traceback)
1154
+
1155
+ @_generative
1156
+ def yield_per(self, num: int) -> Self:
1157
+ """Configure the row-fetching strategy to fetch ``num`` rows at a time.
1158
+
1159
+ The :meth:`_engine.FilterResult.yield_per` method is a pass through
1160
+ to the :meth:`_engine.Result.yield_per` method. See that method's
1161
+ documentation for usage notes.
1162
+
1163
+ .. versionadded:: 1.4.40 - added :meth:`_engine.FilterResult.yield_per`
1164
+ so that the method is available on all result set implementations
1165
+
1166
+ .. seealso::
1167
+
1168
+ :ref:`engine_stream_results` - describes Core behavior for
1169
+ :meth:`_engine.Result.yield_per`
1170
+
1171
+ :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel`
1172
+
1173
+ """
1174
+ self._real_result = self._real_result.yield_per(num)
1175
+ return self
1176
+
1177
+ def _soft_close(self, hard: bool = False) -> None:
1178
+ self._real_result._soft_close(hard=hard)
1179
+
1180
+ @property
1181
+ def _soft_closed(self) -> bool:
1182
+ return self._real_result._soft_closed
1183
+
1184
+ @property
1185
+ def closed(self) -> bool:
1186
+ """Return ``True`` if the underlying :class:`_engine.Result` reports
1187
+ closed
1188
+
1189
+ .. versionadded:: 1.4.43
1190
+
1191
+ """
1192
+ return self._real_result.closed
1193
+
1194
+ def close(self) -> None:
1195
+ """Close this :class:`_engine.FilterResult`.
1196
+
1197
+ .. versionadded:: 1.4.43
1198
+
1199
+ """
1200
+ self._real_result.close()
1201
+
1202
+ @property
1203
+ def _attributes(self) -> Dict[Any, Any]:
1204
+ return self._real_result._attributes
1205
+
1206
+ def _fetchiter_impl(
1207
+ self,
1208
+ ) -> Iterator[_InterimRowType[Row[Unpack[TupleAny]]]]:
1209
+ return self._real_result._fetchiter_impl()
1210
+
1211
+ def _fetchone_impl(
1212
+ self, hard_close: bool = False
1213
+ ) -> Optional[_InterimRowType[Row[Unpack[TupleAny]]]]:
1214
+ return self._real_result._fetchone_impl(hard_close=hard_close)
1215
+
1216
+ def _fetchall_impl(
1217
+ self,
1218
+ ) -> List[_InterimRowType[Row[Unpack[TupleAny]]]]:
1219
+ return self._real_result._fetchall_impl()
1220
+
1221
+ def _fetchmany_impl(
1222
+ self, size: Optional[int] = None
1223
+ ) -> List[_InterimRowType[Row[Unpack[TupleAny]]]]:
1224
+ return self._real_result._fetchmany_impl(size=size)
1225
+
1226
+
1227
+ class ScalarResult(FilterResult[_R]):
1228
+ """A wrapper for a :class:`_engine.Result` that returns scalar values
1229
+ rather than :class:`_row.Row` values.
1230
+
1231
+ The :class:`_engine.ScalarResult` object is acquired by calling the
1232
+ :meth:`_engine.Result.scalars` method.
1233
+
1234
+ A special limitation of :class:`_engine.ScalarResult` is that it has
1235
+ no ``fetchone()`` method; since the semantics of ``fetchone()`` are that
1236
+ the ``None`` value indicates no more results, this is not compatible
1237
+ with :class:`_engine.ScalarResult` since there is no way to distinguish
1238
+ between ``None`` as a row value versus ``None`` as an indicator. Use
1239
+ ``next(result)`` to receive values individually.
1240
+
1241
+ """
1242
+
1243
+ __slots__ = ()
1244
+
1245
+ _generate_rows = False
1246
+
1247
+ _post_creational_filter: Optional[Callable[[Any], Any]]
1248
+
1249
+ def __init__(
1250
+ self, real_result: Result[Unpack[TupleAny]], index: _KeyIndexType
1251
+ ):
1252
+ self._real_result = real_result
1253
+
1254
+ if real_result._source_supports_scalars:
1255
+ self._metadata = real_result._metadata
1256
+ self._post_creational_filter = None
1257
+ else:
1258
+ self._metadata = real_result._metadata._reduce([index])
1259
+ self._post_creational_filter = operator.itemgetter(0)
1260
+
1261
+ self._unique_filter_state = real_result._unique_filter_state
1262
+
1263
+ def unique(self, strategy: Optional[_UniqueFilterType] = None) -> Self:
1264
+ """Apply unique filtering to the objects returned by this
1265
+ :class:`_engine.ScalarResult`.
1266
+
1267
+ See :meth:`_engine.Result.unique` for usage details.
1268
+
1269
+ """
1270
+ self._unique_filter_state = (set(), strategy)
1271
+ return self
1272
+
1273
+ def partitions(self, size: Optional[int] = None) -> Iterator[Sequence[_R]]:
1274
+ """Iterate through sub-lists of elements of the size given.
1275
+
1276
+ Equivalent to :meth:`_engine.Result.partitions` except that
1277
+ scalar values, rather than :class:`_engine.Row` objects,
1278
+ are returned.
1279
+
1280
+ """
1281
+
1282
+ getter = self._manyrow_getter
1283
+
1284
+ while True:
1285
+ partition = getter(self, size)
1286
+ if partition:
1287
+ yield partition
1288
+ else:
1289
+ break
1290
+
1291
+ def fetchall(self) -> Sequence[_R]:
1292
+ """A synonym for the :meth:`_engine.ScalarResult.all` method."""
1293
+
1294
+ return self._allrows()
1295
+
1296
+ def fetchmany(self, size: Optional[int] = None) -> Sequence[_R]:
1297
+ """Fetch many objects.
1298
+
1299
+ Equivalent to :meth:`_engine.Result.fetchmany` except that
1300
+ scalar values, rather than :class:`_engine.Row` objects,
1301
+ are returned.
1302
+
1303
+ """
1304
+ return self._manyrow_getter(self, size)
1305
+
1306
+ def all(self) -> Sequence[_R]:
1307
+ """Return all scalar values in a sequence.
1308
+
1309
+ Equivalent to :meth:`_engine.Result.all` except that
1310
+ scalar values, rather than :class:`_engine.Row` objects,
1311
+ are returned.
1312
+
1313
+ """
1314
+ return self._allrows()
1315
+
1316
+ def __iter__(self) -> Iterator[_R]:
1317
+ return self._iter_impl()
1318
+
1319
+ def __next__(self) -> _R:
1320
+ return self._next_impl()
1321
+
1322
+ def first(self) -> Optional[_R]:
1323
+ """Fetch the first object or ``None`` if no object is present.
1324
+
1325
+ Equivalent to :meth:`_engine.Result.first` except that
1326
+ scalar values, rather than :class:`_engine.Row` objects,
1327
+ are returned.
1328
+
1329
+
1330
+ """
1331
+ return self._only_one_row(
1332
+ raise_for_second_row=False, raise_for_none=False, scalar=False
1333
+ )
1334
+
1335
+ def one_or_none(self) -> Optional[_R]:
1336
+ """Return at most one object or raise an exception.
1337
+
1338
+ Equivalent to :meth:`_engine.Result.one_or_none` except that
1339
+ scalar values, rather than :class:`_engine.Row` objects,
1340
+ are returned.
1341
+
1342
+ """
1343
+ return self._only_one_row(
1344
+ raise_for_second_row=True, raise_for_none=False, scalar=False
1345
+ )
1346
+
1347
+ def one(self) -> _R:
1348
+ """Return exactly one object or raise an exception.
1349
+
1350
+ Equivalent to :meth:`_engine.Result.one` except that
1351
+ scalar values, rather than :class:`_engine.Row` objects,
1352
+ are returned.
1353
+
1354
+ """
1355
+ return self._only_one_row(
1356
+ raise_for_second_row=True, raise_for_none=True, scalar=False
1357
+ )
1358
+
1359
+
1360
+ class TupleResult(FilterResult[_R], util.TypingOnly):
1361
+ """A :class:`_engine.Result` that's typed as returning plain
1362
+ Python tuples instead of rows.
1363
+
1364
+ Since :class:`_engine.Row` acts like a tuple in every way already,
1365
+ this class is a typing only class, regular :class:`_engine.Result` is
1366
+ still used at runtime.
1367
+
1368
+ """
1369
+
1370
+ __slots__ = ()
1371
+
1372
+ if TYPE_CHECKING:
1373
+
1374
+ def partitions(
1375
+ self, size: Optional[int] = None
1376
+ ) -> Iterator[Sequence[_R]]:
1377
+ """Iterate through sub-lists of elements of the size given.
1378
+
1379
+ Equivalent to :meth:`_engine.Result.partitions` except that
1380
+ tuple values, rather than :class:`_engine.Row` objects,
1381
+ are returned.
1382
+
1383
+ """
1384
+ ...
1385
+
1386
+ def fetchone(self) -> Optional[_R]:
1387
+ """Fetch one tuple.
1388
+
1389
+ Equivalent to :meth:`_engine.Result.fetchone` except that
1390
+ tuple values, rather than :class:`_engine.Row`
1391
+ objects, are returned.
1392
+
1393
+ """
1394
+ ...
1395
+
1396
+ def fetchall(self) -> Sequence[_R]:
1397
+ """A synonym for the :meth:`_engine.ScalarResult.all` method."""
1398
+ ...
1399
+
1400
+ def fetchmany(self, size: Optional[int] = None) -> Sequence[_R]:
1401
+ """Fetch many objects.
1402
+
1403
+ Equivalent to :meth:`_engine.Result.fetchmany` except that
1404
+ tuple values, rather than :class:`_engine.Row` objects,
1405
+ are returned.
1406
+
1407
+ """
1408
+ ...
1409
+
1410
+ def all(self) -> Sequence[_R]: # noqa: A001
1411
+ """Return all scalar values in a sequence.
1412
+
1413
+ Equivalent to :meth:`_engine.Result.all` except that
1414
+ tuple values, rather than :class:`_engine.Row` objects,
1415
+ are returned.
1416
+
1417
+ """
1418
+ ...
1419
+
1420
+ def __iter__(self) -> Iterator[_R]: ...
1421
+
1422
+ def __next__(self) -> _R: ...
1423
+
1424
+ def first(self) -> Optional[_R]:
1425
+ """Fetch the first object or ``None`` if no object is present.
1426
+
1427
+ Equivalent to :meth:`_engine.Result.first` except that
1428
+ tuple values, rather than :class:`_engine.Row` objects,
1429
+ are returned.
1430
+
1431
+
1432
+ """
1433
+ ...
1434
+
1435
+ def one_or_none(self) -> Optional[_R]:
1436
+ """Return at most one object or raise an exception.
1437
+
1438
+ Equivalent to :meth:`_engine.Result.one_or_none` except that
1439
+ tuple values, rather than :class:`_engine.Row` objects,
1440
+ are returned.
1441
+
1442
+ """
1443
+ ...
1444
+
1445
+ def one(self) -> _R:
1446
+ """Return exactly one object or raise an exception.
1447
+
1448
+ Equivalent to :meth:`_engine.Result.one` except that
1449
+ tuple values, rather than :class:`_engine.Row` objects,
1450
+ are returned.
1451
+
1452
+ """
1453
+ ...
1454
+
1455
+ @overload
1456
+ def scalar_one(self: TupleResult[Tuple[_T]]) -> _T: ...
1457
+
1458
+ @overload
1459
+ def scalar_one(self) -> Any: ...
1460
+
1461
+ def scalar_one(self) -> Any:
1462
+ """Return exactly one scalar result or raise an exception.
1463
+
1464
+ This is equivalent to calling :meth:`_engine.Result.scalars`
1465
+ and then :meth:`_engine.ScalarResult.one`.
1466
+
1467
+ .. seealso::
1468
+
1469
+ :meth:`_engine.ScalarResult.one`
1470
+
1471
+ :meth:`_engine.Result.scalars`
1472
+
1473
+ """
1474
+ ...
1475
+
1476
+ @overload
1477
+ def scalar_one_or_none(
1478
+ self: TupleResult[Tuple[_T]],
1479
+ ) -> Optional[_T]: ...
1480
+
1481
+ @overload
1482
+ def scalar_one_or_none(self) -> Optional[Any]: ...
1483
+
1484
+ def scalar_one_or_none(self) -> Optional[Any]:
1485
+ """Return exactly one or no scalar result.
1486
+
1487
+ This is equivalent to calling :meth:`_engine.Result.scalars`
1488
+ and then :meth:`_engine.ScalarResult.one_or_none`.
1489
+
1490
+ .. seealso::
1491
+
1492
+ :meth:`_engine.ScalarResult.one_or_none`
1493
+
1494
+ :meth:`_engine.Result.scalars`
1495
+
1496
+ """
1497
+ ...
1498
+
1499
+ @overload
1500
+ def scalar(self: TupleResult[Tuple[_T]]) -> Optional[_T]: ...
1501
+
1502
+ @overload
1503
+ def scalar(self) -> Any: ...
1504
+
1505
+ def scalar(self) -> Any:
1506
+ """Fetch the first column of the first row, and close the result
1507
+ set.
1508
+
1509
+ Returns ``None`` if there are no rows to fetch.
1510
+
1511
+ No validation is performed to test if additional rows remain.
1512
+
1513
+ After calling this method, the object is fully closed,
1514
+ e.g. the :meth:`_engine.CursorResult.close`
1515
+ method will have been called.
1516
+
1517
+ :return: a Python scalar value , or ``None`` if no rows remain.
1518
+
1519
+ """
1520
+ ...
1521
+
1522
+
1523
+ class MappingResult(_WithKeys, FilterResult[RowMapping]):
1524
+ """A wrapper for a :class:`_engine.Result` that returns dictionary values
1525
+ rather than :class:`_engine.Row` values.
1526
+
1527
+ The :class:`_engine.MappingResult` object is acquired by calling the
1528
+ :meth:`_engine.Result.mappings` method.
1529
+
1530
+ """
1531
+
1532
+ __slots__ = ()
1533
+
1534
+ _generate_rows = True
1535
+
1536
+ _post_creational_filter = operator.attrgetter("_mapping")
1537
+
1538
+ def __init__(self, result: Result[Unpack[TupleAny]]):
1539
+ self._real_result = result
1540
+ self._unique_filter_state = result._unique_filter_state
1541
+ self._metadata = result._metadata
1542
+ if result._source_supports_scalars:
1543
+ self._metadata = self._metadata._reduce([0])
1544
+
1545
+ def unique(self, strategy: Optional[_UniqueFilterType] = None) -> Self:
1546
+ """Apply unique filtering to the objects returned by this
1547
+ :class:`_engine.MappingResult`.
1548
+
1549
+ See :meth:`_engine.Result.unique` for usage details.
1550
+
1551
+ """
1552
+ self._unique_filter_state = (set(), strategy)
1553
+ return self
1554
+
1555
+ def columns(self, *col_expressions: _KeyIndexType) -> Self:
1556
+ """Establish the columns that should be returned in each row."""
1557
+ return self._column_slices(col_expressions)
1558
+
1559
+ def partitions(
1560
+ self, size: Optional[int] = None
1561
+ ) -> Iterator[Sequence[RowMapping]]:
1562
+ """Iterate through sub-lists of elements of the size given.
1563
+
1564
+ Equivalent to :meth:`_engine.Result.partitions` except that
1565
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1566
+ objects, are returned.
1567
+
1568
+ """
1569
+
1570
+ getter = self._manyrow_getter
1571
+
1572
+ while True:
1573
+ partition = getter(self, size)
1574
+ if partition:
1575
+ yield partition
1576
+ else:
1577
+ break
1578
+
1579
+ def fetchall(self) -> Sequence[RowMapping]:
1580
+ """A synonym for the :meth:`_engine.MappingResult.all` method."""
1581
+
1582
+ return self._allrows()
1583
+
1584
+ def fetchone(self) -> Optional[RowMapping]:
1585
+ """Fetch one object.
1586
+
1587
+ Equivalent to :meth:`_engine.Result.fetchone` except that
1588
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1589
+ objects, are returned.
1590
+
1591
+ """
1592
+
1593
+ row = self._onerow_getter(self)
1594
+ if row is _NO_ROW:
1595
+ return None
1596
+ else:
1597
+ return row
1598
+
1599
+ def fetchmany(self, size: Optional[int] = None) -> Sequence[RowMapping]:
1600
+ """Fetch many objects.
1601
+
1602
+ Equivalent to :meth:`_engine.Result.fetchmany` except that
1603
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1604
+ objects, are returned.
1605
+
1606
+ """
1607
+
1608
+ return self._manyrow_getter(self, size)
1609
+
1610
+ def all(self) -> Sequence[RowMapping]:
1611
+ """Return all scalar values in a sequence.
1612
+
1613
+ Equivalent to :meth:`_engine.Result.all` except that
1614
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1615
+ objects, are returned.
1616
+
1617
+ """
1618
+
1619
+ return self._allrows()
1620
+
1621
+ def __iter__(self) -> Iterator[RowMapping]:
1622
+ return self._iter_impl()
1623
+
1624
+ def __next__(self) -> RowMapping:
1625
+ return self._next_impl()
1626
+
1627
+ def first(self) -> Optional[RowMapping]:
1628
+ """Fetch the first object or ``None`` if no object is present.
1629
+
1630
+ Equivalent to :meth:`_engine.Result.first` except that
1631
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1632
+ objects, are returned.
1633
+
1634
+
1635
+ """
1636
+ return self._only_one_row(
1637
+ raise_for_second_row=False, raise_for_none=False, scalar=False
1638
+ )
1639
+
1640
+ def one_or_none(self) -> Optional[RowMapping]:
1641
+ """Return at most one object or raise an exception.
1642
+
1643
+ Equivalent to :meth:`_engine.Result.one_or_none` except that
1644
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1645
+ objects, are returned.
1646
+
1647
+ """
1648
+ return self._only_one_row(
1649
+ raise_for_second_row=True, raise_for_none=False, scalar=False
1650
+ )
1651
+
1652
+ def one(self) -> RowMapping:
1653
+ """Return exactly one object or raise an exception.
1654
+
1655
+ Equivalent to :meth:`_engine.Result.one` except that
1656
+ :class:`_engine.RowMapping` values, rather than :class:`_engine.Row`
1657
+ objects, are returned.
1658
+
1659
+ """
1660
+ return self._only_one_row(
1661
+ raise_for_second_row=True, raise_for_none=True, scalar=False
1662
+ )
1663
+
1664
+
1665
+ class FrozenResult(Generic[Unpack[_Ts]]):
1666
+ """Represents a :class:`_engine.Result` object in a "frozen" state suitable
1667
+ for caching.
1668
+
1669
+ The :class:`_engine.FrozenResult` object is returned from the
1670
+ :meth:`_engine.Result.freeze` method of any :class:`_engine.Result`
1671
+ object.
1672
+
1673
+ A new iterable :class:`_engine.Result` object is generated from a fixed
1674
+ set of data each time the :class:`_engine.FrozenResult` is invoked as
1675
+ a callable::
1676
+
1677
+
1678
+ result = connection.execute(query)
1679
+
1680
+ frozen = result.freeze()
1681
+
1682
+ unfrozen_result_one = frozen()
1683
+
1684
+ for row in unfrozen_result_one:
1685
+ print(row)
1686
+
1687
+ unfrozen_result_two = frozen()
1688
+ rows = unfrozen_result_two.all()
1689
+
1690
+ # ... etc
1691
+
1692
+ .. versionadded:: 1.4
1693
+
1694
+ .. seealso::
1695
+
1696
+ :ref:`do_orm_execute_re_executing` - example usage within the
1697
+ ORM to implement a result-set cache.
1698
+
1699
+ :func:`_orm.loading.merge_frozen_result` - ORM function to merge
1700
+ a frozen result back into a :class:`_orm.Session`.
1701
+
1702
+ """
1703
+
1704
+ data: Sequence[Any]
1705
+
1706
+ def __init__(self, result: Result[Unpack[_Ts]]):
1707
+ self.metadata = result._metadata._for_freeze()
1708
+ self._source_supports_scalars = result._source_supports_scalars
1709
+ self._attributes = result._attributes
1710
+
1711
+ if self._source_supports_scalars:
1712
+ self.data = list(result._raw_row_iterator())
1713
+ else:
1714
+ self.data = result.fetchall()
1715
+
1716
+ def _rewrite_rows(self) -> Sequence[Sequence[Any]]:
1717
+ # used only by the orm fn merge_frozen_result
1718
+ if self._source_supports_scalars:
1719
+ return [[elem] for elem in self.data]
1720
+ else:
1721
+ return [list(row) for row in self.data]
1722
+
1723
+ def with_new_rows(
1724
+ self, tuple_data: Sequence[Row[Unpack[_Ts]]]
1725
+ ) -> FrozenResult[Unpack[_Ts]]:
1726
+ fr = FrozenResult.__new__(FrozenResult)
1727
+ fr.metadata = self.metadata
1728
+ fr._attributes = self._attributes
1729
+ fr._source_supports_scalars = self._source_supports_scalars
1730
+
1731
+ if self._source_supports_scalars:
1732
+ fr.data = [d[0] for d in tuple_data] # type: ignore[misc]
1733
+ else:
1734
+ fr.data = tuple_data
1735
+ return fr
1736
+
1737
+ def __call__(self) -> Result[Unpack[_Ts]]:
1738
+ result: IteratorResult[Unpack[_Ts]] = IteratorResult(
1739
+ self.metadata, iter(self.data)
1740
+ )
1741
+ result._attributes = self._attributes
1742
+ result._source_supports_scalars = self._source_supports_scalars
1743
+ return result
1744
+
1745
+
1746
+ class IteratorResult(Result[Unpack[_Ts]]):
1747
+ """A :class:`_engine.Result` that gets data from a Python iterator of
1748
+ :class:`_engine.Row` objects or similar row-like data.
1749
+
1750
+ .. versionadded:: 1.4
1751
+
1752
+ """
1753
+
1754
+ _hard_closed = False
1755
+ _soft_closed = False
1756
+
1757
+ def __init__(
1758
+ self,
1759
+ cursor_metadata: ResultMetaData,
1760
+ iterator: Iterator[_InterimSupportsScalarsRowType],
1761
+ raw: Optional[Result[Any]] = None,
1762
+ _source_supports_scalars: bool = False,
1763
+ ):
1764
+ self._metadata = cursor_metadata
1765
+ self.iterator = iterator
1766
+ self.raw = raw
1767
+ self._source_supports_scalars = _source_supports_scalars
1768
+
1769
+ @property
1770
+ def closed(self) -> bool:
1771
+ """Return ``True`` if this :class:`_engine.IteratorResult` has
1772
+ been closed
1773
+
1774
+ .. versionadded:: 1.4.43
1775
+
1776
+ """
1777
+ return self._hard_closed
1778
+
1779
+ def _soft_close(self, hard: bool = False, **kw: Any) -> None:
1780
+ if hard:
1781
+ self._hard_closed = True
1782
+ if self.raw is not None:
1783
+ self.raw._soft_close(hard=hard, **kw)
1784
+ self.iterator = iter([])
1785
+ self._reset_memoizations()
1786
+ self._soft_closed = True
1787
+
1788
+ def _raise_hard_closed(self) -> NoReturn:
1789
+ raise exc.ResourceClosedError("This result object is closed.")
1790
+
1791
+ def _raw_row_iterator(self) -> Iterator[_RowData]:
1792
+ return self.iterator
1793
+
1794
+ def _fetchiter_impl(self) -> Iterator[_InterimSupportsScalarsRowType]:
1795
+ if self._hard_closed:
1796
+ self._raise_hard_closed()
1797
+ return self.iterator
1798
+
1799
+ def _fetchone_impl(
1800
+ self, hard_close: bool = False
1801
+ ) -> Optional[_InterimRowType[Row[Unpack[TupleAny]]]]:
1802
+ if self._hard_closed:
1803
+ self._raise_hard_closed()
1804
+
1805
+ row = next(self.iterator, _NO_ROW)
1806
+ if row is _NO_ROW:
1807
+ self._soft_close(hard=hard_close)
1808
+ return None
1809
+ else:
1810
+ return row
1811
+
1812
+ def _fetchall_impl(
1813
+ self,
1814
+ ) -> List[_InterimRowType[Row[Unpack[TupleAny]]]]:
1815
+ if self._hard_closed:
1816
+ self._raise_hard_closed()
1817
+ try:
1818
+ return list(self.iterator)
1819
+ finally:
1820
+ self._soft_close()
1821
+
1822
+ def _fetchmany_impl(
1823
+ self, size: Optional[int] = None
1824
+ ) -> List[_InterimRowType[Row[Unpack[TupleAny]]]]:
1825
+ if self._hard_closed:
1826
+ self._raise_hard_closed()
1827
+
1828
+ return list(itertools.islice(self.iterator, 0, size))
1829
+
1830
+
1831
+ def null_result() -> IteratorResult[Any]:
1832
+ return IteratorResult(SimpleResultMetaData([]), iter([]))
1833
+
1834
+
1835
+ class ChunkedIteratorResult(IteratorResult[Unpack[_Ts]]):
1836
+ """An :class:`_engine.IteratorResult` that works from an
1837
+ iterator-producing callable.
1838
+
1839
+ The given ``chunks`` argument is a function that is given a number of rows
1840
+ to return in each chunk, or ``None`` for all rows. The function should
1841
+ then return an un-consumed iterator of lists, each list of the requested
1842
+ size.
1843
+
1844
+ The function can be called at any time again, in which case it should
1845
+ continue from the same result set but adjust the chunk size as given.
1846
+
1847
+ .. versionadded:: 1.4
1848
+
1849
+ """
1850
+
1851
+ def __init__(
1852
+ self,
1853
+ cursor_metadata: ResultMetaData,
1854
+ chunks: Callable[
1855
+ [Optional[int]], Iterator[Sequence[_InterimRowType[_R]]]
1856
+ ],
1857
+ source_supports_scalars: bool = False,
1858
+ raw: Optional[Result[Any]] = None,
1859
+ dynamic_yield_per: bool = False,
1860
+ ):
1861
+ self._metadata = cursor_metadata
1862
+ self.chunks = chunks
1863
+ self._source_supports_scalars = source_supports_scalars
1864
+ self.raw = raw
1865
+ self.iterator = itertools.chain.from_iterable(self.chunks(None))
1866
+ self.dynamic_yield_per = dynamic_yield_per
1867
+
1868
+ @_generative
1869
+ def yield_per(self, num: int) -> Self:
1870
+ # TODO: this throws away the iterator which may be holding
1871
+ # onto a chunk. the yield_per cannot be changed once any
1872
+ # rows have been fetched. either find a way to enforce this,
1873
+ # or we can't use itertools.chain and will instead have to
1874
+ # keep track.
1875
+
1876
+ self._yield_per = num
1877
+ self.iterator = itertools.chain.from_iterable(self.chunks(num))
1878
+ return self
1879
+
1880
+ def _soft_close(self, hard: bool = False, **kw: Any) -> None:
1881
+ super()._soft_close(hard=hard, **kw)
1882
+ self.chunks = lambda size: [] # type: ignore
1883
+
1884
+ def _fetchmany_impl(
1885
+ self, size: Optional[int] = None
1886
+ ) -> List[_InterimRowType[Row[Unpack[TupleAny]]]]:
1887
+ if self.dynamic_yield_per:
1888
+ self.iterator = itertools.chain.from_iterable(self.chunks(size))
1889
+ return super()._fetchmany_impl(size=size)
1890
+
1891
+
1892
+ class MergedResult(IteratorResult[Unpack[_Ts]]):
1893
+ """A :class:`_engine.Result` that is merged from any number of
1894
+ :class:`_engine.Result` objects.
1895
+
1896
+ Returned by the :meth:`_engine.Result.merge` method.
1897
+
1898
+ .. versionadded:: 1.4
1899
+
1900
+ """
1901
+
1902
+ closed = False
1903
+ rowcount: Optional[int]
1904
+
1905
+ def __init__(
1906
+ self,
1907
+ cursor_metadata: ResultMetaData,
1908
+ results: Sequence[Result[Unpack[_Ts]]],
1909
+ ):
1910
+ self._results = results
1911
+ super().__init__(
1912
+ cursor_metadata,
1913
+ itertools.chain.from_iterable(
1914
+ r._raw_row_iterator() for r in results
1915
+ ),
1916
+ )
1917
+
1918
+ self._unique_filter_state = results[0]._unique_filter_state
1919
+ self._yield_per = results[0]._yield_per
1920
+
1921
+ # going to try something w/ this in next rev
1922
+ self._source_supports_scalars = results[0]._source_supports_scalars
1923
+
1924
+ self._attributes = self._attributes.merge_with(
1925
+ *[r._attributes for r in results]
1926
+ )
1927
+
1928
+ def _soft_close(self, hard: bool = False, **kw: Any) -> None:
1929
+ for r in self._results:
1930
+ r._soft_close(hard=hard, **kw)
1931
+ if hard:
1932
+ self.closed = True