row.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # engine/row.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """Define row constructs including :class:`.Row`."""
  8. import operator
  9. from .. import util
  10. from ..sql import util as sql_util
  11. from ..util.compat import collections_abc
  12. MD_INDEX = 0 # integer index in cursor.description
  13. # This reconstructor is necessary so that pickles with the C extension or
  14. # without use the same Binary format.
  15. try:
  16. # We need a different reconstructor on the C extension so that we can
  17. # add extra checks that fields have correctly been initialized by
  18. # __setstate__.
  19. from sqlalchemy.cresultproxy import safe_rowproxy_reconstructor
  20. # The extra function embedding is needed so that the
  21. # reconstructor function has the same signature whether or not
  22. # the extension is present.
  23. def rowproxy_reconstructor(cls, state):
  24. return safe_rowproxy_reconstructor(cls, state)
  25. except ImportError:
  26. def rowproxy_reconstructor(cls, state):
  27. obj = cls.__new__(cls)
  28. obj.__setstate__(state)
  29. return obj
  30. KEY_INTEGER_ONLY = 0
  31. """__getitem__ only allows integer values, raises TypeError otherwise"""
  32. KEY_OBJECTS_ONLY = 1
  33. """__getitem__ only allows string/object values, raises TypeError otherwise"""
  34. KEY_OBJECTS_BUT_WARN = 2
  35. """__getitem__ allows integer or string/object values, but emits a 2.0
  36. deprecation warning if string/object is passed"""
  37. KEY_OBJECTS_NO_WARN = 3
  38. """__getitem__ allows integer or string/object values with no warnings
  39. or errors."""
  40. try:
  41. from sqlalchemy.cresultproxy import BaseRow
  42. _baserow_usecext = True
  43. except ImportError:
  44. _baserow_usecext = False
  45. class BaseRow(object):
  46. __slots__ = ("_parent", "_data", "_keymap", "_key_style")
  47. def __init__(self, parent, processors, keymap, key_style, data):
  48. """Row objects are constructed by CursorResult objects."""
  49. object.__setattr__(self, "_parent", parent)
  50. if processors:
  51. object.__setattr__(
  52. self,
  53. "_data",
  54. tuple(
  55. [
  56. proc(value) if proc else value
  57. for proc, value in zip(processors, data)
  58. ]
  59. ),
  60. )
  61. else:
  62. object.__setattr__(self, "_data", tuple(data))
  63. object.__setattr__(self, "_keymap", keymap)
  64. object.__setattr__(self, "_key_style", key_style)
  65. def __reduce__(self):
  66. return (
  67. rowproxy_reconstructor,
  68. (self.__class__, self.__getstate__()),
  69. )
  70. def _filter_on_values(self, filters):
  71. return Row(
  72. self._parent,
  73. filters,
  74. self._keymap,
  75. self._key_style,
  76. self._data,
  77. )
  78. def _values_impl(self):
  79. return list(self)
  80. def __iter__(self):
  81. return iter(self._data)
  82. def __len__(self):
  83. return len(self._data)
  84. def __hash__(self):
  85. return hash(self._data)
  86. def _get_by_int_impl(self, key):
  87. return self._data[key]
  88. def _get_by_key_impl(self, key):
  89. if int in key.__class__.__mro__:
  90. return self._data[key]
  91. if self._key_style == KEY_INTEGER_ONLY:
  92. self._parent._raise_for_nonint(key)
  93. # the following is all LegacyRow support. none of this
  94. # should be called if not LegacyRow
  95. # assert isinstance(self, LegacyRow)
  96. try:
  97. rec = self._keymap[key]
  98. except KeyError as ke:
  99. rec = self._parent._key_fallback(key, ke)
  100. except TypeError:
  101. if isinstance(key, slice):
  102. return tuple(self._data[key])
  103. else:
  104. raise
  105. mdindex = rec[MD_INDEX]
  106. if mdindex is None:
  107. self._parent._raise_for_ambiguous_column_name(rec)
  108. elif self._key_style == KEY_OBJECTS_BUT_WARN and mdindex != key:
  109. self._parent._warn_for_nonint(key)
  110. return self._data[mdindex]
  111. # The original 1.4 plan was that Row would not allow row["str"]
  112. # access, however as the C extensions were inadvertently allowing
  113. # this coupled with the fact that orm Session sets future=True,
  114. # this allows a softer upgrade path. see #6218
  115. __getitem__ = _get_by_key_impl
  116. def _get_by_key_impl_mapping(self, key):
  117. try:
  118. rec = self._keymap[key]
  119. except KeyError as ke:
  120. rec = self._parent._key_fallback(key, ke)
  121. mdindex = rec[MD_INDEX]
  122. if mdindex is None:
  123. self._parent._raise_for_ambiguous_column_name(rec)
  124. elif (
  125. self._key_style == KEY_OBJECTS_ONLY
  126. and int in key.__class__.__mro__
  127. ):
  128. raise KeyError(key)
  129. return self._data[mdindex]
  130. def __getattr__(self, name):
  131. try:
  132. return self._get_by_key_impl_mapping(name)
  133. except KeyError as e:
  134. util.raise_(AttributeError(e.args[0]), replace_context=e)
  135. class Row(BaseRow, collections_abc.Sequence):
  136. """Represent a single result row.
  137. The :class:`.Row` object represents a row of a database result. It is
  138. typically associated in the 1.x series of SQLAlchemy with the
  139. :class:`_engine.CursorResult` object, however is also used by the ORM for
  140. tuple-like results as of SQLAlchemy 1.4.
  141. The :class:`.Row` object seeks to act as much like a Python named
  142. tuple as possible. For mapping (i.e. dictionary) behavior on a row,
  143. such as testing for containment of keys, refer to the :attr:`.Row._mapping`
  144. attribute.
  145. .. seealso::
  146. :ref:`coretutorial_selecting` - includes examples of selecting
  147. rows from SELECT statements.
  148. :class:`.LegacyRow` - Compatibility interface introduced in SQLAlchemy
  149. 1.4.
  150. .. versionchanged:: 1.4
  151. Renamed ``RowProxy`` to :class:`.Row`. :class:`.Row` is no longer a
  152. "proxy" object in that it contains the final form of data within it,
  153. and now acts mostly like a named tuple. Mapping-like functionality is
  154. moved to the :attr:`.Row._mapping` attribute, but will remain available
  155. in SQLAlchemy 1.x series via the :class:`.LegacyRow` class that is used
  156. by :class:`_engine.LegacyCursorResult`.
  157. See :ref:`change_4710_core` for background
  158. on this change.
  159. """
  160. __slots__ = ()
  161. # in 2.0, this should be KEY_INTEGER_ONLY
  162. _default_key_style = KEY_OBJECTS_BUT_WARN
  163. def __setattr__(self, name, value):
  164. raise AttributeError("can't set attribute")
  165. def __delattr__(self, name):
  166. raise AttributeError("can't delete attribute")
  167. @property
  168. def _mapping(self):
  169. """Return a :class:`.RowMapping` for this :class:`.Row`.
  170. This object provides a consistent Python mapping (i.e. dictionary)
  171. interface for the data contained within the row. The :class:`.Row`
  172. by itself behaves like a named tuple, however in the 1.4 series of
  173. SQLAlchemy, the :class:`.LegacyRow` class is still used by Core which
  174. continues to have mapping-like behaviors against the row object
  175. itself.
  176. .. seealso::
  177. :attr:`.Row._fields`
  178. .. versionadded:: 1.4
  179. """
  180. return RowMapping(
  181. self._parent,
  182. None,
  183. self._keymap,
  184. RowMapping._default_key_style,
  185. self._data,
  186. )
  187. def _special_name_accessor(name):
  188. """Handle ambiguous names such as "count" and "index" """
  189. @property
  190. def go(self):
  191. if self._parent._has_key(name):
  192. return self.__getattr__(name)
  193. else:
  194. def meth(*arg, **kw):
  195. return getattr(collections_abc.Sequence, name)(
  196. self, *arg, **kw
  197. )
  198. return meth
  199. return go
  200. count = _special_name_accessor("count")
  201. index = _special_name_accessor("index")
  202. def __contains__(self, key):
  203. return key in self._data
  204. def __getstate__(self):
  205. return {
  206. "_parent": self._parent,
  207. "_data": self._data,
  208. "_key_style": self._key_style,
  209. }
  210. def __setstate__(self, state):
  211. parent = state["_parent"]
  212. object.__setattr__(self, "_parent", parent)
  213. object.__setattr__(self, "_data", state["_data"])
  214. object.__setattr__(self, "_keymap", parent._keymap)
  215. object.__setattr__(self, "_key_style", state["_key_style"])
  216. def _op(self, other, op):
  217. return (
  218. op(tuple(self), tuple(other))
  219. if isinstance(other, Row)
  220. else op(tuple(self), other)
  221. )
  222. __hash__ = BaseRow.__hash__
  223. def __lt__(self, other):
  224. return self._op(other, operator.lt)
  225. def __le__(self, other):
  226. return self._op(other, operator.le)
  227. def __ge__(self, other):
  228. return self._op(other, operator.ge)
  229. def __gt__(self, other):
  230. return self._op(other, operator.gt)
  231. def __eq__(self, other):
  232. return self._op(other, operator.eq)
  233. def __ne__(self, other):
  234. return self._op(other, operator.ne)
  235. def __repr__(self):
  236. return repr(sql_util._repr_row(self))
  237. @util.deprecated_20(
  238. ":meth:`.Row.keys`",
  239. alternative="Use the namedtuple standard accessor "
  240. ":attr:`.Row._fields`, or for full mapping behavior use "
  241. "row._mapping.keys() ",
  242. )
  243. def keys(self):
  244. """Return the list of keys as strings represented by this
  245. :class:`.Row`.
  246. The keys can represent the labels of the columns returned by a core
  247. statement or the names of the orm classes returned by an orm
  248. execution.
  249. This method is analogous to the Python dictionary ``.keys()`` method,
  250. except that it returns a list, not an iterator.
  251. .. seealso::
  252. :attr:`.Row._fields`
  253. :attr:`.Row._mapping`
  254. """
  255. return self._parent.keys
  256. @property
  257. def _fields(self):
  258. """Return a tuple of string keys as represented by this
  259. :class:`.Row`.
  260. The keys can represent the labels of the columns returned by a core
  261. statement or the names of the orm classes returned by an orm
  262. execution.
  263. This attribute is analogous to the Python named tuple ``._fields``
  264. attribute.
  265. .. versionadded:: 1.4
  266. .. seealso::
  267. :attr:`.Row._mapping`
  268. """
  269. return tuple([k for k in self._parent.keys if k is not None])
  270. def _asdict(self):
  271. """Return a new dict which maps field names to their corresponding
  272. values.
  273. This method is analogous to the Python named tuple ``._asdict()``
  274. method, and works by applying the ``dict()`` constructor to the
  275. :attr:`.Row._mapping` attribute.
  276. .. versionadded:: 1.4
  277. .. seealso::
  278. :attr:`.Row._mapping`
  279. """
  280. return dict(self._mapping)
  281. def _replace(self):
  282. raise NotImplementedError()
  283. @property
  284. def _field_defaults(self):
  285. raise NotImplementedError()
  286. class LegacyRow(Row):
  287. """A subclass of :class:`.Row` that delivers 1.x SQLAlchemy behaviors
  288. for Core.
  289. The :class:`.LegacyRow` class is where most of the Python mapping
  290. (i.e. dictionary-like)
  291. behaviors are implemented for the row object. The mapping behavior
  292. of :class:`.Row` going forward is accessible via the :class:`.Row._mapping`
  293. attribute.
  294. .. versionadded:: 1.4 - added :class:`.LegacyRow` which encapsulates most
  295. of the deprecated behaviors of :class:`.Row`.
  296. """
  297. __slots__ = ()
  298. if util.SQLALCHEMY_WARN_20:
  299. _default_key_style = KEY_OBJECTS_BUT_WARN
  300. else:
  301. _default_key_style = KEY_OBJECTS_NO_WARN
  302. def __contains__(self, key):
  303. return self._parent._contains(key, self)
  304. # prior to #6218, LegacyRow would redirect the behavior of __getitem__
  305. # for the non C version of BaseRow. This is now set up by Python BaseRow
  306. # in all cases
  307. # if not _baserow_usecext:
  308. # __getitem__ = BaseRow._get_by_key_impl
  309. @util.deprecated(
  310. "1.4",
  311. "The :meth:`.LegacyRow.has_key` method is deprecated and will be "
  312. "removed in a future release. To test for key membership, use "
  313. "the :attr:`Row._mapping` attribute, i.e. 'key in row._mapping`.",
  314. )
  315. def has_key(self, key):
  316. """Return True if this :class:`.LegacyRow` contains the given key.
  317. Through the SQLAlchemy 1.x series, the ``__contains__()`` method of
  318. :class:`.Row` (or :class:`.LegacyRow` as of SQLAlchemy 1.4) also links
  319. to :meth:`.Row.has_key`, in that an expression such as ::
  320. "some_col" in row
  321. Will return True if the row contains a column named ``"some_col"``,
  322. in the way that a Python mapping works.
  323. However, it is planned that the 2.0 series of SQLAlchemy will reverse
  324. this behavior so that ``__contains__()`` will refer to a value being
  325. present in the row, in the way that a Python tuple works.
  326. .. seealso::
  327. :ref:`change_4710_core`
  328. """
  329. return self._parent._has_key(key)
  330. @util.deprecated(
  331. "1.4",
  332. "The :meth:`.LegacyRow.items` method is deprecated and will be "
  333. "removed in a future release. Use the :attr:`Row._mapping` "
  334. "attribute, i.e., 'row._mapping.items()'.",
  335. )
  336. def items(self):
  337. """Return a list of tuples, each tuple containing a key/value pair.
  338. This method is analogous to the Python dictionary ``.items()`` method,
  339. except that it returns a list, not an iterator.
  340. """
  341. return [(key, self[key]) for key in self.keys()]
  342. @util.deprecated(
  343. "1.4",
  344. "The :meth:`.LegacyRow.iterkeys` method is deprecated and will be "
  345. "removed in a future release. Use the :attr:`Row._mapping` "
  346. "attribute, i.e., 'row._mapping.keys()'.",
  347. )
  348. def iterkeys(self):
  349. """Return a an iterator against the :meth:`.Row.keys` method.
  350. This method is analogous to the Python-2-only dictionary
  351. ``.iterkeys()`` method.
  352. """
  353. return iter(self._parent.keys)
  354. @util.deprecated(
  355. "1.4",
  356. "The :meth:`.LegacyRow.itervalues` method is deprecated and will be "
  357. "removed in a future release. Use the :attr:`Row._mapping` "
  358. "attribute, i.e., 'row._mapping.values()'.",
  359. )
  360. def itervalues(self):
  361. """Return a an iterator against the :meth:`.Row.values` method.
  362. This method is analogous to the Python-2-only dictionary
  363. ``.itervalues()`` method.
  364. """
  365. return iter(self)
  366. @util.deprecated(
  367. "1.4",
  368. "The :meth:`.LegacyRow.values` method is deprecated and will be "
  369. "removed in a future release. Use the :attr:`Row._mapping` "
  370. "attribute, i.e., 'row._mapping.values()'.",
  371. )
  372. def values(self):
  373. """Return the values represented by this :class:`.Row` as a list.
  374. This method is analogous to the Python dictionary ``.values()`` method,
  375. except that it returns a list, not an iterator.
  376. """
  377. return self._values_impl()
  378. BaseRowProxy = BaseRow
  379. RowProxy = Row
  380. class ROMappingView(
  381. collections_abc.KeysView,
  382. collections_abc.ValuesView,
  383. collections_abc.ItemsView,
  384. ):
  385. __slots__ = (
  386. "_mapping",
  387. "_items",
  388. )
  389. def __init__(self, mapping, items):
  390. self._mapping = mapping
  391. self._items = items
  392. def __len__(self):
  393. return len(self._items)
  394. def __repr__(self):
  395. return "{0.__class__.__name__}({0._mapping!r})".format(self)
  396. def __iter__(self):
  397. return iter(self._items)
  398. def __contains__(self, item):
  399. return item in self._items
  400. def __eq__(self, other):
  401. return list(other) == list(self)
  402. def __ne__(self, other):
  403. return list(other) != list(self)
  404. class RowMapping(BaseRow, collections_abc.Mapping):
  405. """A ``Mapping`` that maps column names and objects to :class:`.Row` values.
  406. The :class:`.RowMapping` is available from a :class:`.Row` via the
  407. :attr:`.Row._mapping` attribute, as well as from the iterable interface
  408. provided by the :class:`.MappingResult` object returned by the
  409. :meth:`_engine.Result.mappings` method.
  410. :class:`.RowMapping` supplies Python mapping (i.e. dictionary) access to
  411. the contents of the row. This includes support for testing of
  412. containment of specific keys (string column names or objects), as well
  413. as iteration of keys, values, and items::
  414. for row in result:
  415. if 'a' in row._mapping:
  416. print("Column 'a': %s" % row._mapping['a'])
  417. print("Column b: %s" % row._mapping[table.c.b])
  418. .. versionadded:: 1.4 The :class:`.RowMapping` object replaces the
  419. mapping-like access previously provided by a database result row,
  420. which now seeks to behave mostly like a named tuple.
  421. """
  422. __slots__ = ()
  423. _default_key_style = KEY_OBJECTS_ONLY
  424. if not _baserow_usecext:
  425. __getitem__ = BaseRow._get_by_key_impl_mapping
  426. def _values_impl(self):
  427. return list(self._data)
  428. def __iter__(self):
  429. return (k for k in self._parent.keys if k is not None)
  430. def __len__(self):
  431. return len(self._data)
  432. def __contains__(self, key):
  433. return self._parent._has_key(key)
  434. def __repr__(self):
  435. return repr(dict(self))
  436. def items(self):
  437. """Return a view of key/value tuples for the elements in the
  438. underlying :class:`.Row`.
  439. """
  440. return ROMappingView(self, [(key, self[key]) for key in self.keys()])
  441. def keys(self):
  442. """Return a view of 'keys' for string column names represented
  443. by the underlying :class:`.Row`.
  444. """
  445. return self._parent.keys
  446. def values(self):
  447. """Return a view of values for the values represented in the
  448. underlying :class:`.Row`.
  449. """
  450. return ROMappingView(self, self._values_impl())