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

610 lines
18KB

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