result.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. # ext/asyncio/result.py
  2. # Copyright (C) 2020-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. import operator
  8. from ...engine.result import _NO_ROW
  9. from ...engine.result import FilterResult
  10. from ...engine.result import FrozenResult
  11. from ...engine.result import MergedResult
  12. from ...util.concurrency import greenlet_spawn
  13. class AsyncCommon(FilterResult):
  14. async def close(self):
  15. """Close this result."""
  16. await greenlet_spawn(self._real_result.close)
  17. class AsyncResult(AsyncCommon):
  18. """An asyncio wrapper around a :class:`_result.Result` object.
  19. The :class:`_asyncio.AsyncResult` only applies to statement executions that
  20. use a server-side cursor. It is returned only from the
  21. :meth:`_asyncio.AsyncConnection.stream` and
  22. :meth:`_asyncio.AsyncSession.stream` methods.
  23. .. note:: As is the case with :class:`_engine.Result`, this object is
  24. used for ORM results returned by :meth:`_asyncio.AsyncSession.execute`,
  25. which can yield instances of ORM mapped objects either individually or
  26. within tuple-like rows. Note that these result objects do not
  27. deduplicate instances or rows automatically as is the case with the
  28. legacy :class:`_orm.Query` object. For in-Python de-duplication of
  29. instances or rows, use the :meth:`_asyncio.AsyncResult.unique` modifier
  30. method.
  31. .. versionadded:: 1.4
  32. """
  33. def __init__(self, real_result):
  34. self._real_result = real_result
  35. self._metadata = real_result._metadata
  36. self._unique_filter_state = real_result._unique_filter_state
  37. # BaseCursorResult pre-generates the "_row_getter". Use that
  38. # if available rather than building a second one
  39. if "_row_getter" in real_result.__dict__:
  40. self._set_memoized_attribute(
  41. "_row_getter", real_result.__dict__["_row_getter"]
  42. )
  43. def keys(self):
  44. """Return the :meth:`_engine.Result.keys` collection from the
  45. underlying :class:`_engine.Result`.
  46. """
  47. return self._metadata.keys
  48. def unique(self, strategy=None):
  49. """Apply unique filtering to the objects returned by this
  50. :class:`_asyncio.AsyncResult`.
  51. Refer to :meth:`_engine.Result.unique` in the synchronous
  52. SQLAlchemy API for a complete behavioral description.
  53. """
  54. self._unique_filter_state = (set(), strategy)
  55. return self
  56. def columns(self, *col_expressions):
  57. r"""Establish the columns that should be returned in each row.
  58. Refer to :meth:`_engine.Result.columns` in the synchronous
  59. SQLAlchemy API for a complete behavioral description.
  60. """
  61. return self._column_slices(col_expressions)
  62. async def partitions(self, size=None):
  63. """Iterate through sub-lists of rows of the size given.
  64. An async iterator is returned::
  65. async def scroll_results(connection):
  66. result = await connection.stream(select(users_table))
  67. async for partition in result.partitions(100):
  68. print("list of rows: %s" % partition)
  69. .. seealso::
  70. :meth:`_engine.Result.partitions`
  71. """
  72. getter = self._manyrow_getter
  73. while True:
  74. partition = await greenlet_spawn(getter, self, size)
  75. if partition:
  76. yield partition
  77. else:
  78. break
  79. async def fetchone(self):
  80. """Fetch one row.
  81. When all rows are exhausted, returns None.
  82. This method is provided for backwards compatibility with
  83. SQLAlchemy 1.x.x.
  84. To fetch the first row of a result only, use the
  85. :meth:`_engine.Result.first` method. To iterate through all
  86. rows, iterate the :class:`_engine.Result` object directly.
  87. :return: a :class:`.Row` object if no filters are applied, or None
  88. if no rows remain.
  89. """
  90. row = await greenlet_spawn(self._onerow_getter, self)
  91. if row is _NO_ROW:
  92. return None
  93. else:
  94. return row
  95. async def fetchmany(self, size=None):
  96. """Fetch many rows.
  97. When all rows are exhausted, returns an empty list.
  98. This method is provided for backwards compatibility with
  99. SQLAlchemy 1.x.x.
  100. To fetch rows in groups, use the
  101. :meth:`._asyncio.AsyncResult.partitions` method.
  102. :return: a list of :class:`.Row` objects.
  103. .. seealso::
  104. :meth:`_asyncio.AsyncResult.partitions`
  105. """
  106. return await greenlet_spawn(self._manyrow_getter, self, size)
  107. async def all(self):
  108. """Return all rows in a list.
  109. Closes the result set after invocation. Subsequent invocations
  110. will return an empty list.
  111. :return: a list of :class:`.Row` objects.
  112. """
  113. return await greenlet_spawn(self._allrows)
  114. def __aiter__(self):
  115. return self
  116. async def __anext__(self):
  117. row = await greenlet_spawn(self._onerow_getter, self)
  118. if row is _NO_ROW:
  119. raise StopAsyncIteration()
  120. else:
  121. return row
  122. async def first(self):
  123. """Fetch the first row or None if no row is present.
  124. Closes the result set and discards remaining rows.
  125. .. note:: This method returns one **row**, e.g. tuple, by default. To
  126. return exactly one single scalar value, that is, the first column of
  127. the first row, use the :meth:`_asyncio.AsyncResult.scalar` method,
  128. or combine :meth:`_asyncio.AsyncResult.scalars` and
  129. :meth:`_asyncio.AsyncResult.first`.
  130. :return: a :class:`.Row` object, or None
  131. if no rows remain.
  132. .. seealso::
  133. :meth:`_asyncio.AsyncResult.scalar`
  134. :meth:`_asyncio.AsyncResult.one`
  135. """
  136. return await greenlet_spawn(self._only_one_row, False, False, False)
  137. async def one_or_none(self):
  138. """Return at most one result or raise an exception.
  139. Returns ``None`` if the result has no rows.
  140. Raises :class:`.MultipleResultsFound`
  141. if multiple rows are returned.
  142. .. versionadded:: 1.4
  143. :return: The first :class:`.Row` or None if no row is available.
  144. :raises: :class:`.MultipleResultsFound`
  145. .. seealso::
  146. :meth:`_asyncio.AsyncResult.first`
  147. :meth:`_asyncio.AsyncResult.one`
  148. """
  149. return await greenlet_spawn(self._only_one_row, True, False, False)
  150. async def scalar_one(self):
  151. """Return exactly one scalar result or raise an exception.
  152. This is equivalent to calling :meth:`_asyncio.AsyncResult.scalars` and
  153. then :meth:`_asyncio.AsyncResult.one`.
  154. .. seealso::
  155. :meth:`_asyncio.AsyncResult.one`
  156. :meth:`_asyncio.AsyncResult.scalars`
  157. """
  158. return await greenlet_spawn(self._only_one_row, True, True, True)
  159. async def scalar_one_or_none(self):
  160. """Return exactly one or no scalar result.
  161. This is equivalent to calling :meth:`_asyncio.AsyncResult.scalars` and
  162. then :meth:`_asyncio.AsyncResult.one_or_none`.
  163. .. seealso::
  164. :meth:`_asyncio.AsyncResult.one_or_none`
  165. :meth:`_asyncio.AsyncResult.scalars`
  166. """
  167. return await greenlet_spawn(self._only_one_row, True, False, True)
  168. async def one(self):
  169. """Return exactly one row or raise an exception.
  170. Raises :class:`.NoResultFound` if the result returns no
  171. rows, or :class:`.MultipleResultsFound` if multiple rows
  172. would be returned.
  173. .. note:: This method returns one **row**, e.g. tuple, by default.
  174. To return exactly one single scalar value, that is, the first
  175. column of the first row, use the
  176. :meth:`_asyncio.AsyncResult.scalar_one` method, or combine
  177. :meth:`_asyncio.AsyncResult.scalars` and
  178. :meth:`_asyncio.AsyncResult.one`.
  179. .. versionadded:: 1.4
  180. :return: The first :class:`.Row`.
  181. :raises: :class:`.MultipleResultsFound`, :class:`.NoResultFound`
  182. .. seealso::
  183. :meth:`_asyncio.AsyncResult.first`
  184. :meth:`_asyncio.AsyncResult.one_or_none`
  185. :meth:`_asyncio.AsyncResult.scalar_one`
  186. """
  187. return await greenlet_spawn(self._only_one_row, True, True, False)
  188. async def scalar(self):
  189. """Fetch the first column of the first row, and close the result set.
  190. Returns None if there are no rows to fetch.
  191. No validation is performed to test if additional rows remain.
  192. After calling this method, the object is fully closed,
  193. e.g. the :meth:`_engine.CursorResult.close`
  194. method will have been called.
  195. :return: a Python scalar value , or None if no rows remain.
  196. """
  197. return await greenlet_spawn(self._only_one_row, False, False, True)
  198. async def freeze(self):
  199. """Return a callable object that will produce copies of this
  200. :class:`_asyncio.AsyncResult` when invoked.
  201. The callable object returned is an instance of
  202. :class:`_engine.FrozenResult`.
  203. This is used for result set caching. The method must be called
  204. on the result when it has been unconsumed, and calling the method
  205. will consume the result fully. When the :class:`_engine.FrozenResult`
  206. is retrieved from a cache, it can be called any number of times where
  207. it will produce a new :class:`_engine.Result` object each time
  208. against its stored set of rows.
  209. .. seealso::
  210. :ref:`do_orm_execute_re_executing` - example usage within the
  211. ORM to implement a result-set cache.
  212. """
  213. return await greenlet_spawn(FrozenResult, self)
  214. def merge(self, *others):
  215. """Merge this :class:`_asyncio.AsyncResult` with other compatible result
  216. objects.
  217. The object returned is an instance of :class:`_engine.MergedResult`,
  218. which will be composed of iterators from the given result
  219. objects.
  220. The new result will use the metadata from this result object.
  221. The subsequent result objects must be against an identical
  222. set of result / cursor metadata, otherwise the behavior is
  223. undefined.
  224. """
  225. return MergedResult(self._metadata, (self,) + others)
  226. def scalars(self, index=0):
  227. """Return an :class:`_asyncio.AsyncScalarResult` filtering object which
  228. will return single elements rather than :class:`_row.Row` objects.
  229. Refer to :meth:`_result.Result.scalars` in the synchronous
  230. SQLAlchemy API for a complete behavioral description.
  231. :param index: integer or row key indicating the column to be fetched
  232. from each row, defaults to ``0`` indicating the first column.
  233. :return: a new :class:`_asyncio.AsyncScalarResult` filtering object
  234. referring to this :class:`_asyncio.AsyncResult` object.
  235. """
  236. return AsyncScalarResult(self._real_result, index)
  237. def mappings(self):
  238. """Apply a mappings filter to returned rows, returning an instance of
  239. :class:`_asyncio.AsyncMappingResult`.
  240. When this filter is applied, fetching rows will return
  241. :class:`.RowMapping` objects instead of :class:`.Row` objects.
  242. Refer to :meth:`_result.Result.mappings` in the synchronous
  243. SQLAlchemy API for a complete behavioral description.
  244. :return: a new :class:`_asyncio.AsyncMappingResult` filtering object
  245. referring to the underlying :class:`_result.Result` object.
  246. """
  247. return AsyncMappingResult(self._real_result)
  248. class AsyncScalarResult(AsyncCommon):
  249. """A wrapper for a :class:`_asyncio.AsyncResult` that returns scalar values
  250. rather than :class:`_row.Row` values.
  251. The :class:`_asyncio.AsyncScalarResult` object is acquired by calling the
  252. :meth:`_asyncio.AsyncResult.scalars` method.
  253. Refer to the :class:`_result.ScalarResult` object in the synchronous
  254. SQLAlchemy API for a complete behavioral description.
  255. .. versionadded:: 1.4
  256. """
  257. _generate_rows = False
  258. def __init__(self, real_result, index):
  259. self._real_result = real_result
  260. if real_result._source_supports_scalars:
  261. self._metadata = real_result._metadata
  262. self._post_creational_filter = None
  263. else:
  264. self._metadata = real_result._metadata._reduce([index])
  265. self._post_creational_filter = operator.itemgetter(0)
  266. self._unique_filter_state = real_result._unique_filter_state
  267. def unique(self, strategy=None):
  268. """Apply unique filtering to the objects returned by this
  269. :class:`_asyncio.AsyncScalarResult`.
  270. See :meth:`_asyncio.AsyncResult.unique` for usage details.
  271. """
  272. self._unique_filter_state = (set(), strategy)
  273. return self
  274. async def partitions(self, size=None):
  275. """Iterate through sub-lists of elements of the size given.
  276. Equivalent to :meth:`_asyncio.AsyncResult.partitions` except that
  277. scalar values, rather than :class:`_result.Row` objects,
  278. are returned.
  279. """
  280. getter = self._manyrow_getter
  281. while True:
  282. partition = await greenlet_spawn(getter, self, size)
  283. if partition:
  284. yield partition
  285. else:
  286. break
  287. async def fetchall(self):
  288. """A synonym for the :meth:`_asyncio.AsyncScalarResult.all` method."""
  289. return await greenlet_spawn(self._allrows)
  290. async def fetchmany(self, size=None):
  291. """Fetch many objects.
  292. Equivalent to :meth:`_asyncio.AsyncResult.fetchmany` except that
  293. scalar values, rather than :class:`_result.Row` objects,
  294. are returned.
  295. """
  296. return await greenlet_spawn(self._manyrow_getter, self, size)
  297. async def all(self):
  298. """Return all scalar values in a list.
  299. Equivalent to :meth:`_asyncio.AsyncResult.all` except that
  300. scalar values, rather than :class:`_result.Row` objects,
  301. are returned.
  302. """
  303. return await greenlet_spawn(self._allrows)
  304. def __aiter__(self):
  305. return self
  306. async def __anext__(self):
  307. row = await greenlet_spawn(self._onerow_getter, self)
  308. if row is _NO_ROW:
  309. raise StopAsyncIteration()
  310. else:
  311. return row
  312. async def first(self):
  313. """Fetch the first object or None if no object is present.
  314. Equivalent to :meth:`_asyncio.AsyncResult.first` except that
  315. scalar values, rather than :class:`_result.Row` objects,
  316. are returned.
  317. """
  318. return await greenlet_spawn(self._only_one_row, False, False, False)
  319. async def one_or_none(self):
  320. """Return at most one object or raise an exception.
  321. Equivalent to :meth:`_asyncio.AsyncResult.one_or_none` except that
  322. scalar values, rather than :class:`_result.Row` objects,
  323. are returned.
  324. """
  325. return await greenlet_spawn(self._only_one_row, True, False, False)
  326. async def one(self):
  327. """Return exactly one object or raise an exception.
  328. Equivalent to :meth:`_asyncio.AsyncResult.one` except that
  329. scalar values, rather than :class:`_result.Row` objects,
  330. are returned.
  331. """
  332. return await greenlet_spawn(self._only_one_row, True, True, False)
  333. class AsyncMappingResult(AsyncCommon):
  334. """A wrapper for a :class:`_asyncio.AsyncResult` that returns dictionary values
  335. rather than :class:`_engine.Row` values.
  336. The :class:`_asyncio.AsyncMappingResult` object is acquired by calling the
  337. :meth:`_asyncio.AsyncResult.mappings` method.
  338. Refer to the :class:`_result.MappingResult` object in the synchronous
  339. SQLAlchemy API for a complete behavioral description.
  340. .. versionadded:: 1.4
  341. """
  342. _generate_rows = True
  343. _post_creational_filter = operator.attrgetter("_mapping")
  344. def __init__(self, result):
  345. self._real_result = result
  346. self._unique_filter_state = result._unique_filter_state
  347. self._metadata = result._metadata
  348. if result._source_supports_scalars:
  349. self._metadata = self._metadata._reduce([0])
  350. def keys(self):
  351. """Return an iterable view which yields the string keys that would
  352. be represented by each :class:`.Row`.
  353. The view also can be tested for key containment using the Python
  354. ``in`` operator, which will test both for the string keys represented
  355. in the view, as well as for alternate keys such as column objects.
  356. .. versionchanged:: 1.4 a key view object is returned rather than a
  357. plain list.
  358. """
  359. return self._metadata.keys
  360. def unique(self, strategy=None):
  361. """Apply unique filtering to the objects returned by this
  362. :class:`_asyncio.AsyncMappingResult`.
  363. See :meth:`_asyncio.AsyncResult.unique` for usage details.
  364. """
  365. self._unique_filter_state = (set(), strategy)
  366. return self
  367. def columns(self, *col_expressions):
  368. r"""Establish the columns that should be returned in each row."""
  369. return self._column_slices(col_expressions)
  370. async def partitions(self, size=None):
  371. """Iterate through sub-lists of elements of the size given.
  372. Equivalent to :meth:`_asyncio.AsyncResult.partitions` except that
  373. mapping values, rather than :class:`_result.Row` objects,
  374. are returned.
  375. """
  376. getter = self._manyrow_getter
  377. while True:
  378. partition = await greenlet_spawn(getter, self, size)
  379. if partition:
  380. yield partition
  381. else:
  382. break
  383. async def fetchall(self):
  384. """A synonym for the :meth:`_asyncio.AsyncMappingResult.all` method."""
  385. return await greenlet_spawn(self._allrows)
  386. async def fetchone(self):
  387. """Fetch one object.
  388. Equivalent to :meth:`_asyncio.AsyncResult.fetchone` except that
  389. mapping values, rather than :class:`_result.Row` objects,
  390. are returned.
  391. """
  392. row = await greenlet_spawn(self._onerow_getter, self)
  393. if row is _NO_ROW:
  394. return None
  395. else:
  396. return row
  397. async def fetchmany(self, size=None):
  398. """Fetch many objects.
  399. Equivalent to :meth:`_asyncio.AsyncResult.fetchmany` except that
  400. mapping values, rather than :class:`_result.Row` objects,
  401. are returned.
  402. """
  403. return await greenlet_spawn(self._manyrow_getter, self, size)
  404. async def all(self):
  405. """Return all scalar values in a list.
  406. Equivalent to :meth:`_asyncio.AsyncResult.all` except that
  407. mapping values, rather than :class:`_result.Row` objects,
  408. are returned.
  409. """
  410. return await greenlet_spawn(self._allrows)
  411. def __aiter__(self):
  412. return self
  413. async def __anext__(self):
  414. row = await greenlet_spawn(self._onerow_getter, self)
  415. if row is _NO_ROW:
  416. raise StopAsyncIteration()
  417. else:
  418. return row
  419. async def first(self):
  420. """Fetch the first object or None if no object is present.
  421. Equivalent to :meth:`_asyncio.AsyncResult.first` except that
  422. mapping values, rather than :class:`_result.Row` objects,
  423. are returned.
  424. """
  425. return await greenlet_spawn(self._only_one_row, False, False, False)
  426. async def one_or_none(self):
  427. """Return at most one object or raise an exception.
  428. Equivalent to :meth:`_asyncio.AsyncResult.one_or_none` except that
  429. mapping values, rather than :class:`_result.Row` objects,
  430. are returned.
  431. """
  432. return await greenlet_spawn(self._only_one_row, True, False, False)
  433. async def one(self):
  434. """Return exactly one object or raise an exception.
  435. Equivalent to :meth:`_asyncio.AsyncResult.one` except that
  436. mapping values, rather than :class:`_result.Row` objects,
  437. are returned.
  438. """
  439. return await greenlet_spawn(self._only_one_row, True, True, False)