You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

640 lines
19KB

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