result.py 76 KB

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