hstore.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. # dialects/postgresql/hstore.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: ignore-errors
  8. import re
  9. from .array import ARRAY
  10. from .operators import CONTAINED_BY
  11. from .operators import CONTAINS
  12. from .operators import GETITEM
  13. from .operators import HAS_ALL
  14. from .operators import HAS_ANY
  15. from .operators import HAS_KEY
  16. from ... import types as sqltypes
  17. from ...sql import functions as sqlfunc
  18. __all__ = ("HSTORE", "hstore")
  19. class HSTORE(sqltypes.Indexable, sqltypes.Concatenable, sqltypes.TypeEngine):
  20. """Represent the PostgreSQL HSTORE type.
  21. The :class:`.HSTORE` type stores dictionaries containing strings, e.g.::
  22. data_table = Table(
  23. "data_table",
  24. metadata,
  25. Column("id", Integer, primary_key=True),
  26. Column("data", HSTORE),
  27. )
  28. with engine.connect() as conn:
  29. conn.execute(
  30. data_table.insert(), data={"key1": "value1", "key2": "value2"}
  31. )
  32. :class:`.HSTORE` provides for a wide range of operations, including:
  33. * Index operations::
  34. data_table.c.data["some key"] == "some value"
  35. * Containment operations::
  36. data_table.c.data.has_key("some key")
  37. data_table.c.data.has_all(["one", "two", "three"])
  38. * Concatenation::
  39. data_table.c.data + {"k1": "v1"}
  40. For a full list of special methods see
  41. :class:`.HSTORE.comparator_factory`.
  42. .. container:: topic
  43. **Detecting Changes in HSTORE columns when using the ORM**
  44. For usage with the SQLAlchemy ORM, it may be desirable to combine the
  45. usage of :class:`.HSTORE` with :class:`.MutableDict` dictionary now
  46. part of the :mod:`sqlalchemy.ext.mutable` extension. This extension
  47. will allow "in-place" changes to the dictionary, e.g. addition of new
  48. keys or replacement/removal of existing keys to/from the current
  49. dictionary, to produce events which will be detected by the unit of
  50. work::
  51. from sqlalchemy.ext.mutable import MutableDict
  52. class MyClass(Base):
  53. __tablename__ = "data_table"
  54. id = Column(Integer, primary_key=True)
  55. data = Column(MutableDict.as_mutable(HSTORE))
  56. my_object = session.query(MyClass).one()
  57. # in-place mutation, requires Mutable extension
  58. # in order for the ORM to detect
  59. my_object.data["some_key"] = "some value"
  60. session.commit()
  61. When the :mod:`sqlalchemy.ext.mutable` extension is not used, the ORM
  62. will not be alerted to any changes to the contents of an existing
  63. dictionary, unless that dictionary value is re-assigned to the
  64. HSTORE-attribute itself, thus generating a change event.
  65. .. seealso::
  66. :class:`.hstore` - render the PostgreSQL ``hstore()`` function.
  67. """ # noqa: E501
  68. __visit_name__ = "HSTORE"
  69. hashable = False
  70. text_type = sqltypes.Text()
  71. def __init__(self, text_type=None):
  72. """Construct a new :class:`.HSTORE`.
  73. :param text_type: the type that should be used for indexed values.
  74. Defaults to :class:`_types.Text`.
  75. """
  76. if text_type is not None:
  77. self.text_type = text_type
  78. class Comparator(
  79. sqltypes.Indexable.Comparator, sqltypes.Concatenable.Comparator
  80. ):
  81. """Define comparison operations for :class:`.HSTORE`."""
  82. def has_key(self, other):
  83. """Boolean expression. Test for presence of a key. Note that the
  84. key may be a SQLA expression.
  85. """
  86. return self.operate(HAS_KEY, other, result_type=sqltypes.Boolean)
  87. def has_all(self, other):
  88. """Boolean expression. Test for presence of all keys in jsonb"""
  89. return self.operate(HAS_ALL, other, result_type=sqltypes.Boolean)
  90. def has_any(self, other):
  91. """Boolean expression. Test for presence of any key in jsonb"""
  92. return self.operate(HAS_ANY, other, result_type=sqltypes.Boolean)
  93. def contains(self, other, **kwargs):
  94. """Boolean expression. Test if keys (or array) are a superset
  95. of/contained the keys of the argument jsonb expression.
  96. kwargs may be ignored by this operator but are required for API
  97. conformance.
  98. """
  99. return self.operate(CONTAINS, other, result_type=sqltypes.Boolean)
  100. def contained_by(self, other):
  101. """Boolean expression. Test if keys are a proper subset of the
  102. keys of the argument jsonb expression.
  103. """
  104. return self.operate(
  105. CONTAINED_BY, other, result_type=sqltypes.Boolean
  106. )
  107. def _setup_getitem(self, index):
  108. return GETITEM, index, self.type.text_type
  109. def defined(self, key):
  110. """Boolean expression. Test for presence of a non-NULL value for
  111. the key. Note that the key may be a SQLA expression.
  112. """
  113. return _HStoreDefinedFunction(self.expr, key)
  114. def delete(self, key):
  115. """HStore expression. Returns the contents of this hstore with the
  116. given key deleted. Note that the key may be a SQLA expression.
  117. """
  118. if isinstance(key, dict):
  119. key = _serialize_hstore(key)
  120. return _HStoreDeleteFunction(self.expr, key)
  121. def slice(self, array):
  122. """HStore expression. Returns a subset of an hstore defined by
  123. array of keys.
  124. """
  125. return _HStoreSliceFunction(self.expr, array)
  126. def keys(self):
  127. """Text array expression. Returns array of keys."""
  128. return _HStoreKeysFunction(self.expr)
  129. def vals(self):
  130. """Text array expression. Returns array of values."""
  131. return _HStoreValsFunction(self.expr)
  132. def array(self):
  133. """Text array expression. Returns array of alternating keys and
  134. values.
  135. """
  136. return _HStoreArrayFunction(self.expr)
  137. def matrix(self):
  138. """Text array expression. Returns array of [key, value] pairs."""
  139. return _HStoreMatrixFunction(self.expr)
  140. comparator_factory = Comparator
  141. def bind_processor(self, dialect):
  142. # note that dialect-specific types like that of psycopg and
  143. # psycopg2 will override this method to allow driver-level conversion
  144. # instead, see _PsycopgHStore
  145. def process(value):
  146. if isinstance(value, dict):
  147. return _serialize_hstore(value)
  148. else:
  149. return value
  150. return process
  151. def result_processor(self, dialect, coltype):
  152. # note that dialect-specific types like that of psycopg and
  153. # psycopg2 will override this method to allow driver-level conversion
  154. # instead, see _PsycopgHStore
  155. def process(value):
  156. if value is not None:
  157. return _parse_hstore(value)
  158. else:
  159. return value
  160. return process
  161. class hstore(sqlfunc.GenericFunction):
  162. """Construct an hstore value within a SQL expression using the
  163. PostgreSQL ``hstore()`` function.
  164. The :class:`.hstore` function accepts one or two arguments as described
  165. in the PostgreSQL documentation.
  166. E.g.::
  167. from sqlalchemy.dialects.postgresql import array, hstore
  168. select(hstore("key1", "value1"))
  169. select(
  170. hstore(
  171. array(["key1", "key2", "key3"]),
  172. array(["value1", "value2", "value3"]),
  173. )
  174. )
  175. .. seealso::
  176. :class:`.HSTORE` - the PostgreSQL ``HSTORE`` datatype.
  177. """
  178. type = HSTORE
  179. name = "hstore"
  180. inherit_cache = True
  181. class _HStoreDefinedFunction(sqlfunc.GenericFunction):
  182. type = sqltypes.Boolean
  183. name = "defined"
  184. inherit_cache = True
  185. class _HStoreDeleteFunction(sqlfunc.GenericFunction):
  186. type = HSTORE
  187. name = "delete"
  188. inherit_cache = True
  189. class _HStoreSliceFunction(sqlfunc.GenericFunction):
  190. type = HSTORE
  191. name = "slice"
  192. inherit_cache = True
  193. class _HStoreKeysFunction(sqlfunc.GenericFunction):
  194. type = ARRAY(sqltypes.Text)
  195. name = "akeys"
  196. inherit_cache = True
  197. class _HStoreValsFunction(sqlfunc.GenericFunction):
  198. type = ARRAY(sqltypes.Text)
  199. name = "avals"
  200. inherit_cache = True
  201. class _HStoreArrayFunction(sqlfunc.GenericFunction):
  202. type = ARRAY(sqltypes.Text)
  203. name = "hstore_to_array"
  204. inherit_cache = True
  205. class _HStoreMatrixFunction(sqlfunc.GenericFunction):
  206. type = ARRAY(sqltypes.Text)
  207. name = "hstore_to_matrix"
  208. inherit_cache = True
  209. #
  210. # parsing. note that none of this is used with the psycopg2 backend,
  211. # which provides its own native extensions.
  212. #
  213. # My best guess at the parsing rules of hstore literals, since no formal
  214. # grammar is given. This is mostly reverse engineered from PG's input parser
  215. # behavior.
  216. HSTORE_PAIR_RE = re.compile(
  217. r"""
  218. (
  219. "(?P<key> (\\ . | [^"])* )" # Quoted key
  220. )
  221. [ ]* => [ ]* # Pair operator, optional adjoining whitespace
  222. (
  223. (?P<value_null> NULL ) # NULL value
  224. | "(?P<value> (\\ . | [^"])* )" # Quoted value
  225. )
  226. """,
  227. re.VERBOSE,
  228. )
  229. HSTORE_DELIMITER_RE = re.compile(
  230. r"""
  231. [ ]* , [ ]*
  232. """,
  233. re.VERBOSE,
  234. )
  235. def _parse_error(hstore_str, pos):
  236. """format an unmarshalling error."""
  237. ctx = 20
  238. hslen = len(hstore_str)
  239. parsed_tail = hstore_str[max(pos - ctx - 1, 0) : min(pos, hslen)]
  240. residual = hstore_str[min(pos, hslen) : min(pos + ctx + 1, hslen)]
  241. if len(parsed_tail) > ctx:
  242. parsed_tail = "[...]" + parsed_tail[1:]
  243. if len(residual) > ctx:
  244. residual = residual[:-1] + "[...]"
  245. return "After %r, could not parse residual at position %d: %r" % (
  246. parsed_tail,
  247. pos,
  248. residual,
  249. )
  250. def _parse_hstore(hstore_str):
  251. """Parse an hstore from its literal string representation.
  252. Attempts to approximate PG's hstore input parsing rules as closely as
  253. possible. Although currently this is not strictly necessary, since the
  254. current implementation of hstore's output syntax is stricter than what it
  255. accepts as input, the documentation makes no guarantees that will always
  256. be the case.
  257. """
  258. result = {}
  259. pos = 0
  260. pair_match = HSTORE_PAIR_RE.match(hstore_str)
  261. while pair_match is not None:
  262. key = pair_match.group("key").replace(r"\"", '"').replace("\\\\", "\\")
  263. if pair_match.group("value_null"):
  264. value = None
  265. else:
  266. value = (
  267. pair_match.group("value")
  268. .replace(r"\"", '"')
  269. .replace("\\\\", "\\")
  270. )
  271. result[key] = value
  272. pos += pair_match.end()
  273. delim_match = HSTORE_DELIMITER_RE.match(hstore_str[pos:])
  274. if delim_match is not None:
  275. pos += delim_match.end()
  276. pair_match = HSTORE_PAIR_RE.match(hstore_str[pos:])
  277. if pos != len(hstore_str):
  278. raise ValueError(_parse_error(hstore_str, pos))
  279. return result
  280. def _serialize_hstore(val):
  281. """Serialize a dictionary into an hstore literal. Keys and values must
  282. both be strings (except None for values).
  283. """
  284. def esc(s, position):
  285. if position == "value" and s is None:
  286. return "NULL"
  287. elif isinstance(s, str):
  288. return '"%s"' % s.replace("\\", "\\\\").replace('"', r"\"")
  289. else:
  290. raise ValueError(
  291. "%r in %s position is not a string." % (s, position)
  292. )
  293. return ", ".join(
  294. "%s=>%s" % (esc(k, "key"), esc(v, "value")) for k, v in val.items()
  295. )