cursor.py 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. # engine/cursor.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """Define cursor-specific result set constructs including
  9. :class:`.CursorResult`."""
  10. from __future__ import annotations
  11. import collections
  12. import functools
  13. import operator
  14. import typing
  15. from typing import Any
  16. from typing import cast
  17. from typing import ClassVar
  18. from typing import Dict
  19. from typing import Iterable
  20. from typing import Iterator
  21. from typing import List
  22. from typing import Mapping
  23. from typing import NoReturn
  24. from typing import Optional
  25. from typing import Sequence
  26. from typing import Tuple
  27. from typing import TYPE_CHECKING
  28. from typing import TypeVar
  29. from typing import Union
  30. from .result import IteratorResult
  31. from .result import MergedResult
  32. from .result import Result
  33. from .result import ResultMetaData
  34. from .result import SimpleResultMetaData
  35. from .result import tuplegetter
  36. from .row import Row
  37. from .. import exc
  38. from .. import util
  39. from ..sql import elements
  40. from ..sql import sqltypes
  41. from ..sql import util as sql_util
  42. from ..sql.base import _generative
  43. from ..sql.compiler import ResultColumnsEntry
  44. from ..sql.compiler import RM_NAME
  45. from ..sql.compiler import RM_OBJECTS
  46. from ..sql.compiler import RM_RENDERED_NAME
  47. from ..sql.compiler import RM_TYPE
  48. from ..sql.type_api import TypeEngine
  49. from ..util import compat
  50. from ..util.typing import Literal
  51. from ..util.typing import Self
  52. if typing.TYPE_CHECKING:
  53. from .base import Connection
  54. from .default import DefaultExecutionContext
  55. from .interfaces import _DBAPICursorDescription
  56. from .interfaces import DBAPICursor
  57. from .interfaces import Dialect
  58. from .interfaces import ExecutionContext
  59. from .result import _KeyIndexType
  60. from .result import _KeyMapRecType
  61. from .result import _KeyMapType
  62. from .result import _KeyType
  63. from .result import _ProcessorsType
  64. from .result import _TupleGetterType
  65. from ..sql.type_api import _ResultProcessorType
  66. _T = TypeVar("_T", bound=Any)
  67. # metadata entry tuple indexes.
  68. # using raw tuple is faster than namedtuple.
  69. # these match up to the positions in
  70. # _CursorKeyMapRecType
  71. MD_INDEX: Literal[0] = 0
  72. """integer index in cursor.description
  73. """
  74. MD_RESULT_MAP_INDEX: Literal[1] = 1
  75. """integer index in compiled._result_columns"""
  76. MD_OBJECTS: Literal[2] = 2
  77. """other string keys and ColumnElement obj that can match.
  78. This comes from compiler.RM_OBJECTS / compiler.ResultColumnsEntry.objects
  79. """
  80. MD_LOOKUP_KEY: Literal[3] = 3
  81. """string key we usually expect for key-based lookup
  82. this comes from compiler.RM_NAME / compiler.ResultColumnsEntry.name
  83. """
  84. MD_RENDERED_NAME: Literal[4] = 4
  85. """name that is usually in cursor.description
  86. this comes from compiler.RENDERED_NAME / compiler.ResultColumnsEntry.keyname
  87. """
  88. MD_PROCESSOR: Literal[5] = 5
  89. """callable to process a result value into a row"""
  90. MD_UNTRANSLATED: Literal[6] = 6
  91. """raw name from cursor.description"""
  92. _CursorKeyMapRecType = Tuple[
  93. Optional[int], # MD_INDEX, None means the record is ambiguously named
  94. int, # MD_RESULT_MAP_INDEX
  95. List[Any], # MD_OBJECTS
  96. str, # MD_LOOKUP_KEY
  97. str, # MD_RENDERED_NAME
  98. Optional["_ResultProcessorType[Any]"], # MD_PROCESSOR
  99. Optional[str], # MD_UNTRANSLATED
  100. ]
  101. _CursorKeyMapType = Mapping["_KeyType", _CursorKeyMapRecType]
  102. # same as _CursorKeyMapRecType except the MD_INDEX value is definitely
  103. # not None
  104. _NonAmbigCursorKeyMapRecType = Tuple[
  105. int,
  106. int,
  107. List[Any],
  108. str,
  109. str,
  110. Optional["_ResultProcessorType[Any]"],
  111. str,
  112. ]
  113. class CursorResultMetaData(ResultMetaData):
  114. """Result metadata for DBAPI cursors."""
  115. __slots__ = (
  116. "_keymap",
  117. "_processors",
  118. "_keys",
  119. "_keymap_by_result_column_idx",
  120. "_tuplefilter",
  121. "_translated_indexes",
  122. "_safe_for_cache",
  123. "_unpickled",
  124. "_key_to_index",
  125. # don't need _unique_filters support here for now. Can be added
  126. # if a need arises.
  127. )
  128. _keymap: _CursorKeyMapType
  129. _processors: _ProcessorsType
  130. _keymap_by_result_column_idx: Optional[Dict[int, _KeyMapRecType]]
  131. _unpickled: bool
  132. _safe_for_cache: bool
  133. _translated_indexes: Optional[List[int]]
  134. returns_rows: ClassVar[bool] = True
  135. def _has_key(self, key: Any) -> bool:
  136. return key in self._keymap
  137. def _for_freeze(self) -> ResultMetaData:
  138. return SimpleResultMetaData(
  139. self._keys,
  140. extra=[self._keymap[key][MD_OBJECTS] for key in self._keys],
  141. )
  142. def _make_new_metadata(
  143. self,
  144. *,
  145. unpickled: bool,
  146. processors: _ProcessorsType,
  147. keys: Sequence[str],
  148. keymap: _KeyMapType,
  149. tuplefilter: Optional[_TupleGetterType],
  150. translated_indexes: Optional[List[int]],
  151. safe_for_cache: bool,
  152. keymap_by_result_column_idx: Any,
  153. ) -> CursorResultMetaData:
  154. new_obj = self.__class__.__new__(self.__class__)
  155. new_obj._unpickled = unpickled
  156. new_obj._processors = processors
  157. new_obj._keys = keys
  158. new_obj._keymap = keymap
  159. new_obj._tuplefilter = tuplefilter
  160. new_obj._translated_indexes = translated_indexes
  161. new_obj._safe_for_cache = safe_for_cache
  162. new_obj._keymap_by_result_column_idx = keymap_by_result_column_idx
  163. new_obj._key_to_index = self._make_key_to_index(keymap, MD_INDEX)
  164. return new_obj
  165. def _remove_processors(self) -> CursorResultMetaData:
  166. assert not self._tuplefilter
  167. return self._make_new_metadata(
  168. unpickled=self._unpickled,
  169. processors=[None] * len(self._processors),
  170. tuplefilter=None,
  171. translated_indexes=None,
  172. keymap={
  173. key: value[0:5] + (None,) + value[6:]
  174. for key, value in self._keymap.items()
  175. },
  176. keys=self._keys,
  177. safe_for_cache=self._safe_for_cache,
  178. keymap_by_result_column_idx=self._keymap_by_result_column_idx,
  179. )
  180. def _splice_horizontally(
  181. self, other: CursorResultMetaData
  182. ) -> CursorResultMetaData:
  183. assert not self._tuplefilter
  184. keymap = dict(self._keymap)
  185. offset = len(self._keys)
  186. keymap.update(
  187. {
  188. key: (
  189. # int index should be None for ambiguous key
  190. (
  191. value[0] + offset
  192. if value[0] is not None and key not in keymap
  193. else None
  194. ),
  195. value[1] + offset,
  196. *value[2:],
  197. )
  198. for key, value in other._keymap.items()
  199. }
  200. )
  201. return self._make_new_metadata(
  202. unpickled=self._unpickled,
  203. processors=self._processors + other._processors, # type: ignore
  204. tuplefilter=None,
  205. translated_indexes=None,
  206. keys=self._keys + other._keys, # type: ignore
  207. keymap=keymap,
  208. safe_for_cache=self._safe_for_cache,
  209. keymap_by_result_column_idx={
  210. metadata_entry[MD_RESULT_MAP_INDEX]: metadata_entry
  211. for metadata_entry in keymap.values()
  212. },
  213. )
  214. def _reduce(self, keys: Sequence[_KeyIndexType]) -> ResultMetaData:
  215. recs = list(self._metadata_for_keys(keys))
  216. indexes = [rec[MD_INDEX] for rec in recs]
  217. new_keys: List[str] = [rec[MD_LOOKUP_KEY] for rec in recs]
  218. if self._translated_indexes:
  219. indexes = [self._translated_indexes[idx] for idx in indexes]
  220. tup = tuplegetter(*indexes)
  221. new_recs = [(index,) + rec[1:] for index, rec in enumerate(recs)]
  222. keymap = {rec[MD_LOOKUP_KEY]: rec for rec in new_recs}
  223. # TODO: need unit test for:
  224. # result = connection.execute("raw sql, no columns").scalars()
  225. # without the "or ()" it's failing because MD_OBJECTS is None
  226. keymap.update(
  227. (e, new_rec)
  228. for new_rec in new_recs
  229. for e in new_rec[MD_OBJECTS] or ()
  230. )
  231. return self._make_new_metadata(
  232. unpickled=self._unpickled,
  233. processors=self._processors,
  234. keys=new_keys,
  235. tuplefilter=tup,
  236. translated_indexes=indexes,
  237. keymap=keymap, # type: ignore[arg-type]
  238. safe_for_cache=self._safe_for_cache,
  239. keymap_by_result_column_idx=self._keymap_by_result_column_idx,
  240. )
  241. def _adapt_to_context(self, context: ExecutionContext) -> ResultMetaData:
  242. """When using a cached Compiled construct that has a _result_map,
  243. for a new statement that used the cached Compiled, we need to ensure
  244. the keymap has the Column objects from our new statement as keys.
  245. So here we rewrite keymap with new entries for the new columns
  246. as matched to those of the cached statement.
  247. """
  248. if not context.compiled or not context.compiled._result_columns:
  249. return self
  250. compiled_statement = context.compiled.statement
  251. invoked_statement = context.invoked_statement
  252. if TYPE_CHECKING:
  253. assert isinstance(invoked_statement, elements.ClauseElement)
  254. if compiled_statement is invoked_statement:
  255. return self
  256. assert invoked_statement is not None
  257. # this is the most common path for Core statements when
  258. # caching is used. In ORM use, this codepath is not really used
  259. # as the _result_disable_adapt_to_context execution option is
  260. # set by the ORM.
  261. # make a copy and add the columns from the invoked statement
  262. # to the result map.
  263. keymap_by_position = self._keymap_by_result_column_idx
  264. if keymap_by_position is None:
  265. # first retrival from cache, this map will not be set up yet,
  266. # initialize lazily
  267. keymap_by_position = self._keymap_by_result_column_idx = {
  268. metadata_entry[MD_RESULT_MAP_INDEX]: metadata_entry
  269. for metadata_entry in self._keymap.values()
  270. }
  271. assert not self._tuplefilter
  272. return self._make_new_metadata(
  273. keymap=compat.dict_union(
  274. self._keymap,
  275. {
  276. new: keymap_by_position[idx]
  277. for idx, new in enumerate(
  278. invoked_statement._all_selected_columns
  279. )
  280. if idx in keymap_by_position
  281. },
  282. ),
  283. unpickled=self._unpickled,
  284. processors=self._processors,
  285. tuplefilter=None,
  286. translated_indexes=None,
  287. keys=self._keys,
  288. safe_for_cache=self._safe_for_cache,
  289. keymap_by_result_column_idx=self._keymap_by_result_column_idx,
  290. )
  291. def __init__(
  292. self,
  293. parent: CursorResult[Any],
  294. cursor_description: _DBAPICursorDescription,
  295. ):
  296. context = parent.context
  297. self._tuplefilter = None
  298. self._translated_indexes = None
  299. self._safe_for_cache = self._unpickled = False
  300. if context.result_column_struct:
  301. (
  302. result_columns,
  303. cols_are_ordered,
  304. textual_ordered,
  305. ad_hoc_textual,
  306. loose_column_name_matching,
  307. ) = context.result_column_struct
  308. num_ctx_cols = len(result_columns)
  309. else:
  310. result_columns = cols_are_ordered = ( # type: ignore
  311. num_ctx_cols
  312. ) = ad_hoc_textual = loose_column_name_matching = (
  313. textual_ordered
  314. ) = False
  315. # merge cursor.description with the column info
  316. # present in the compiled structure, if any
  317. raw = self._merge_cursor_description(
  318. context,
  319. cursor_description,
  320. result_columns,
  321. num_ctx_cols,
  322. cols_are_ordered,
  323. textual_ordered,
  324. ad_hoc_textual,
  325. loose_column_name_matching,
  326. )
  327. # processors in key order which are used when building up
  328. # a row
  329. self._processors = [
  330. metadata_entry[MD_PROCESSOR] for metadata_entry in raw
  331. ]
  332. # this is used when using this ResultMetaData in a Core-only cache
  333. # retrieval context. it's initialized on first cache retrieval
  334. # when the _result_disable_adapt_to_context execution option
  335. # (which the ORM generally sets) is not set.
  336. self._keymap_by_result_column_idx = None
  337. # for compiled SQL constructs, copy additional lookup keys into
  338. # the key lookup map, such as Column objects, labels,
  339. # column keys and other names
  340. if num_ctx_cols:
  341. # keymap by primary string...
  342. by_key = {
  343. metadata_entry[MD_LOOKUP_KEY]: metadata_entry
  344. for metadata_entry in raw
  345. }
  346. if len(by_key) != num_ctx_cols:
  347. # if by-primary-string dictionary smaller than
  348. # number of columns, assume we have dupes; (this check
  349. # is also in place if string dictionary is bigger, as
  350. # can occur when '*' was used as one of the compiled columns,
  351. # which may or may not be suggestive of dupes), rewrite
  352. # dupe records with "None" for index which results in
  353. # ambiguous column exception when accessed.
  354. #
  355. # this is considered to be the less common case as it is not
  356. # common to have dupe column keys in a SELECT statement.
  357. #
  358. # new in 1.4: get the complete set of all possible keys,
  359. # strings, objects, whatever, that are dupes across two
  360. # different records, first.
  361. index_by_key: Dict[Any, Any] = {}
  362. dupes = set()
  363. for metadata_entry in raw:
  364. for key in (metadata_entry[MD_RENDERED_NAME],) + (
  365. metadata_entry[MD_OBJECTS] or ()
  366. ):
  367. idx = metadata_entry[MD_INDEX]
  368. # if this key has been associated with more than one
  369. # positional index, it's a dupe
  370. if index_by_key.setdefault(key, idx) != idx:
  371. dupes.add(key)
  372. # then put everything we have into the keymap excluding only
  373. # those keys that are dupes.
  374. self._keymap = {
  375. obj_elem: metadata_entry
  376. for metadata_entry in raw
  377. if metadata_entry[MD_OBJECTS]
  378. for obj_elem in metadata_entry[MD_OBJECTS]
  379. if obj_elem not in dupes
  380. }
  381. # then for the dupe keys, put the "ambiguous column"
  382. # record into by_key.
  383. by_key.update(
  384. {
  385. key: (None, None, [], key, key, None, None)
  386. for key in dupes
  387. }
  388. )
  389. else:
  390. # no dupes - copy secondary elements from compiled
  391. # columns into self._keymap. this is the most common
  392. # codepath for Core / ORM statement executions before the
  393. # result metadata is cached
  394. self._keymap = {
  395. obj_elem: metadata_entry
  396. for metadata_entry in raw
  397. if metadata_entry[MD_OBJECTS]
  398. for obj_elem in metadata_entry[MD_OBJECTS]
  399. }
  400. # update keymap with primary string names taking
  401. # precedence
  402. self._keymap.update(by_key)
  403. else:
  404. # no compiled objects to map, just create keymap by primary string
  405. self._keymap = {
  406. metadata_entry[MD_LOOKUP_KEY]: metadata_entry
  407. for metadata_entry in raw
  408. }
  409. # update keymap with "translated" names. In SQLAlchemy this is a
  410. # sqlite only thing, and in fact impacting only extremely old SQLite
  411. # versions unlikely to be present in modern Python versions.
  412. # however, the pyhive third party dialect is
  413. # also using this hook, which means others still might use it as well.
  414. # I dislike having this awkward hook here but as long as we need
  415. # to use names in cursor.description in some cases we need to have
  416. # some hook to accomplish this.
  417. if not num_ctx_cols and context._translate_colname:
  418. self._keymap.update(
  419. {
  420. metadata_entry[MD_UNTRANSLATED]: self._keymap[
  421. metadata_entry[MD_LOOKUP_KEY]
  422. ]
  423. for metadata_entry in raw
  424. if metadata_entry[MD_UNTRANSLATED]
  425. }
  426. )
  427. self._key_to_index = self._make_key_to_index(self._keymap, MD_INDEX)
  428. def _merge_cursor_description(
  429. self,
  430. context,
  431. cursor_description,
  432. result_columns,
  433. num_ctx_cols,
  434. cols_are_ordered,
  435. textual_ordered,
  436. ad_hoc_textual,
  437. loose_column_name_matching,
  438. ):
  439. """Merge a cursor.description with compiled result column information.
  440. There are at least four separate strategies used here, selected
  441. depending on the type of SQL construct used to start with.
  442. The most common case is that of the compiled SQL expression construct,
  443. which generated the column names present in the raw SQL string and
  444. which has the identical number of columns as were reported by
  445. cursor.description. In this case, we assume a 1-1 positional mapping
  446. between the entries in cursor.description and the compiled object.
  447. This is also the most performant case as we disregard extracting /
  448. decoding the column names present in cursor.description since we
  449. already have the desired name we generated in the compiled SQL
  450. construct.
  451. The next common case is that of the completely raw string SQL,
  452. such as passed to connection.execute(). In this case we have no
  453. compiled construct to work with, so we extract and decode the
  454. names from cursor.description and index those as the primary
  455. result row target keys.
  456. The remaining fairly common case is that of the textual SQL
  457. that includes at least partial column information; this is when
  458. we use a :class:`_expression.TextualSelect` construct.
  459. This construct may have
  460. unordered or ordered column information. In the ordered case, we
  461. merge the cursor.description and the compiled construct's information
  462. positionally, and warn if there are additional description names
  463. present, however we still decode the names in cursor.description
  464. as we don't have a guarantee that the names in the columns match
  465. on these. In the unordered case, we match names in cursor.description
  466. to that of the compiled construct based on name matching.
  467. In both of these cases, the cursor.description names and the column
  468. expression objects and names are indexed as result row target keys.
  469. The final case is much less common, where we have a compiled
  470. non-textual SQL expression construct, but the number of columns
  471. in cursor.description doesn't match what's in the compiled
  472. construct. We make the guess here that there might be textual
  473. column expressions in the compiled construct that themselves include
  474. a comma in them causing them to split. We do the same name-matching
  475. as with textual non-ordered columns.
  476. The name-matched system of merging is the same as that used by
  477. SQLAlchemy for all cases up through the 0.9 series. Positional
  478. matching for compiled SQL expressions was introduced in 1.0 as a
  479. major performance feature, and positional matching for textual
  480. :class:`_expression.TextualSelect` objects in 1.1.
  481. As name matching is no longer
  482. a common case, it was acceptable to factor it into smaller generator-
  483. oriented methods that are easier to understand, but incur slightly
  484. more performance overhead.
  485. """
  486. if (
  487. num_ctx_cols
  488. and cols_are_ordered
  489. and not textual_ordered
  490. and num_ctx_cols == len(cursor_description)
  491. ):
  492. self._keys = [elem[0] for elem in result_columns]
  493. # pure positional 1-1 case; doesn't need to read
  494. # the names from cursor.description
  495. # most common case for Core and ORM
  496. # this metadata is safe to cache because we are guaranteed
  497. # to have the columns in the same order for new executions
  498. self._safe_for_cache = True
  499. return [
  500. (
  501. idx,
  502. idx,
  503. rmap_entry[RM_OBJECTS],
  504. rmap_entry[RM_NAME],
  505. rmap_entry[RM_RENDERED_NAME],
  506. context.get_result_processor(
  507. rmap_entry[RM_TYPE],
  508. rmap_entry[RM_RENDERED_NAME],
  509. cursor_description[idx][1],
  510. ),
  511. None,
  512. )
  513. for idx, rmap_entry in enumerate(result_columns)
  514. ]
  515. else:
  516. # name-based or text-positional cases, where we need
  517. # to read cursor.description names
  518. if textual_ordered or (
  519. ad_hoc_textual and len(cursor_description) == num_ctx_cols
  520. ):
  521. self._safe_for_cache = True
  522. # textual positional case
  523. raw_iterator = self._merge_textual_cols_by_position(
  524. context, cursor_description, result_columns
  525. )
  526. elif num_ctx_cols:
  527. # compiled SQL with a mismatch of description cols
  528. # vs. compiled cols, or textual w/ unordered columns
  529. # the order of columns can change if the query is
  530. # against a "select *", so not safe to cache
  531. self._safe_for_cache = False
  532. raw_iterator = self._merge_cols_by_name(
  533. context,
  534. cursor_description,
  535. result_columns,
  536. loose_column_name_matching,
  537. )
  538. else:
  539. # no compiled SQL, just a raw string, order of columns
  540. # can change for "select *"
  541. self._safe_for_cache = False
  542. raw_iterator = self._merge_cols_by_none(
  543. context, cursor_description
  544. )
  545. return [
  546. (
  547. idx,
  548. ridx,
  549. obj,
  550. cursor_colname,
  551. cursor_colname,
  552. context.get_result_processor(
  553. mapped_type, cursor_colname, coltype
  554. ),
  555. untranslated,
  556. )
  557. for (
  558. idx,
  559. ridx,
  560. cursor_colname,
  561. mapped_type,
  562. coltype,
  563. obj,
  564. untranslated,
  565. ) in raw_iterator
  566. ]
  567. def _colnames_from_description(self, context, cursor_description):
  568. """Extract column names and data types from a cursor.description.
  569. Applies unicode decoding, column translation, "normalization",
  570. and case sensitivity rules to the names based on the dialect.
  571. """
  572. dialect = context.dialect
  573. translate_colname = context._translate_colname
  574. normalize_name = (
  575. dialect.normalize_name if dialect.requires_name_normalize else None
  576. )
  577. untranslated = None
  578. self._keys = []
  579. for idx, rec in enumerate(cursor_description):
  580. colname = rec[0]
  581. coltype = rec[1]
  582. if translate_colname:
  583. colname, untranslated = translate_colname(colname)
  584. if normalize_name:
  585. colname = normalize_name(colname)
  586. self._keys.append(colname)
  587. yield idx, colname, untranslated, coltype
  588. def _merge_textual_cols_by_position(
  589. self, context, cursor_description, result_columns
  590. ):
  591. num_ctx_cols = len(result_columns)
  592. if num_ctx_cols > len(cursor_description):
  593. util.warn(
  594. "Number of columns in textual SQL (%d) is "
  595. "smaller than number of columns requested (%d)"
  596. % (num_ctx_cols, len(cursor_description))
  597. )
  598. seen = set()
  599. for (
  600. idx,
  601. colname,
  602. untranslated,
  603. coltype,
  604. ) in self._colnames_from_description(context, cursor_description):
  605. if idx < num_ctx_cols:
  606. ctx_rec = result_columns[idx]
  607. obj = ctx_rec[RM_OBJECTS]
  608. ridx = idx
  609. mapped_type = ctx_rec[RM_TYPE]
  610. if obj[0] in seen:
  611. raise exc.InvalidRequestError(
  612. "Duplicate column expression requested "
  613. "in textual SQL: %r" % obj[0]
  614. )
  615. seen.add(obj[0])
  616. else:
  617. mapped_type = sqltypes.NULLTYPE
  618. obj = None
  619. ridx = None
  620. yield idx, ridx, colname, mapped_type, coltype, obj, untranslated
  621. def _merge_cols_by_name(
  622. self,
  623. context,
  624. cursor_description,
  625. result_columns,
  626. loose_column_name_matching,
  627. ):
  628. match_map = self._create_description_match_map(
  629. result_columns, loose_column_name_matching
  630. )
  631. mapped_type: TypeEngine[Any]
  632. for (
  633. idx,
  634. colname,
  635. untranslated,
  636. coltype,
  637. ) in self._colnames_from_description(context, cursor_description):
  638. try:
  639. ctx_rec = match_map[colname]
  640. except KeyError:
  641. mapped_type = sqltypes.NULLTYPE
  642. obj = None
  643. result_columns_idx = None
  644. else:
  645. obj = ctx_rec[1]
  646. mapped_type = ctx_rec[2]
  647. result_columns_idx = ctx_rec[3]
  648. yield (
  649. idx,
  650. result_columns_idx,
  651. colname,
  652. mapped_type,
  653. coltype,
  654. obj,
  655. untranslated,
  656. )
  657. @classmethod
  658. def _create_description_match_map(
  659. cls,
  660. result_columns: List[ResultColumnsEntry],
  661. loose_column_name_matching: bool = False,
  662. ) -> Dict[
  663. Union[str, object], Tuple[str, Tuple[Any, ...], TypeEngine[Any], int]
  664. ]:
  665. """when matching cursor.description to a set of names that are present
  666. in a Compiled object, as is the case with TextualSelect, get all the
  667. names we expect might match those in cursor.description.
  668. """
  669. d: Dict[
  670. Union[str, object],
  671. Tuple[str, Tuple[Any, ...], TypeEngine[Any], int],
  672. ] = {}
  673. for ridx, elem in enumerate(result_columns):
  674. key = elem[RM_RENDERED_NAME]
  675. if key in d:
  676. # conflicting keyname - just add the column-linked objects
  677. # to the existing record. if there is a duplicate column
  678. # name in the cursor description, this will allow all of those
  679. # objects to raise an ambiguous column error
  680. e_name, e_obj, e_type, e_ridx = d[key]
  681. d[key] = e_name, e_obj + elem[RM_OBJECTS], e_type, ridx
  682. else:
  683. d[key] = (elem[RM_NAME], elem[RM_OBJECTS], elem[RM_TYPE], ridx)
  684. if loose_column_name_matching:
  685. # when using a textual statement with an unordered set
  686. # of columns that line up, we are expecting the user
  687. # to be using label names in the SQL that match to the column
  688. # expressions. Enable more liberal matching for this case;
  689. # duplicate keys that are ambiguous will be fixed later.
  690. for r_key in elem[RM_OBJECTS]:
  691. d.setdefault(
  692. r_key,
  693. (elem[RM_NAME], elem[RM_OBJECTS], elem[RM_TYPE], ridx),
  694. )
  695. return d
  696. def _merge_cols_by_none(self, context, cursor_description):
  697. for (
  698. idx,
  699. colname,
  700. untranslated,
  701. coltype,
  702. ) in self._colnames_from_description(context, cursor_description):
  703. yield (
  704. idx,
  705. None,
  706. colname,
  707. sqltypes.NULLTYPE,
  708. coltype,
  709. None,
  710. untranslated,
  711. )
  712. if not TYPE_CHECKING:
  713. def _key_fallback(
  714. self, key: Any, err: Optional[Exception], raiseerr: bool = True
  715. ) -> Optional[NoReturn]:
  716. if raiseerr:
  717. if self._unpickled and isinstance(key, elements.ColumnElement):
  718. raise exc.NoSuchColumnError(
  719. "Row was unpickled; lookup by ColumnElement "
  720. "is unsupported"
  721. ) from err
  722. else:
  723. raise exc.NoSuchColumnError(
  724. "Could not locate column in row for column '%s'"
  725. % util.string_or_unprintable(key)
  726. ) from err
  727. else:
  728. return None
  729. def _raise_for_ambiguous_column_name(self, rec):
  730. raise exc.InvalidRequestError(
  731. "Ambiguous column name '%s' in "
  732. "result set column descriptions" % rec[MD_LOOKUP_KEY]
  733. )
  734. def _index_for_key(self, key: Any, raiseerr: bool = True) -> Optional[int]:
  735. # TODO: can consider pre-loading ints and negative ints
  736. # into _keymap - also no coverage here
  737. if isinstance(key, int):
  738. key = self._keys[key]
  739. try:
  740. rec = self._keymap[key]
  741. except KeyError as ke:
  742. x = self._key_fallback(key, ke, raiseerr)
  743. assert x is None
  744. return None
  745. index = rec[0]
  746. if index is None:
  747. self._raise_for_ambiguous_column_name(rec)
  748. return index
  749. def _indexes_for_keys(self, keys):
  750. try:
  751. return [self._keymap[key][0] for key in keys]
  752. except KeyError as ke:
  753. # ensure it raises
  754. CursorResultMetaData._key_fallback(self, ke.args[0], ke)
  755. def _metadata_for_keys(
  756. self, keys: Sequence[Any]
  757. ) -> Iterator[_NonAmbigCursorKeyMapRecType]:
  758. for key in keys:
  759. if int in key.__class__.__mro__:
  760. key = self._keys[key]
  761. try:
  762. rec = self._keymap[key]
  763. except KeyError as ke:
  764. # ensure it raises
  765. CursorResultMetaData._key_fallback(self, ke.args[0], ke)
  766. index = rec[MD_INDEX]
  767. if index is None:
  768. self._raise_for_ambiguous_column_name(rec)
  769. yield cast(_NonAmbigCursorKeyMapRecType, rec)
  770. def __getstate__(self):
  771. # TODO: consider serializing this as SimpleResultMetaData
  772. return {
  773. "_keymap": {
  774. key: (
  775. rec[MD_INDEX],
  776. rec[MD_RESULT_MAP_INDEX],
  777. [],
  778. key,
  779. rec[MD_RENDERED_NAME],
  780. None,
  781. None,
  782. )
  783. for key, rec in self._keymap.items()
  784. if isinstance(key, (str, int))
  785. },
  786. "_keys": self._keys,
  787. "_translated_indexes": self._translated_indexes,
  788. }
  789. def __setstate__(self, state):
  790. self._processors = [None for _ in range(len(state["_keys"]))]
  791. self._keymap = state["_keymap"]
  792. self._keymap_by_result_column_idx = None
  793. self._key_to_index = self._make_key_to_index(self._keymap, MD_INDEX)
  794. self._keys = state["_keys"]
  795. self._unpickled = True
  796. if state["_translated_indexes"]:
  797. self._translated_indexes = cast(
  798. "List[int]", state["_translated_indexes"]
  799. )
  800. self._tuplefilter = tuplegetter(*self._translated_indexes)
  801. else:
  802. self._translated_indexes = self._tuplefilter = None
  803. class ResultFetchStrategy:
  804. """Define a fetching strategy for a result object.
  805. .. versionadded:: 1.4
  806. """
  807. __slots__ = ()
  808. alternate_cursor_description: Optional[_DBAPICursorDescription] = None
  809. def soft_close(
  810. self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor]
  811. ) -> None:
  812. raise NotImplementedError()
  813. def hard_close(
  814. self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor]
  815. ) -> None:
  816. raise NotImplementedError()
  817. def yield_per(
  818. self,
  819. result: CursorResult[Any],
  820. dbapi_cursor: Optional[DBAPICursor],
  821. num: int,
  822. ) -> None:
  823. return
  824. def fetchone(
  825. self,
  826. result: CursorResult[Any],
  827. dbapi_cursor: DBAPICursor,
  828. hard_close: bool = False,
  829. ) -> Any:
  830. raise NotImplementedError()
  831. def fetchmany(
  832. self,
  833. result: CursorResult[Any],
  834. dbapi_cursor: DBAPICursor,
  835. size: Optional[int] = None,
  836. ) -> Any:
  837. raise NotImplementedError()
  838. def fetchall(
  839. self,
  840. result: CursorResult[Any],
  841. dbapi_cursor: DBAPICursor,
  842. ) -> Any:
  843. raise NotImplementedError()
  844. def handle_exception(
  845. self,
  846. result: CursorResult[Any],
  847. dbapi_cursor: Optional[DBAPICursor],
  848. err: BaseException,
  849. ) -> NoReturn:
  850. raise err
  851. class NoCursorFetchStrategy(ResultFetchStrategy):
  852. """Cursor strategy for a result that has no open cursor.
  853. There are two varieties of this strategy, one for DQL and one for
  854. DML (and also DDL), each of which represent a result that had a cursor
  855. but no longer has one.
  856. """
  857. __slots__ = ()
  858. def soft_close(self, result, dbapi_cursor):
  859. pass
  860. def hard_close(self, result, dbapi_cursor):
  861. pass
  862. def fetchone(self, result, dbapi_cursor, hard_close=False):
  863. return self._non_result(result, None)
  864. def fetchmany(self, result, dbapi_cursor, size=None):
  865. return self._non_result(result, [])
  866. def fetchall(self, result, dbapi_cursor):
  867. return self._non_result(result, [])
  868. def _non_result(self, result, default, err=None):
  869. raise NotImplementedError()
  870. class NoCursorDQLFetchStrategy(NoCursorFetchStrategy):
  871. """Cursor strategy for a DQL result that has no open cursor.
  872. This is a result set that can return rows, i.e. for a SELECT, or for an
  873. INSERT, UPDATE, DELETE that includes RETURNING. However it is in the state
  874. where the cursor is closed and no rows remain available. The owning result
  875. object may or may not be "hard closed", which determines if the fetch
  876. methods send empty results or raise for closed result.
  877. """
  878. __slots__ = ()
  879. def _non_result(self, result, default, err=None):
  880. if result.closed:
  881. raise exc.ResourceClosedError(
  882. "This result object is closed."
  883. ) from err
  884. else:
  885. return default
  886. _NO_CURSOR_DQL = NoCursorDQLFetchStrategy()
  887. class NoCursorDMLFetchStrategy(NoCursorFetchStrategy):
  888. """Cursor strategy for a DML result that has no open cursor.
  889. This is a result set that does not return rows, i.e. for an INSERT,
  890. UPDATE, DELETE that does not include RETURNING.
  891. """
  892. __slots__ = ()
  893. def _non_result(self, result, default, err=None):
  894. # we only expect to have a _NoResultMetaData() here right now.
  895. assert not result._metadata.returns_rows
  896. result._metadata._we_dont_return_rows(err)
  897. _NO_CURSOR_DML = NoCursorDMLFetchStrategy()
  898. class CursorFetchStrategy(ResultFetchStrategy):
  899. """Call fetch methods from a DBAPI cursor.
  900. Alternate versions of this class may instead buffer the rows from
  901. cursors or not use cursors at all.
  902. """
  903. __slots__ = ()
  904. def soft_close(
  905. self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor]
  906. ) -> None:
  907. result.cursor_strategy = _NO_CURSOR_DQL
  908. def hard_close(
  909. self, result: CursorResult[Any], dbapi_cursor: Optional[DBAPICursor]
  910. ) -> None:
  911. result.cursor_strategy = _NO_CURSOR_DQL
  912. def handle_exception(
  913. self,
  914. result: CursorResult[Any],
  915. dbapi_cursor: Optional[DBAPICursor],
  916. err: BaseException,
  917. ) -> NoReturn:
  918. result.connection._handle_dbapi_exception(
  919. err, None, None, dbapi_cursor, result.context
  920. )
  921. def yield_per(
  922. self,
  923. result: CursorResult[Any],
  924. dbapi_cursor: Optional[DBAPICursor],
  925. num: int,
  926. ) -> None:
  927. result.cursor_strategy = BufferedRowCursorFetchStrategy(
  928. dbapi_cursor,
  929. {"max_row_buffer": num},
  930. initial_buffer=collections.deque(),
  931. growth_factor=0,
  932. )
  933. def fetchone(
  934. self,
  935. result: CursorResult[Any],
  936. dbapi_cursor: DBAPICursor,
  937. hard_close: bool = False,
  938. ) -> Any:
  939. try:
  940. row = dbapi_cursor.fetchone()
  941. if row is None:
  942. result._soft_close(hard=hard_close)
  943. return row
  944. except BaseException as e:
  945. self.handle_exception(result, dbapi_cursor, e)
  946. def fetchmany(
  947. self,
  948. result: CursorResult[Any],
  949. dbapi_cursor: DBAPICursor,
  950. size: Optional[int] = None,
  951. ) -> Any:
  952. try:
  953. if size is None:
  954. l = dbapi_cursor.fetchmany()
  955. else:
  956. l = dbapi_cursor.fetchmany(size)
  957. if not l:
  958. result._soft_close()
  959. return l
  960. except BaseException as e:
  961. self.handle_exception(result, dbapi_cursor, e)
  962. def fetchall(
  963. self,
  964. result: CursorResult[Any],
  965. dbapi_cursor: DBAPICursor,
  966. ) -> Any:
  967. try:
  968. rows = dbapi_cursor.fetchall()
  969. result._soft_close()
  970. return rows
  971. except BaseException as e:
  972. self.handle_exception(result, dbapi_cursor, e)
  973. _DEFAULT_FETCH = CursorFetchStrategy()
  974. class BufferedRowCursorFetchStrategy(CursorFetchStrategy):
  975. """A cursor fetch strategy with row buffering behavior.
  976. This strategy buffers the contents of a selection of rows
  977. before ``fetchone()`` is called. This is to allow the results of
  978. ``cursor.description`` to be available immediately, when
  979. interfacing with a DB-API that requires rows to be consumed before
  980. this information is available (currently psycopg2, when used with
  981. server-side cursors).
  982. The pre-fetching behavior fetches only one row initially, and then
  983. grows its buffer size by a fixed amount with each successive need
  984. for additional rows up the ``max_row_buffer`` size, which defaults
  985. to 1000::
  986. with psycopg2_engine.connect() as conn:
  987. result = conn.execution_options(
  988. stream_results=True, max_row_buffer=50
  989. ).execute(text("select * from table"))
  990. .. versionadded:: 1.4 ``max_row_buffer`` may now exceed 1000 rows.
  991. .. seealso::
  992. :ref:`psycopg2_execution_options`
  993. """
  994. __slots__ = ("_max_row_buffer", "_rowbuffer", "_bufsize", "_growth_factor")
  995. def __init__(
  996. self,
  997. dbapi_cursor,
  998. execution_options,
  999. growth_factor=5,
  1000. initial_buffer=None,
  1001. ):
  1002. self._max_row_buffer = execution_options.get("max_row_buffer", 1000)
  1003. if initial_buffer is not None:
  1004. self._rowbuffer = initial_buffer
  1005. else:
  1006. self._rowbuffer = collections.deque(dbapi_cursor.fetchmany(1))
  1007. self._growth_factor = growth_factor
  1008. if growth_factor:
  1009. self._bufsize = min(self._max_row_buffer, self._growth_factor)
  1010. else:
  1011. self._bufsize = self._max_row_buffer
  1012. @classmethod
  1013. def create(cls, result):
  1014. return BufferedRowCursorFetchStrategy(
  1015. result.cursor,
  1016. result.context.execution_options,
  1017. )
  1018. def _buffer_rows(self, result, dbapi_cursor):
  1019. """this is currently used only by fetchone()."""
  1020. size = self._bufsize
  1021. try:
  1022. if size < 1:
  1023. new_rows = dbapi_cursor.fetchall()
  1024. else:
  1025. new_rows = dbapi_cursor.fetchmany(size)
  1026. except BaseException as e:
  1027. self.handle_exception(result, dbapi_cursor, e)
  1028. if not new_rows:
  1029. return
  1030. self._rowbuffer = collections.deque(new_rows)
  1031. if self._growth_factor and size < self._max_row_buffer:
  1032. self._bufsize = min(
  1033. self._max_row_buffer, size * self._growth_factor
  1034. )
  1035. def yield_per(self, result, dbapi_cursor, num):
  1036. self._growth_factor = 0
  1037. self._max_row_buffer = self._bufsize = num
  1038. def soft_close(self, result, dbapi_cursor):
  1039. self._rowbuffer.clear()
  1040. super().soft_close(result, dbapi_cursor)
  1041. def hard_close(self, result, dbapi_cursor):
  1042. self._rowbuffer.clear()
  1043. super().hard_close(result, dbapi_cursor)
  1044. def fetchone(self, result, dbapi_cursor, hard_close=False):
  1045. if not self._rowbuffer:
  1046. self._buffer_rows(result, dbapi_cursor)
  1047. if not self._rowbuffer:
  1048. try:
  1049. result._soft_close(hard=hard_close)
  1050. except BaseException as e:
  1051. self.handle_exception(result, dbapi_cursor, e)
  1052. return None
  1053. return self._rowbuffer.popleft()
  1054. def fetchmany(self, result, dbapi_cursor, size=None):
  1055. if size is None:
  1056. return self.fetchall(result, dbapi_cursor)
  1057. rb = self._rowbuffer
  1058. lb = len(rb)
  1059. close = False
  1060. if size > lb:
  1061. try:
  1062. new = dbapi_cursor.fetchmany(size - lb)
  1063. except BaseException as e:
  1064. self.handle_exception(result, dbapi_cursor, e)
  1065. else:
  1066. if not new:
  1067. # defer closing since it may clear the row buffer
  1068. close = True
  1069. else:
  1070. rb.extend(new)
  1071. res = [rb.popleft() for _ in range(min(size, len(rb)))]
  1072. if close:
  1073. result._soft_close()
  1074. return res
  1075. def fetchall(self, result, dbapi_cursor):
  1076. try:
  1077. ret = list(self._rowbuffer) + list(dbapi_cursor.fetchall())
  1078. self._rowbuffer.clear()
  1079. result._soft_close()
  1080. return ret
  1081. except BaseException as e:
  1082. self.handle_exception(result, dbapi_cursor, e)
  1083. class FullyBufferedCursorFetchStrategy(CursorFetchStrategy):
  1084. """A cursor strategy that buffers rows fully upon creation.
  1085. Used for operations where a result is to be delivered
  1086. after the database conversation can not be continued,
  1087. such as MSSQL INSERT...OUTPUT after an autocommit.
  1088. """
  1089. __slots__ = ("_rowbuffer", "alternate_cursor_description")
  1090. def __init__(
  1091. self,
  1092. dbapi_cursor: Optional[DBAPICursor],
  1093. alternate_description: Optional[_DBAPICursorDescription] = None,
  1094. initial_buffer: Optional[Iterable[Any]] = None,
  1095. ):
  1096. self.alternate_cursor_description = alternate_description
  1097. if initial_buffer is not None:
  1098. self._rowbuffer = collections.deque(initial_buffer)
  1099. else:
  1100. assert dbapi_cursor is not None
  1101. self._rowbuffer = collections.deque(dbapi_cursor.fetchall())
  1102. def yield_per(self, result, dbapi_cursor, num):
  1103. pass
  1104. def soft_close(self, result, dbapi_cursor):
  1105. self._rowbuffer.clear()
  1106. super().soft_close(result, dbapi_cursor)
  1107. def hard_close(self, result, dbapi_cursor):
  1108. self._rowbuffer.clear()
  1109. super().hard_close(result, dbapi_cursor)
  1110. def fetchone(self, result, dbapi_cursor, hard_close=False):
  1111. if self._rowbuffer:
  1112. return self._rowbuffer.popleft()
  1113. else:
  1114. result._soft_close(hard=hard_close)
  1115. return None
  1116. def fetchmany(self, result, dbapi_cursor, size=None):
  1117. if size is None:
  1118. return self.fetchall(result, dbapi_cursor)
  1119. rb = self._rowbuffer
  1120. rows = [rb.popleft() for _ in range(min(size, len(rb)))]
  1121. if not rows:
  1122. result._soft_close()
  1123. return rows
  1124. def fetchall(self, result, dbapi_cursor):
  1125. ret = self._rowbuffer
  1126. self._rowbuffer = collections.deque()
  1127. result._soft_close()
  1128. return ret
  1129. class _NoResultMetaData(ResultMetaData):
  1130. __slots__ = ()
  1131. returns_rows = False
  1132. def _we_dont_return_rows(self, err=None):
  1133. raise exc.ResourceClosedError(
  1134. "This result object does not return rows. "
  1135. "It has been closed automatically."
  1136. ) from err
  1137. def _index_for_key(self, keys, raiseerr):
  1138. self._we_dont_return_rows()
  1139. def _metadata_for_keys(self, key):
  1140. self._we_dont_return_rows()
  1141. def _reduce(self, keys):
  1142. self._we_dont_return_rows()
  1143. @property
  1144. def _keymap(self): # type: ignore[override]
  1145. self._we_dont_return_rows()
  1146. @property
  1147. def _key_to_index(self): # type: ignore[override]
  1148. self._we_dont_return_rows()
  1149. @property
  1150. def _processors(self): # type: ignore[override]
  1151. self._we_dont_return_rows()
  1152. @property
  1153. def keys(self):
  1154. self._we_dont_return_rows()
  1155. _NO_RESULT_METADATA = _NoResultMetaData()
  1156. def null_dml_result() -> IteratorResult[Any]:
  1157. it: IteratorResult[Any] = IteratorResult(_NoResultMetaData(), iter([]))
  1158. it._soft_close()
  1159. return it
  1160. class CursorResult(Result[_T]):
  1161. """A Result that is representing state from a DBAPI cursor.
  1162. .. versionchanged:: 1.4 The :class:`.CursorResult``
  1163. class replaces the previous :class:`.ResultProxy` interface.
  1164. This classes are based on the :class:`.Result` calling API
  1165. which provides an updated usage model and calling facade for
  1166. SQLAlchemy Core and SQLAlchemy ORM.
  1167. Returns database rows via the :class:`.Row` class, which provides
  1168. additional API features and behaviors on top of the raw data returned by
  1169. the DBAPI. Through the use of filters such as the :meth:`.Result.scalars`
  1170. method, other kinds of objects may also be returned.
  1171. .. seealso::
  1172. :ref:`tutorial_selecting_data` - introductory material for accessing
  1173. :class:`_engine.CursorResult` and :class:`.Row` objects.
  1174. """
  1175. __slots__ = (
  1176. "context",
  1177. "dialect",
  1178. "cursor",
  1179. "cursor_strategy",
  1180. "_echo",
  1181. "connection",
  1182. )
  1183. _metadata: Union[CursorResultMetaData, _NoResultMetaData]
  1184. _no_result_metadata = _NO_RESULT_METADATA
  1185. _soft_closed: bool = False
  1186. closed: bool = False
  1187. _is_cursor = True
  1188. context: DefaultExecutionContext
  1189. dialect: Dialect
  1190. cursor_strategy: ResultFetchStrategy
  1191. connection: Connection
  1192. def __init__(
  1193. self,
  1194. context: DefaultExecutionContext,
  1195. cursor_strategy: ResultFetchStrategy,
  1196. cursor_description: Optional[_DBAPICursorDescription],
  1197. ):
  1198. self.context = context
  1199. self.dialect = context.dialect
  1200. self.cursor = context.cursor
  1201. self.cursor_strategy = cursor_strategy
  1202. self.connection = context.root_connection
  1203. self._echo = echo = (
  1204. self.connection._echo and context.engine._should_log_debug()
  1205. )
  1206. if cursor_description is not None:
  1207. # inline of Result._row_getter(), set up an initial row
  1208. # getter assuming no transformations will be called as this
  1209. # is the most common case
  1210. metadata = self._init_metadata(context, cursor_description)
  1211. _make_row: Any
  1212. _make_row = functools.partial(
  1213. Row,
  1214. metadata,
  1215. metadata._effective_processors,
  1216. metadata._key_to_index,
  1217. )
  1218. if context._num_sentinel_cols:
  1219. sentinel_filter = operator.itemgetter(
  1220. slice(-context._num_sentinel_cols)
  1221. )
  1222. def _sliced_row(raw_data):
  1223. return _make_row(sentinel_filter(raw_data))
  1224. sliced_row = _sliced_row
  1225. else:
  1226. sliced_row = _make_row
  1227. if echo:
  1228. log = self.context.connection._log_debug
  1229. def _log_row(row):
  1230. log("Row %r", sql_util._repr_row(row))
  1231. return row
  1232. self._row_logging_fn = _log_row
  1233. def _make_row_2(row):
  1234. return _log_row(sliced_row(row))
  1235. make_row = _make_row_2
  1236. else:
  1237. make_row = sliced_row
  1238. self._set_memoized_attribute("_row_getter", make_row)
  1239. else:
  1240. assert context._num_sentinel_cols == 0
  1241. self._metadata = self._no_result_metadata
  1242. def _init_metadata(self, context, cursor_description):
  1243. if context.compiled:
  1244. compiled = context.compiled
  1245. if compiled._cached_metadata:
  1246. metadata = compiled._cached_metadata
  1247. else:
  1248. metadata = CursorResultMetaData(self, cursor_description)
  1249. if metadata._safe_for_cache:
  1250. compiled._cached_metadata = metadata
  1251. # result rewrite/ adapt step. this is to suit the case
  1252. # when we are invoked against a cached Compiled object, we want
  1253. # to rewrite the ResultMetaData to reflect the Column objects
  1254. # that are in our current SQL statement object, not the one
  1255. # that is associated with the cached Compiled object.
  1256. # the Compiled object may also tell us to not
  1257. # actually do this step; this is to support the ORM where
  1258. # it is to produce a new Result object in any case, and will
  1259. # be using the cached Column objects against this database result
  1260. # so we don't want to rewrite them.
  1261. #
  1262. # Basically this step suits the use case where the end user
  1263. # is using Core SQL expressions and is accessing columns in the
  1264. # result row using row._mapping[table.c.column].
  1265. if (
  1266. not context.execution_options.get(
  1267. "_result_disable_adapt_to_context", False
  1268. )
  1269. and compiled._result_columns
  1270. and context.cache_hit is context.dialect.CACHE_HIT
  1271. and compiled.statement is not context.invoked_statement
  1272. ):
  1273. metadata = metadata._adapt_to_context(context)
  1274. self._metadata = metadata
  1275. else:
  1276. self._metadata = metadata = CursorResultMetaData(
  1277. self, cursor_description
  1278. )
  1279. if self._echo:
  1280. context.connection._log_debug(
  1281. "Col %r", tuple(x[0] for x in cursor_description)
  1282. )
  1283. return metadata
  1284. def _soft_close(self, hard=False):
  1285. """Soft close this :class:`_engine.CursorResult`.
  1286. This releases all DBAPI cursor resources, but leaves the
  1287. CursorResult "open" from a semantic perspective, meaning the
  1288. fetchXXX() methods will continue to return empty results.
  1289. This method is called automatically when:
  1290. * all result rows are exhausted using the fetchXXX() methods.
  1291. * cursor.description is None.
  1292. This method is **not public**, but is documented in order to clarify
  1293. the "autoclose" process used.
  1294. .. seealso::
  1295. :meth:`_engine.CursorResult.close`
  1296. """
  1297. if (not hard and self._soft_closed) or (hard and self.closed):
  1298. return
  1299. if hard:
  1300. self.closed = True
  1301. self.cursor_strategy.hard_close(self, self.cursor)
  1302. else:
  1303. self.cursor_strategy.soft_close(self, self.cursor)
  1304. if not self._soft_closed:
  1305. cursor = self.cursor
  1306. self.cursor = None # type: ignore
  1307. self.connection._safe_close_cursor(cursor)
  1308. self._soft_closed = True
  1309. @property
  1310. def inserted_primary_key_rows(self):
  1311. """Return the value of
  1312. :attr:`_engine.CursorResult.inserted_primary_key`
  1313. as a row contained within a list; some dialects may support a
  1314. multiple row form as well.
  1315. .. note:: As indicated below, in current SQLAlchemy versions this
  1316. accessor is only useful beyond what's already supplied by
  1317. :attr:`_engine.CursorResult.inserted_primary_key` when using the
  1318. :ref:`postgresql_psycopg2` dialect. Future versions hope to
  1319. generalize this feature to more dialects.
  1320. This accessor is added to support dialects that offer the feature
  1321. that is currently implemented by the :ref:`psycopg2_executemany_mode`
  1322. feature, currently **only the psycopg2 dialect**, which provides
  1323. for many rows to be INSERTed at once while still retaining the
  1324. behavior of being able to return server-generated primary key values.
  1325. * **When using the psycopg2 dialect, or other dialects that may support
  1326. "fast executemany" style inserts in upcoming releases** : When
  1327. invoking an INSERT statement while passing a list of rows as the
  1328. second argument to :meth:`_engine.Connection.execute`, this accessor
  1329. will then provide a list of rows, where each row contains the primary
  1330. key value for each row that was INSERTed.
  1331. * **When using all other dialects / backends that don't yet support
  1332. this feature**: This accessor is only useful for **single row INSERT
  1333. statements**, and returns the same information as that of the
  1334. :attr:`_engine.CursorResult.inserted_primary_key` within a
  1335. single-element list. When an INSERT statement is executed in
  1336. conjunction with a list of rows to be INSERTed, the list will contain
  1337. one row per row inserted in the statement, however it will contain
  1338. ``None`` for any server-generated values.
  1339. Future releases of SQLAlchemy will further generalize the
  1340. "fast execution helper" feature of psycopg2 to suit other dialects,
  1341. thus allowing this accessor to be of more general use.
  1342. .. versionadded:: 1.4
  1343. .. seealso::
  1344. :attr:`_engine.CursorResult.inserted_primary_key`
  1345. """
  1346. if not self.context.compiled:
  1347. raise exc.InvalidRequestError(
  1348. "Statement is not a compiled expression construct."
  1349. )
  1350. elif not self.context.isinsert:
  1351. raise exc.InvalidRequestError(
  1352. "Statement is not an insert() expression construct."
  1353. )
  1354. elif self.context._is_explicit_returning:
  1355. raise exc.InvalidRequestError(
  1356. "Can't call inserted_primary_key "
  1357. "when returning() "
  1358. "is used."
  1359. )
  1360. return self.context.inserted_primary_key_rows
  1361. @property
  1362. def inserted_primary_key(self):
  1363. """Return the primary key for the row just inserted.
  1364. The return value is a :class:`_result.Row` object representing
  1365. a named tuple of primary key values in the order in which the
  1366. primary key columns are configured in the source
  1367. :class:`_schema.Table`.
  1368. .. versionchanged:: 1.4.8 - the
  1369. :attr:`_engine.CursorResult.inserted_primary_key`
  1370. value is now a named tuple via the :class:`_result.Row` class,
  1371. rather than a plain tuple.
  1372. This accessor only applies to single row :func:`_expression.insert`
  1373. constructs which did not explicitly specify
  1374. :meth:`_expression.Insert.returning`. Support for multirow inserts,
  1375. while not yet available for most backends, would be accessed using
  1376. the :attr:`_engine.CursorResult.inserted_primary_key_rows` accessor.
  1377. Note that primary key columns which specify a server_default clause, or
  1378. otherwise do not qualify as "autoincrement" columns (see the notes at
  1379. :class:`_schema.Column`), and were generated using the database-side
  1380. default, will appear in this list as ``None`` unless the backend
  1381. supports "returning" and the insert statement executed with the
  1382. "implicit returning" enabled.
  1383. Raises :class:`~sqlalchemy.exc.InvalidRequestError` if the executed
  1384. statement is not a compiled expression construct
  1385. or is not an insert() construct.
  1386. """
  1387. if self.context.executemany:
  1388. raise exc.InvalidRequestError(
  1389. "This statement was an executemany call; if primary key "
  1390. "returning is supported, please "
  1391. "use .inserted_primary_key_rows."
  1392. )
  1393. ikp = self.inserted_primary_key_rows
  1394. if ikp:
  1395. return ikp[0]
  1396. else:
  1397. return None
  1398. def last_updated_params(self):
  1399. """Return the collection of updated parameters from this
  1400. execution.
  1401. Raises :class:`~sqlalchemy.exc.InvalidRequestError` if the executed
  1402. statement is not a compiled expression construct
  1403. or is not an update() construct.
  1404. """
  1405. if not self.context.compiled:
  1406. raise exc.InvalidRequestError(
  1407. "Statement is not a compiled expression construct."
  1408. )
  1409. elif not self.context.isupdate:
  1410. raise exc.InvalidRequestError(
  1411. "Statement is not an update() expression construct."
  1412. )
  1413. elif self.context.executemany:
  1414. return self.context.compiled_parameters
  1415. else:
  1416. return self.context.compiled_parameters[0]
  1417. def last_inserted_params(self):
  1418. """Return the collection of inserted parameters from this
  1419. execution.
  1420. Raises :class:`~sqlalchemy.exc.InvalidRequestError` if the executed
  1421. statement is not a compiled expression construct
  1422. or is not an insert() construct.
  1423. """
  1424. if not self.context.compiled:
  1425. raise exc.InvalidRequestError(
  1426. "Statement is not a compiled expression construct."
  1427. )
  1428. elif not self.context.isinsert:
  1429. raise exc.InvalidRequestError(
  1430. "Statement is not an insert() expression construct."
  1431. )
  1432. elif self.context.executemany:
  1433. return self.context.compiled_parameters
  1434. else:
  1435. return self.context.compiled_parameters[0]
  1436. @property
  1437. def returned_defaults_rows(self):
  1438. """Return a list of rows each containing the values of default
  1439. columns that were fetched using
  1440. the :meth:`.ValuesBase.return_defaults` feature.
  1441. The return value is a list of :class:`.Row` objects.
  1442. .. versionadded:: 1.4
  1443. """
  1444. return self.context.returned_default_rows
  1445. def splice_horizontally(self, other):
  1446. """Return a new :class:`.CursorResult` that "horizontally splices"
  1447. together the rows of this :class:`.CursorResult` with that of another
  1448. :class:`.CursorResult`.
  1449. .. tip:: This method is for the benefit of the SQLAlchemy ORM and is
  1450. not intended for general use.
  1451. "horizontally splices" means that for each row in the first and second
  1452. result sets, a new row that concatenates the two rows together is
  1453. produced, which then becomes the new row. The incoming
  1454. :class:`.CursorResult` must have the identical number of rows. It is
  1455. typically expected that the two result sets come from the same sort
  1456. order as well, as the result rows are spliced together based on their
  1457. position in the result.
  1458. The expected use case here is so that multiple INSERT..RETURNING
  1459. statements (which definitely need to be sorted) against different
  1460. tables can produce a single result that looks like a JOIN of those two
  1461. tables.
  1462. E.g.::
  1463. r1 = connection.execute(
  1464. users.insert().returning(
  1465. users.c.user_name, users.c.user_id, sort_by_parameter_order=True
  1466. ),
  1467. user_values,
  1468. )
  1469. r2 = connection.execute(
  1470. addresses.insert().returning(
  1471. addresses.c.address_id,
  1472. addresses.c.address,
  1473. addresses.c.user_id,
  1474. sort_by_parameter_order=True,
  1475. ),
  1476. address_values,
  1477. )
  1478. rows = r1.splice_horizontally(r2).all()
  1479. assert rows == [
  1480. ("john", 1, 1, "foo@bar.com", 1),
  1481. ("jack", 2, 2, "bar@bat.com", 2),
  1482. ]
  1483. .. versionadded:: 2.0
  1484. .. seealso::
  1485. :meth:`.CursorResult.splice_vertically`
  1486. """ # noqa: E501
  1487. clone = self._generate()
  1488. total_rows = [
  1489. tuple(r1) + tuple(r2)
  1490. for r1, r2 in zip(
  1491. list(self._raw_row_iterator()),
  1492. list(other._raw_row_iterator()),
  1493. )
  1494. ]
  1495. clone._metadata = clone._metadata._splice_horizontally(other._metadata)
  1496. clone.cursor_strategy = FullyBufferedCursorFetchStrategy(
  1497. None,
  1498. initial_buffer=total_rows,
  1499. )
  1500. clone._reset_memoizations()
  1501. return clone
  1502. def splice_vertically(self, other):
  1503. """Return a new :class:`.CursorResult` that "vertically splices",
  1504. i.e. "extends", the rows of this :class:`.CursorResult` with that of
  1505. another :class:`.CursorResult`.
  1506. .. tip:: This method is for the benefit of the SQLAlchemy ORM and is
  1507. not intended for general use.
  1508. "vertically splices" means the rows of the given result are appended to
  1509. the rows of this cursor result. The incoming :class:`.CursorResult`
  1510. must have rows that represent the identical list of columns in the
  1511. identical order as they are in this :class:`.CursorResult`.
  1512. .. versionadded:: 2.0
  1513. .. seealso::
  1514. :meth:`.CursorResult.splice_horizontally`
  1515. """
  1516. clone = self._generate()
  1517. total_rows = list(self._raw_row_iterator()) + list(
  1518. other._raw_row_iterator()
  1519. )
  1520. clone.cursor_strategy = FullyBufferedCursorFetchStrategy(
  1521. None,
  1522. initial_buffer=total_rows,
  1523. )
  1524. clone._reset_memoizations()
  1525. return clone
  1526. def _rewind(self, rows):
  1527. """rewind this result back to the given rowset.
  1528. this is used internally for the case where an :class:`.Insert`
  1529. construct combines the use of
  1530. :meth:`.Insert.return_defaults` along with the
  1531. "supplemental columns" feature.
  1532. """
  1533. if self._echo:
  1534. self.context.connection._log_debug(
  1535. "CursorResult rewound %d row(s)", len(rows)
  1536. )
  1537. # the rows given are expected to be Row objects, so we
  1538. # have to clear out processors which have already run on these
  1539. # rows
  1540. self._metadata = cast(
  1541. CursorResultMetaData, self._metadata
  1542. )._remove_processors()
  1543. self.cursor_strategy = FullyBufferedCursorFetchStrategy(
  1544. None,
  1545. # TODO: if these are Row objects, can we save on not having to
  1546. # re-make new Row objects out of them a second time? is that
  1547. # what's actually happening right now? maybe look into this
  1548. initial_buffer=rows,
  1549. )
  1550. self._reset_memoizations()
  1551. return self
  1552. @property
  1553. def returned_defaults(self):
  1554. """Return the values of default columns that were fetched using
  1555. the :meth:`.ValuesBase.return_defaults` feature.
  1556. The value is an instance of :class:`.Row`, or ``None``
  1557. if :meth:`.ValuesBase.return_defaults` was not used or if the
  1558. backend does not support RETURNING.
  1559. .. seealso::
  1560. :meth:`.ValuesBase.return_defaults`
  1561. """
  1562. if self.context.executemany:
  1563. raise exc.InvalidRequestError(
  1564. "This statement was an executemany call; if return defaults "
  1565. "is supported, please use .returned_defaults_rows."
  1566. )
  1567. rows = self.context.returned_default_rows
  1568. if rows:
  1569. return rows[0]
  1570. else:
  1571. return None
  1572. def lastrow_has_defaults(self):
  1573. """Return ``lastrow_has_defaults()`` from the underlying
  1574. :class:`.ExecutionContext`.
  1575. See :class:`.ExecutionContext` for details.
  1576. """
  1577. return self.context.lastrow_has_defaults()
  1578. def postfetch_cols(self):
  1579. """Return ``postfetch_cols()`` from the underlying
  1580. :class:`.ExecutionContext`.
  1581. See :class:`.ExecutionContext` for details.
  1582. Raises :class:`~sqlalchemy.exc.InvalidRequestError` if the executed
  1583. statement is not a compiled expression construct
  1584. or is not an insert() or update() construct.
  1585. """
  1586. if not self.context.compiled:
  1587. raise exc.InvalidRequestError(
  1588. "Statement is not a compiled expression construct."
  1589. )
  1590. elif not self.context.isinsert and not self.context.isupdate:
  1591. raise exc.InvalidRequestError(
  1592. "Statement is not an insert() or update() "
  1593. "expression construct."
  1594. )
  1595. return self.context.postfetch_cols
  1596. def prefetch_cols(self):
  1597. """Return ``prefetch_cols()`` from the underlying
  1598. :class:`.ExecutionContext`.
  1599. See :class:`.ExecutionContext` for details.
  1600. Raises :class:`~sqlalchemy.exc.InvalidRequestError` if the executed
  1601. statement is not a compiled expression construct
  1602. or is not an insert() or update() construct.
  1603. """
  1604. if not self.context.compiled:
  1605. raise exc.InvalidRequestError(
  1606. "Statement is not a compiled expression construct."
  1607. )
  1608. elif not self.context.isinsert and not self.context.isupdate:
  1609. raise exc.InvalidRequestError(
  1610. "Statement is not an insert() or update() "
  1611. "expression construct."
  1612. )
  1613. return self.context.prefetch_cols
  1614. def supports_sane_rowcount(self):
  1615. """Return ``supports_sane_rowcount`` from the dialect.
  1616. See :attr:`_engine.CursorResult.rowcount` for background.
  1617. """
  1618. return self.dialect.supports_sane_rowcount
  1619. def supports_sane_multi_rowcount(self):
  1620. """Return ``supports_sane_multi_rowcount`` from the dialect.
  1621. See :attr:`_engine.CursorResult.rowcount` for background.
  1622. """
  1623. return self.dialect.supports_sane_multi_rowcount
  1624. @util.memoized_property
  1625. def rowcount(self) -> int:
  1626. """Return the 'rowcount' for this result.
  1627. The primary purpose of 'rowcount' is to report the number of rows
  1628. matched by the WHERE criterion of an UPDATE or DELETE statement
  1629. executed once (i.e. for a single parameter set), which may then be
  1630. compared to the number of rows expected to be updated or deleted as a
  1631. means of asserting data integrity.
  1632. This attribute is transferred from the ``cursor.rowcount`` attribute
  1633. of the DBAPI before the cursor is closed, to support DBAPIs that
  1634. don't make this value available after cursor close. Some DBAPIs may
  1635. offer meaningful values for other kinds of statements, such as INSERT
  1636. and SELECT statements as well. In order to retrieve ``cursor.rowcount``
  1637. for these statements, set the
  1638. :paramref:`.Connection.execution_options.preserve_rowcount`
  1639. execution option to True, which will cause the ``cursor.rowcount``
  1640. value to be unconditionally memoized before any results are returned
  1641. or the cursor is closed, regardless of statement type.
  1642. For cases where the DBAPI does not support rowcount for a particular
  1643. kind of statement and/or execution, the returned value will be ``-1``,
  1644. which is delivered directly from the DBAPI and is part of :pep:`249`.
  1645. All DBAPIs should support rowcount for single-parameter-set
  1646. UPDATE and DELETE statements, however.
  1647. .. note::
  1648. Notes regarding :attr:`_engine.CursorResult.rowcount`:
  1649. * This attribute returns the number of rows *matched*,
  1650. which is not necessarily the same as the number of rows
  1651. that were actually *modified*. For example, an UPDATE statement
  1652. may have no net change on a given row if the SET values
  1653. given are the same as those present in the row already.
  1654. Such a row would be matched but not modified.
  1655. On backends that feature both styles, such as MySQL,
  1656. rowcount is configured to return the match
  1657. count in all cases.
  1658. * :attr:`_engine.CursorResult.rowcount` in the default case is
  1659. *only* useful in conjunction with an UPDATE or DELETE statement,
  1660. and only with a single set of parameters. For other kinds of
  1661. statements, SQLAlchemy will not attempt to pre-memoize the value
  1662. unless the
  1663. :paramref:`.Connection.execution_options.preserve_rowcount`
  1664. execution option is used. Note that contrary to :pep:`249`, many
  1665. DBAPIs do not support rowcount values for statements that are not
  1666. UPDATE or DELETE, particularly when rows are being returned which
  1667. are not fully pre-buffered. DBAPIs that dont support rowcount
  1668. for a particular kind of statement should return the value ``-1``
  1669. for such statements.
  1670. * :attr:`_engine.CursorResult.rowcount` may not be meaningful
  1671. when executing a single statement with multiple parameter sets
  1672. (i.e. an :term:`executemany`). Most DBAPIs do not sum "rowcount"
  1673. values across multiple parameter sets and will return ``-1``
  1674. when accessed.
  1675. * SQLAlchemy's :ref:`engine_insertmanyvalues` feature does support
  1676. a correct population of :attr:`_engine.CursorResult.rowcount`
  1677. when the :paramref:`.Connection.execution_options.preserve_rowcount`
  1678. execution option is set to True.
  1679. * Statements that use RETURNING may not support rowcount, returning
  1680. a ``-1`` value instead.
  1681. .. seealso::
  1682. :ref:`tutorial_update_delete_rowcount` - in the :ref:`unified_tutorial`
  1683. :paramref:`.Connection.execution_options.preserve_rowcount`
  1684. """ # noqa: E501
  1685. try:
  1686. return self.context.rowcount
  1687. except BaseException as e:
  1688. self.cursor_strategy.handle_exception(self, self.cursor, e)
  1689. raise # not called
  1690. @property
  1691. def lastrowid(self):
  1692. """Return the 'lastrowid' accessor on the DBAPI cursor.
  1693. This is a DBAPI specific method and is only functional
  1694. for those backends which support it, for statements
  1695. where it is appropriate. It's behavior is not
  1696. consistent across backends.
  1697. Usage of this method is normally unnecessary when
  1698. using insert() expression constructs; the
  1699. :attr:`~CursorResult.inserted_primary_key` attribute provides a
  1700. tuple of primary key values for a newly inserted row,
  1701. regardless of database backend.
  1702. """
  1703. try:
  1704. return self.context.get_lastrowid()
  1705. except BaseException as e:
  1706. self.cursor_strategy.handle_exception(self, self.cursor, e)
  1707. @property
  1708. def returns_rows(self):
  1709. """True if this :class:`_engine.CursorResult` returns zero or more
  1710. rows.
  1711. I.e. if it is legal to call the methods
  1712. :meth:`_engine.CursorResult.fetchone`,
  1713. :meth:`_engine.CursorResult.fetchmany`
  1714. :meth:`_engine.CursorResult.fetchall`.
  1715. Overall, the value of :attr:`_engine.CursorResult.returns_rows` should
  1716. always be synonymous with whether or not the DBAPI cursor had a
  1717. ``.description`` attribute, indicating the presence of result columns,
  1718. noting that a cursor that returns zero rows still has a
  1719. ``.description`` if a row-returning statement was emitted.
  1720. This attribute should be True for all results that are against
  1721. SELECT statements, as well as for DML statements INSERT/UPDATE/DELETE
  1722. that use RETURNING. For INSERT/UPDATE/DELETE statements that were
  1723. not using RETURNING, the value will usually be False, however
  1724. there are some dialect-specific exceptions to this, such as when
  1725. using the MSSQL / pyodbc dialect a SELECT is emitted inline in
  1726. order to retrieve an inserted primary key value.
  1727. """
  1728. return self._metadata.returns_rows
  1729. @property
  1730. def is_insert(self):
  1731. """True if this :class:`_engine.CursorResult` is the result
  1732. of a executing an expression language compiled
  1733. :func:`_expression.insert` construct.
  1734. When True, this implies that the
  1735. :attr:`inserted_primary_key` attribute is accessible,
  1736. assuming the statement did not include
  1737. a user defined "returning" construct.
  1738. """
  1739. return self.context.isinsert
  1740. def _fetchiter_impl(self):
  1741. fetchone = self.cursor_strategy.fetchone
  1742. while True:
  1743. row = fetchone(self, self.cursor)
  1744. if row is None:
  1745. break
  1746. yield row
  1747. def _fetchone_impl(self, hard_close=False):
  1748. return self.cursor_strategy.fetchone(self, self.cursor, hard_close)
  1749. def _fetchall_impl(self):
  1750. return self.cursor_strategy.fetchall(self, self.cursor)
  1751. def _fetchmany_impl(self, size=None):
  1752. return self.cursor_strategy.fetchmany(self, self.cursor, size)
  1753. def _raw_row_iterator(self):
  1754. return self._fetchiter_impl()
  1755. def merge(self, *others: Result[Any]) -> MergedResult[Any]:
  1756. merged_result = super().merge(*others)
  1757. if self.context._has_rowcount:
  1758. merged_result.rowcount = sum(
  1759. cast("CursorResult[Any]", result).rowcount
  1760. for result in (self,) + others
  1761. )
  1762. return merged_result
  1763. def close(self) -> Any:
  1764. """Close this :class:`_engine.CursorResult`.
  1765. This closes out the underlying DBAPI cursor corresponding to the
  1766. statement execution, if one is still present. Note that the DBAPI
  1767. cursor is automatically released when the :class:`_engine.CursorResult`
  1768. exhausts all available rows. :meth:`_engine.CursorResult.close` is
  1769. generally an optional method except in the case when discarding a
  1770. :class:`_engine.CursorResult` that still has additional rows pending
  1771. for fetch.
  1772. After this method is called, it is no longer valid to call upon
  1773. the fetch methods, which will raise a :class:`.ResourceClosedError`
  1774. on subsequent use.
  1775. .. seealso::
  1776. :ref:`connections_toplevel`
  1777. """
  1778. self._soft_close(hard=True)
  1779. @_generative
  1780. def yield_per(self, num: int) -> Self:
  1781. self._yield_per = num
  1782. self.cursor_strategy.yield_per(self, self.cursor, num)
  1783. return self
  1784. ResultProxy = CursorResult