result.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. # engine/result.py
  2. # Copyright (C) 2005-2022 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. import functools
  9. import itertools
  10. import operator
  11. from .row import _baserow_usecext
  12. from .row import Row
  13. from .. import exc
  14. from .. import util
  15. from ..sql.base import _generative
  16. from ..sql.base import HasMemoized
  17. from ..sql.base import InPlaceGenerative
  18. from ..util import collections_abc
  19. from ..util import py2k
  20. if _baserow_usecext:
  21. from sqlalchemy.cresultproxy import tuplegetter
  22. _row_as_tuple = tuplegetter
  23. else:
  24. def tuplegetter(*indexes):
  25. it = operator.itemgetter(*indexes)
  26. if len(indexes) > 1:
  27. return it
  28. else:
  29. return lambda row: (it(row),)
  30. def _row_as_tuple(*indexes):
  31. # circumvent LegacyRow.__getitem__ pointing to
  32. # _get_by_key_impl_mapping for now. otherwise we could
  33. # use itemgetter
  34. getters = [
  35. operator.methodcaller("_get_by_int_impl", index)
  36. for index in indexes
  37. ]
  38. return lambda rec: tuple([getter(rec) for getter in getters])
  39. class ResultMetaData(object):
  40. """Base for metadata about result rows."""
  41. __slots__ = ()
  42. _tuplefilter = None
  43. _translated_indexes = None
  44. _unique_filters = None
  45. @property
  46. def keys(self):
  47. return RMKeyView(self)
  48. def _has_key(self, key):
  49. raise NotImplementedError()
  50. def _for_freeze(self):
  51. raise NotImplementedError()
  52. def _key_fallback(self, key, err, raiseerr=True):
  53. assert raiseerr
  54. util.raise_(KeyError(key), replace_context=err)
  55. def _warn_for_nonint(self, key):
  56. util.warn_deprecated_20(
  57. "Retrieving row members using strings or other non-integers is "
  58. "deprecated; use row._mapping for a dictionary interface "
  59. "to the row"
  60. )
  61. def _raise_for_nonint(self, key):
  62. raise TypeError(
  63. "TypeError: tuple indices must be integers or slices, not %s"
  64. % type(key).__name__
  65. )
  66. def _index_for_key(self, keys, raiseerr):
  67. raise NotImplementedError()
  68. def _metadata_for_keys(self, key):
  69. raise NotImplementedError()
  70. def _reduce(self, keys):
  71. raise NotImplementedError()
  72. def _getter(self, key, raiseerr=True):
  73. index = self._index_for_key(key, raiseerr)
  74. if index is not None:
  75. return operator.itemgetter(index)
  76. else:
  77. return None
  78. def _row_as_tuple_getter(self, keys):
  79. indexes = self._indexes_for_keys(keys)
  80. return _row_as_tuple(*indexes)
  81. class RMKeyView(collections_abc.KeysView):
  82. __slots__ = ("_parent", "_keys")
  83. def __init__(self, parent):
  84. self._parent = parent
  85. self._keys = [k for k in parent._keys if k is not None]
  86. def __len__(self):
  87. return len(self._keys)
  88. def __repr__(self):
  89. return "{0.__class__.__name__}({0._keys!r})".format(self)
  90. def __iter__(self):
  91. return iter(self._keys)
  92. def __contains__(self, item):
  93. if not _baserow_usecext and isinstance(item, int):
  94. return False
  95. # note this also includes special key fallback behaviors
  96. # which also don't seem to be tested in test_resultset right now
  97. return self._parent._has_key(item)
  98. def __eq__(self, other):
  99. return list(other) == list(self)
  100. def __ne__(self, other):
  101. return list(other) != list(self)
  102. class SimpleResultMetaData(ResultMetaData):
  103. """result metadata for in-memory collections."""
  104. __slots__ = (
  105. "_keys",
  106. "_keymap",
  107. "_processors",
  108. "_tuplefilter",
  109. "_translated_indexes",
  110. "_unique_filters",
  111. )
  112. def __init__(
  113. self,
  114. keys,
  115. extra=None,
  116. _processors=None,
  117. _tuplefilter=None,
  118. _translated_indexes=None,
  119. _unique_filters=None,
  120. ):
  121. self._keys = list(keys)
  122. self._tuplefilter = _tuplefilter
  123. self._translated_indexes = _translated_indexes
  124. self._unique_filters = _unique_filters
  125. if extra:
  126. recs_names = [
  127. (
  128. (name,) + extras,
  129. (index, name, extras),
  130. )
  131. for index, (name, extras) in enumerate(zip(self._keys, extra))
  132. ]
  133. else:
  134. recs_names = [
  135. ((name,), (index, name, ()))
  136. for index, name in enumerate(self._keys)
  137. ]
  138. self._keymap = {key: rec for keys, rec in recs_names for key in keys}
  139. self._processors = _processors
  140. def _has_key(self, key):
  141. return key in self._keymap
  142. def _for_freeze(self):
  143. unique_filters = self._unique_filters
  144. if unique_filters and self._tuplefilter:
  145. unique_filters = self._tuplefilter(unique_filters)
  146. # TODO: are we freezing the result with or without uniqueness
  147. # applied?
  148. return SimpleResultMetaData(
  149. self._keys,
  150. extra=[self._keymap[key][2] for key in self._keys],
  151. _unique_filters=unique_filters,
  152. )
  153. def __getstate__(self):
  154. return {
  155. "_keys": self._keys,
  156. "_translated_indexes": self._translated_indexes,
  157. }
  158. def __setstate__(self, state):
  159. if state["_translated_indexes"]:
  160. _translated_indexes = state["_translated_indexes"]
  161. _tuplefilter = tuplegetter(*_translated_indexes)
  162. else:
  163. _translated_indexes = _tuplefilter = None
  164. self.__init__(
  165. state["_keys"],
  166. _translated_indexes=_translated_indexes,
  167. _tuplefilter=_tuplefilter,
  168. )
  169. def _contains(self, value, row):
  170. return value in row._data
  171. def _index_for_key(self, key, raiseerr=True):
  172. if int in key.__class__.__mro__:
  173. key = self._keys[key]
  174. try:
  175. rec = self._keymap[key]
  176. except KeyError as ke:
  177. rec = self._key_fallback(key, ke, raiseerr)
  178. return rec[0]
  179. def _indexes_for_keys(self, keys):
  180. return [self._keymap[key][0] for key in keys]
  181. def _metadata_for_keys(self, keys):
  182. for key in keys:
  183. if int in key.__class__.__mro__:
  184. key = self._keys[key]
  185. try:
  186. rec = self._keymap[key]
  187. except KeyError as ke:
  188. rec = self._key_fallback(key, ke, True)
  189. yield rec
  190. def _reduce(self, keys):
  191. try:
  192. metadata_for_keys = [
  193. self._keymap[
  194. self._keys[key] if int in key.__class__.__mro__ else key
  195. ]
  196. for key in keys
  197. ]
  198. except KeyError as ke:
  199. self._key_fallback(ke.args[0], ke, True)
  200. indexes, new_keys, extra = zip(*metadata_for_keys)
  201. if self._translated_indexes:
  202. indexes = [self._translated_indexes[idx] for idx in indexes]
  203. tup = tuplegetter(*indexes)
  204. new_metadata = SimpleResultMetaData(
  205. new_keys,
  206. extra=extra,
  207. _tuplefilter=tup,
  208. _translated_indexes=indexes,
  209. _processors=self._processors,
  210. _unique_filters=self._unique_filters,
  211. )
  212. return new_metadata
  213. def result_tuple(fields, extra=None):
  214. parent = SimpleResultMetaData(fields, extra)
  215. return functools.partial(
  216. Row, parent, parent._processors, parent._keymap, Row._default_key_style
  217. )
  218. # a symbol that indicates to internal Result methods that
  219. # "no row is returned". We can't use None for those cases where a scalar
  220. # filter is applied to rows.
  221. _NO_ROW = util.symbol("NO_ROW")
  222. class ResultInternal(InPlaceGenerative):
  223. _real_result = None
  224. _generate_rows = True
  225. _unique_filter_state = None
  226. _post_creational_filter = None
  227. @HasMemoized.memoized_attribute
  228. def _row_getter(self):
  229. real_result = self._real_result if self._real_result else self
  230. if real_result._source_supports_scalars:
  231. if not self._generate_rows:
  232. return None
  233. else:
  234. _proc = real_result._process_row
  235. def process_row(
  236. metadata, processors, keymap, key_style, scalar_obj
  237. ):
  238. return _proc(
  239. metadata, processors, keymap, key_style, (scalar_obj,)
  240. )
  241. else:
  242. process_row = real_result._process_row
  243. key_style = real_result._process_row._default_key_style
  244. metadata = self._metadata
  245. keymap = metadata._keymap
  246. processors = metadata._processors
  247. tf = metadata._tuplefilter
  248. if tf and not real_result._source_supports_scalars:
  249. if processors:
  250. processors = tf(processors)
  251. _make_row_orig = functools.partial(
  252. process_row, metadata, processors, keymap, key_style
  253. )
  254. def make_row(row):
  255. return _make_row_orig(tf(row))
  256. else:
  257. make_row = functools.partial(
  258. process_row, metadata, processors, keymap, key_style
  259. )
  260. fns = ()
  261. if real_result._row_logging_fn:
  262. fns = (real_result._row_logging_fn,)
  263. else:
  264. fns = ()
  265. if fns:
  266. _make_row = make_row
  267. def make_row(row):
  268. row = _make_row(row)
  269. for fn in fns:
  270. row = fn(row)
  271. return row
  272. return make_row
  273. @HasMemoized.memoized_attribute
  274. def _iterator_getter(self):
  275. make_row = self._row_getter
  276. post_creational_filter = self._post_creational_filter
  277. if self._unique_filter_state:
  278. uniques, strategy = self._unique_strategy
  279. def iterrows(self):
  280. for row in self._fetchiter_impl():
  281. obj = make_row(row) if make_row else row
  282. hashed = strategy(obj) if strategy else obj
  283. if hashed in uniques:
  284. continue
  285. uniques.add(hashed)
  286. if post_creational_filter:
  287. obj = post_creational_filter(obj)
  288. yield obj
  289. else:
  290. def iterrows(self):
  291. for row in self._fetchiter_impl():
  292. row = make_row(row) if make_row else row
  293. if post_creational_filter:
  294. row = post_creational_filter(row)
  295. yield row
  296. return iterrows
  297. def _raw_all_rows(self):
  298. make_row = self._row_getter
  299. rows = self._fetchall_impl()
  300. return [make_row(row) for row in rows]
  301. def _allrows(self):
  302. post_creational_filter = self._post_creational_filter
  303. make_row = self._row_getter
  304. rows = self._fetchall_impl()
  305. if make_row:
  306. made_rows = [make_row(row) for row in rows]
  307. else:
  308. made_rows = rows
  309. if self._unique_filter_state:
  310. uniques, strategy = self._unique_strategy
  311. rows = [
  312. made_row
  313. for made_row, sig_row in [
  314. (
  315. made_row,
  316. strategy(made_row) if strategy else made_row,
  317. )
  318. for made_row in made_rows
  319. ]
  320. if sig_row not in uniques and not uniques.add(sig_row)
  321. ]
  322. else:
  323. rows = made_rows
  324. if post_creational_filter:
  325. rows = [post_creational_filter(row) for row in rows]
  326. return rows
  327. @HasMemoized.memoized_attribute
  328. def _onerow_getter(self):
  329. make_row = self._row_getter
  330. post_creational_filter = self._post_creational_filter
  331. if self._unique_filter_state:
  332. uniques, strategy = self._unique_strategy
  333. def onerow(self):
  334. _onerow = self._fetchone_impl
  335. while True:
  336. row = _onerow()
  337. if row is None:
  338. return _NO_ROW
  339. else:
  340. obj = make_row(row) if make_row else row
  341. hashed = strategy(obj) if strategy else obj
  342. if hashed in uniques:
  343. continue
  344. else:
  345. uniques.add(hashed)
  346. if post_creational_filter:
  347. obj = post_creational_filter(obj)
  348. return obj
  349. else:
  350. def onerow(self):
  351. row = self._fetchone_impl()
  352. if row is None:
  353. return _NO_ROW
  354. else:
  355. row = make_row(row) if make_row else row
  356. if post_creational_filter:
  357. row = post_creational_filter(row)
  358. return row
  359. return onerow
  360. @HasMemoized.memoized_attribute
  361. def _manyrow_getter(self):
  362. make_row = self._row_getter
  363. post_creational_filter = self._post_creational_filter
  364. if self._unique_filter_state:
  365. uniques, strategy = self._unique_strategy
  366. def filterrows(make_row, rows, strategy, uniques):
  367. if make_row:
  368. rows = [make_row(row) for row in rows]
  369. if strategy:
  370. made_rows = (
  371. (made_row, strategy(made_row)) for made_row in rows
  372. )
  373. else:
  374. made_rows = ((made_row, made_row) for made_row in rows)
  375. return [
  376. made_row
  377. for made_row, sig_row in made_rows
  378. if sig_row not in uniques and not uniques.add(sig_row)
  379. ]
  380. def manyrows(self, num):
  381. collect = []
  382. _manyrows = self._fetchmany_impl
  383. if num is None:
  384. # if None is passed, we don't know the default
  385. # manyrows number, DBAPI has this as cursor.arraysize
  386. # different DBAPIs / fetch strategies may be different.
  387. # do a fetch to find what the number is. if there are
  388. # only fewer rows left, then it doesn't matter.
  389. real_result = (
  390. self._real_result if self._real_result else self
  391. )
  392. if real_result._yield_per:
  393. num_required = num = real_result._yield_per
  394. else:
  395. rows = _manyrows(num)
  396. num = len(rows)
  397. collect.extend(
  398. filterrows(make_row, rows, strategy, uniques)
  399. )
  400. num_required = num - len(collect)
  401. else:
  402. num_required = num
  403. while num_required:
  404. rows = _manyrows(num_required)
  405. if not rows:
  406. break
  407. collect.extend(
  408. filterrows(make_row, rows, strategy, uniques)
  409. )
  410. num_required = num - len(collect)
  411. if post_creational_filter:
  412. collect = [post_creational_filter(row) for row in collect]
  413. return collect
  414. else:
  415. def manyrows(self, num):
  416. if num is None:
  417. real_result = (
  418. self._real_result if self._real_result else self
  419. )
  420. num = real_result._yield_per
  421. rows = self._fetchmany_impl(num)
  422. if make_row:
  423. rows = [make_row(row) for row in rows]
  424. if post_creational_filter:
  425. rows = [post_creational_filter(row) for row in rows]
  426. return rows
  427. return manyrows
  428. def _only_one_row(
  429. self,
  430. raise_for_second_row,
  431. raise_for_none,
  432. scalar,
  433. ):
  434. onerow = self._fetchone_impl
  435. row = onerow(hard_close=True)
  436. if row is None:
  437. if raise_for_none:
  438. raise exc.NoResultFound(
  439. "No row was found when one was required"
  440. )
  441. else:
  442. return None
  443. if scalar and self._source_supports_scalars:
  444. self._generate_rows = False
  445. make_row = None
  446. else:
  447. make_row = self._row_getter
  448. try:
  449. row = make_row(row) if make_row else row
  450. except:
  451. self._soft_close(hard=True)
  452. raise
  453. if raise_for_second_row:
  454. if self._unique_filter_state:
  455. # for no second row but uniqueness, need to essentially
  456. # consume the entire result :(
  457. uniques, strategy = self._unique_strategy
  458. existing_row_hash = strategy(row) if strategy else row
  459. while True:
  460. next_row = onerow(hard_close=True)
  461. if next_row is None:
  462. next_row = _NO_ROW
  463. break
  464. try:
  465. next_row = make_row(next_row) if make_row else next_row
  466. if strategy:
  467. if existing_row_hash == strategy(next_row):
  468. continue
  469. elif row == next_row:
  470. continue
  471. # here, we have a row and it's different
  472. break
  473. except:
  474. self._soft_close(hard=True)
  475. raise
  476. else:
  477. next_row = onerow(hard_close=True)
  478. if next_row is None:
  479. next_row = _NO_ROW
  480. if next_row is not _NO_ROW:
  481. self._soft_close(hard=True)
  482. raise exc.MultipleResultsFound(
  483. "Multiple rows were found when exactly one was required"
  484. if raise_for_none
  485. else "Multiple rows were found when one or none "
  486. "was required"
  487. )
  488. else:
  489. next_row = _NO_ROW
  490. # if we checked for second row then that would have
  491. # closed us :)
  492. self._soft_close(hard=True)
  493. if not scalar:
  494. post_creational_filter = self._post_creational_filter
  495. if post_creational_filter:
  496. row = post_creational_filter(row)
  497. if scalar and make_row:
  498. return row[0]
  499. else:
  500. return row
  501. def _iter_impl(self):
  502. return self._iterator_getter(self)
  503. def _next_impl(self):
  504. row = self._onerow_getter(self)
  505. if row is _NO_ROW:
  506. raise StopIteration()
  507. else:
  508. return row
  509. @_generative
  510. def _column_slices(self, indexes):
  511. real_result = self._real_result if self._real_result else self
  512. if real_result._source_supports_scalars and len(indexes) == 1:
  513. self._generate_rows = False
  514. else:
  515. self._generate_rows = True
  516. self._metadata = self._metadata._reduce(indexes)
  517. @HasMemoized.memoized_attribute
  518. def _unique_strategy(self):
  519. uniques, strategy = self._unique_filter_state
  520. real_result = (
  521. self._real_result if self._real_result is not None else self
  522. )
  523. if not strategy and self._metadata._unique_filters:
  524. if (
  525. real_result._source_supports_scalars
  526. and not self._generate_rows
  527. ):
  528. strategy = self._metadata._unique_filters[0]
  529. else:
  530. filters = self._metadata._unique_filters
  531. if self._metadata._tuplefilter:
  532. filters = self._metadata._tuplefilter(filters)
  533. strategy = operator.methodcaller("_filter_on_values", filters)
  534. return uniques, strategy
  535. class _WithKeys(object):
  536. # used mainly to share documentation on the keys method.
  537. # py2k does not allow overriding the __doc__ attribute.
  538. def keys(self):
  539. """Return an iterable view which yields the string keys that would
  540. be represented by each :class:`.Row`.
  541. The keys can represent the labels of the columns returned by a core
  542. statement or the names of the orm classes returned by an orm
  543. execution.
  544. The view also can be tested for key containment using the Python
  545. ``in`` operator, which will test both for the string keys represented
  546. in the view, as well as for alternate keys such as column objects.
  547. .. versionchanged:: 1.4 a key view object is returned rather than a
  548. plain list.
  549. """
  550. return self._metadata.keys
  551. class Result(_WithKeys, ResultInternal):
  552. """Represent a set of database results.
  553. .. versionadded:: 1.4 The :class:`.Result` object provides a completely
  554. updated usage model and calling facade for SQLAlchemy Core and
  555. SQLAlchemy ORM. In Core, it forms the basis of the
  556. :class:`.CursorResult` object which replaces the previous
  557. :class:`.ResultProxy` interface. When using the ORM, a higher level
  558. object called :class:`.ChunkedIteratorResult` is normally used.
  559. .. note:: In SQLAlchemy 1.4 and above, this object is
  560. used for ORM results returned by :meth:`_orm.Session.execute`, which can
  561. yield instances of ORM mapped objects either individually or within
  562. tuple-like rows. Note that the :class:`_result.Result` object does not
  563. deduplicate instances or rows automatically as is the case with the
  564. legacy :class:`_orm.Query` object. For in-Python de-duplication of
  565. instances or rows, use the :meth:`_result.Result.unique` modifier
  566. method.
  567. .. seealso::
  568. :ref:`tutorial_fetching_rows` - in the :doc:`/tutorial/index`
  569. """
  570. _process_row = Row
  571. _row_logging_fn = None
  572. _source_supports_scalars = False
  573. _yield_per = None
  574. _attributes = util.immutabledict()
  575. def __init__(self, cursor_metadata):
  576. self._metadata = cursor_metadata
  577. def _soft_close(self, hard=False):
  578. raise NotImplementedError()
  579. def close(self):
  580. """close this :class:`_result.Result`.
  581. The behavior of this method is implementation specific, and is
  582. not implemented by default. The method should generally end
  583. the resources in use by the result object and also cause any
  584. subsequent iteration or row fetching to raise
  585. :class:`.ResourceClosedError`.
  586. .. versionadded:: 1.4.27 - ``.close()`` was previously not generally
  587. available for all :class:`_result.Result` classes, instead only
  588. being available on the :class:`_engine.CursorResult` returned for
  589. Core statement executions. As most other result objects, namely the
  590. ones used by the ORM, are proxying a :class:`_engine.CursorResult`
  591. in any case, this allows the underlying cursor result to be closed
  592. from the outside facade for the case when the ORM query is using
  593. the ``yield_per`` execution option where it does not immediately
  594. exhaust and autoclose the database cursor.
  595. """
  596. self._soft_close(hard=True)
  597. @_generative
  598. def yield_per(self, num):
  599. """Configure the row-fetching strategy to fetch num rows at a time.
  600. This impacts the underlying behavior of the result when iterating over
  601. the result object, or otherwise making use of methods such as
  602. :meth:`_engine.Result.fetchone` that return one row at a time. Data
  603. from the underlying cursor or other data source will be buffered up to
  604. this many rows in memory, and the buffered collection will then be
  605. yielded out one row at at time or as many rows are requested. Each time
  606. the buffer clears, it will be refreshed to this many rows or as many
  607. rows remain if fewer remain.
  608. The :meth:`_engine.Result.yield_per` method is generally used in
  609. conjunction with the
  610. :paramref:`_engine.Connection.execution_options.stream_results`
  611. execution option, which will allow the database dialect in use to make
  612. use of a server side cursor, if the DBAPI supports it.
  613. Most DBAPIs do not use server side cursors by default, which means all
  614. rows will be fetched upfront from the database regardless of the
  615. :meth:`_engine.Result.yield_per` setting. However,
  616. :meth:`_engine.Result.yield_per` may still be useful in that it batches
  617. the SQLAlchemy-side processing of the raw data from the database, and
  618. additionally when used for ORM scenarios will batch the conversion of
  619. database rows into ORM entity rows.
  620. .. versionadded:: 1.4
  621. :param num: number of rows to fetch each time the buffer is refilled.
  622. If set to a value below 1, fetches all rows for the next buffer.
  623. """
  624. self._yield_per = num
  625. @_generative
  626. def unique(self, strategy=None):
  627. """Apply unique filtering to the objects returned by this
  628. :class:`_engine.Result`.
  629. When this filter is applied with no arguments, the rows or objects
  630. returned will filtered such that each row is returned uniquely. The
  631. algorithm used to determine this uniqueness is by default the Python
  632. hashing identity of the whole tuple. In some cases a specialized
  633. per-entity hashing scheme may be used, such as when using the ORM, a
  634. scheme is applied which works against the primary key identity of
  635. returned objects.
  636. The unique filter is applied **after all other filters**, which means
  637. if the columns returned have been refined using a method such as the
  638. :meth:`_engine.Result.columns` or :meth:`_engine.Result.scalars`
  639. method, the uniquing is applied to **only the column or columns
  640. returned**. This occurs regardless of the order in which these
  641. methods have been called upon the :class:`_engine.Result` object.
  642. The unique filter also changes the calculus used for methods like
  643. :meth:`_engine.Result.fetchmany` and :meth:`_engine.Result.partitions`.
  644. When using :meth:`_engine.Result.unique`, these methods will continue
  645. to yield the number of rows or objects requested, after uniquing
  646. has been applied. However, this necessarily impacts the buffering
  647. behavior of the underlying cursor or datasource, such that multiple
  648. underlying calls to ``cursor.fetchmany()`` may be necessary in order
  649. to accumulate enough objects in order to provide a unique collection
  650. of the requested size.
  651. :param strategy: a callable that will be applied to rows or objects
  652. being iterated, which should return an object that represents the
  653. unique value of the row. A Python ``set()`` is used to store
  654. these identities. If not passed, a default uniqueness strategy
  655. is used which may have been assembled by the source of this
  656. :class:`_engine.Result` object.
  657. """
  658. self._unique_filter_state = (set(), strategy)
  659. def columns(self, *col_expressions):
  660. r"""Establish the columns that should be returned in each row.
  661. This method may be used to limit the columns returned as well
  662. as to reorder them. The given list of expressions are normally
  663. a series of integers or string key names. They may also be
  664. appropriate :class:`.ColumnElement` objects which correspond to
  665. a given statement construct.
  666. E.g.::
  667. statement = select(table.c.x, table.c.y, table.c.z)
  668. result = connection.execute(statement)
  669. for z, y in result.columns('z', 'y'):
  670. # ...
  671. Example of using the column objects from the statement itself::
  672. for z, y in result.columns(
  673. statement.selected_columns.c.z,
  674. statement.selected_columns.c.y
  675. ):
  676. # ...
  677. .. versionadded:: 1.4
  678. :param \*col_expressions: indicates columns to be returned. Elements
  679. may be integer row indexes, string column names, or appropriate
  680. :class:`.ColumnElement` objects corresponding to a select construct.
  681. :return: this :class:`_engine.Result` object with the modifications
  682. given.
  683. """
  684. return self._column_slices(col_expressions)
  685. def scalars(self, index=0):
  686. """Return a :class:`_result.ScalarResult` filtering object which
  687. will return single elements rather than :class:`_row.Row` objects.
  688. E.g.::
  689. >>> result = conn.execute(text("select int_id from table"))
  690. >>> result.scalars().all()
  691. [1, 2, 3]
  692. When results are fetched from the :class:`_result.ScalarResult`
  693. filtering object, the single column-row that would be returned by the
  694. :class:`_result.Result` is instead returned as the column's value.
  695. .. versionadded:: 1.4
  696. :param index: integer or row key indicating the column to be fetched
  697. from each row, defaults to ``0`` indicating the first column.
  698. :return: a new :class:`_result.ScalarResult` filtering object referring
  699. to this :class:`_result.Result` object.
  700. """
  701. return ScalarResult(self, index)
  702. def _getter(self, key, raiseerr=True):
  703. """return a callable that will retrieve the given key from a
  704. :class:`.Row`.
  705. """
  706. if self._source_supports_scalars:
  707. raise NotImplementedError(
  708. "can't use this function in 'only scalars' mode"
  709. )
  710. return self._metadata._getter(key, raiseerr)
  711. def _tuple_getter(self, keys):
  712. """return a callable that will retrieve the given keys from a
  713. :class:`.Row`.
  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. def mappings(self):
  721. """Apply a mappings filter to returned rows, returning an instance of
  722. :class:`_result.MappingResult`.
  723. When this filter is applied, fetching rows will return
  724. :class:`.RowMapping` objects instead of :class:`.Row` objects.
  725. .. versionadded:: 1.4
  726. :return: a new :class:`_result.MappingResult` filtering object
  727. referring to this :class:`_result.Result` object.
  728. """
  729. return MappingResult(self)
  730. def _raw_row_iterator(self):
  731. """Return a safe iterator that yields raw row data.
  732. This is used by the :meth:`._engine.Result.merge` method
  733. to merge multiple compatible results together.
  734. """
  735. raise NotImplementedError()
  736. def _fetchiter_impl(self):
  737. raise NotImplementedError()
  738. def _fetchone_impl(self, hard_close=False):
  739. raise NotImplementedError()
  740. def _fetchall_impl(self):
  741. raise NotImplementedError()
  742. def _fetchmany_impl(self, size=None):
  743. raise NotImplementedError()
  744. def __iter__(self):
  745. return self._iter_impl()
  746. def __next__(self):
  747. return self._next_impl()
  748. if py2k:
  749. def next(self): # noqa
  750. return self._next_impl()
  751. def partitions(self, size=None):
  752. """Iterate through sub-lists of rows of the size given.
  753. Each list will be of the size given, excluding the last list to
  754. be yielded, which may have a small number of rows. No empty
  755. lists will be yielded.
  756. The result object is automatically closed when the iterator
  757. is fully consumed.
  758. Note that the backend driver will usually buffer the entire result
  759. ahead of time unless the
  760. :paramref:`.Connection.execution_options.stream_results` execution
  761. option is used indicating that the driver should not pre-buffer
  762. results, if possible. Not all drivers support this option and
  763. the option is silently ignored for those who do not.
  764. .. versionadded:: 1.4
  765. :param size: indicate the maximum number of rows to be present
  766. in each list yielded. If None, makes use of the value set by
  767. :meth:`_engine.Result.yield_per`, if present, otherwise uses the
  768. :meth:`_engine.Result.fetchmany` default which may be backend
  769. specific.
  770. :return: iterator of lists
  771. """
  772. getter = self._manyrow_getter
  773. while True:
  774. partition = getter(self, size)
  775. if partition:
  776. yield partition
  777. else:
  778. break
  779. def fetchall(self):
  780. """A synonym for the :meth:`_engine.Result.all` method."""
  781. return self._allrows()
  782. def fetchone(self):
  783. """Fetch one row.
  784. When all rows are exhausted, returns None.
  785. This method is provided for backwards compatibility with
  786. SQLAlchemy 1.x.x.
  787. To fetch the first row of a result only, use the
  788. :meth:`_engine.Result.first` method. To iterate through all
  789. rows, iterate the :class:`_engine.Result` object directly.
  790. :return: a :class:`.Row` object if no filters are applied, or None
  791. if no rows remain.
  792. """
  793. row = self._onerow_getter(self)
  794. if row is _NO_ROW:
  795. return None
  796. else:
  797. return row
  798. def fetchmany(self, size=None):
  799. """Fetch many rows.
  800. When all rows are exhausted, returns an empty list.
  801. This method is provided for backwards compatibility with
  802. SQLAlchemy 1.x.x.
  803. To fetch rows in groups, use the :meth:`._result.Result.partitions`
  804. method.
  805. :return: a list of :class:`.Row` objects.
  806. """
  807. return self._manyrow_getter(self, size)
  808. def all(self):
  809. """Return all rows in a list.
  810. Closes the result set after invocation. Subsequent invocations
  811. will return an empty list.
  812. .. versionadded:: 1.4
  813. :return: a list of :class:`.Row` objects.
  814. """
  815. return self._allrows()
  816. def first(self):
  817. """Fetch the first row or None if no row is present.
  818. Closes the result set and discards remaining rows.
  819. .. note:: This method returns one **row**, e.g. tuple, by default.
  820. To return exactly one single scalar value, that is, the first
  821. column of the first row, use the :meth:`.Result.scalar` method,
  822. or combine :meth:`.Result.scalars` and :meth:`.Result.first`.
  823. Additionally, in contrast to the behavior of the legacy ORM
  824. :meth:`_orm.Query.first` method, **no limit is applied** to the
  825. SQL query which was invoked to produce this :class:`_engine.Result`;
  826. for a DBAPI driver that buffers results in memory before yielding
  827. rows, all rows will be sent to the Python process and all but
  828. the first row will be discarded.
  829. .. seealso::
  830. :ref:`migration_20_unify_select`
  831. :return: a :class:`.Row` object, or None
  832. if no rows remain.
  833. .. seealso::
  834. :meth:`_result.Result.scalar`
  835. :meth:`_result.Result.one`
  836. """
  837. return self._only_one_row(
  838. raise_for_second_row=False, raise_for_none=False, scalar=False
  839. )
  840. def one_or_none(self):
  841. """Return at most one result or raise an exception.
  842. Returns ``None`` if the result has no rows.
  843. Raises :class:`.MultipleResultsFound`
  844. if multiple rows are returned.
  845. .. versionadded:: 1.4
  846. :return: The first :class:`.Row` or None if no row is available.
  847. :raises: :class:`.MultipleResultsFound`
  848. .. seealso::
  849. :meth:`_result.Result.first`
  850. :meth:`_result.Result.one`
  851. """
  852. return self._only_one_row(
  853. raise_for_second_row=True, raise_for_none=False, scalar=False
  854. )
  855. def scalar_one(self):
  856. """Return exactly one scalar result or raise an exception.
  857. This is equivalent to calling :meth:`.Result.scalars` and then
  858. :meth:`.Result.one`.
  859. .. seealso::
  860. :meth:`.Result.one`
  861. :meth:`.Result.scalars`
  862. """
  863. return self._only_one_row(
  864. raise_for_second_row=True, raise_for_none=True, scalar=True
  865. )
  866. def scalar_one_or_none(self):
  867. """Return exactly one or no scalar result.
  868. This is equivalent to calling :meth:`.Result.scalars` and then
  869. :meth:`.Result.one_or_none`.
  870. .. seealso::
  871. :meth:`.Result.one_or_none`
  872. :meth:`.Result.scalars`
  873. """
  874. return self._only_one_row(
  875. raise_for_second_row=True, raise_for_none=False, scalar=True
  876. )
  877. def one(self):
  878. """Return exactly one row or raise an exception.
  879. Raises :class:`.NoResultFound` if the result returns no
  880. rows, or :class:`.MultipleResultsFound` if multiple rows
  881. would be returned.
  882. .. note:: This method returns one **row**, e.g. tuple, by default.
  883. To return exactly one single scalar value, that is, the first
  884. column of the first row, use the :meth:`.Result.scalar_one` method,
  885. or combine :meth:`.Result.scalars` and :meth:`.Result.one`.
  886. .. versionadded:: 1.4
  887. :return: The first :class:`.Row`.
  888. :raises: :class:`.MultipleResultsFound`, :class:`.NoResultFound`
  889. .. seealso::
  890. :meth:`_result.Result.first`
  891. :meth:`_result.Result.one_or_none`
  892. :meth:`_result.Result.scalar_one`
  893. """
  894. return self._only_one_row(
  895. raise_for_second_row=True, raise_for_none=True, scalar=False
  896. )
  897. def scalar(self):
  898. """Fetch the first column of the first row, and close the result set.
  899. Returns None if there are no rows to fetch.
  900. No validation is performed to test if additional rows remain.
  901. After calling this method, the object is fully closed,
  902. e.g. the :meth:`_engine.CursorResult.close`
  903. method will have been called.
  904. :return: a Python scalar value , or None if no rows remain.
  905. """
  906. return self._only_one_row(
  907. raise_for_second_row=False, raise_for_none=False, scalar=True
  908. )
  909. def freeze(self):
  910. """Return a callable object that will produce copies of this
  911. :class:`.Result` when invoked.
  912. The callable object returned is an instance of
  913. :class:`_engine.FrozenResult`.
  914. This is used for result set caching. The method must be called
  915. on the result when it has been unconsumed, and calling the method
  916. will consume the result fully. When the :class:`_engine.FrozenResult`
  917. is retrieved from a cache, it can be called any number of times where
  918. it will produce a new :class:`_engine.Result` object each time
  919. against its stored set of rows.
  920. .. seealso::
  921. :ref:`do_orm_execute_re_executing` - example usage within the
  922. ORM to implement a result-set cache.
  923. """
  924. return FrozenResult(self)
  925. def merge(self, *others):
  926. """Merge this :class:`.Result` with other compatible result
  927. objects.
  928. The object returned is an instance of :class:`_engine.MergedResult`,
  929. which will be composed of iterators from the given result
  930. objects.
  931. The new result will use the metadata from this result object.
  932. The subsequent result objects must be against an identical
  933. set of result / cursor metadata, otherwise the behavior is
  934. undefined.
  935. """
  936. return MergedResult(self._metadata, (self,) + others)
  937. class FilterResult(ResultInternal):
  938. """A wrapper for a :class:`_engine.Result` that returns objects other than
  939. :class:`_result.Row` objects, such as dictionaries or scalar objects.
  940. """
  941. _post_creational_filter = None
  942. def _soft_close(self, hard=False):
  943. self._real_result._soft_close(hard=hard)
  944. @property
  945. def _attributes(self):
  946. return self._real_result._attributes
  947. def _fetchiter_impl(self):
  948. return self._real_result._fetchiter_impl()
  949. def _fetchone_impl(self, hard_close=False):
  950. return self._real_result._fetchone_impl(hard_close=hard_close)
  951. def _fetchall_impl(self):
  952. return self._real_result._fetchall_impl()
  953. def _fetchmany_impl(self, size=None):
  954. return self._real_result._fetchmany_impl(size=size)
  955. class ScalarResult(FilterResult):
  956. """A wrapper for a :class:`_result.Result` that returns scalar values
  957. rather than :class:`_row.Row` values.
  958. The :class:`_result.ScalarResult` object is acquired by calling the
  959. :meth:`_result.Result.scalars` method.
  960. A special limitation of :class:`_result.ScalarResult` is that it has
  961. no ``fetchone()`` method; since the semantics of ``fetchone()`` are that
  962. the ``None`` value indicates no more results, this is not compatible
  963. with :class:`_result.ScalarResult` since there is no way to distinguish
  964. between ``None`` as a row value versus ``None`` as an indicator. Use
  965. ``next(result)`` to receive values individually.
  966. """
  967. _generate_rows = False
  968. def __init__(self, real_result, index):
  969. self._real_result = real_result
  970. if real_result._source_supports_scalars:
  971. self._metadata = real_result._metadata
  972. self._post_creational_filter = None
  973. else:
  974. self._metadata = real_result._metadata._reduce([index])
  975. self._post_creational_filter = operator.itemgetter(0)
  976. self._unique_filter_state = real_result._unique_filter_state
  977. def unique(self, strategy=None):
  978. """Apply unique filtering to the objects returned by this
  979. :class:`_engine.ScalarResult`.
  980. See :meth:`_engine.Result.unique` for usage details.
  981. """
  982. self._unique_filter_state = (set(), strategy)
  983. return self
  984. def partitions(self, size=None):
  985. """Iterate through sub-lists of elements of the size given.
  986. Equivalent to :meth:`_result.Result.partitions` except that
  987. scalar values, rather than :class:`_result.Row` objects,
  988. are returned.
  989. """
  990. getter = self._manyrow_getter
  991. while True:
  992. partition = getter(self, size)
  993. if partition:
  994. yield partition
  995. else:
  996. break
  997. def fetchall(self):
  998. """A synonym for the :meth:`_engine.ScalarResult.all` method."""
  999. return self._allrows()
  1000. def fetchmany(self, size=None):
  1001. """Fetch many objects.
  1002. Equivalent to :meth:`_result.Result.fetchmany` except that
  1003. scalar values, rather than :class:`_result.Row` objects,
  1004. are returned.
  1005. """
  1006. return self._manyrow_getter(self, size)
  1007. def all(self):
  1008. """Return all scalar values in a list.
  1009. Equivalent to :meth:`_result.Result.all` except that
  1010. scalar values, rather than :class:`_result.Row` objects,
  1011. are returned.
  1012. """
  1013. return self._allrows()
  1014. def __iter__(self):
  1015. return self._iter_impl()
  1016. def __next__(self):
  1017. return self._next_impl()
  1018. if py2k:
  1019. def next(self): # noqa
  1020. return self._next_impl()
  1021. def first(self):
  1022. """Fetch the first object or None if no object is present.
  1023. Equivalent to :meth:`_result.Result.first` except that
  1024. scalar values, rather than :class:`_result.Row` objects,
  1025. are returned.
  1026. """
  1027. return self._only_one_row(
  1028. raise_for_second_row=False, raise_for_none=False, scalar=False
  1029. )
  1030. def one_or_none(self):
  1031. """Return at most one object or raise an exception.
  1032. Equivalent to :meth:`_result.Result.one_or_none` except that
  1033. scalar values, rather than :class:`_result.Row` objects,
  1034. are returned.
  1035. """
  1036. return self._only_one_row(
  1037. raise_for_second_row=True, raise_for_none=False, scalar=False
  1038. )
  1039. def one(self):
  1040. """Return exactly one object or raise an exception.
  1041. Equivalent to :meth:`_result.Result.one` except that
  1042. scalar values, rather than :class:`_result.Row` objects,
  1043. are returned.
  1044. """
  1045. return self._only_one_row(
  1046. raise_for_second_row=True, raise_for_none=True, scalar=False
  1047. )
  1048. class MappingResult(_WithKeys, FilterResult):
  1049. """A wrapper for a :class:`_engine.Result` that returns dictionary values
  1050. rather than :class:`_engine.Row` values.
  1051. The :class:`_engine.MappingResult` object is acquired by calling the
  1052. :meth:`_engine.Result.mappings` method.
  1053. """
  1054. _generate_rows = True
  1055. _post_creational_filter = operator.attrgetter("_mapping")
  1056. def __init__(self, result):
  1057. self._real_result = result
  1058. self._unique_filter_state = result._unique_filter_state
  1059. self._metadata = result._metadata
  1060. if result._source_supports_scalars:
  1061. self._metadata = self._metadata._reduce([0])
  1062. def unique(self, strategy=None):
  1063. """Apply unique filtering to the objects returned by this
  1064. :class:`_engine.MappingResult`.
  1065. See :meth:`_engine.Result.unique` for usage details.
  1066. """
  1067. self._unique_filter_state = (set(), strategy)
  1068. return self
  1069. def columns(self, *col_expressions):
  1070. r"""Establish the columns that should be returned in each row."""
  1071. return self._column_slices(col_expressions)
  1072. def partitions(self, size=None):
  1073. """Iterate through sub-lists of elements of the size given.
  1074. Equivalent to :meth:`_result.Result.partitions` except that
  1075. mapping values, rather than :class:`_result.Row` objects,
  1076. are returned.
  1077. """
  1078. getter = self._manyrow_getter
  1079. while True:
  1080. partition = getter(self, size)
  1081. if partition:
  1082. yield partition
  1083. else:
  1084. break
  1085. def fetchall(self):
  1086. """A synonym for the :meth:`_engine.MappingResult.all` method."""
  1087. return self._allrows()
  1088. def fetchone(self):
  1089. """Fetch one object.
  1090. Equivalent to :meth:`_result.Result.fetchone` except that
  1091. mapping values, rather than :class:`_result.Row` objects,
  1092. are returned.
  1093. """
  1094. row = self._onerow_getter(self)
  1095. if row is _NO_ROW:
  1096. return None
  1097. else:
  1098. return row
  1099. def fetchmany(self, size=None):
  1100. """Fetch many objects.
  1101. Equivalent to :meth:`_result.Result.fetchmany` except that
  1102. mapping values, rather than :class:`_result.Row` objects,
  1103. are returned.
  1104. """
  1105. return self._manyrow_getter(self, size)
  1106. def all(self):
  1107. """Return all scalar values in a list.
  1108. Equivalent to :meth:`_result.Result.all` except that
  1109. mapping values, rather than :class:`_result.Row` objects,
  1110. are returned.
  1111. """
  1112. return self._allrows()
  1113. def __iter__(self):
  1114. return self._iter_impl()
  1115. def __next__(self):
  1116. return self._next_impl()
  1117. if py2k:
  1118. def next(self): # noqa
  1119. return self._next_impl()
  1120. def first(self):
  1121. """Fetch the first object or None if no object is present.
  1122. Equivalent to :meth:`_result.Result.first` except that
  1123. mapping values, rather than :class:`_result.Row` objects,
  1124. are returned.
  1125. """
  1126. return self._only_one_row(
  1127. raise_for_second_row=False, raise_for_none=False, scalar=False
  1128. )
  1129. def one_or_none(self):
  1130. """Return at most one object or raise an exception.
  1131. Equivalent to :meth:`_result.Result.one_or_none` except that
  1132. mapping values, rather than :class:`_result.Row` objects,
  1133. are returned.
  1134. """
  1135. return self._only_one_row(
  1136. raise_for_second_row=True, raise_for_none=False, scalar=False
  1137. )
  1138. def one(self):
  1139. """Return exactly one object or raise an exception.
  1140. Equivalent to :meth:`_result.Result.one` except that
  1141. mapping values, rather than :class:`_result.Row` objects,
  1142. are returned.
  1143. """
  1144. return self._only_one_row(
  1145. raise_for_second_row=True, raise_for_none=True, scalar=False
  1146. )
  1147. class FrozenResult(object):
  1148. """Represents a :class:`.Result` object in a "frozen" state suitable
  1149. for caching.
  1150. The :class:`_engine.FrozenResult` object is returned from the
  1151. :meth:`_engine.Result.freeze` method of any :class:`_engine.Result`
  1152. object.
  1153. A new iterable :class:`.Result` object is generated from a fixed
  1154. set of data each time the :class:`.FrozenResult` is invoked as
  1155. a callable::
  1156. result = connection.execute(query)
  1157. frozen = result.freeze()
  1158. unfrozen_result_one = frozen()
  1159. for row in unfrozen_result_one:
  1160. print(row)
  1161. unfrozen_result_two = frozen()
  1162. rows = unfrozen_result_two.all()
  1163. # ... etc
  1164. .. versionadded:: 1.4
  1165. .. seealso::
  1166. :ref:`do_orm_execute_re_executing` - example usage within the
  1167. ORM to implement a result-set cache.
  1168. :func:`_orm.loading.merge_frozen_result` - ORM function to merge
  1169. a frozen result back into a :class:`_orm.Session`.
  1170. """
  1171. def __init__(self, result):
  1172. self.metadata = result._metadata._for_freeze()
  1173. self._source_supports_scalars = result._source_supports_scalars
  1174. self._attributes = result._attributes
  1175. if self._source_supports_scalars:
  1176. self.data = list(result._raw_row_iterator())
  1177. else:
  1178. self.data = result.fetchall()
  1179. def rewrite_rows(self):
  1180. if self._source_supports_scalars:
  1181. return [[elem] for elem in self.data]
  1182. else:
  1183. return [list(row) for row in self.data]
  1184. def with_new_rows(self, tuple_data):
  1185. fr = FrozenResult.__new__(FrozenResult)
  1186. fr.metadata = self.metadata
  1187. fr._attributes = self._attributes
  1188. fr._source_supports_scalars = self._source_supports_scalars
  1189. if self._source_supports_scalars:
  1190. fr.data = [d[0] for d in tuple_data]
  1191. else:
  1192. fr.data = tuple_data
  1193. return fr
  1194. def __call__(self):
  1195. result = IteratorResult(self.metadata, iter(self.data))
  1196. result._attributes = self._attributes
  1197. result._source_supports_scalars = self._source_supports_scalars
  1198. return result
  1199. class IteratorResult(Result):
  1200. """A :class:`.Result` that gets data from a Python iterator of
  1201. :class:`.Row` objects.
  1202. .. versionadded:: 1.4
  1203. """
  1204. _hard_closed = False
  1205. def __init__(
  1206. self,
  1207. cursor_metadata,
  1208. iterator,
  1209. raw=None,
  1210. _source_supports_scalars=False,
  1211. ):
  1212. self._metadata = cursor_metadata
  1213. self.iterator = iterator
  1214. self.raw = raw
  1215. self._source_supports_scalars = _source_supports_scalars
  1216. def _soft_close(self, hard=False, **kw):
  1217. if hard:
  1218. self._hard_closed = True
  1219. if self.raw is not None:
  1220. self.raw._soft_close(hard=hard, **kw)
  1221. self.iterator = iter([])
  1222. self._reset_memoizations()
  1223. def _raise_hard_closed(self):
  1224. raise exc.ResourceClosedError("This result object is closed.")
  1225. def _raw_row_iterator(self):
  1226. return self.iterator
  1227. def _fetchiter_impl(self):
  1228. if self._hard_closed:
  1229. self._raise_hard_closed()
  1230. return self.iterator
  1231. def _fetchone_impl(self, hard_close=False):
  1232. if self._hard_closed:
  1233. self._raise_hard_closed()
  1234. row = next(self.iterator, _NO_ROW)
  1235. if row is _NO_ROW:
  1236. self._soft_close(hard=hard_close)
  1237. return None
  1238. else:
  1239. return row
  1240. def _fetchall_impl(self):
  1241. if self._hard_closed:
  1242. self._raise_hard_closed()
  1243. try:
  1244. return list(self.iterator)
  1245. finally:
  1246. self._soft_close()
  1247. def _fetchmany_impl(self, size=None):
  1248. if self._hard_closed:
  1249. self._raise_hard_closed()
  1250. return list(itertools.islice(self.iterator, 0, size))
  1251. def null_result():
  1252. return IteratorResult(SimpleResultMetaData([]), iter([]))
  1253. class ChunkedIteratorResult(IteratorResult):
  1254. """An :class:`.IteratorResult` that works from an iterator-producing callable.
  1255. The given ``chunks`` argument is a function that is given a number of rows
  1256. to return in each chunk, or ``None`` for all rows. The function should
  1257. then return an un-consumed iterator of lists, each list of the requested
  1258. size.
  1259. The function can be called at any time again, in which case it should
  1260. continue from the same result set but adjust the chunk size as given.
  1261. .. versionadded:: 1.4
  1262. """
  1263. def __init__(
  1264. self,
  1265. cursor_metadata,
  1266. chunks,
  1267. source_supports_scalars=False,
  1268. raw=None,
  1269. dynamic_yield_per=False,
  1270. ):
  1271. self._metadata = cursor_metadata
  1272. self.chunks = chunks
  1273. self._source_supports_scalars = source_supports_scalars
  1274. self.raw = raw
  1275. self.iterator = itertools.chain.from_iterable(self.chunks(None))
  1276. self.dynamic_yield_per = dynamic_yield_per
  1277. @_generative
  1278. def yield_per(self, num):
  1279. # TODO: this throws away the iterator which may be holding
  1280. # onto a chunk. the yield_per cannot be changed once any
  1281. # rows have been fetched. either find a way to enforce this,
  1282. # or we can't use itertools.chain and will instead have to
  1283. # keep track.
  1284. self._yield_per = num
  1285. self.iterator = itertools.chain.from_iterable(self.chunks(num))
  1286. def _soft_close(self, **kw):
  1287. super(ChunkedIteratorResult, self)._soft_close(**kw)
  1288. self.chunks = lambda size: []
  1289. def _fetchmany_impl(self, size=None):
  1290. if self.dynamic_yield_per:
  1291. self.iterator = itertools.chain.from_iterable(self.chunks(size))
  1292. return super(ChunkedIteratorResult, self)._fetchmany_impl(size=size)
  1293. class MergedResult(IteratorResult):
  1294. """A :class:`_engine.Result` that is merged from any number of
  1295. :class:`_engine.Result` objects.
  1296. Returned by the :meth:`_engine.Result.merge` method.
  1297. .. versionadded:: 1.4
  1298. """
  1299. closed = False
  1300. def __init__(self, cursor_metadata, results):
  1301. self._results = results
  1302. super(MergedResult, self).__init__(
  1303. cursor_metadata,
  1304. itertools.chain.from_iterable(
  1305. r._raw_row_iterator() for r in results
  1306. ),
  1307. )
  1308. self._unique_filter_state = results[0]._unique_filter_state
  1309. self._yield_per = results[0]._yield_per
  1310. # going to try something w/ this in next rev
  1311. self._source_supports_scalars = results[0]._source_supports_scalars
  1312. self._attributes = self._attributes.merge_with(
  1313. *[r._attributes for r in results]
  1314. )
  1315. def _soft_close(self, hard=False, **kw):
  1316. for r in self._results:
  1317. r._soft_close(hard=hard, **kw)
  1318. if hard:
  1319. self.closed = True