SQLAlchemy 2.0.36__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. SQLAlchemy-2.0.36.dist-info/LICENSE +19 -0
  2. SQLAlchemy-2.0.36.dist-info/METADATA +243 -0
  3. SQLAlchemy-2.0.36.dist-info/RECORD +273 -0
  4. SQLAlchemy-2.0.36.dist-info/WHEEL +5 -0
  5. SQLAlchemy-2.0.36.dist-info/top_level.txt +1 -0
  6. sqlalchemy/__init__.py +294 -0
  7. sqlalchemy/connectors/__init__.py +18 -0
  8. sqlalchemy/connectors/aioodbc.py +174 -0
  9. sqlalchemy/connectors/asyncio.py +213 -0
  10. sqlalchemy/connectors/pyodbc.py +249 -0
  11. sqlalchemy/cyextension/__init__.py +6 -0
  12. sqlalchemy/cyextension/collections.cp313-win_amd64.pyd +0 -0
  13. sqlalchemy/cyextension/collections.pyx +409 -0
  14. sqlalchemy/cyextension/immutabledict.cp313-win_amd64.pyd +0 -0
  15. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  16. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  17. sqlalchemy/cyextension/processors.cp313-win_amd64.pyd +0 -0
  18. sqlalchemy/cyextension/processors.pyx +68 -0
  19. sqlalchemy/cyextension/resultproxy.cp313-win_amd64.pyd +0 -0
  20. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  21. sqlalchemy/cyextension/util.cp313-win_amd64.pyd +0 -0
  22. sqlalchemy/cyextension/util.pyx +91 -0
  23. sqlalchemy/dialects/__init__.py +61 -0
  24. sqlalchemy/dialects/_typing.py +25 -0
  25. sqlalchemy/dialects/mssql/__init__.py +88 -0
  26. sqlalchemy/dialects/mssql/aioodbc.py +64 -0
  27. sqlalchemy/dialects/mssql/base.py +4010 -0
  28. sqlalchemy/dialects/mssql/information_schema.py +254 -0
  29. sqlalchemy/dialects/mssql/json.py +133 -0
  30. sqlalchemy/dialects/mssql/provision.py +162 -0
  31. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  32. sqlalchemy/dialects/mssql/pyodbc.py +745 -0
  33. sqlalchemy/dialects/mysql/__init__.py +101 -0
  34. sqlalchemy/dialects/mysql/aiomysql.py +333 -0
  35. sqlalchemy/dialects/mysql/asyncmy.py +337 -0
  36. sqlalchemy/dialects/mysql/base.py +3494 -0
  37. sqlalchemy/dialects/mysql/cymysql.py +84 -0
  38. sqlalchemy/dialects/mysql/dml.py +219 -0
  39. sqlalchemy/dialects/mysql/enumerated.py +244 -0
  40. sqlalchemy/dialects/mysql/expression.py +141 -0
  41. sqlalchemy/dialects/mysql/json.py +81 -0
  42. sqlalchemy/dialects/mysql/mariadb.py +32 -0
  43. sqlalchemy/dialects/mysql/mariadbconnector.py +277 -0
  44. sqlalchemy/dialects/mysql/mysqlconnector.py +180 -0
  45. sqlalchemy/dialects/mysql/mysqldb.py +303 -0
  46. sqlalchemy/dialects/mysql/provision.py +110 -0
  47. sqlalchemy/dialects/mysql/pymysql.py +137 -0
  48. sqlalchemy/dialects/mysql/pyodbc.py +138 -0
  49. sqlalchemy/dialects/mysql/reflection.py +677 -0
  50. sqlalchemy/dialects/mysql/reserved_words.py +571 -0
  51. sqlalchemy/dialects/mysql/types.py +774 -0
  52. sqlalchemy/dialects/oracle/__init__.py +67 -0
  53. sqlalchemy/dialects/oracle/base.py +3271 -0
  54. sqlalchemy/dialects/oracle/cx_oracle.py +1483 -0
  55. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  56. sqlalchemy/dialects/oracle/oracledb.py +431 -0
  57. sqlalchemy/dialects/oracle/provision.py +220 -0
  58. sqlalchemy/dialects/oracle/types.py +287 -0
  59. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  60. sqlalchemy/dialects/postgresql/_psycopg_common.py +187 -0
  61. sqlalchemy/dialects/postgresql/array.py +425 -0
  62. sqlalchemy/dialects/postgresql/asyncpg.py +1274 -0
  63. sqlalchemy/dialects/postgresql/base.py +5008 -0
  64. sqlalchemy/dialects/postgresql/dml.py +310 -0
  65. sqlalchemy/dialects/postgresql/ext.py +496 -0
  66. sqlalchemy/dialects/postgresql/hstore.py +397 -0
  67. sqlalchemy/dialects/postgresql/json.py +333 -0
  68. sqlalchemy/dialects/postgresql/named_types.py +509 -0
  69. sqlalchemy/dialects/postgresql/operators.py +129 -0
  70. sqlalchemy/dialects/postgresql/pg8000.py +662 -0
  71. sqlalchemy/dialects/postgresql/pg_catalog.py +300 -0
  72. sqlalchemy/dialects/postgresql/provision.py +175 -0
  73. sqlalchemy/dialects/postgresql/psycopg.py +772 -0
  74. sqlalchemy/dialects/postgresql/psycopg2.py +886 -0
  75. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  76. sqlalchemy/dialects/postgresql/ranges.py +1029 -0
  77. sqlalchemy/dialects/postgresql/types.py +303 -0
  78. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  79. sqlalchemy/dialects/sqlite/aiosqlite.py +396 -0
  80. sqlalchemy/dialects/sqlite/base.py +2805 -0
  81. sqlalchemy/dialects/sqlite/dml.py +240 -0
  82. sqlalchemy/dialects/sqlite/json.py +92 -0
  83. sqlalchemy/dialects/sqlite/provision.py +198 -0
  84. sqlalchemy/dialects/sqlite/pysqlcipher.py +155 -0
  85. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  86. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  87. sqlalchemy/engine/__init__.py +62 -0
  88. sqlalchemy/engine/_py_processors.py +136 -0
  89. sqlalchemy/engine/_py_row.py +128 -0
  90. sqlalchemy/engine/_py_util.py +74 -0
  91. sqlalchemy/engine/base.py +3375 -0
  92. sqlalchemy/engine/characteristics.py +155 -0
  93. sqlalchemy/engine/create.py +875 -0
  94. sqlalchemy/engine/cursor.py +2181 -0
  95. sqlalchemy/engine/default.py +2365 -0
  96. sqlalchemy/engine/events.py +951 -0
  97. sqlalchemy/engine/interfaces.py +3403 -0
  98. sqlalchemy/engine/mock.py +131 -0
  99. sqlalchemy/engine/processors.py +61 -0
  100. sqlalchemy/engine/reflection.py +2098 -0
  101. sqlalchemy/engine/result.py +2382 -0
  102. sqlalchemy/engine/row.py +401 -0
  103. sqlalchemy/engine/strategies.py +19 -0
  104. sqlalchemy/engine/url.py +910 -0
  105. sqlalchemy/engine/util.py +167 -0
  106. sqlalchemy/event/__init__.py +25 -0
  107. sqlalchemy/event/api.py +225 -0
  108. sqlalchemy/event/attr.py +655 -0
  109. sqlalchemy/event/base.py +470 -0
  110. sqlalchemy/event/legacy.py +246 -0
  111. sqlalchemy/event/registry.py +386 -0
  112. sqlalchemy/events.py +17 -0
  113. sqlalchemy/exc.py +830 -0
  114. sqlalchemy/ext/__init__.py +11 -0
  115. sqlalchemy/ext/associationproxy.py +2013 -0
  116. sqlalchemy/ext/asyncio/__init__.py +25 -0
  117. sqlalchemy/ext/asyncio/base.py +279 -0
  118. sqlalchemy/ext/asyncio/engine.py +1466 -0
  119. sqlalchemy/ext/asyncio/exc.py +21 -0
  120. sqlalchemy/ext/asyncio/result.py +961 -0
  121. sqlalchemy/ext/asyncio/scoping.py +1614 -0
  122. sqlalchemy/ext/asyncio/session.py +1936 -0
  123. sqlalchemy/ext/automap.py +1691 -0
  124. sqlalchemy/ext/baked.py +574 -0
  125. sqlalchemy/ext/compiler.py +570 -0
  126. sqlalchemy/ext/declarative/__init__.py +65 -0
  127. sqlalchemy/ext/declarative/extensions.py +548 -0
  128. sqlalchemy/ext/horizontal_shard.py +481 -0
  129. sqlalchemy/ext/hybrid.py +1514 -0
  130. sqlalchemy/ext/indexable.py +341 -0
  131. sqlalchemy/ext/instrumentation.py +450 -0
  132. sqlalchemy/ext/mutable.py +1073 -0
  133. sqlalchemy/ext/mypy/__init__.py +6 -0
  134. sqlalchemy/ext/mypy/apply.py +320 -0
  135. sqlalchemy/ext/mypy/decl_class.py +515 -0
  136. sqlalchemy/ext/mypy/infer.py +590 -0
  137. sqlalchemy/ext/mypy/names.py +335 -0
  138. sqlalchemy/ext/mypy/plugin.py +303 -0
  139. sqlalchemy/ext/mypy/util.py +357 -0
  140. sqlalchemy/ext/orderinglist.py +416 -0
  141. sqlalchemy/ext/serializer.py +181 -0
  142. sqlalchemy/future/__init__.py +16 -0
  143. sqlalchemy/future/engine.py +15 -0
  144. sqlalchemy/inspection.py +174 -0
  145. sqlalchemy/log.py +288 -0
  146. sqlalchemy/orm/__init__.py +170 -0
  147. sqlalchemy/orm/_orm_constructors.py +2571 -0
  148. sqlalchemy/orm/_typing.py +179 -0
  149. sqlalchemy/orm/attributes.py +2835 -0
  150. sqlalchemy/orm/base.py +973 -0
  151. sqlalchemy/orm/bulk_persistence.py +2123 -0
  152. sqlalchemy/orm/clsregistry.py +571 -0
  153. sqlalchemy/orm/collections.py +1620 -0
  154. sqlalchemy/orm/context.py +3268 -0
  155. sqlalchemy/orm/decl_api.py +1883 -0
  156. sqlalchemy/orm/decl_base.py +2190 -0
  157. sqlalchemy/orm/dependency.py +1304 -0
  158. sqlalchemy/orm/descriptor_props.py +1076 -0
  159. sqlalchemy/orm/dynamic.py +300 -0
  160. sqlalchemy/orm/evaluator.py +379 -0
  161. sqlalchemy/orm/events.py +3261 -0
  162. sqlalchemy/orm/exc.py +228 -0
  163. sqlalchemy/orm/identity.py +302 -0
  164. sqlalchemy/orm/instrumentation.py +754 -0
  165. sqlalchemy/orm/interfaces.py +1474 -0
  166. sqlalchemy/orm/loading.py +1682 -0
  167. sqlalchemy/orm/mapped_collection.py +557 -0
  168. sqlalchemy/orm/mapper.py +4432 -0
  169. sqlalchemy/orm/path_registry.py +811 -0
  170. sqlalchemy/orm/persistence.py +1782 -0
  171. sqlalchemy/orm/properties.py +886 -0
  172. sqlalchemy/orm/query.py +3396 -0
  173. sqlalchemy/orm/relationships.py +3500 -0
  174. sqlalchemy/orm/scoping.py +2165 -0
  175. sqlalchemy/orm/session.py +5301 -0
  176. sqlalchemy/orm/state.py +1143 -0
  177. sqlalchemy/orm/state_changes.py +198 -0
  178. sqlalchemy/orm/strategies.py +3473 -0
  179. sqlalchemy/orm/strategy_options.py +2569 -0
  180. sqlalchemy/orm/sync.py +164 -0
  181. sqlalchemy/orm/unitofwork.py +796 -0
  182. sqlalchemy/orm/util.py +2424 -0
  183. sqlalchemy/orm/writeonly.py +678 -0
  184. sqlalchemy/pool/__init__.py +44 -0
  185. sqlalchemy/pool/base.py +1515 -0
  186. sqlalchemy/pool/events.py +370 -0
  187. sqlalchemy/pool/impl.py +581 -0
  188. sqlalchemy/py.typed +0 -0
  189. sqlalchemy/schema.py +70 -0
  190. sqlalchemy/sql/__init__.py +145 -0
  191. sqlalchemy/sql/_dml_constructors.py +140 -0
  192. sqlalchemy/sql/_elements_constructors.py +1850 -0
  193. sqlalchemy/sql/_orm_types.py +20 -0
  194. sqlalchemy/sql/_py_util.py +75 -0
  195. sqlalchemy/sql/_selectable_constructors.py +635 -0
  196. sqlalchemy/sql/_typing.py +460 -0
  197. sqlalchemy/sql/annotation.py +585 -0
  198. sqlalchemy/sql/base.py +2185 -0
  199. sqlalchemy/sql/cache_key.py +1057 -0
  200. sqlalchemy/sql/coercions.py +1405 -0
  201. sqlalchemy/sql/compiler.py +7818 -0
  202. sqlalchemy/sql/crud.py +1669 -0
  203. sqlalchemy/sql/ddl.py +1378 -0
  204. sqlalchemy/sql/default_comparator.py +552 -0
  205. sqlalchemy/sql/dml.py +1817 -0
  206. sqlalchemy/sql/elements.py +5499 -0
  207. sqlalchemy/sql/events.py +455 -0
  208. sqlalchemy/sql/expression.py +162 -0
  209. sqlalchemy/sql/functions.py +2055 -0
  210. sqlalchemy/sql/lambdas.py +1449 -0
  211. sqlalchemy/sql/naming.py +212 -0
  212. sqlalchemy/sql/operators.py +2579 -0
  213. sqlalchemy/sql/roles.py +323 -0
  214. sqlalchemy/sql/schema.py +6158 -0
  215. sqlalchemy/sql/selectable.py +7004 -0
  216. sqlalchemy/sql/sqltypes.py +3827 -0
  217. sqlalchemy/sql/traversals.py +1024 -0
  218. sqlalchemy/sql/type_api.py +2339 -0
  219. sqlalchemy/sql/util.py +1486 -0
  220. sqlalchemy/sql/visitors.py +1165 -0
  221. sqlalchemy/testing/__init__.py +96 -0
  222. sqlalchemy/testing/assertions.py +989 -0
  223. sqlalchemy/testing/assertsql.py +516 -0
  224. sqlalchemy/testing/asyncio.py +135 -0
  225. sqlalchemy/testing/config.py +427 -0
  226. sqlalchemy/testing/engines.py +472 -0
  227. sqlalchemy/testing/entities.py +117 -0
  228. sqlalchemy/testing/exclusions.py +435 -0
  229. sqlalchemy/testing/fixtures/__init__.py +28 -0
  230. sqlalchemy/testing/fixtures/base.py +366 -0
  231. sqlalchemy/testing/fixtures/mypy.py +312 -0
  232. sqlalchemy/testing/fixtures/orm.py +227 -0
  233. sqlalchemy/testing/fixtures/sql.py +503 -0
  234. sqlalchemy/testing/pickleable.py +155 -0
  235. sqlalchemy/testing/plugin/__init__.py +6 -0
  236. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  237. sqlalchemy/testing/plugin/plugin_base.py +779 -0
  238. sqlalchemy/testing/plugin/pytestplugin.py +868 -0
  239. sqlalchemy/testing/profiling.py +324 -0
  240. sqlalchemy/testing/provision.py +496 -0
  241. sqlalchemy/testing/requirements.py +1818 -0
  242. sqlalchemy/testing/schema.py +224 -0
  243. sqlalchemy/testing/suite/__init__.py +19 -0
  244. sqlalchemy/testing/suite/test_cte.py +211 -0
  245. sqlalchemy/testing/suite/test_ddl.py +389 -0
  246. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  247. sqlalchemy/testing/suite/test_dialect.py +740 -0
  248. sqlalchemy/testing/suite/test_insert.py +630 -0
  249. sqlalchemy/testing/suite/test_reflection.py +3225 -0
  250. sqlalchemy/testing/suite/test_results.py +502 -0
  251. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  252. sqlalchemy/testing/suite/test_select.py +1999 -0
  253. sqlalchemy/testing/suite/test_sequence.py +317 -0
  254. sqlalchemy/testing/suite/test_types.py +2141 -0
  255. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  256. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  257. sqlalchemy/testing/util.py +537 -0
  258. sqlalchemy/testing/warnings.py +52 -0
  259. sqlalchemy/types.py +76 -0
  260. sqlalchemy/util/__init__.py +160 -0
  261. sqlalchemy/util/_collections.py +715 -0
  262. sqlalchemy/util/_concurrency_py3k.py +288 -0
  263. sqlalchemy/util/_has_cy.py +40 -0
  264. sqlalchemy/util/_py_collections.py +541 -0
  265. sqlalchemy/util/compat.py +301 -0
  266. sqlalchemy/util/concurrency.py +108 -0
  267. sqlalchemy/util/deprecations.py +401 -0
  268. sqlalchemy/util/langhelpers.py +2218 -0
  269. sqlalchemy/util/preloaded.py +150 -0
  270. sqlalchemy/util/queue.py +322 -0
  271. sqlalchemy/util/tool_support.py +201 -0
  272. sqlalchemy/util/topological.py +120 -0
  273. sqlalchemy/util/typing.py +629 -0
@@ -0,0 +1,3827 @@
1
+ # sql/sqltypes.py
2
+ # Copyright (C) 2005-2024 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
+
9
+ """SQL specific types.
10
+
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import collections.abc as collections_abc
15
+ import datetime as dt
16
+ import decimal
17
+ import enum
18
+ import json
19
+ import pickle
20
+ from typing import Any
21
+ from typing import Callable
22
+ from typing import cast
23
+ from typing import Dict
24
+ from typing import List
25
+ from typing import Optional
26
+ from typing import overload
27
+ from typing import Sequence
28
+ from typing import Tuple
29
+ from typing import Type
30
+ from typing import TYPE_CHECKING
31
+ from typing import TypeVar
32
+ from typing import Union
33
+ from uuid import UUID as _python_UUID
34
+
35
+ from . import coercions
36
+ from . import elements
37
+ from . import operators
38
+ from . import roles
39
+ from . import type_api
40
+ from .base import _NONE_NAME
41
+ from .base import NO_ARG
42
+ from .base import SchemaEventTarget
43
+ from .cache_key import HasCacheKey
44
+ from .elements import quoted_name
45
+ from .elements import Slice
46
+ from .elements import TypeCoerce as type_coerce # noqa
47
+ from .type_api import Emulated
48
+ from .type_api import NativeForEmulated # noqa
49
+ from .type_api import to_instance as to_instance
50
+ from .type_api import TypeDecorator as TypeDecorator
51
+ from .type_api import TypeEngine as TypeEngine
52
+ from .type_api import TypeEngineMixin
53
+ from .type_api import Variant # noqa
54
+ from .visitors import InternalTraversal
55
+ from .. import event
56
+ from .. import exc
57
+ from .. import inspection
58
+ from .. import util
59
+ from ..engine import processors
60
+ from ..util import langhelpers
61
+ from ..util import OrderedDict
62
+ from ..util.typing import is_literal
63
+ from ..util.typing import Literal
64
+ from ..util.typing import typing_get_args
65
+
66
+ if TYPE_CHECKING:
67
+ from ._typing import _ColumnExpressionArgument
68
+ from ._typing import _TypeEngineArgument
69
+ from .operators import OperatorType
70
+ from .schema import MetaData
71
+ from .type_api import _BindProcessorType
72
+ from .type_api import _ComparatorFactory
73
+ from .type_api import _MatchedOnType
74
+ from .type_api import _ResultProcessorType
75
+ from ..engine.interfaces import Dialect
76
+
77
+ _T = TypeVar("_T", bound="Any")
78
+ _CT = TypeVar("_CT", bound=Any)
79
+ _TE = TypeVar("_TE", bound="TypeEngine[Any]")
80
+
81
+
82
+ class HasExpressionLookup(TypeEngineMixin):
83
+ """Mixin expression adaptations based on lookup tables.
84
+
85
+ These rules are currently used by the numeric, integer and date types
86
+ which have detailed cross-expression coercion rules.
87
+
88
+ """
89
+
90
+ @property
91
+ def _expression_adaptations(self):
92
+ raise NotImplementedError()
93
+
94
+ class Comparator(TypeEngine.Comparator[_CT]):
95
+ __slots__ = ()
96
+
97
+ _blank_dict = util.EMPTY_DICT
98
+
99
+ def _adapt_expression(
100
+ self,
101
+ op: OperatorType,
102
+ other_comparator: TypeEngine.Comparator[Any],
103
+ ) -> Tuple[OperatorType, TypeEngine[Any]]:
104
+ othertype = other_comparator.type._type_affinity
105
+ if TYPE_CHECKING:
106
+ assert isinstance(self.type, HasExpressionLookup)
107
+ lookup = self.type._expression_adaptations.get(
108
+ op, self._blank_dict
109
+ ).get(othertype, self.type)
110
+ if lookup is othertype:
111
+ return (op, other_comparator.type)
112
+ elif lookup is self.type._type_affinity:
113
+ return (op, self.type)
114
+ else:
115
+ return (op, to_instance(lookup))
116
+
117
+ comparator_factory: _ComparatorFactory[Any] = Comparator
118
+
119
+
120
+ class Concatenable(TypeEngineMixin):
121
+ """A mixin that marks a type as supporting 'concatenation',
122
+ typically strings."""
123
+
124
+ class Comparator(TypeEngine.Comparator[_T]):
125
+ __slots__ = ()
126
+
127
+ def _adapt_expression(
128
+ self,
129
+ op: OperatorType,
130
+ other_comparator: TypeEngine.Comparator[Any],
131
+ ) -> Tuple[OperatorType, TypeEngine[Any]]:
132
+ if op is operators.add and isinstance(
133
+ other_comparator,
134
+ (Concatenable.Comparator, NullType.Comparator),
135
+ ):
136
+ return operators.concat_op, self.expr.type
137
+ else:
138
+ return super()._adapt_expression(op, other_comparator)
139
+
140
+ comparator_factory: _ComparatorFactory[Any] = Comparator
141
+
142
+
143
+ class Indexable(TypeEngineMixin):
144
+ """A mixin that marks a type as supporting indexing operations,
145
+ such as array or JSON structures.
146
+
147
+ """
148
+
149
+ class Comparator(TypeEngine.Comparator[_T]):
150
+ __slots__ = ()
151
+
152
+ def _setup_getitem(self, index):
153
+ raise NotImplementedError()
154
+
155
+ def __getitem__(self, index):
156
+ (
157
+ adjusted_op,
158
+ adjusted_right_expr,
159
+ result_type,
160
+ ) = self._setup_getitem(index)
161
+ return self.operate(
162
+ adjusted_op, adjusted_right_expr, result_type=result_type
163
+ )
164
+
165
+ comparator_factory: _ComparatorFactory[Any] = Comparator
166
+
167
+
168
+ class String(Concatenable, TypeEngine[str]):
169
+ """The base for all string and character types.
170
+
171
+ In SQL, corresponds to VARCHAR.
172
+
173
+ The `length` field is usually required when the `String` type is
174
+ used within a CREATE TABLE statement, as VARCHAR requires a length
175
+ on most databases.
176
+
177
+ """
178
+
179
+ __visit_name__ = "string"
180
+
181
+ def __init__(
182
+ self,
183
+ length: Optional[int] = None,
184
+ collation: Optional[str] = None,
185
+ ):
186
+ """
187
+ Create a string-holding type.
188
+
189
+ :param length: optional, a length for the column for use in
190
+ DDL and CAST expressions. May be safely omitted if no ``CREATE
191
+ TABLE`` will be issued. Certain databases may require a
192
+ ``length`` for use in DDL, and will raise an exception when
193
+ the ``CREATE TABLE`` DDL is issued if a ``VARCHAR``
194
+ with no length is included. Whether the value is
195
+ interpreted as bytes or characters is database specific.
196
+
197
+ :param collation: Optional, a column-level collation for
198
+ use in DDL and CAST expressions. Renders using the
199
+ COLLATE keyword supported by SQLite, MySQL, and PostgreSQL.
200
+ E.g.:
201
+
202
+ .. sourcecode:: pycon+sql
203
+
204
+ >>> from sqlalchemy import cast, select, String
205
+ >>> print(select(cast('some string', String(collation='utf8'))))
206
+ {printsql}SELECT CAST(:param_1 AS VARCHAR COLLATE utf8) AS anon_1
207
+
208
+ .. note::
209
+
210
+ In most cases, the :class:`.Unicode` or :class:`.UnicodeText`
211
+ datatypes should be used for a :class:`_schema.Column` that expects
212
+ to store non-ascii data. These datatypes will ensure that the
213
+ correct types are used on the database.
214
+
215
+ """
216
+
217
+ self.length = length
218
+ self.collation = collation
219
+
220
+ def _with_collation(self, collation):
221
+ new_type = self.copy()
222
+ new_type.collation = collation
223
+ return new_type
224
+
225
+ def _resolve_for_literal(self, value):
226
+ # I was SO PROUD of my regex trick, but we dont need it.
227
+ # re.search(r"[^\u0000-\u007F]", value)
228
+
229
+ if value.isascii():
230
+ return _STRING
231
+ else:
232
+ return _UNICODE
233
+
234
+ def literal_processor(self, dialect):
235
+ def process(value):
236
+ value = value.replace("'", "''")
237
+
238
+ if dialect.identifier_preparer._double_percents:
239
+ value = value.replace("%", "%%")
240
+
241
+ return "'%s'" % value
242
+
243
+ return process
244
+
245
+ def bind_processor(self, dialect):
246
+ return None
247
+
248
+ def result_processor(self, dialect, coltype):
249
+ return None
250
+
251
+ @property
252
+ def python_type(self):
253
+ return str
254
+
255
+ def get_dbapi_type(self, dbapi):
256
+ return dbapi.STRING
257
+
258
+
259
+ class Text(String):
260
+ """A variably sized string type.
261
+
262
+ In SQL, usually corresponds to CLOB or TEXT. In general, TEXT objects
263
+ do not have a length; while some databases will accept a length
264
+ argument here, it will be rejected by others.
265
+
266
+ """
267
+
268
+ __visit_name__ = "text"
269
+
270
+
271
+ class Unicode(String):
272
+ """A variable length Unicode string type.
273
+
274
+ The :class:`.Unicode` type is a :class:`.String` subclass that assumes
275
+ input and output strings that may contain non-ASCII characters, and for
276
+ some backends implies an underlying column type that is explicitly
277
+ supporting of non-ASCII data, such as ``NVARCHAR`` on Oracle and SQL
278
+ Server. This will impact the output of ``CREATE TABLE`` statements and
279
+ ``CAST`` functions at the dialect level.
280
+
281
+ The character encoding used by the :class:`.Unicode` type that is used to
282
+ transmit and receive data to the database is usually determined by the
283
+ DBAPI itself. All modern DBAPIs accommodate non-ASCII strings but may have
284
+ different methods of managing database encodings; if necessary, this
285
+ encoding should be configured as detailed in the notes for the target DBAPI
286
+ in the :ref:`dialect_toplevel` section.
287
+
288
+ In modern SQLAlchemy, use of the :class:`.Unicode` datatype does not
289
+ imply any encoding/decoding behavior within SQLAlchemy itself. In Python
290
+ 3, all string objects are inherently Unicode capable, and SQLAlchemy
291
+ does not produce bytestring objects nor does it accommodate a DBAPI that
292
+ does not return Python Unicode objects in result sets for string values.
293
+
294
+ .. warning:: Some database backends, particularly SQL Server with pyodbc,
295
+ are known to have undesirable behaviors regarding data that is noted
296
+ as being of ``NVARCHAR`` type as opposed to ``VARCHAR``, including
297
+ datatype mismatch errors and non-use of indexes. See the section
298
+ on :meth:`.DialectEvents.do_setinputsizes` for background on working
299
+ around unicode character issues for backends like SQL Server with
300
+ pyodbc as well as cx_Oracle.
301
+
302
+ .. seealso::
303
+
304
+ :class:`.UnicodeText` - unlengthed textual counterpart
305
+ to :class:`.Unicode`.
306
+
307
+ :meth:`.DialectEvents.do_setinputsizes`
308
+
309
+
310
+ """
311
+
312
+ __visit_name__ = "unicode"
313
+
314
+
315
+ class UnicodeText(Text):
316
+ """An unbounded-length Unicode string type.
317
+
318
+ See :class:`.Unicode` for details on the unicode
319
+ behavior of this object.
320
+
321
+ Like :class:`.Unicode`, usage the :class:`.UnicodeText` type implies a
322
+ unicode-capable type being used on the backend, such as
323
+ ``NCLOB``, ``NTEXT``.
324
+
325
+ """
326
+
327
+ __visit_name__ = "unicode_text"
328
+
329
+
330
+ class Integer(HasExpressionLookup, TypeEngine[int]):
331
+ """A type for ``int`` integers."""
332
+
333
+ __visit_name__ = "integer"
334
+
335
+ if TYPE_CHECKING:
336
+
337
+ @util.ro_memoized_property
338
+ def _type_affinity(self) -> Type[Integer]: ...
339
+
340
+ def get_dbapi_type(self, dbapi):
341
+ return dbapi.NUMBER
342
+
343
+ @property
344
+ def python_type(self):
345
+ return int
346
+
347
+ def _resolve_for_literal(self, value):
348
+ if value.bit_length() >= 32:
349
+ return _BIGINTEGER
350
+ else:
351
+ return self
352
+
353
+ def literal_processor(self, dialect):
354
+ def process(value):
355
+ return str(int(value))
356
+
357
+ return process
358
+
359
+ @util.memoized_property
360
+ def _expression_adaptations(self):
361
+ return {
362
+ operators.add: {
363
+ Date: Date,
364
+ Integer: self.__class__,
365
+ Numeric: Numeric,
366
+ },
367
+ operators.mul: {
368
+ Interval: Interval,
369
+ Integer: self.__class__,
370
+ Numeric: Numeric,
371
+ },
372
+ operators.truediv: {Integer: Numeric, Numeric: Numeric},
373
+ operators.floordiv: {Integer: self.__class__, Numeric: Numeric},
374
+ operators.sub: {Integer: self.__class__, Numeric: Numeric},
375
+ }
376
+
377
+
378
+ class SmallInteger(Integer):
379
+ """A type for smaller ``int`` integers.
380
+
381
+ Typically generates a ``SMALLINT`` in DDL, and otherwise acts like
382
+ a normal :class:`.Integer` on the Python side.
383
+
384
+ """
385
+
386
+ __visit_name__ = "small_integer"
387
+
388
+
389
+ class BigInteger(Integer):
390
+ """A type for bigger ``int`` integers.
391
+
392
+ Typically generates a ``BIGINT`` in DDL, and otherwise acts like
393
+ a normal :class:`.Integer` on the Python side.
394
+
395
+ """
396
+
397
+ __visit_name__ = "big_integer"
398
+
399
+
400
+ _N = TypeVar("_N", bound=Union[decimal.Decimal, float])
401
+
402
+
403
+ class Numeric(HasExpressionLookup, TypeEngine[_N]):
404
+ """Base for non-integer numeric types, such as
405
+ ``NUMERIC``, ``FLOAT``, ``DECIMAL``, and other variants.
406
+
407
+ The :class:`.Numeric` datatype when used directly will render DDL
408
+ corresponding to precision numerics if available, such as
409
+ ``NUMERIC(precision, scale)``. The :class:`.Float` subclass will
410
+ attempt to render a floating-point datatype such as ``FLOAT(precision)``.
411
+
412
+ :class:`.Numeric` returns Python ``decimal.Decimal`` objects by default,
413
+ based on the default value of ``True`` for the
414
+ :paramref:`.Numeric.asdecimal` parameter. If this parameter is set to
415
+ False, returned values are coerced to Python ``float`` objects.
416
+
417
+ The :class:`.Float` subtype, being more specific to floating point,
418
+ defaults the :paramref:`.Float.asdecimal` flag to False so that the
419
+ default Python datatype is ``float``.
420
+
421
+ .. note::
422
+
423
+ When using a :class:`.Numeric` datatype against a database type that
424
+ returns Python floating point values to the driver, the accuracy of the
425
+ decimal conversion indicated by :paramref:`.Numeric.asdecimal` may be
426
+ limited. The behavior of specific numeric/floating point datatypes
427
+ is a product of the SQL datatype in use, the Python :term:`DBAPI`
428
+ in use, as well as strategies that may be present within
429
+ the SQLAlchemy dialect in use. Users requiring specific precision/
430
+ scale are encouraged to experiment with the available datatypes
431
+ in order to determine the best results.
432
+
433
+ """
434
+
435
+ __visit_name__ = "numeric"
436
+
437
+ if TYPE_CHECKING:
438
+
439
+ @util.ro_memoized_property
440
+ def _type_affinity(self) -> Type[Numeric[_N]]: ...
441
+
442
+ _default_decimal_return_scale = 10
443
+
444
+ @overload
445
+ def __init__(
446
+ self: Numeric[decimal.Decimal],
447
+ precision: Optional[int] = ...,
448
+ scale: Optional[int] = ...,
449
+ decimal_return_scale: Optional[int] = ...,
450
+ asdecimal: Literal[True] = ...,
451
+ ): ...
452
+
453
+ @overload
454
+ def __init__(
455
+ self: Numeric[float],
456
+ precision: Optional[int] = ...,
457
+ scale: Optional[int] = ...,
458
+ decimal_return_scale: Optional[int] = ...,
459
+ asdecimal: Literal[False] = ...,
460
+ ): ...
461
+
462
+ def __init__(
463
+ self,
464
+ precision: Optional[int] = None,
465
+ scale: Optional[int] = None,
466
+ decimal_return_scale: Optional[int] = None,
467
+ asdecimal: bool = True,
468
+ ):
469
+ """
470
+ Construct a Numeric.
471
+
472
+ :param precision: the numeric precision for use in DDL ``CREATE
473
+ TABLE``.
474
+
475
+ :param scale: the numeric scale for use in DDL ``CREATE TABLE``.
476
+
477
+ :param asdecimal: default True. Return whether or not
478
+ values should be sent as Python Decimal objects, or
479
+ as floats. Different DBAPIs send one or the other based on
480
+ datatypes - the Numeric type will ensure that return values
481
+ are one or the other across DBAPIs consistently.
482
+
483
+ :param decimal_return_scale: Default scale to use when converting
484
+ from floats to Python decimals. Floating point values will typically
485
+ be much longer due to decimal inaccuracy, and most floating point
486
+ database types don't have a notion of "scale", so by default the
487
+ float type looks for the first ten decimal places when converting.
488
+ Specifying this value will override that length. Types which
489
+ do include an explicit ".scale" value, such as the base
490
+ :class:`.Numeric` as well as the MySQL float types, will use the
491
+ value of ".scale" as the default for decimal_return_scale, if not
492
+ otherwise specified.
493
+
494
+ When using the ``Numeric`` type, care should be taken to ensure
495
+ that the asdecimal setting is appropriate for the DBAPI in use -
496
+ when Numeric applies a conversion from Decimal->float or float->
497
+ Decimal, this conversion incurs an additional performance overhead
498
+ for all result columns received.
499
+
500
+ DBAPIs that return Decimal natively (e.g. psycopg2) will have
501
+ better accuracy and higher performance with a setting of ``True``,
502
+ as the native translation to Decimal reduces the amount of floating-
503
+ point issues at play, and the Numeric type itself doesn't need
504
+ to apply any further conversions. However, another DBAPI which
505
+ returns floats natively *will* incur an additional conversion
506
+ overhead, and is still subject to floating point data loss - in
507
+ which case ``asdecimal=False`` will at least remove the extra
508
+ conversion overhead.
509
+
510
+ """
511
+ self.precision = precision
512
+ self.scale = scale
513
+ self.decimal_return_scale = decimal_return_scale
514
+ self.asdecimal = asdecimal
515
+
516
+ @property
517
+ def _effective_decimal_return_scale(self):
518
+ if self.decimal_return_scale is not None:
519
+ return self.decimal_return_scale
520
+ elif getattr(self, "scale", None) is not None:
521
+ return self.scale
522
+ else:
523
+ return self._default_decimal_return_scale
524
+
525
+ def get_dbapi_type(self, dbapi):
526
+ return dbapi.NUMBER
527
+
528
+ def literal_processor(self, dialect):
529
+ def process(value):
530
+ return str(value)
531
+
532
+ return process
533
+
534
+ @property
535
+ def python_type(self):
536
+ if self.asdecimal:
537
+ return decimal.Decimal
538
+ else:
539
+ return float
540
+
541
+ def bind_processor(self, dialect):
542
+ if dialect.supports_native_decimal:
543
+ return None
544
+ else:
545
+ return processors.to_float
546
+
547
+ def result_processor(self, dialect, coltype):
548
+ if self.asdecimal:
549
+ if dialect.supports_native_decimal:
550
+ # we're a "numeric", DBAPI will give us Decimal directly
551
+ return None
552
+ else:
553
+ # we're a "numeric", DBAPI returns floats, convert.
554
+ return processors.to_decimal_processor_factory(
555
+ decimal.Decimal,
556
+ (
557
+ self.scale
558
+ if self.scale is not None
559
+ else self._default_decimal_return_scale
560
+ ),
561
+ )
562
+ else:
563
+ if dialect.supports_native_decimal:
564
+ return processors.to_float
565
+ else:
566
+ return None
567
+
568
+ @util.memoized_property
569
+ def _expression_adaptations(self):
570
+ return {
571
+ operators.mul: {
572
+ Interval: Interval,
573
+ Numeric: self.__class__,
574
+ Integer: self.__class__,
575
+ },
576
+ operators.truediv: {
577
+ Numeric: self.__class__,
578
+ Integer: self.__class__,
579
+ },
580
+ operators.add: {Numeric: self.__class__, Integer: self.__class__},
581
+ operators.sub: {Numeric: self.__class__, Integer: self.__class__},
582
+ }
583
+
584
+
585
+ class Float(Numeric[_N]):
586
+ """Type representing floating point types, such as ``FLOAT`` or ``REAL``.
587
+
588
+ This type returns Python ``float`` objects by default, unless the
589
+ :paramref:`.Float.asdecimal` flag is set to ``True``, in which case they
590
+ are coerced to ``decimal.Decimal`` objects.
591
+
592
+ When a :paramref:`.Float.precision` is not provided in a
593
+ :class:`_types.Float` type some backend may compile this type as
594
+ an 8 bytes / 64 bit float datatype. To use a 4 bytes / 32 bit float
595
+ datatype a precision <= 24 can usually be provided or the
596
+ :class:`_types.REAL` type can be used.
597
+ This is known to be the case in the PostgreSQL and MSSQL dialects
598
+ that render the type as ``FLOAT`` that's in both an alias of
599
+ ``DOUBLE PRECISION``. Other third party dialects may have similar
600
+ behavior.
601
+ """
602
+
603
+ __visit_name__ = "float"
604
+
605
+ scale = None
606
+
607
+ @overload
608
+ def __init__(
609
+ self: Float[float],
610
+ precision: Optional[int] = ...,
611
+ asdecimal: Literal[False] = ...,
612
+ decimal_return_scale: Optional[int] = ...,
613
+ ): ...
614
+
615
+ @overload
616
+ def __init__(
617
+ self: Float[decimal.Decimal],
618
+ precision: Optional[int] = ...,
619
+ asdecimal: Literal[True] = ...,
620
+ decimal_return_scale: Optional[int] = ...,
621
+ ): ...
622
+
623
+ def __init__(
624
+ self: Float[_N],
625
+ precision: Optional[int] = None,
626
+ asdecimal: bool = False,
627
+ decimal_return_scale: Optional[int] = None,
628
+ ):
629
+ r"""
630
+ Construct a Float.
631
+
632
+ :param precision: the numeric precision for use in DDL ``CREATE
633
+ TABLE``. Backends **should** attempt to ensure this precision
634
+ indicates a number of digits for the generic
635
+ :class:`_sqltypes.Float` datatype.
636
+
637
+ .. note:: For the Oracle backend, the
638
+ :paramref:`_sqltypes.Float.precision` parameter is not accepted
639
+ when rendering DDL, as Oracle does not support float precision
640
+ specified as a number of decimal places. Instead, use the
641
+ Oracle-specific :class:`_oracle.FLOAT` datatype and specify the
642
+ :paramref:`_oracle.FLOAT.binary_precision` parameter. This is new
643
+ in version 2.0 of SQLAlchemy.
644
+
645
+ To create a database agnostic :class:`_types.Float` that
646
+ separately specifies binary precision for Oracle, use
647
+ :meth:`_types.TypeEngine.with_variant` as follows::
648
+
649
+ from sqlalchemy import Column
650
+ from sqlalchemy import Float
651
+ from sqlalchemy.dialects import oracle
652
+
653
+ Column(
654
+ "float_data",
655
+ Float(5).with_variant(oracle.FLOAT(binary_precision=16), "oracle")
656
+ )
657
+
658
+ :param asdecimal: the same flag as that of :class:`.Numeric`, but
659
+ defaults to ``False``. Note that setting this flag to ``True``
660
+ results in floating point conversion.
661
+
662
+ :param decimal_return_scale: Default scale to use when converting
663
+ from floats to Python decimals. Floating point values will typically
664
+ be much longer due to decimal inaccuracy, and most floating point
665
+ database types don't have a notion of "scale", so by default the
666
+ float type looks for the first ten decimal places when converting.
667
+ Specifying this value will override that length. Note that the
668
+ MySQL float types, which do include "scale", will use "scale"
669
+ as the default for decimal_return_scale, if not otherwise specified.
670
+
671
+ """ # noqa: E501
672
+ self.precision = precision
673
+ self.asdecimal = asdecimal
674
+ self.decimal_return_scale = decimal_return_scale
675
+
676
+ def result_processor(self, dialect, coltype):
677
+ if self.asdecimal:
678
+ return processors.to_decimal_processor_factory(
679
+ decimal.Decimal, self._effective_decimal_return_scale
680
+ )
681
+ elif dialect.supports_native_decimal:
682
+ return processors.to_float
683
+ else:
684
+ return None
685
+
686
+
687
+ class Double(Float[_N]):
688
+ """A type for double ``FLOAT`` floating point types.
689
+
690
+ Typically generates a ``DOUBLE`` or ``DOUBLE_PRECISION`` in DDL,
691
+ and otherwise acts like a normal :class:`.Float` on the Python
692
+ side.
693
+
694
+ .. versionadded:: 2.0
695
+
696
+ """
697
+
698
+ __visit_name__ = "double"
699
+
700
+
701
+ class _RenderISO8601NoT:
702
+ def _literal_processor_datetime(self, dialect):
703
+ return self._literal_processor_portion(dialect, None)
704
+
705
+ def _literal_processor_date(self, dialect):
706
+ return self._literal_processor_portion(dialect, 0)
707
+
708
+ def _literal_processor_time(self, dialect):
709
+ return self._literal_processor_portion(dialect, -1)
710
+
711
+ def _literal_processor_portion(self, dialect, _portion=None):
712
+ assert _portion in (None, 0, -1)
713
+ if _portion is not None:
714
+
715
+ def process(value):
716
+ return f"""'{value.isoformat().split("T")[_portion]}'"""
717
+
718
+ else:
719
+
720
+ def process(value):
721
+ return f"""'{value.isoformat().replace("T", " ")}'"""
722
+
723
+ return process
724
+
725
+
726
+ class DateTime(
727
+ _RenderISO8601NoT, HasExpressionLookup, TypeEngine[dt.datetime]
728
+ ):
729
+ """A type for ``datetime.datetime()`` objects.
730
+
731
+ Date and time types return objects from the Python ``datetime``
732
+ module. Most DBAPIs have built in support for the datetime
733
+ module, with the noted exception of SQLite. In the case of
734
+ SQLite, date and time types are stored as strings which are then
735
+ converted back to datetime objects when rows are returned.
736
+
737
+ For the time representation within the datetime type, some
738
+ backends include additional options, such as timezone support and
739
+ fractional seconds support. For fractional seconds, use the
740
+ dialect-specific datatype, such as :class:`.mysql.TIME`. For
741
+ timezone support, use at least the :class:`_types.TIMESTAMP` datatype,
742
+ if not the dialect-specific datatype object.
743
+
744
+ """
745
+
746
+ __visit_name__ = "datetime"
747
+
748
+ def __init__(self, timezone: bool = False):
749
+ """Construct a new :class:`.DateTime`.
750
+
751
+ :param timezone: boolean. Indicates that the datetime type should
752
+ enable timezone support, if available on the
753
+ **base date/time-holding type only**. It is recommended
754
+ to make use of the :class:`_types.TIMESTAMP` datatype directly when
755
+ using this flag, as some databases include separate generic
756
+ date/time-holding types distinct from the timezone-capable
757
+ TIMESTAMP datatype, such as Oracle.
758
+
759
+
760
+ """
761
+ self.timezone = timezone
762
+
763
+ def get_dbapi_type(self, dbapi):
764
+ return dbapi.DATETIME
765
+
766
+ def _resolve_for_literal(self, value):
767
+ with_timezone = value.tzinfo is not None
768
+ if with_timezone and not self.timezone:
769
+ return DATETIME_TIMEZONE
770
+ else:
771
+ return self
772
+
773
+ def literal_processor(self, dialect):
774
+ return self._literal_processor_datetime(dialect)
775
+
776
+ @property
777
+ def python_type(self):
778
+ return dt.datetime
779
+
780
+ @util.memoized_property
781
+ def _expression_adaptations(self):
782
+ # Based on
783
+ # https://www.postgresql.org/docs/current/static/functions-datetime.html.
784
+
785
+ return {
786
+ operators.add: {Interval: self.__class__},
787
+ operators.sub: {Interval: self.__class__, DateTime: Interval},
788
+ }
789
+
790
+
791
+ class Date(_RenderISO8601NoT, HasExpressionLookup, TypeEngine[dt.date]):
792
+ """A type for ``datetime.date()`` objects."""
793
+
794
+ __visit_name__ = "date"
795
+
796
+ def get_dbapi_type(self, dbapi):
797
+ return dbapi.DATETIME
798
+
799
+ @property
800
+ def python_type(self):
801
+ return dt.date
802
+
803
+ def literal_processor(self, dialect):
804
+ return self._literal_processor_date(dialect)
805
+
806
+ @util.memoized_property
807
+ def _expression_adaptations(self):
808
+ # Based on
809
+ # https://www.postgresql.org/docs/current/static/functions-datetime.html.
810
+
811
+ return {
812
+ operators.add: {
813
+ Integer: self.__class__,
814
+ Interval: DateTime,
815
+ Time: DateTime,
816
+ },
817
+ operators.sub: {
818
+ # date - integer = date
819
+ Integer: self.__class__,
820
+ # date - date = integer.
821
+ Date: Integer,
822
+ Interval: DateTime,
823
+ # date - datetime = interval,
824
+ # this one is not in the PG docs
825
+ # but works
826
+ DateTime: Interval,
827
+ },
828
+ }
829
+
830
+
831
+ class Time(_RenderISO8601NoT, HasExpressionLookup, TypeEngine[dt.time]):
832
+ """A type for ``datetime.time()`` objects."""
833
+
834
+ __visit_name__ = "time"
835
+
836
+ def __init__(self, timezone: bool = False):
837
+ self.timezone = timezone
838
+
839
+ def get_dbapi_type(self, dbapi):
840
+ return dbapi.DATETIME
841
+
842
+ @property
843
+ def python_type(self):
844
+ return dt.time
845
+
846
+ def _resolve_for_literal(self, value):
847
+ with_timezone = value.tzinfo is not None
848
+ if with_timezone and not self.timezone:
849
+ return TIME_TIMEZONE
850
+ else:
851
+ return self
852
+
853
+ @util.memoized_property
854
+ def _expression_adaptations(self):
855
+ # Based on
856
+ # https://www.postgresql.org/docs/current/static/functions-datetime.html.
857
+
858
+ return {
859
+ operators.add: {Date: DateTime, Interval: self.__class__},
860
+ operators.sub: {Time: Interval, Interval: self.__class__},
861
+ }
862
+
863
+ def literal_processor(self, dialect):
864
+ return self._literal_processor_time(dialect)
865
+
866
+
867
+ class _Binary(TypeEngine[bytes]):
868
+ """Define base behavior for binary types."""
869
+
870
+ def __init__(self, length: Optional[int] = None):
871
+ self.length = length
872
+
873
+ @util.ro_memoized_property
874
+ def _generic_type_affinity(
875
+ self,
876
+ ) -> Type[TypeEngine[bytes]]:
877
+ return LargeBinary
878
+
879
+ def literal_processor(self, dialect):
880
+ def process(value):
881
+ # TODO: this is useless for real world scenarios; implement
882
+ # real binary literals
883
+ value = value.decode(
884
+ dialect._legacy_binary_type_literal_encoding
885
+ ).replace("'", "''")
886
+ return "'%s'" % value
887
+
888
+ return process
889
+
890
+ @property
891
+ def python_type(self):
892
+ return bytes
893
+
894
+ # Python 3 - sqlite3 doesn't need the `Binary` conversion
895
+ # here, though pg8000 does to indicate "bytea"
896
+ def bind_processor(self, dialect):
897
+ if dialect.dbapi is None:
898
+ return None
899
+
900
+ DBAPIBinary = dialect.dbapi.Binary
901
+
902
+ def process(value):
903
+ if value is not None:
904
+ return DBAPIBinary(value)
905
+ else:
906
+ return None
907
+
908
+ return process
909
+
910
+ # Python 3 has native bytes() type
911
+ # both sqlite3 and pg8000 seem to return it,
912
+ # psycopg2 as of 2.5 returns 'memoryview'
913
+ def result_processor(self, dialect, coltype):
914
+ if dialect.returns_native_bytes:
915
+ return None
916
+
917
+ def process(value):
918
+ if value is not None:
919
+ value = bytes(value)
920
+ return value
921
+
922
+ return process
923
+
924
+ def coerce_compared_value(self, op, value):
925
+ """See :meth:`.TypeEngine.coerce_compared_value` for a description."""
926
+
927
+ if isinstance(value, str):
928
+ return self
929
+ else:
930
+ return super().coerce_compared_value(op, value)
931
+
932
+ def get_dbapi_type(self, dbapi):
933
+ return dbapi.BINARY
934
+
935
+
936
+ class LargeBinary(_Binary):
937
+ """A type for large binary byte data.
938
+
939
+ The :class:`.LargeBinary` type corresponds to a large and/or unlengthed
940
+ binary type for the target platform, such as BLOB on MySQL and BYTEA for
941
+ PostgreSQL. It also handles the necessary conversions for the DBAPI.
942
+
943
+ """
944
+
945
+ __visit_name__ = "large_binary"
946
+
947
+ def __init__(self, length: Optional[int] = None):
948
+ """
949
+ Construct a LargeBinary type.
950
+
951
+ :param length: optional, a length for the column for use in
952
+ DDL statements, for those binary types that accept a length,
953
+ such as the MySQL BLOB type.
954
+
955
+ """
956
+ _Binary.__init__(self, length=length)
957
+
958
+
959
+ class SchemaType(SchemaEventTarget, TypeEngineMixin):
960
+ """Add capabilities to a type which allow for schema-level DDL to be
961
+ associated with a type.
962
+
963
+ Supports types that must be explicitly created/dropped (i.e. PG ENUM type)
964
+ as well as types that are complimented by table or schema level
965
+ constraints, triggers, and other rules.
966
+
967
+ :class:`.SchemaType` classes can also be targets for the
968
+ :meth:`.DDLEvents.before_parent_attach` and
969
+ :meth:`.DDLEvents.after_parent_attach` events, where the events fire off
970
+ surrounding the association of the type object with a parent
971
+ :class:`_schema.Column`.
972
+
973
+ .. seealso::
974
+
975
+ :class:`.Enum`
976
+
977
+ :class:`.Boolean`
978
+
979
+
980
+ """
981
+
982
+ _use_schema_map = True
983
+
984
+ name: Optional[str]
985
+
986
+ def __init__(
987
+ self,
988
+ name: Optional[str] = None,
989
+ schema: Optional[str] = None,
990
+ metadata: Optional[MetaData] = None,
991
+ inherit_schema: bool = False,
992
+ quote: Optional[bool] = None,
993
+ _create_events: bool = True,
994
+ _adapted_from: Optional[SchemaType] = None,
995
+ ):
996
+ if name is not None:
997
+ self.name = quoted_name(name, quote)
998
+ else:
999
+ self.name = None
1000
+ self.schema = schema
1001
+ self.metadata = metadata
1002
+ self.inherit_schema = inherit_schema
1003
+ self._create_events = _create_events
1004
+
1005
+ if _create_events and self.metadata:
1006
+ event.listen(
1007
+ self.metadata,
1008
+ "before_create",
1009
+ util.portable_instancemethod(self._on_metadata_create),
1010
+ )
1011
+ event.listen(
1012
+ self.metadata,
1013
+ "after_drop",
1014
+ util.portable_instancemethod(self._on_metadata_drop),
1015
+ )
1016
+
1017
+ if _adapted_from:
1018
+ self.dispatch = self.dispatch._join(_adapted_from.dispatch)
1019
+
1020
+ def _set_parent(self, parent, **kw):
1021
+ # set parent hook is when this type is associated with a column.
1022
+ # Column calls it for all SchemaEventTarget instances, either the
1023
+ # base type and/or variants in _variant_mapping.
1024
+
1025
+ # we want to register a second hook to trigger when that column is
1026
+ # associated with a table. in that event, we and all of our variants
1027
+ # may want to set up some state on the table such as a CheckConstraint
1028
+ # that will conditionally render at DDL render time.
1029
+
1030
+ # the base SchemaType also sets up events for
1031
+ # on_table/metadata_create/drop in this method, which is used by
1032
+ # "native" types with a separate CREATE/DROP e.g. Postgresql.ENUM
1033
+
1034
+ parent._on_table_attach(util.portable_instancemethod(self._set_table))
1035
+
1036
+ def _variant_mapping_for_set_table(self, column):
1037
+ if column.type._variant_mapping:
1038
+ variant_mapping = dict(column.type._variant_mapping)
1039
+ variant_mapping["_default"] = column.type
1040
+ else:
1041
+ variant_mapping = None
1042
+ return variant_mapping
1043
+
1044
+ def _set_table(self, column, table):
1045
+ if self.inherit_schema:
1046
+ self.schema = table.schema
1047
+ elif self.metadata and self.schema is None and self.metadata.schema:
1048
+ self.schema = self.metadata.schema
1049
+
1050
+ if not self._create_events:
1051
+ return
1052
+
1053
+ variant_mapping = self._variant_mapping_for_set_table(column)
1054
+
1055
+ event.listen(
1056
+ table,
1057
+ "before_create",
1058
+ util.portable_instancemethod(
1059
+ self._on_table_create, {"variant_mapping": variant_mapping}
1060
+ ),
1061
+ )
1062
+ event.listen(
1063
+ table,
1064
+ "after_drop",
1065
+ util.portable_instancemethod(
1066
+ self._on_table_drop, {"variant_mapping": variant_mapping}
1067
+ ),
1068
+ )
1069
+ if self.metadata is None:
1070
+ # if SchemaType were created w/ a metadata argument, these
1071
+ # events would already have been associated with that metadata
1072
+ # and would preclude an association with table.metadata
1073
+ event.listen(
1074
+ table.metadata,
1075
+ "before_create",
1076
+ util.portable_instancemethod(
1077
+ self._on_metadata_create,
1078
+ {"variant_mapping": variant_mapping},
1079
+ ),
1080
+ )
1081
+ event.listen(
1082
+ table.metadata,
1083
+ "after_drop",
1084
+ util.portable_instancemethod(
1085
+ self._on_metadata_drop,
1086
+ {"variant_mapping": variant_mapping},
1087
+ ),
1088
+ )
1089
+
1090
+ def copy(self, **kw):
1091
+ return self.adapt(
1092
+ cast("Type[TypeEngine[Any]]", self.__class__),
1093
+ _create_events=True,
1094
+ metadata=(
1095
+ kw.get("_to_metadata", self.metadata)
1096
+ if self.metadata is not None
1097
+ else None
1098
+ ),
1099
+ )
1100
+
1101
+ @overload
1102
+ def adapt(self, cls: Type[_TE], **kw: Any) -> _TE: ...
1103
+
1104
+ @overload
1105
+ def adapt(
1106
+ self, cls: Type[TypeEngineMixin], **kw: Any
1107
+ ) -> TypeEngine[Any]: ...
1108
+
1109
+ def adapt(
1110
+ self, cls: Type[Union[TypeEngine[Any], TypeEngineMixin]], **kw: Any
1111
+ ) -> TypeEngine[Any]:
1112
+ kw.setdefault("_create_events", False)
1113
+ kw.setdefault("_adapted_from", self)
1114
+ return super().adapt(cls, **kw)
1115
+
1116
+ def create(self, bind, checkfirst=False):
1117
+ """Issue CREATE DDL for this type, if applicable."""
1118
+
1119
+ t = self.dialect_impl(bind.dialect)
1120
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1121
+ t.create(bind, checkfirst=checkfirst)
1122
+
1123
+ def drop(self, bind, checkfirst=False):
1124
+ """Issue DROP DDL for this type, if applicable."""
1125
+
1126
+ t = self.dialect_impl(bind.dialect)
1127
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1128
+ t.drop(bind, checkfirst=checkfirst)
1129
+
1130
+ def _on_table_create(self, target, bind, **kw):
1131
+ if not self._is_impl_for_variant(bind.dialect, kw):
1132
+ return
1133
+
1134
+ t = self.dialect_impl(bind.dialect)
1135
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1136
+ t._on_table_create(target, bind, **kw)
1137
+
1138
+ def _on_table_drop(self, target, bind, **kw):
1139
+ if not self._is_impl_for_variant(bind.dialect, kw):
1140
+ return
1141
+
1142
+ t = self.dialect_impl(bind.dialect)
1143
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1144
+ t._on_table_drop(target, bind, **kw)
1145
+
1146
+ def _on_metadata_create(self, target, bind, **kw):
1147
+ if not self._is_impl_for_variant(bind.dialect, kw):
1148
+ return
1149
+
1150
+ t = self.dialect_impl(bind.dialect)
1151
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1152
+ t._on_metadata_create(target, bind, **kw)
1153
+
1154
+ def _on_metadata_drop(self, target, bind, **kw):
1155
+ if not self._is_impl_for_variant(bind.dialect, kw):
1156
+ return
1157
+
1158
+ t = self.dialect_impl(bind.dialect)
1159
+ if isinstance(t, SchemaType) and t.__class__ is not self.__class__:
1160
+ t._on_metadata_drop(target, bind, **kw)
1161
+
1162
+ def _is_impl_for_variant(self, dialect, kw):
1163
+ variant_mapping = kw.pop("variant_mapping", None)
1164
+
1165
+ if not variant_mapping:
1166
+ return True
1167
+
1168
+ # for types that have _variant_mapping, all the impls in the map
1169
+ # that are SchemaEventTarget subclasses get set up as event holders.
1170
+ # this is so that constructs that need
1171
+ # to be associated with the Table at dialect-agnostic time etc. like
1172
+ # CheckConstraints can be set up with that table. they then add
1173
+ # to these constraints a DDL check_rule that among other things
1174
+ # will check this _is_impl_for_variant() method to determine when
1175
+ # the dialect is known that we are part of the table's DDL sequence.
1176
+
1177
+ # since PostgreSQL is the only DB that has ARRAY this can only
1178
+ # be integration tested by PG-specific tests
1179
+ def _we_are_the_impl(typ):
1180
+ return (
1181
+ typ is self
1182
+ or isinstance(typ, ARRAY)
1183
+ and typ.item_type is self # type: ignore[comparison-overlap]
1184
+ )
1185
+
1186
+ if dialect.name in variant_mapping and _we_are_the_impl(
1187
+ variant_mapping[dialect.name]
1188
+ ):
1189
+ return True
1190
+ elif dialect.name not in variant_mapping:
1191
+ return _we_are_the_impl(variant_mapping["_default"])
1192
+
1193
+
1194
+ class Enum(String, SchemaType, Emulated, TypeEngine[Union[str, enum.Enum]]):
1195
+ """Generic Enum Type.
1196
+
1197
+ The :class:`.Enum` type provides a set of possible string values
1198
+ which the column is constrained towards.
1199
+
1200
+ The :class:`.Enum` type will make use of the backend's native "ENUM"
1201
+ type if one is available; otherwise, it uses a VARCHAR datatype.
1202
+ An option also exists to automatically produce a CHECK constraint
1203
+ when the VARCHAR (so called "non-native") variant is produced;
1204
+ see the :paramref:`.Enum.create_constraint` flag.
1205
+
1206
+ The :class:`.Enum` type also provides in-Python validation of string
1207
+ values during both read and write operations. When reading a value
1208
+ from the database in a result set, the string value is always checked
1209
+ against the list of possible values and a ``LookupError`` is raised
1210
+ if no match is found. When passing a value to the database as a
1211
+ plain string within a SQL statement, if the
1212
+ :paramref:`.Enum.validate_strings` parameter is
1213
+ set to True, a ``LookupError`` is raised for any string value that's
1214
+ not located in the given list of possible values; note that this
1215
+ impacts usage of LIKE expressions with enumerated values (an unusual
1216
+ use case).
1217
+
1218
+ The source of enumerated values may be a list of string values, or
1219
+ alternatively a PEP-435-compliant enumerated class. For the purposes
1220
+ of the :class:`.Enum` datatype, this class need only provide a
1221
+ ``__members__`` method.
1222
+
1223
+ When using an enumerated class, the enumerated objects are used
1224
+ both for input and output, rather than strings as is the case with
1225
+ a plain-string enumerated type::
1226
+
1227
+ import enum
1228
+ from sqlalchemy import Enum
1229
+
1230
+ class MyEnum(enum.Enum):
1231
+ one = 1
1232
+ two = 2
1233
+ three = 3
1234
+
1235
+ t = Table(
1236
+ 'data', MetaData(),
1237
+ Column('value', Enum(MyEnum))
1238
+ )
1239
+
1240
+ connection.execute(t.insert(), {"value": MyEnum.two})
1241
+ assert connection.scalar(t.select()) is MyEnum.two
1242
+
1243
+ Above, the string names of each element, e.g. "one", "two", "three",
1244
+ are persisted to the database; the values of the Python Enum, here
1245
+ indicated as integers, are **not** used; the value of each enum can
1246
+ therefore be any kind of Python object whether or not it is persistable.
1247
+
1248
+ In order to persist the values and not the names, the
1249
+ :paramref:`.Enum.values_callable` parameter may be used. The value of
1250
+ this parameter is a user-supplied callable, which is intended to be used
1251
+ with a PEP-435-compliant enumerated class and returns a list of string
1252
+ values to be persisted. For a simple enumeration that uses string values,
1253
+ a callable such as ``lambda x: [e.value for e in x]`` is sufficient.
1254
+
1255
+ .. seealso::
1256
+
1257
+ :ref:`orm_declarative_mapped_column_enums` - background on using
1258
+ the :class:`_sqltypes.Enum` datatype with the ORM's
1259
+ :ref:`ORM Annotated Declarative <orm_declarative_mapped_column>`
1260
+ feature.
1261
+
1262
+ :class:`_postgresql.ENUM` - PostgreSQL-specific type,
1263
+ which has additional functionality.
1264
+
1265
+ :class:`.mysql.ENUM` - MySQL-specific type
1266
+
1267
+ """
1268
+
1269
+ __visit_name__ = "enum"
1270
+
1271
+ def __init__(self, *enums: object, **kw: Any):
1272
+ r"""Construct an enum.
1273
+
1274
+ Keyword arguments which don't apply to a specific backend are ignored
1275
+ by that backend.
1276
+
1277
+ :param \*enums: either exactly one PEP-435 compliant enumerated type
1278
+ or one or more string labels.
1279
+
1280
+ :param create_constraint: defaults to False. When creating a
1281
+ non-native enumerated type, also build a CHECK constraint on the
1282
+ database against the valid values.
1283
+
1284
+ .. note:: it is strongly recommended that the CHECK constraint
1285
+ have an explicit name in order to support schema-management
1286
+ concerns. This can be established either by setting the
1287
+ :paramref:`.Enum.name` parameter or by setting up an
1288
+ appropriate naming convention; see
1289
+ :ref:`constraint_naming_conventions` for background.
1290
+
1291
+ .. versionchanged:: 1.4 - this flag now defaults to False, meaning
1292
+ no CHECK constraint is generated for a non-native enumerated
1293
+ type.
1294
+
1295
+ :param metadata: Associate this type directly with a ``MetaData``
1296
+ object. For types that exist on the target database as an
1297
+ independent schema construct (PostgreSQL), this type will be
1298
+ created and dropped within ``create_all()`` and ``drop_all()``
1299
+ operations. If the type is not associated with any ``MetaData``
1300
+ object, it will associate itself with each ``Table`` in which it is
1301
+ used, and will be created when any of those individual tables are
1302
+ created, after a check is performed for its existence. The type is
1303
+ only dropped when ``drop_all()`` is called for that ``Table``
1304
+ object's metadata, however.
1305
+
1306
+ The value of the :paramref:`_schema.MetaData.schema` parameter of
1307
+ the :class:`_schema.MetaData` object, if set, will be used as the
1308
+ default value of the :paramref:`_types.Enum.schema` on this object
1309
+ if an explicit value is not otherwise supplied.
1310
+
1311
+ .. versionchanged:: 1.4.12 :class:`_types.Enum` inherits the
1312
+ :paramref:`_schema.MetaData.schema` parameter of the
1313
+ :class:`_schema.MetaData` object if present, when passed using
1314
+ the :paramref:`_types.Enum.metadata` parameter.
1315
+
1316
+ :param name: The name of this type. This is required for PostgreSQL
1317
+ and any future supported database which requires an explicitly
1318
+ named type, or an explicitly named constraint in order to generate
1319
+ the type and/or a table that uses it. If a PEP-435 enumerated
1320
+ class was used, its name (converted to lower case) is used by
1321
+ default.
1322
+
1323
+ :param native_enum: Use the database's native ENUM type when
1324
+ available. Defaults to True. When False, uses VARCHAR + check
1325
+ constraint for all backends. When False, the VARCHAR length can be
1326
+ controlled with :paramref:`.Enum.length`; currently "length" is
1327
+ ignored if native_enum=True.
1328
+
1329
+ :param length: Allows specifying a custom length for the VARCHAR
1330
+ when a non-native enumeration datatype is used. By default it uses
1331
+ the length of the longest value.
1332
+
1333
+ .. versionchanged:: 2.0.0 The :paramref:`.Enum.length` parameter
1334
+ is used unconditionally for ``VARCHAR`` rendering regardless of
1335
+ the :paramref:`.Enum.native_enum` parameter, for those backends
1336
+ where ``VARCHAR`` is used for enumerated datatypes.
1337
+
1338
+
1339
+ :param schema: Schema name of this type. For types that exist on the
1340
+ target database as an independent schema construct (PostgreSQL),
1341
+ this parameter specifies the named schema in which the type is
1342
+ present.
1343
+
1344
+ If not present, the schema name will be taken from the
1345
+ :class:`_schema.MetaData` collection if passed as
1346
+ :paramref:`_types.Enum.metadata`, for a :class:`_schema.MetaData`
1347
+ that includes the :paramref:`_schema.MetaData.schema` parameter.
1348
+
1349
+ .. versionchanged:: 1.4.12 :class:`_types.Enum` inherits the
1350
+ :paramref:`_schema.MetaData.schema` parameter of the
1351
+ :class:`_schema.MetaData` object if present, when passed using
1352
+ the :paramref:`_types.Enum.metadata` parameter.
1353
+
1354
+ Otherwise, if the :paramref:`_types.Enum.inherit_schema` flag is set
1355
+ to ``True``, the schema will be inherited from the associated
1356
+ :class:`_schema.Table` object if any; when
1357
+ :paramref:`_types.Enum.inherit_schema` is at its default of
1358
+ ``False``, the owning table's schema is **not** used.
1359
+
1360
+
1361
+ :param quote: Set explicit quoting preferences for the type's name.
1362
+
1363
+ :param inherit_schema: When ``True``, the "schema" from the owning
1364
+ :class:`_schema.Table`
1365
+ will be copied to the "schema" attribute of this
1366
+ :class:`.Enum`, replacing whatever value was passed for the
1367
+ ``schema`` attribute. This also takes effect when using the
1368
+ :meth:`_schema.Table.to_metadata` operation.
1369
+
1370
+ :param validate_strings: when True, string values that are being
1371
+ passed to the database in a SQL statement will be checked
1372
+ for validity against the list of enumerated values. Unrecognized
1373
+ values will result in a ``LookupError`` being raised.
1374
+
1375
+ :param values_callable: A callable which will be passed the PEP-435
1376
+ compliant enumerated type, which should then return a list of string
1377
+ values to be persisted. This allows for alternate usages such as
1378
+ using the string value of an enum to be persisted to the database
1379
+ instead of its name. The callable must return the values to be
1380
+ persisted in the same order as iterating through the Enum's
1381
+ ``__member__`` attribute. For example
1382
+ ``lambda x: [i.value for i in x]``.
1383
+
1384
+ .. versionadded:: 1.2.3
1385
+
1386
+ :param sort_key_function: a Python callable which may be used as the
1387
+ "key" argument in the Python ``sorted()`` built-in. The SQLAlchemy
1388
+ ORM requires that primary key columns which are mapped must
1389
+ be sortable in some way. When using an unsortable enumeration
1390
+ object such as a Python 3 ``Enum`` object, this parameter may be
1391
+ used to set a default sort key function for the objects. By
1392
+ default, the database value of the enumeration is used as the
1393
+ sorting function.
1394
+
1395
+ .. versionadded:: 1.3.8
1396
+
1397
+ :param omit_aliases: A boolean that when true will remove aliases from
1398
+ pep 435 enums. defaults to ``True``.
1399
+
1400
+ .. versionchanged:: 2.0 This parameter now defaults to True.
1401
+
1402
+ """
1403
+ self._enum_init(enums, kw)
1404
+
1405
+ @property
1406
+ def _enums_argument(self):
1407
+ if self.enum_class is not None:
1408
+ return [self.enum_class]
1409
+ else:
1410
+ return self.enums
1411
+
1412
+ def _enum_init(self, enums, kw):
1413
+ """internal init for :class:`.Enum` and subclasses.
1414
+
1415
+ friendly init helper used by subclasses to remove
1416
+ all the Enum-specific keyword arguments from kw. Allows all
1417
+ other arguments in kw to pass through.
1418
+
1419
+ """
1420
+ self.native_enum = kw.pop("native_enum", True)
1421
+ self.create_constraint = kw.pop("create_constraint", False)
1422
+ self.values_callable = kw.pop("values_callable", None)
1423
+ self._sort_key_function = kw.pop("sort_key_function", NO_ARG)
1424
+ length_arg = kw.pop("length", NO_ARG)
1425
+ self._omit_aliases = kw.pop("omit_aliases", True)
1426
+ _disable_warnings = kw.pop("_disable_warnings", False)
1427
+ values, objects = self._parse_into_values(enums, kw)
1428
+ self._setup_for_values(values, objects, kw)
1429
+
1430
+ self.validate_strings = kw.pop("validate_strings", False)
1431
+
1432
+ if self.enums:
1433
+ self._default_length = length = max(len(x) for x in self.enums)
1434
+ else:
1435
+ self._default_length = length = 0
1436
+
1437
+ if length_arg is not NO_ARG:
1438
+ if (
1439
+ not _disable_warnings
1440
+ and length_arg is not None
1441
+ and length_arg < length
1442
+ ):
1443
+ raise ValueError(
1444
+ "When provided, length must be larger or equal"
1445
+ " than the length of the longest enum value. %s < %s"
1446
+ % (length_arg, length)
1447
+ )
1448
+ length = length_arg
1449
+
1450
+ self._valid_lookup[None] = self._object_lookup[None] = None
1451
+
1452
+ super().__init__(length=length)
1453
+
1454
+ # assign name to the given enum class if no other name, and this
1455
+ # enum is not an "empty" enum. if the enum is "empty" we assume
1456
+ # this is a template enum that will be used to generate
1457
+ # new Enum classes.
1458
+ if self.enum_class and values:
1459
+ kw.setdefault("name", self.enum_class.__name__.lower())
1460
+ SchemaType.__init__(
1461
+ self,
1462
+ name=kw.pop("name", None),
1463
+ schema=kw.pop("schema", None),
1464
+ metadata=kw.pop("metadata", None),
1465
+ inherit_schema=kw.pop("inherit_schema", False),
1466
+ quote=kw.pop("quote", None),
1467
+ _create_events=kw.pop("_create_events", True),
1468
+ _adapted_from=kw.pop("_adapted_from", None),
1469
+ )
1470
+
1471
+ def _parse_into_values(self, enums, kw):
1472
+ if not enums and "_enums" in kw:
1473
+ enums = kw.pop("_enums")
1474
+
1475
+ if len(enums) == 1 and hasattr(enums[0], "__members__"):
1476
+ self.enum_class = enums[0]
1477
+
1478
+ _members = self.enum_class.__members__
1479
+
1480
+ if self._omit_aliases is True:
1481
+ # remove aliases
1482
+ members = OrderedDict(
1483
+ (n, v) for n, v in _members.items() if v.name == n
1484
+ )
1485
+ else:
1486
+ members = _members
1487
+ if self.values_callable:
1488
+ values = self.values_callable(self.enum_class)
1489
+ else:
1490
+ values = list(members)
1491
+ objects = [members[k] for k in members]
1492
+ return values, objects
1493
+ else:
1494
+ self.enum_class = None
1495
+ return enums, enums
1496
+
1497
+ def _resolve_for_literal(self, value: Any) -> Enum:
1498
+ tv = type(value)
1499
+ typ = self._resolve_for_python_type(tv, tv, tv)
1500
+ assert typ is not None
1501
+ return typ
1502
+
1503
+ def _resolve_for_python_type(
1504
+ self,
1505
+ python_type: Type[Any],
1506
+ matched_on: _MatchedOnType,
1507
+ matched_on_flattened: Type[Any],
1508
+ ) -> Optional[Enum]:
1509
+ # "generic form" indicates we were placed in a type map
1510
+ # as ``sqlalchemy.Enum(enum.Enum)`` which indicates we need to
1511
+ # get enumerated values from the datatype
1512
+ we_are_generic_form = self._enums_argument == [enum.Enum]
1513
+
1514
+ native_enum = None
1515
+
1516
+ if not we_are_generic_form and python_type is matched_on:
1517
+ # if we have enumerated values, and the incoming python
1518
+ # type is exactly the one that matched in the type map,
1519
+ # then we use these enumerated values and dont try to parse
1520
+ # what's incoming
1521
+ enum_args = self._enums_argument
1522
+
1523
+ elif is_literal(python_type):
1524
+ # for a literal, where we need to get its contents, parse it out.
1525
+ enum_args = typing_get_args(python_type)
1526
+ bad_args = [arg for arg in enum_args if not isinstance(arg, str)]
1527
+ if bad_args:
1528
+ raise exc.ArgumentError(
1529
+ f"Can't create string-based Enum datatype from non-string "
1530
+ f"values: {', '.join(repr(x) for x in bad_args)}. Please "
1531
+ f"provide an explicit Enum datatype for this Python type"
1532
+ )
1533
+ native_enum = False
1534
+ elif isinstance(python_type, type) and issubclass(
1535
+ python_type, enum.Enum
1536
+ ):
1537
+ # same for an enum.Enum
1538
+ enum_args = [python_type]
1539
+
1540
+ else:
1541
+ enum_args = self._enums_argument
1542
+
1543
+ # make a new Enum that looks like this one.
1544
+ # arguments or other rules
1545
+ kw = self._make_enum_kw({})
1546
+
1547
+ if native_enum is False:
1548
+ kw["native_enum"] = False
1549
+
1550
+ kw["length"] = NO_ARG if self.length == 0 else self.length
1551
+ return cast(
1552
+ Enum,
1553
+ self._generic_type_affinity(_enums=enum_args, **kw), # type: ignore # noqa: E501
1554
+ )
1555
+
1556
+ def _setup_for_values(self, values, objects, kw):
1557
+ self.enums = list(values)
1558
+
1559
+ self._valid_lookup = dict(zip(reversed(objects), reversed(values)))
1560
+
1561
+ self._object_lookup = dict(zip(values, objects))
1562
+
1563
+ self._valid_lookup.update(
1564
+ [
1565
+ (value, self._valid_lookup[self._object_lookup[value]])
1566
+ for value in values
1567
+ ]
1568
+ )
1569
+
1570
+ @property
1571
+ def sort_key_function(self):
1572
+ if self._sort_key_function is NO_ARG:
1573
+ return self._db_value_for_elem
1574
+ else:
1575
+ return self._sort_key_function
1576
+
1577
+ @property
1578
+ def native(self):
1579
+ return self.native_enum
1580
+
1581
+ def _db_value_for_elem(self, elem):
1582
+ try:
1583
+ return self._valid_lookup[elem]
1584
+ except KeyError as err:
1585
+ # for unknown string values, we return as is. While we can
1586
+ # validate these if we wanted, that does not allow for lesser-used
1587
+ # end-user use cases, such as using a LIKE comparison with an enum,
1588
+ # or for an application that wishes to apply string tests to an
1589
+ # ENUM (see [ticket:3725]). While we can decide to differentiate
1590
+ # here between an INSERT statement and a criteria used in a SELECT,
1591
+ # for now we're staying conservative w/ behavioral changes (perhaps
1592
+ # someone has a trigger that handles strings on INSERT)
1593
+ if not self.validate_strings and isinstance(elem, str):
1594
+ return elem
1595
+ else:
1596
+ raise LookupError(
1597
+ "'%s' is not among the defined enum values. "
1598
+ "Enum name: %s. Possible values: %s"
1599
+ % (
1600
+ elem,
1601
+ self.name,
1602
+ langhelpers.repr_tuple_names(self.enums),
1603
+ )
1604
+ ) from err
1605
+
1606
+ class Comparator(String.Comparator[str]):
1607
+ __slots__ = ()
1608
+
1609
+ type: String
1610
+
1611
+ def _adapt_expression(
1612
+ self,
1613
+ op: OperatorType,
1614
+ other_comparator: TypeEngine.Comparator[Any],
1615
+ ) -> Tuple[OperatorType, TypeEngine[Any]]:
1616
+ op, typ = super()._adapt_expression(op, other_comparator)
1617
+ if op is operators.concat_op:
1618
+ typ = String(self.type.length)
1619
+ return op, typ
1620
+
1621
+ comparator_factory = Comparator
1622
+
1623
+ def _object_value_for_elem(self, elem):
1624
+ try:
1625
+ return self._object_lookup[elem]
1626
+ except KeyError as err:
1627
+ raise LookupError(
1628
+ "'%s' is not among the defined enum values. "
1629
+ "Enum name: %s. Possible values: %s"
1630
+ % (
1631
+ elem,
1632
+ self.name,
1633
+ langhelpers.repr_tuple_names(self.enums),
1634
+ )
1635
+ ) from err
1636
+
1637
+ def __repr__(self):
1638
+ return util.generic_repr(
1639
+ self,
1640
+ additional_kw=[
1641
+ ("native_enum", True),
1642
+ ("create_constraint", False),
1643
+ ("length", self._default_length),
1644
+ ],
1645
+ to_inspect=[Enum, SchemaType],
1646
+ )
1647
+
1648
+ def as_generic(self, allow_nulltype=False):
1649
+ try:
1650
+ args = self.enums
1651
+ except AttributeError:
1652
+ raise NotImplementedError(
1653
+ "TypeEngine.as_generic() heuristic "
1654
+ "is undefined for types that inherit Enum but do not have "
1655
+ "an `enums` attribute."
1656
+ ) from None
1657
+
1658
+ return util.constructor_copy(
1659
+ self, self._generic_type_affinity, *args, _disable_warnings=True
1660
+ )
1661
+
1662
+ def _make_enum_kw(self, kw):
1663
+ kw.setdefault("validate_strings", self.validate_strings)
1664
+ if self.name:
1665
+ kw.setdefault("name", self.name)
1666
+ kw.setdefault("schema", self.schema)
1667
+ kw.setdefault("inherit_schema", self.inherit_schema)
1668
+ kw.setdefault("metadata", self.metadata)
1669
+ kw.setdefault("native_enum", self.native_enum)
1670
+ kw.setdefault("values_callable", self.values_callable)
1671
+ kw.setdefault("create_constraint", self.create_constraint)
1672
+ kw.setdefault("length", self.length)
1673
+ kw.setdefault("omit_aliases", self._omit_aliases)
1674
+ return kw
1675
+
1676
+ def adapt_to_emulated(self, impltype, **kw):
1677
+ self._make_enum_kw(kw)
1678
+ kw["_disable_warnings"] = True
1679
+ kw.setdefault("_create_events", False)
1680
+ assert "_enums" in kw
1681
+ return impltype(**kw)
1682
+
1683
+ def adapt(self, cls, **kw):
1684
+ kw["_enums"] = self._enums_argument
1685
+ kw["_disable_warnings"] = True
1686
+ return super().adapt(cls, **kw)
1687
+
1688
+ def _should_create_constraint(self, compiler, **kw):
1689
+ if not self._is_impl_for_variant(compiler.dialect, kw):
1690
+ return False
1691
+ return (
1692
+ not self.native_enum or not compiler.dialect.supports_native_enum
1693
+ )
1694
+
1695
+ @util.preload_module("sqlalchemy.sql.schema")
1696
+ def _set_table(self, column, table):
1697
+ schema = util.preloaded.sql_schema
1698
+ SchemaType._set_table(self, column, table)
1699
+
1700
+ if not self.create_constraint:
1701
+ return
1702
+
1703
+ variant_mapping = self._variant_mapping_for_set_table(column)
1704
+
1705
+ e = schema.CheckConstraint(
1706
+ type_coerce(column, String()).in_(self.enums),
1707
+ name=_NONE_NAME if self.name is None else self.name,
1708
+ _create_rule=util.portable_instancemethod(
1709
+ self._should_create_constraint,
1710
+ {"variant_mapping": variant_mapping},
1711
+ ),
1712
+ _type_bound=True,
1713
+ )
1714
+ assert e.table is table
1715
+
1716
+ def literal_processor(self, dialect):
1717
+ parent_processor = super().literal_processor(dialect)
1718
+
1719
+ def process(value):
1720
+ value = self._db_value_for_elem(value)
1721
+ if parent_processor:
1722
+ value = parent_processor(value)
1723
+ return value
1724
+
1725
+ return process
1726
+
1727
+ def bind_processor(self, dialect):
1728
+ parent_processor = super().bind_processor(dialect)
1729
+
1730
+ def process(value):
1731
+ value = self._db_value_for_elem(value)
1732
+ if parent_processor:
1733
+ value = parent_processor(value)
1734
+ return value
1735
+
1736
+ return process
1737
+
1738
+ def result_processor(self, dialect, coltype):
1739
+ parent_processor = super().result_processor(dialect, coltype)
1740
+
1741
+ def process(value):
1742
+ if parent_processor:
1743
+ value = parent_processor(value)
1744
+
1745
+ value = self._object_value_for_elem(value)
1746
+ return value
1747
+
1748
+ return process
1749
+
1750
+ def copy(self, **kw):
1751
+ return SchemaType.copy(self, **kw)
1752
+
1753
+ @property
1754
+ def python_type(self):
1755
+ if self.enum_class:
1756
+ return self.enum_class
1757
+ else:
1758
+ return super().python_type
1759
+
1760
+
1761
+ class PickleType(TypeDecorator[object]):
1762
+ """Holds Python objects, which are serialized using pickle.
1763
+
1764
+ PickleType builds upon the Binary type to apply Python's
1765
+ ``pickle.dumps()`` to incoming objects, and ``pickle.loads()`` on
1766
+ the way out, allowing any pickleable Python object to be stored as
1767
+ a serialized binary field.
1768
+
1769
+ To allow ORM change events to propagate for elements associated
1770
+ with :class:`.PickleType`, see :ref:`mutable_toplevel`.
1771
+
1772
+ """
1773
+
1774
+ impl = LargeBinary
1775
+ cache_ok = True
1776
+
1777
+ def __init__(
1778
+ self,
1779
+ protocol: int = pickle.HIGHEST_PROTOCOL,
1780
+ pickler: Any = None,
1781
+ comparator: Optional[Callable[[Any, Any], bool]] = None,
1782
+ impl: Optional[_TypeEngineArgument[Any]] = None,
1783
+ ):
1784
+ """
1785
+ Construct a PickleType.
1786
+
1787
+ :param protocol: defaults to ``pickle.HIGHEST_PROTOCOL``.
1788
+
1789
+ :param pickler: defaults to pickle. May be any object with
1790
+ pickle-compatible ``dumps`` and ``loads`` methods.
1791
+
1792
+ :param comparator: a 2-arg callable predicate used
1793
+ to compare values of this type. If left as ``None``,
1794
+ the Python "equals" operator is used to compare values.
1795
+
1796
+ :param impl: A binary-storing :class:`_types.TypeEngine` class or
1797
+ instance to use in place of the default :class:`_types.LargeBinary`.
1798
+ For example the :class: `_mysql.LONGBLOB` class may be more effective
1799
+ when using MySQL.
1800
+
1801
+ .. versionadded:: 1.4.20
1802
+
1803
+ """
1804
+ self.protocol = protocol
1805
+ self.pickler = pickler or pickle
1806
+ self.comparator = comparator
1807
+ super().__init__()
1808
+
1809
+ if impl:
1810
+ # custom impl is not necessarily a LargeBinary subclass.
1811
+ # make an exception to typing for this
1812
+ self.impl = to_instance(impl) # type: ignore
1813
+
1814
+ def __reduce__(self):
1815
+ return PickleType, (self.protocol, None, self.comparator)
1816
+
1817
+ def bind_processor(self, dialect):
1818
+ impl_processor = self.impl_instance.bind_processor(dialect)
1819
+ dumps = self.pickler.dumps
1820
+ protocol = self.protocol
1821
+ if impl_processor:
1822
+ fixed_impl_processor = impl_processor
1823
+
1824
+ def process(value):
1825
+ if value is not None:
1826
+ value = dumps(value, protocol)
1827
+ return fixed_impl_processor(value)
1828
+
1829
+ else:
1830
+
1831
+ def process(value):
1832
+ if value is not None:
1833
+ value = dumps(value, protocol)
1834
+ return value
1835
+
1836
+ return process
1837
+
1838
+ def result_processor(self, dialect, coltype):
1839
+ impl_processor = self.impl_instance.result_processor(dialect, coltype)
1840
+ loads = self.pickler.loads
1841
+ if impl_processor:
1842
+ fixed_impl_processor = impl_processor
1843
+
1844
+ def process(value):
1845
+ value = fixed_impl_processor(value)
1846
+ if value is None:
1847
+ return None
1848
+ return loads(value)
1849
+
1850
+ else:
1851
+
1852
+ def process(value):
1853
+ if value is None:
1854
+ return None
1855
+ return loads(value)
1856
+
1857
+ return process
1858
+
1859
+ def compare_values(self, x, y):
1860
+ if self.comparator:
1861
+ return self.comparator(x, y)
1862
+ else:
1863
+ return x == y
1864
+
1865
+
1866
+ class Boolean(SchemaType, Emulated, TypeEngine[bool]):
1867
+ """A bool datatype.
1868
+
1869
+ :class:`.Boolean` typically uses BOOLEAN or SMALLINT on the DDL side,
1870
+ and on the Python side deals in ``True`` or ``False``.
1871
+
1872
+ The :class:`.Boolean` datatype currently has two levels of assertion
1873
+ that the values persisted are simple true/false values. For all
1874
+ backends, only the Python values ``None``, ``True``, ``False``, ``1``
1875
+ or ``0`` are accepted as parameter values. For those backends that
1876
+ don't support a "native boolean" datatype, an option exists to
1877
+ also create a CHECK constraint on the target column
1878
+
1879
+ .. versionchanged:: 1.2 the :class:`.Boolean` datatype now asserts that
1880
+ incoming Python values are already in pure boolean form.
1881
+
1882
+
1883
+ """
1884
+
1885
+ __visit_name__ = "boolean"
1886
+ native = True
1887
+
1888
+ def __init__(
1889
+ self,
1890
+ create_constraint: bool = False,
1891
+ name: Optional[str] = None,
1892
+ _create_events: bool = True,
1893
+ _adapted_from: Optional[SchemaType] = None,
1894
+ ):
1895
+ """Construct a Boolean.
1896
+
1897
+ :param create_constraint: defaults to False. If the boolean
1898
+ is generated as an int/smallint, also create a CHECK constraint
1899
+ on the table that ensures 1 or 0 as a value.
1900
+
1901
+ .. note:: it is strongly recommended that the CHECK constraint
1902
+ have an explicit name in order to support schema-management
1903
+ concerns. This can be established either by setting the
1904
+ :paramref:`.Boolean.name` parameter or by setting up an
1905
+ appropriate naming convention; see
1906
+ :ref:`constraint_naming_conventions` for background.
1907
+
1908
+ .. versionchanged:: 1.4 - this flag now defaults to False, meaning
1909
+ no CHECK constraint is generated for a non-native enumerated
1910
+ type.
1911
+
1912
+ :param name: if a CHECK constraint is generated, specify
1913
+ the name of the constraint.
1914
+
1915
+ """
1916
+ self.create_constraint = create_constraint
1917
+ self.name = name
1918
+ self._create_events = _create_events
1919
+ if _adapted_from:
1920
+ self.dispatch = self.dispatch._join(_adapted_from.dispatch)
1921
+
1922
+ def copy(self, **kw):
1923
+ # override SchemaType.copy() to not include to_metadata logic
1924
+ return self.adapt(
1925
+ cast("Type[TypeEngine[Any]]", self.__class__),
1926
+ _create_events=True,
1927
+ )
1928
+
1929
+ def _should_create_constraint(self, compiler, **kw):
1930
+ if not self._is_impl_for_variant(compiler.dialect, kw):
1931
+ return False
1932
+ return (
1933
+ not compiler.dialect.supports_native_boolean
1934
+ and compiler.dialect.non_native_boolean_check_constraint
1935
+ )
1936
+
1937
+ @util.preload_module("sqlalchemy.sql.schema")
1938
+ def _set_table(self, column, table):
1939
+ schema = util.preloaded.sql_schema
1940
+ if not self.create_constraint:
1941
+ return
1942
+
1943
+ variant_mapping = self._variant_mapping_for_set_table(column)
1944
+
1945
+ e = schema.CheckConstraint(
1946
+ type_coerce(column, self).in_([0, 1]),
1947
+ name=_NONE_NAME if self.name is None else self.name,
1948
+ _create_rule=util.portable_instancemethod(
1949
+ self._should_create_constraint,
1950
+ {"variant_mapping": variant_mapping},
1951
+ ),
1952
+ _type_bound=True,
1953
+ )
1954
+ assert e.table is table
1955
+
1956
+ @property
1957
+ def python_type(self):
1958
+ return bool
1959
+
1960
+ _strict_bools = frozenset([None, True, False])
1961
+
1962
+ def _strict_as_bool(self, value):
1963
+ if value not in self._strict_bools:
1964
+ if not isinstance(value, int):
1965
+ raise TypeError("Not a boolean value: %r" % (value,))
1966
+ else:
1967
+ raise ValueError(
1968
+ "Value %r is not None, True, or False" % (value,)
1969
+ )
1970
+ return value
1971
+
1972
+ def literal_processor(self, dialect):
1973
+ compiler = dialect.statement_compiler(dialect, None)
1974
+ true = compiler.visit_true(None)
1975
+ false = compiler.visit_false(None)
1976
+
1977
+ def process(value):
1978
+ return true if self._strict_as_bool(value) else false
1979
+
1980
+ return process
1981
+
1982
+ def bind_processor(self, dialect):
1983
+ _strict_as_bool = self._strict_as_bool
1984
+
1985
+ _coerce: Union[Type[bool], Type[int]]
1986
+
1987
+ if dialect.supports_native_boolean:
1988
+ _coerce = bool
1989
+ else:
1990
+ _coerce = int
1991
+
1992
+ def process(value):
1993
+ value = _strict_as_bool(value)
1994
+ if value is not None:
1995
+ value = _coerce(value)
1996
+ return value
1997
+
1998
+ return process
1999
+
2000
+ def result_processor(self, dialect, coltype):
2001
+ if dialect.supports_native_boolean:
2002
+ return None
2003
+ else:
2004
+ return processors.int_to_boolean
2005
+
2006
+
2007
+ class _AbstractInterval(HasExpressionLookup, TypeEngine[dt.timedelta]):
2008
+ @util.memoized_property
2009
+ def _expression_adaptations(self):
2010
+ # Based on
2011
+ # https://www.postgresql.org/docs/current/static/functions-datetime.html.
2012
+
2013
+ return {
2014
+ operators.add: {
2015
+ Date: DateTime,
2016
+ Interval: self.__class__,
2017
+ DateTime: DateTime,
2018
+ Time: Time,
2019
+ },
2020
+ operators.sub: {Interval: self.__class__},
2021
+ operators.mul: {Numeric: self.__class__},
2022
+ operators.truediv: {Numeric: self.__class__},
2023
+ }
2024
+
2025
+ @util.ro_non_memoized_property
2026
+ def _type_affinity(self) -> Type[Interval]:
2027
+ return Interval
2028
+
2029
+
2030
+ class Interval(Emulated, _AbstractInterval, TypeDecorator[dt.timedelta]):
2031
+ """A type for ``datetime.timedelta()`` objects.
2032
+
2033
+ The Interval type deals with ``datetime.timedelta`` objects. In
2034
+ PostgreSQL and Oracle, the native ``INTERVAL`` type is used; for others,
2035
+ the value is stored as a date which is relative to the "epoch"
2036
+ (Jan. 1, 1970).
2037
+
2038
+ Note that the ``Interval`` type does not currently provide date arithmetic
2039
+ operations on platforms which do not support interval types natively. Such
2040
+ operations usually require transformation of both sides of the expression
2041
+ (such as, conversion of both sides into integer epoch values first) which
2042
+ currently is a manual procedure (such as via
2043
+ :attr:`~sqlalchemy.sql.expression.func`).
2044
+
2045
+ """
2046
+
2047
+ impl = DateTime
2048
+ epoch = dt.datetime.fromtimestamp(0, dt.timezone.utc).replace(tzinfo=None)
2049
+ cache_ok = True
2050
+
2051
+ def __init__(
2052
+ self,
2053
+ native: bool = True,
2054
+ second_precision: Optional[int] = None,
2055
+ day_precision: Optional[int] = None,
2056
+ ):
2057
+ """Construct an Interval object.
2058
+
2059
+ :param native: when True, use the actual
2060
+ INTERVAL type provided by the database, if
2061
+ supported (currently PostgreSQL, Oracle).
2062
+ Otherwise, represent the interval data as
2063
+ an epoch value regardless.
2064
+
2065
+ :param second_precision: For native interval types
2066
+ which support a "fractional seconds precision" parameter,
2067
+ i.e. Oracle and PostgreSQL
2068
+
2069
+ :param day_precision: for native interval types which
2070
+ support a "day precision" parameter, i.e. Oracle.
2071
+
2072
+ """
2073
+ super().__init__()
2074
+ self.native = native
2075
+ self.second_precision = second_precision
2076
+ self.day_precision = day_precision
2077
+
2078
+ class Comparator(
2079
+ TypeDecorator.Comparator[_CT],
2080
+ _AbstractInterval.Comparator[_CT],
2081
+ ):
2082
+ __slots__ = ()
2083
+
2084
+ comparator_factory = Comparator
2085
+
2086
+ @property
2087
+ def python_type(self):
2088
+ return dt.timedelta
2089
+
2090
+ def adapt_to_emulated(self, impltype, **kw):
2091
+ return _AbstractInterval.adapt(self, impltype, **kw)
2092
+
2093
+ def coerce_compared_value(self, op, value):
2094
+ return self.impl_instance.coerce_compared_value(op, value)
2095
+
2096
+ def bind_processor(
2097
+ self, dialect: Dialect
2098
+ ) -> _BindProcessorType[dt.timedelta]:
2099
+ if TYPE_CHECKING:
2100
+ assert isinstance(self.impl_instance, DateTime)
2101
+ impl_processor = self.impl_instance.bind_processor(dialect)
2102
+ epoch = self.epoch
2103
+ if impl_processor:
2104
+ fixed_impl_processor = impl_processor
2105
+
2106
+ def process(
2107
+ value: Optional[dt.timedelta],
2108
+ ) -> Any:
2109
+ if value is not None:
2110
+ dt_value = epoch + value
2111
+ else:
2112
+ dt_value = None
2113
+ return fixed_impl_processor(dt_value)
2114
+
2115
+ else:
2116
+
2117
+ def process(
2118
+ value: Optional[dt.timedelta],
2119
+ ) -> Any:
2120
+ if value is not None:
2121
+ dt_value = epoch + value
2122
+ else:
2123
+ dt_value = None
2124
+ return dt_value
2125
+
2126
+ return process
2127
+
2128
+ def result_processor(
2129
+ self, dialect: Dialect, coltype: Any
2130
+ ) -> _ResultProcessorType[dt.timedelta]:
2131
+ if TYPE_CHECKING:
2132
+ assert isinstance(self.impl_instance, DateTime)
2133
+ impl_processor = self.impl_instance.result_processor(dialect, coltype)
2134
+ epoch = self.epoch
2135
+ if impl_processor:
2136
+ fixed_impl_processor = impl_processor
2137
+
2138
+ def process(value: Any) -> Optional[dt.timedelta]:
2139
+ dt_value = fixed_impl_processor(value)
2140
+ if dt_value is None:
2141
+ return None
2142
+ return dt_value - epoch
2143
+
2144
+ else:
2145
+
2146
+ def process(value: Any) -> Optional[dt.timedelta]:
2147
+ if value is None:
2148
+ return None
2149
+ return value - epoch # type: ignore
2150
+
2151
+ return process
2152
+
2153
+
2154
+ class JSON(Indexable, TypeEngine[Any]):
2155
+ """Represent a SQL JSON type.
2156
+
2157
+ .. note:: :class:`_types.JSON`
2158
+ is provided as a facade for vendor-specific
2159
+ JSON types. Since it supports JSON SQL operations, it only
2160
+ works on backends that have an actual JSON type, currently:
2161
+
2162
+ * PostgreSQL - see :class:`sqlalchemy.dialects.postgresql.JSON` and
2163
+ :class:`sqlalchemy.dialects.postgresql.JSONB` for backend-specific
2164
+ notes
2165
+
2166
+ * MySQL - see
2167
+ :class:`sqlalchemy.dialects.mysql.JSON` for backend-specific notes
2168
+
2169
+ * SQLite as of version 3.9 - see
2170
+ :class:`sqlalchemy.dialects.sqlite.JSON` for backend-specific notes
2171
+
2172
+ * Microsoft SQL Server 2016 and later - see
2173
+ :class:`sqlalchemy.dialects.mssql.JSON` for backend-specific notes
2174
+
2175
+ :class:`_types.JSON` is part of the Core in support of the growing
2176
+ popularity of native JSON datatypes.
2177
+
2178
+ The :class:`_types.JSON` type stores arbitrary JSON format data, e.g.::
2179
+
2180
+ data_table = Table('data_table', metadata,
2181
+ Column('id', Integer, primary_key=True),
2182
+ Column('data', JSON)
2183
+ )
2184
+
2185
+ with engine.connect() as conn:
2186
+ conn.execute(
2187
+ data_table.insert(),
2188
+ {"data": {"key1": "value1", "key2": "value2"}}
2189
+ )
2190
+
2191
+ **JSON-Specific Expression Operators**
2192
+
2193
+ The :class:`_types.JSON`
2194
+ datatype provides these additional SQL operations:
2195
+
2196
+ * Keyed index operations::
2197
+
2198
+ data_table.c.data['some key']
2199
+
2200
+ * Integer index operations::
2201
+
2202
+ data_table.c.data[3]
2203
+
2204
+ * Path index operations::
2205
+
2206
+ data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')]
2207
+
2208
+ * Data casters for specific JSON element types, subsequent to an index
2209
+ or path operation being invoked::
2210
+
2211
+ data_table.c.data["some key"].as_integer()
2212
+
2213
+ .. versionadded:: 1.3.11
2214
+
2215
+ Additional operations may be available from the dialect-specific versions
2216
+ of :class:`_types.JSON`, such as
2217
+ :class:`sqlalchemy.dialects.postgresql.JSON` and
2218
+ :class:`sqlalchemy.dialects.postgresql.JSONB` which both offer additional
2219
+ PostgreSQL-specific operations.
2220
+
2221
+ **Casting JSON Elements to Other Types**
2222
+
2223
+ Index operations, i.e. those invoked by calling upon the expression using
2224
+ the Python bracket operator as in ``some_column['some key']``, return an
2225
+ expression object whose type defaults to :class:`_types.JSON` by default,
2226
+ so that
2227
+ further JSON-oriented instructions may be called upon the result type.
2228
+ However, it is likely more common that an index operation is expected
2229
+ to return a specific scalar element, such as a string or integer. In
2230
+ order to provide access to these elements in a backend-agnostic way,
2231
+ a series of data casters are provided:
2232
+
2233
+ * :meth:`.JSON.Comparator.as_string` - return the element as a string
2234
+
2235
+ * :meth:`.JSON.Comparator.as_boolean` - return the element as a boolean
2236
+
2237
+ * :meth:`.JSON.Comparator.as_float` - return the element as a float
2238
+
2239
+ * :meth:`.JSON.Comparator.as_integer` - return the element as an integer
2240
+
2241
+ These data casters are implemented by supporting dialects in order to
2242
+ assure that comparisons to the above types will work as expected, such as::
2243
+
2244
+ # integer comparison
2245
+ data_table.c.data["some_integer_key"].as_integer() == 5
2246
+
2247
+ # boolean comparison
2248
+ data_table.c.data["some_boolean"].as_boolean() == True
2249
+
2250
+ .. versionadded:: 1.3.11 Added type-specific casters for the basic JSON
2251
+ data element types.
2252
+
2253
+ .. note::
2254
+
2255
+ The data caster functions are new in version 1.3.11, and supersede
2256
+ the previous documented approaches of using CAST; for reference,
2257
+ this looked like::
2258
+
2259
+ from sqlalchemy import cast, type_coerce
2260
+ from sqlalchemy import String, JSON
2261
+ cast(
2262
+ data_table.c.data['some_key'], String
2263
+ ) == type_coerce(55, JSON)
2264
+
2265
+ The above case now works directly as::
2266
+
2267
+ data_table.c.data['some_key'].as_integer() == 5
2268
+
2269
+ For details on the previous comparison approach within the 1.3.x
2270
+ series, see the documentation for SQLAlchemy 1.2 or the included HTML
2271
+ files in the doc/ directory of the version's distribution.
2272
+
2273
+ **Detecting Changes in JSON columns when using the ORM**
2274
+
2275
+ The :class:`_types.JSON` type, when used with the SQLAlchemy ORM, does not
2276
+ detect in-place mutations to the structure. In order to detect these, the
2277
+ :mod:`sqlalchemy.ext.mutable` extension must be used, most typically
2278
+ using the :class:`.MutableDict` class. This extension will
2279
+ allow "in-place" changes to the datastructure to produce events which
2280
+ will be detected by the unit of work. See the example at :class:`.HSTORE`
2281
+ for a simple example involving a dictionary.
2282
+
2283
+ Alternatively, assigning a JSON structure to an ORM element that
2284
+ replaces the old one will always trigger a change event.
2285
+
2286
+ **Support for JSON null vs. SQL NULL**
2287
+
2288
+ When working with NULL values, the :class:`_types.JSON` type recommends the
2289
+ use of two specific constants in order to differentiate between a column
2290
+ that evaluates to SQL NULL, e.g. no value, vs. the JSON-encoded string of
2291
+ ``"null"``. To insert or select against a value that is SQL NULL, use the
2292
+ constant :func:`.null`. This symbol may be passed as a parameter value
2293
+ specifically when using the :class:`_types.JSON` datatype, which contains
2294
+ special logic that interprets this symbol to mean that the column value
2295
+ should be SQL NULL as opposed to JSON ``"null"``::
2296
+
2297
+ from sqlalchemy import null
2298
+ conn.execute(table.insert(), {"json_value": null()})
2299
+
2300
+ To insert or select against a value that is JSON ``"null"``, use the
2301
+ constant :attr:`_types.JSON.NULL`::
2302
+
2303
+ conn.execute(table.insert(), {"json_value": JSON.NULL})
2304
+
2305
+ The :class:`_types.JSON` type supports a flag
2306
+ :paramref:`_types.JSON.none_as_null` which when set to True will result
2307
+ in the Python constant ``None`` evaluating to the value of SQL
2308
+ NULL, and when set to False results in the Python constant
2309
+ ``None`` evaluating to the value of JSON ``"null"``. The Python
2310
+ value ``None`` may be used in conjunction with either
2311
+ :attr:`_types.JSON.NULL` and :func:`.null` in order to indicate NULL
2312
+ values, but care must be taken as to the value of the
2313
+ :paramref:`_types.JSON.none_as_null` in these cases.
2314
+
2315
+ **Customizing the JSON Serializer**
2316
+
2317
+ The JSON serializer and deserializer used by :class:`_types.JSON`
2318
+ defaults to
2319
+ Python's ``json.dumps`` and ``json.loads`` functions; in the case of the
2320
+ psycopg2 dialect, psycopg2 may be using its own custom loader function.
2321
+
2322
+ In order to affect the serializer / deserializer, they are currently
2323
+ configurable at the :func:`_sa.create_engine` level via the
2324
+ :paramref:`_sa.create_engine.json_serializer` and
2325
+ :paramref:`_sa.create_engine.json_deserializer` parameters. For example,
2326
+ to turn off ``ensure_ascii``::
2327
+
2328
+ engine = create_engine(
2329
+ "sqlite://",
2330
+ json_serializer=lambda obj: json.dumps(obj, ensure_ascii=False))
2331
+
2332
+ .. versionchanged:: 1.3.7
2333
+
2334
+ SQLite dialect's ``json_serializer`` and ``json_deserializer``
2335
+ parameters renamed from ``_json_serializer`` and
2336
+ ``_json_deserializer``.
2337
+
2338
+ .. seealso::
2339
+
2340
+ :class:`sqlalchemy.dialects.postgresql.JSON`
2341
+
2342
+ :class:`sqlalchemy.dialects.postgresql.JSONB`
2343
+
2344
+ :class:`sqlalchemy.dialects.mysql.JSON`
2345
+
2346
+ :class:`sqlalchemy.dialects.sqlite.JSON`
2347
+
2348
+ """
2349
+
2350
+ __visit_name__ = "JSON"
2351
+
2352
+ hashable = False
2353
+ NULL = util.symbol("JSON_NULL")
2354
+ """Describe the json value of NULL.
2355
+
2356
+ This value is used to force the JSON value of ``"null"`` to be
2357
+ used as the value. A value of Python ``None`` will be recognized
2358
+ either as SQL NULL or JSON ``"null"``, based on the setting
2359
+ of the :paramref:`_types.JSON.none_as_null` flag; the
2360
+ :attr:`_types.JSON.NULL`
2361
+ constant can be used to always resolve to JSON ``"null"`` regardless
2362
+ of this setting. This is in contrast to the :func:`_expression.null`
2363
+ construct,
2364
+ which always resolves to SQL NULL. E.g.::
2365
+
2366
+ from sqlalchemy import null
2367
+ from sqlalchemy.dialects.postgresql import JSON
2368
+
2369
+ # will *always* insert SQL NULL
2370
+ obj1 = MyObject(json_value=null())
2371
+
2372
+ # will *always* insert JSON string "null"
2373
+ obj2 = MyObject(json_value=JSON.NULL)
2374
+
2375
+ session.add_all([obj1, obj2])
2376
+ session.commit()
2377
+
2378
+ In order to set JSON NULL as a default value for a column, the most
2379
+ transparent method is to use :func:`_expression.text`::
2380
+
2381
+ Table(
2382
+ 'my_table', metadata,
2383
+ Column('json_data', JSON, default=text("'null'"))
2384
+ )
2385
+
2386
+ While it is possible to use :attr:`_types.JSON.NULL` in this context, the
2387
+ :attr:`_types.JSON.NULL` value will be returned as the value of the
2388
+ column,
2389
+ which in the context of the ORM or other repurposing of the default
2390
+ value, may not be desirable. Using a SQL expression means the value
2391
+ will be re-fetched from the database within the context of retrieving
2392
+ generated defaults.
2393
+
2394
+
2395
+ """
2396
+
2397
+ def __init__(self, none_as_null: bool = False):
2398
+ """Construct a :class:`_types.JSON` type.
2399
+
2400
+ :param none_as_null=False: if True, persist the value ``None`` as a
2401
+ SQL NULL value, not the JSON encoding of ``null``. Note that when this
2402
+ flag is False, the :func:`.null` construct can still be used to
2403
+ persist a NULL value, which may be passed directly as a parameter
2404
+ value that is specially interpreted by the :class:`_types.JSON` type
2405
+ as SQL NULL::
2406
+
2407
+ from sqlalchemy import null
2408
+ conn.execute(table.insert(), {"data": null()})
2409
+
2410
+ .. note::
2411
+
2412
+ :paramref:`_types.JSON.none_as_null` does **not** apply to the
2413
+ values passed to :paramref:`_schema.Column.default` and
2414
+ :paramref:`_schema.Column.server_default`; a value of ``None``
2415
+ passed for these parameters means "no default present".
2416
+
2417
+ Additionally, when used in SQL comparison expressions, the
2418
+ Python value ``None`` continues to refer to SQL null, and not
2419
+ JSON NULL. The :paramref:`_types.JSON.none_as_null` flag refers
2420
+ explicitly to the **persistence** of the value within an
2421
+ INSERT or UPDATE statement. The :attr:`_types.JSON.NULL`
2422
+ value should be used for SQL expressions that wish to compare to
2423
+ JSON null.
2424
+
2425
+ .. seealso::
2426
+
2427
+ :attr:`.types.JSON.NULL`
2428
+
2429
+ """
2430
+ self.none_as_null = none_as_null
2431
+
2432
+ class JSONElementType(TypeEngine[Any]):
2433
+ """Common function for index / path elements in a JSON expression."""
2434
+
2435
+ _integer = Integer()
2436
+ _string = String()
2437
+
2438
+ def string_bind_processor(self, dialect):
2439
+ return self._string._cached_bind_processor(dialect)
2440
+
2441
+ def string_literal_processor(self, dialect):
2442
+ return self._string._cached_literal_processor(dialect)
2443
+
2444
+ def bind_processor(self, dialect):
2445
+ int_processor = self._integer._cached_bind_processor(dialect)
2446
+ string_processor = self.string_bind_processor(dialect)
2447
+
2448
+ def process(value):
2449
+ if int_processor and isinstance(value, int):
2450
+ value = int_processor(value)
2451
+ elif string_processor and isinstance(value, str):
2452
+ value = string_processor(value)
2453
+ return value
2454
+
2455
+ return process
2456
+
2457
+ def literal_processor(self, dialect):
2458
+ int_processor = self._integer._cached_literal_processor(dialect)
2459
+ string_processor = self.string_literal_processor(dialect)
2460
+
2461
+ def process(value):
2462
+ if int_processor and isinstance(value, int):
2463
+ value = int_processor(value)
2464
+ elif string_processor and isinstance(value, str):
2465
+ value = string_processor(value)
2466
+ else:
2467
+ raise NotImplementedError()
2468
+
2469
+ return value
2470
+
2471
+ return process
2472
+
2473
+ class JSONIndexType(JSONElementType):
2474
+ """Placeholder for the datatype of a JSON index value.
2475
+
2476
+ This allows execution-time processing of JSON index values
2477
+ for special syntaxes.
2478
+
2479
+ """
2480
+
2481
+ class JSONIntIndexType(JSONIndexType):
2482
+ """Placeholder for the datatype of a JSON index value.
2483
+
2484
+ This allows execution-time processing of JSON index values
2485
+ for special syntaxes.
2486
+
2487
+ """
2488
+
2489
+ class JSONStrIndexType(JSONIndexType):
2490
+ """Placeholder for the datatype of a JSON index value.
2491
+
2492
+ This allows execution-time processing of JSON index values
2493
+ for special syntaxes.
2494
+
2495
+ """
2496
+
2497
+ class JSONPathType(JSONElementType):
2498
+ """Placeholder type for JSON path operations.
2499
+
2500
+ This allows execution-time processing of a path-based
2501
+ index value into a specific SQL syntax.
2502
+
2503
+ """
2504
+
2505
+ __visit_name__ = "json_path"
2506
+
2507
+ class Comparator(Indexable.Comparator[_T], Concatenable.Comparator[_T]):
2508
+ """Define comparison operations for :class:`_types.JSON`."""
2509
+
2510
+ __slots__ = ()
2511
+
2512
+ def _setup_getitem(self, index):
2513
+ if not isinstance(index, str) and isinstance(
2514
+ index, collections_abc.Sequence
2515
+ ):
2516
+ index = coercions.expect(
2517
+ roles.BinaryElementRole,
2518
+ index,
2519
+ expr=self.expr,
2520
+ operator=operators.json_path_getitem_op,
2521
+ bindparam_type=JSON.JSONPathType,
2522
+ )
2523
+
2524
+ operator = operators.json_path_getitem_op
2525
+ else:
2526
+ index = coercions.expect(
2527
+ roles.BinaryElementRole,
2528
+ index,
2529
+ expr=self.expr,
2530
+ operator=operators.json_getitem_op,
2531
+ bindparam_type=(
2532
+ JSON.JSONIntIndexType
2533
+ if isinstance(index, int)
2534
+ else JSON.JSONStrIndexType
2535
+ ),
2536
+ )
2537
+ operator = operators.json_getitem_op
2538
+
2539
+ return operator, index, self.type
2540
+
2541
+ def as_boolean(self):
2542
+ """Consider an indexed value as boolean.
2543
+
2544
+ This is similar to using :class:`_sql.type_coerce`, and will
2545
+ usually not apply a ``CAST()``.
2546
+
2547
+ e.g.::
2548
+
2549
+ stmt = select(
2550
+ mytable.c.json_column['some_data'].as_boolean()
2551
+ ).where(
2552
+ mytable.c.json_column['some_data'].as_boolean() == True
2553
+ )
2554
+
2555
+ .. versionadded:: 1.3.11
2556
+
2557
+ """
2558
+ return self._binary_w_type(Boolean(), "as_boolean")
2559
+
2560
+ def as_string(self):
2561
+ """Consider an indexed value as string.
2562
+
2563
+ This is similar to using :class:`_sql.type_coerce`, and will
2564
+ usually not apply a ``CAST()``.
2565
+
2566
+ e.g.::
2567
+
2568
+ stmt = select(
2569
+ mytable.c.json_column['some_data'].as_string()
2570
+ ).where(
2571
+ mytable.c.json_column['some_data'].as_string() ==
2572
+ 'some string'
2573
+ )
2574
+
2575
+ .. versionadded:: 1.3.11
2576
+
2577
+ """
2578
+ return self._binary_w_type(Unicode(), "as_string")
2579
+
2580
+ def as_integer(self):
2581
+ """Consider an indexed value as integer.
2582
+
2583
+ This is similar to using :class:`_sql.type_coerce`, and will
2584
+ usually not apply a ``CAST()``.
2585
+
2586
+ e.g.::
2587
+
2588
+ stmt = select(
2589
+ mytable.c.json_column['some_data'].as_integer()
2590
+ ).where(
2591
+ mytable.c.json_column['some_data'].as_integer() == 5
2592
+ )
2593
+
2594
+ .. versionadded:: 1.3.11
2595
+
2596
+ """
2597
+ return self._binary_w_type(Integer(), "as_integer")
2598
+
2599
+ def as_float(self):
2600
+ """Consider an indexed value as float.
2601
+
2602
+ This is similar to using :class:`_sql.type_coerce`, and will
2603
+ usually not apply a ``CAST()``.
2604
+
2605
+ e.g.::
2606
+
2607
+ stmt = select(
2608
+ mytable.c.json_column['some_data'].as_float()
2609
+ ).where(
2610
+ mytable.c.json_column['some_data'].as_float() == 29.75
2611
+ )
2612
+
2613
+ .. versionadded:: 1.3.11
2614
+
2615
+ """
2616
+ return self._binary_w_type(Float(), "as_float")
2617
+
2618
+ def as_numeric(self, precision, scale, asdecimal=True):
2619
+ """Consider an indexed value as numeric/decimal.
2620
+
2621
+ This is similar to using :class:`_sql.type_coerce`, and will
2622
+ usually not apply a ``CAST()``.
2623
+
2624
+ e.g.::
2625
+
2626
+ stmt = select(
2627
+ mytable.c.json_column['some_data'].as_numeric(10, 6)
2628
+ ).where(
2629
+ mytable.c.
2630
+ json_column['some_data'].as_numeric(10, 6) == 29.75
2631
+ )
2632
+
2633
+ .. versionadded:: 1.4.0b2
2634
+
2635
+ """
2636
+ return self._binary_w_type(
2637
+ Numeric(precision, scale, asdecimal=asdecimal), "as_numeric"
2638
+ )
2639
+
2640
+ def as_json(self):
2641
+ """Consider an indexed value as JSON.
2642
+
2643
+ This is similar to using :class:`_sql.type_coerce`, and will
2644
+ usually not apply a ``CAST()``.
2645
+
2646
+ e.g.::
2647
+
2648
+ stmt = select(mytable.c.json_column['some_data'].as_json())
2649
+
2650
+ This is typically the default behavior of indexed elements in any
2651
+ case.
2652
+
2653
+ Note that comparison of full JSON structures may not be
2654
+ supported by all backends.
2655
+
2656
+ .. versionadded:: 1.3.11
2657
+
2658
+ """
2659
+ return self.expr
2660
+
2661
+ def _binary_w_type(self, typ, method_name):
2662
+ if not isinstance(
2663
+ self.expr, elements.BinaryExpression
2664
+ ) or self.expr.operator not in (
2665
+ operators.json_getitem_op,
2666
+ operators.json_path_getitem_op,
2667
+ ):
2668
+ raise exc.InvalidRequestError(
2669
+ "The JSON cast operator JSON.%s() only works with a JSON "
2670
+ "index expression e.g. col['q'].%s()"
2671
+ % (method_name, method_name)
2672
+ )
2673
+ expr = self.expr._clone()
2674
+ expr.type = typ
2675
+ return expr
2676
+
2677
+ comparator_factory = Comparator
2678
+
2679
+ @property
2680
+ def python_type(self):
2681
+ return dict
2682
+
2683
+ @property # type: ignore # mypy property bug
2684
+ def should_evaluate_none(self):
2685
+ """Alias of :attr:`_types.JSON.none_as_null`"""
2686
+ return not self.none_as_null
2687
+
2688
+ @should_evaluate_none.setter
2689
+ def should_evaluate_none(self, value):
2690
+ self.none_as_null = not value
2691
+
2692
+ @util.memoized_property
2693
+ def _str_impl(self):
2694
+ return String()
2695
+
2696
+ def _make_bind_processor(self, string_process, json_serializer):
2697
+ if string_process:
2698
+
2699
+ def process(value):
2700
+ if value is self.NULL:
2701
+ value = None
2702
+ elif isinstance(value, elements.Null) or (
2703
+ value is None and self.none_as_null
2704
+ ):
2705
+ return None
2706
+
2707
+ serialized = json_serializer(value)
2708
+ return string_process(serialized)
2709
+
2710
+ else:
2711
+
2712
+ def process(value):
2713
+ if value is self.NULL:
2714
+ value = None
2715
+ elif isinstance(value, elements.Null) or (
2716
+ value is None and self.none_as_null
2717
+ ):
2718
+ return None
2719
+
2720
+ return json_serializer(value)
2721
+
2722
+ return process
2723
+
2724
+ def bind_processor(self, dialect):
2725
+ string_process = self._str_impl.bind_processor(dialect)
2726
+ json_serializer = dialect._json_serializer or json.dumps
2727
+
2728
+ return self._make_bind_processor(string_process, json_serializer)
2729
+
2730
+ def result_processor(self, dialect, coltype):
2731
+ string_process = self._str_impl.result_processor(dialect, coltype)
2732
+ json_deserializer = dialect._json_deserializer or json.loads
2733
+
2734
+ def process(value):
2735
+ if value is None:
2736
+ return None
2737
+ if string_process:
2738
+ value = string_process(value)
2739
+ return json_deserializer(value)
2740
+
2741
+ return process
2742
+
2743
+
2744
+ class ARRAY(
2745
+ SchemaEventTarget, Indexable, Concatenable, TypeEngine[Sequence[Any]]
2746
+ ):
2747
+ """Represent a SQL Array type.
2748
+
2749
+ .. note:: This type serves as the basis for all ARRAY operations.
2750
+ However, currently **only the PostgreSQL backend has support for SQL
2751
+ arrays in SQLAlchemy**. It is recommended to use the PostgreSQL-specific
2752
+ :class:`sqlalchemy.dialects.postgresql.ARRAY` type directly when using
2753
+ ARRAY types with PostgreSQL, as it provides additional operators
2754
+ specific to that backend.
2755
+
2756
+ :class:`_types.ARRAY` is part of the Core in support of various SQL
2757
+ standard functions such as :class:`_functions.array_agg`
2758
+ which explicitly involve
2759
+ arrays; however, with the exception of the PostgreSQL backend and possibly
2760
+ some third-party dialects, no other SQLAlchemy built-in dialect has support
2761
+ for this type.
2762
+
2763
+ An :class:`_types.ARRAY` type is constructed given the "type"
2764
+ of element::
2765
+
2766
+ mytable = Table("mytable", metadata,
2767
+ Column("data", ARRAY(Integer))
2768
+ )
2769
+
2770
+ The above type represents an N-dimensional array,
2771
+ meaning a supporting backend such as PostgreSQL will interpret values
2772
+ with any number of dimensions automatically. To produce an INSERT
2773
+ construct that passes in a 1-dimensional array of integers::
2774
+
2775
+ connection.execute(
2776
+ mytable.insert(),
2777
+ {"data": [1,2,3]}
2778
+ )
2779
+
2780
+ The :class:`_types.ARRAY` type can be constructed given a fixed number
2781
+ of dimensions::
2782
+
2783
+ mytable = Table("mytable", metadata,
2784
+ Column("data", ARRAY(Integer, dimensions=2))
2785
+ )
2786
+
2787
+ Sending a number of dimensions is optional, but recommended if the
2788
+ datatype is to represent arrays of more than one dimension. This number
2789
+ is used:
2790
+
2791
+ * When emitting the type declaration itself to the database, e.g.
2792
+ ``INTEGER[][]``
2793
+
2794
+ * When translating Python values to database values, and vice versa, e.g.
2795
+ an ARRAY of :class:`.Unicode` objects uses this number to efficiently
2796
+ access the string values inside of array structures without resorting
2797
+ to per-row type inspection
2798
+
2799
+ * When used with the Python ``getitem`` accessor, the number of dimensions
2800
+ serves to define the kind of type that the ``[]`` operator should
2801
+ return, e.g. for an ARRAY of INTEGER with two dimensions::
2802
+
2803
+ >>> expr = table.c.column[5] # returns ARRAY(Integer, dimensions=1)
2804
+ >>> expr = expr[6] # returns Integer
2805
+
2806
+ For 1-dimensional arrays, an :class:`_types.ARRAY` instance with no
2807
+ dimension parameter will generally assume single-dimensional behaviors.
2808
+
2809
+ SQL expressions of type :class:`_types.ARRAY` have support for "index" and
2810
+ "slice" behavior. The ``[]`` operator produces expression
2811
+ constructs which will produce the appropriate SQL, both for
2812
+ SELECT statements::
2813
+
2814
+ select(mytable.c.data[5], mytable.c.data[2:7])
2815
+
2816
+ as well as UPDATE statements when the :meth:`_expression.Update.values`
2817
+ method is used::
2818
+
2819
+ mytable.update().values({
2820
+ mytable.c.data[5]: 7,
2821
+ mytable.c.data[2:7]: [1, 2, 3]
2822
+ })
2823
+
2824
+ Indexed access is one-based by default;
2825
+ for zero-based index conversion, set :paramref:`_types.ARRAY.zero_indexes`.
2826
+
2827
+ The :class:`_types.ARRAY` type also provides for the operators
2828
+ :meth:`.types.ARRAY.Comparator.any` and
2829
+ :meth:`.types.ARRAY.Comparator.all`. The PostgreSQL-specific version of
2830
+ :class:`_types.ARRAY` also provides additional operators.
2831
+
2832
+ .. container:: topic
2833
+
2834
+ **Detecting Changes in ARRAY columns when using the ORM**
2835
+
2836
+ The :class:`_sqltypes.ARRAY` type, when used with the SQLAlchemy ORM,
2837
+ does not detect in-place mutations to the array. In order to detect
2838
+ these, the :mod:`sqlalchemy.ext.mutable` extension must be used, using
2839
+ the :class:`.MutableList` class::
2840
+
2841
+ from sqlalchemy import ARRAY
2842
+ from sqlalchemy.ext.mutable import MutableList
2843
+
2844
+ class SomeOrmClass(Base):
2845
+ # ...
2846
+
2847
+ data = Column(MutableList.as_mutable(ARRAY(Integer)))
2848
+
2849
+ This extension will allow "in-place" changes such to the array
2850
+ such as ``.append()`` to produce events which will be detected by the
2851
+ unit of work. Note that changes to elements **inside** the array,
2852
+ including subarrays that are mutated in place, are **not** detected.
2853
+
2854
+ Alternatively, assigning a new array value to an ORM element that
2855
+ replaces the old one will always trigger a change event.
2856
+
2857
+ .. seealso::
2858
+
2859
+ :class:`sqlalchemy.dialects.postgresql.ARRAY`
2860
+
2861
+ """
2862
+
2863
+ __visit_name__ = "ARRAY"
2864
+
2865
+ _is_array = True
2866
+
2867
+ zero_indexes = False
2868
+ """If True, Python zero-based indexes should be interpreted as one-based
2869
+ on the SQL expression side."""
2870
+
2871
+ def __init__(
2872
+ self,
2873
+ item_type: _TypeEngineArgument[Any],
2874
+ as_tuple: bool = False,
2875
+ dimensions: Optional[int] = None,
2876
+ zero_indexes: bool = False,
2877
+ ):
2878
+ """Construct an :class:`_types.ARRAY`.
2879
+
2880
+ E.g.::
2881
+
2882
+ Column('myarray', ARRAY(Integer))
2883
+
2884
+ Arguments are:
2885
+
2886
+ :param item_type: The data type of items of this array. Note that
2887
+ dimensionality is irrelevant here, so multi-dimensional arrays like
2888
+ ``INTEGER[][]``, are constructed as ``ARRAY(Integer)``, not as
2889
+ ``ARRAY(ARRAY(Integer))`` or such.
2890
+
2891
+ :param as_tuple=False: Specify whether return results
2892
+ should be converted to tuples from lists. This parameter is
2893
+ not generally needed as a Python list corresponds well
2894
+ to a SQL array.
2895
+
2896
+ :param dimensions: if non-None, the ARRAY will assume a fixed
2897
+ number of dimensions. This impacts how the array is declared
2898
+ on the database, how it goes about interpreting Python and
2899
+ result values, as well as how expression behavior in conjunction
2900
+ with the "getitem" operator works. See the description at
2901
+ :class:`_types.ARRAY` for additional detail.
2902
+
2903
+ :param zero_indexes=False: when True, index values will be converted
2904
+ between Python zero-based and SQL one-based indexes, e.g.
2905
+ a value of one will be added to all index values before passing
2906
+ to the database.
2907
+
2908
+ """
2909
+ if isinstance(item_type, ARRAY):
2910
+ raise ValueError(
2911
+ "Do not nest ARRAY types; ARRAY(basetype) "
2912
+ "handles multi-dimensional arrays of basetype"
2913
+ )
2914
+ if isinstance(item_type, type):
2915
+ item_type = item_type()
2916
+ self.item_type = item_type
2917
+ self.as_tuple = as_tuple
2918
+ self.dimensions = dimensions
2919
+ self.zero_indexes = zero_indexes
2920
+
2921
+ class Comparator(
2922
+ Indexable.Comparator[Sequence[Any]],
2923
+ Concatenable.Comparator[Sequence[Any]],
2924
+ ):
2925
+ """Define comparison operations for :class:`_types.ARRAY`.
2926
+
2927
+ More operators are available on the dialect-specific form
2928
+ of this type. See :class:`.postgresql.ARRAY.Comparator`.
2929
+
2930
+ """
2931
+
2932
+ __slots__ = ()
2933
+
2934
+ type: ARRAY
2935
+
2936
+ def _setup_getitem(self, index):
2937
+ arr_type = self.type
2938
+
2939
+ return_type: TypeEngine[Any]
2940
+
2941
+ if isinstance(index, slice):
2942
+ return_type = arr_type
2943
+ if arr_type.zero_indexes:
2944
+ index = slice(index.start + 1, index.stop + 1, index.step)
2945
+ slice_ = Slice(
2946
+ index.start, index.stop, index.step, _name=self.expr.key
2947
+ )
2948
+ return operators.getitem, slice_, return_type
2949
+ else:
2950
+ if arr_type.zero_indexes:
2951
+ index += 1
2952
+ if arr_type.dimensions is None or arr_type.dimensions == 1:
2953
+ return_type = arr_type.item_type
2954
+ else:
2955
+ adapt_kw = {"dimensions": arr_type.dimensions - 1}
2956
+ return_type = arr_type.adapt(
2957
+ arr_type.__class__, **adapt_kw
2958
+ )
2959
+
2960
+ return operators.getitem, index, return_type
2961
+
2962
+ def contains(self, *arg, **kw):
2963
+ """``ARRAY.contains()`` not implemented for the base ARRAY type.
2964
+ Use the dialect-specific ARRAY type.
2965
+
2966
+ .. seealso::
2967
+
2968
+ :class:`_postgresql.ARRAY` - PostgreSQL specific version.
2969
+ """
2970
+ raise NotImplementedError(
2971
+ "ARRAY.contains() not implemented for the base "
2972
+ "ARRAY type; please use the dialect-specific ARRAY type"
2973
+ )
2974
+
2975
+ @util.preload_module("sqlalchemy.sql.elements")
2976
+ def any(self, other, operator=None):
2977
+ """Return ``other operator ANY (array)`` clause.
2978
+
2979
+ .. legacy:: This method is an :class:`_types.ARRAY` - specific
2980
+ construct that is now superseded by the :func:`_sql.any_`
2981
+ function, which features a different calling style. The
2982
+ :func:`_sql.any_` function is also mirrored at the method level
2983
+ via the :meth:`_sql.ColumnOperators.any_` method.
2984
+
2985
+ Usage of array-specific :meth:`_types.ARRAY.Comparator.any`
2986
+ is as follows::
2987
+
2988
+ from sqlalchemy.sql import operators
2989
+
2990
+ conn.execute(
2991
+ select(table.c.data).where(
2992
+ table.c.data.any(7, operator=operators.lt)
2993
+ )
2994
+ )
2995
+
2996
+ :param other: expression to be compared
2997
+ :param operator: an operator object from the
2998
+ :mod:`sqlalchemy.sql.operators`
2999
+ package, defaults to :func:`.operators.eq`.
3000
+
3001
+ .. seealso::
3002
+
3003
+ :func:`_expression.any_`
3004
+
3005
+ :meth:`.types.ARRAY.Comparator.all`
3006
+
3007
+ """
3008
+ elements = util.preloaded.sql_elements
3009
+ operator = operator if operator else operators.eq
3010
+
3011
+ arr_type = self.type
3012
+
3013
+ return elements.CollectionAggregate._create_any(self.expr).operate(
3014
+ operators.mirror(operator),
3015
+ coercions.expect(
3016
+ roles.BinaryElementRole,
3017
+ element=other,
3018
+ operator=operator,
3019
+ expr=self.expr,
3020
+ bindparam_type=arr_type.item_type,
3021
+ ),
3022
+ )
3023
+
3024
+ @util.preload_module("sqlalchemy.sql.elements")
3025
+ def all(self, other, operator=None):
3026
+ """Return ``other operator ALL (array)`` clause.
3027
+
3028
+ .. legacy:: This method is an :class:`_types.ARRAY` - specific
3029
+ construct that is now superseded by the :func:`_sql.all_`
3030
+ function, which features a different calling style. The
3031
+ :func:`_sql.all_` function is also mirrored at the method level
3032
+ via the :meth:`_sql.ColumnOperators.all_` method.
3033
+
3034
+ Usage of array-specific :meth:`_types.ARRAY.Comparator.all`
3035
+ is as follows::
3036
+
3037
+ from sqlalchemy.sql import operators
3038
+
3039
+ conn.execute(
3040
+ select(table.c.data).where(
3041
+ table.c.data.all(7, operator=operators.lt)
3042
+ )
3043
+ )
3044
+
3045
+ :param other: expression to be compared
3046
+ :param operator: an operator object from the
3047
+ :mod:`sqlalchemy.sql.operators`
3048
+ package, defaults to :func:`.operators.eq`.
3049
+
3050
+ .. seealso::
3051
+
3052
+ :func:`_expression.all_`
3053
+
3054
+ :meth:`.types.ARRAY.Comparator.any`
3055
+
3056
+ """
3057
+ elements = util.preloaded.sql_elements
3058
+ operator = operator if operator else operators.eq
3059
+
3060
+ arr_type = self.type
3061
+
3062
+ return elements.CollectionAggregate._create_all(self.expr).operate(
3063
+ operators.mirror(operator),
3064
+ coercions.expect(
3065
+ roles.BinaryElementRole,
3066
+ element=other,
3067
+ operator=operator,
3068
+ expr=self.expr,
3069
+ bindparam_type=arr_type.item_type,
3070
+ ),
3071
+ )
3072
+
3073
+ comparator_factory = Comparator
3074
+
3075
+ @property
3076
+ def hashable(self):
3077
+ return self.as_tuple
3078
+
3079
+ @property
3080
+ def python_type(self):
3081
+ return list
3082
+
3083
+ def compare_values(self, x, y):
3084
+ return x == y
3085
+
3086
+ def _set_parent(self, parent, outer=False, **kw):
3087
+ """Support SchemaEventTarget"""
3088
+
3089
+ if not outer and isinstance(self.item_type, SchemaEventTarget):
3090
+ self.item_type._set_parent(parent, **kw)
3091
+
3092
+ def _set_parent_with_dispatch(self, parent, **kw):
3093
+ """Support SchemaEventTarget"""
3094
+
3095
+ super()._set_parent_with_dispatch(parent, outer=True)
3096
+
3097
+ if isinstance(self.item_type, SchemaEventTarget):
3098
+ self.item_type._set_parent_with_dispatch(parent)
3099
+
3100
+ def literal_processor(self, dialect):
3101
+ item_proc = self.item_type.dialect_impl(dialect).literal_processor(
3102
+ dialect
3103
+ )
3104
+ if item_proc is None:
3105
+ return None
3106
+
3107
+ def to_str(elements):
3108
+ return f"[{', '.join(elements)}]"
3109
+
3110
+ def process(value):
3111
+ inner = self._apply_item_processor(
3112
+ value, item_proc, self.dimensions, to_str
3113
+ )
3114
+ return inner
3115
+
3116
+ return process
3117
+
3118
+ def _apply_item_processor(self, arr, itemproc, dim, collection_callable):
3119
+ """Helper method that can be used by bind_processor(),
3120
+ literal_processor(), etc. to apply an item processor to elements of
3121
+ an array value, taking into account the 'dimensions' for this
3122
+ array type.
3123
+
3124
+ See the Postgresql ARRAY datatype for usage examples.
3125
+
3126
+ .. versionadded:: 2.0
3127
+
3128
+ """
3129
+
3130
+ if dim is None:
3131
+ arr = list(arr)
3132
+ if (
3133
+ dim == 1
3134
+ or dim is None
3135
+ and (
3136
+ # this has to be (list, tuple), or at least
3137
+ # not hasattr('__iter__'), since Py3K strings
3138
+ # etc. have __iter__
3139
+ not arr
3140
+ or not isinstance(arr[0], (list, tuple))
3141
+ )
3142
+ ):
3143
+ if itemproc:
3144
+ return collection_callable(itemproc(x) for x in arr)
3145
+ else:
3146
+ return collection_callable(arr)
3147
+ else:
3148
+ return collection_callable(
3149
+ (
3150
+ self._apply_item_processor(
3151
+ x,
3152
+ itemproc,
3153
+ dim - 1 if dim is not None else None,
3154
+ collection_callable,
3155
+ )
3156
+ if x is not None
3157
+ else None
3158
+ )
3159
+ for x in arr
3160
+ )
3161
+
3162
+
3163
+ class TupleType(TypeEngine[Tuple[Any, ...]]):
3164
+ """represent the composite type of a Tuple."""
3165
+
3166
+ _is_tuple_type = True
3167
+
3168
+ types: List[TypeEngine[Any]]
3169
+
3170
+ def __init__(self, *types: _TypeEngineArgument[Any]):
3171
+ self._fully_typed = NULLTYPE not in types
3172
+ self.types = [
3173
+ item_type() if isinstance(item_type, type) else item_type
3174
+ for item_type in types
3175
+ ]
3176
+
3177
+ def coerce_compared_value(
3178
+ self, op: Optional[OperatorType], value: Any
3179
+ ) -> TypeEngine[Any]:
3180
+ if value is type_api._NO_VALUE_IN_LIST:
3181
+ return super().coerce_compared_value(op, value)
3182
+ else:
3183
+ return TupleType(
3184
+ *[
3185
+ typ.coerce_compared_value(op, elem)
3186
+ for typ, elem in zip(self.types, value)
3187
+ ]
3188
+ )
3189
+
3190
+ def _resolve_values_to_types(self, value: Any) -> TupleType:
3191
+ if self._fully_typed:
3192
+ return self
3193
+ else:
3194
+ return TupleType(
3195
+ *[
3196
+ _resolve_value_to_type(elem) if typ is NULLTYPE else typ
3197
+ for typ, elem in zip(self.types, value)
3198
+ ]
3199
+ )
3200
+
3201
+ def result_processor(self, dialect, coltype):
3202
+ raise NotImplementedError(
3203
+ "The tuple type does not support being fetched "
3204
+ "as a column in a result row."
3205
+ )
3206
+
3207
+
3208
+ class REAL(Float[_N]):
3209
+ """The SQL REAL type.
3210
+
3211
+ .. seealso::
3212
+
3213
+ :class:`_types.Float` - documentation for the base type.
3214
+
3215
+ """
3216
+
3217
+ __visit_name__ = "REAL"
3218
+
3219
+
3220
+ class FLOAT(Float[_N]):
3221
+ """The SQL FLOAT type.
3222
+
3223
+ .. seealso::
3224
+
3225
+ :class:`_types.Float` - documentation for the base type.
3226
+
3227
+ """
3228
+
3229
+ __visit_name__ = "FLOAT"
3230
+
3231
+
3232
+ class DOUBLE(Double[_N]):
3233
+ """The SQL DOUBLE type.
3234
+
3235
+ .. versionadded:: 2.0
3236
+
3237
+ .. seealso::
3238
+
3239
+ :class:`_types.Double` - documentation for the base type.
3240
+
3241
+ """
3242
+
3243
+ __visit_name__ = "DOUBLE"
3244
+
3245
+
3246
+ class DOUBLE_PRECISION(Double[_N]):
3247
+ """The SQL DOUBLE PRECISION type.
3248
+
3249
+ .. versionadded:: 2.0
3250
+
3251
+ .. seealso::
3252
+
3253
+ :class:`_types.Double` - documentation for the base type.
3254
+
3255
+ """
3256
+
3257
+ __visit_name__ = "DOUBLE_PRECISION"
3258
+
3259
+
3260
+ class NUMERIC(Numeric[_N]):
3261
+ """The SQL NUMERIC type.
3262
+
3263
+ .. seealso::
3264
+
3265
+ :class:`_types.Numeric` - documentation for the base type.
3266
+
3267
+ """
3268
+
3269
+ __visit_name__ = "NUMERIC"
3270
+
3271
+
3272
+ class DECIMAL(Numeric[_N]):
3273
+ """The SQL DECIMAL type.
3274
+
3275
+ .. seealso::
3276
+
3277
+ :class:`_types.Numeric` - documentation for the base type.
3278
+
3279
+ """
3280
+
3281
+ __visit_name__ = "DECIMAL"
3282
+
3283
+
3284
+ class INTEGER(Integer):
3285
+ """The SQL INT or INTEGER type.
3286
+
3287
+ .. seealso::
3288
+
3289
+ :class:`_types.Integer` - documentation for the base type.
3290
+
3291
+ """
3292
+
3293
+ __visit_name__ = "INTEGER"
3294
+
3295
+
3296
+ INT = INTEGER
3297
+
3298
+
3299
+ class SMALLINT(SmallInteger):
3300
+ """The SQL SMALLINT type.
3301
+
3302
+ .. seealso::
3303
+
3304
+ :class:`_types.SmallInteger` - documentation for the base type.
3305
+
3306
+ """
3307
+
3308
+ __visit_name__ = "SMALLINT"
3309
+
3310
+
3311
+ class BIGINT(BigInteger):
3312
+ """The SQL BIGINT type.
3313
+
3314
+ .. seealso::
3315
+
3316
+ :class:`_types.BigInteger` - documentation for the base type.
3317
+
3318
+ """
3319
+
3320
+ __visit_name__ = "BIGINT"
3321
+
3322
+
3323
+ class TIMESTAMP(DateTime):
3324
+ """The SQL TIMESTAMP type.
3325
+
3326
+ :class:`_types.TIMESTAMP` datatypes have support for timezone
3327
+ storage on some backends, such as PostgreSQL and Oracle. Use the
3328
+ :paramref:`~types.TIMESTAMP.timezone` argument in order to enable
3329
+ "TIMESTAMP WITH TIMEZONE" for these backends.
3330
+
3331
+ """
3332
+
3333
+ __visit_name__ = "TIMESTAMP"
3334
+
3335
+ def __init__(self, timezone: bool = False):
3336
+ """Construct a new :class:`_types.TIMESTAMP`.
3337
+
3338
+ :param timezone: boolean. Indicates that the TIMESTAMP type should
3339
+ enable timezone support, if available on the target database.
3340
+ On a per-dialect basis is similar to "TIMESTAMP WITH TIMEZONE".
3341
+ If the target database does not support timezones, this flag is
3342
+ ignored.
3343
+
3344
+
3345
+ """
3346
+ super().__init__(timezone=timezone)
3347
+
3348
+ def get_dbapi_type(self, dbapi):
3349
+ return dbapi.TIMESTAMP
3350
+
3351
+
3352
+ class DATETIME(DateTime):
3353
+ """The SQL DATETIME type."""
3354
+
3355
+ __visit_name__ = "DATETIME"
3356
+
3357
+
3358
+ class DATE(Date):
3359
+ """The SQL DATE type."""
3360
+
3361
+ __visit_name__ = "DATE"
3362
+
3363
+
3364
+ class TIME(Time):
3365
+ """The SQL TIME type."""
3366
+
3367
+ __visit_name__ = "TIME"
3368
+
3369
+
3370
+ class TEXT(Text):
3371
+ """The SQL TEXT type."""
3372
+
3373
+ __visit_name__ = "TEXT"
3374
+
3375
+
3376
+ class CLOB(Text):
3377
+ """The CLOB type.
3378
+
3379
+ This type is found in Oracle and Informix.
3380
+ """
3381
+
3382
+ __visit_name__ = "CLOB"
3383
+
3384
+
3385
+ class VARCHAR(String):
3386
+ """The SQL VARCHAR type."""
3387
+
3388
+ __visit_name__ = "VARCHAR"
3389
+
3390
+
3391
+ class NVARCHAR(Unicode):
3392
+ """The SQL NVARCHAR type."""
3393
+
3394
+ __visit_name__ = "NVARCHAR"
3395
+
3396
+
3397
+ class CHAR(String):
3398
+ """The SQL CHAR type."""
3399
+
3400
+ __visit_name__ = "CHAR"
3401
+
3402
+
3403
+ class NCHAR(Unicode):
3404
+ """The SQL NCHAR type."""
3405
+
3406
+ __visit_name__ = "NCHAR"
3407
+
3408
+
3409
+ class BLOB(LargeBinary):
3410
+ """The SQL BLOB type."""
3411
+
3412
+ __visit_name__ = "BLOB"
3413
+
3414
+
3415
+ class BINARY(_Binary):
3416
+ """The SQL BINARY type."""
3417
+
3418
+ __visit_name__ = "BINARY"
3419
+
3420
+
3421
+ class VARBINARY(_Binary):
3422
+ """The SQL VARBINARY type."""
3423
+
3424
+ __visit_name__ = "VARBINARY"
3425
+
3426
+
3427
+ class BOOLEAN(Boolean):
3428
+ """The SQL BOOLEAN type."""
3429
+
3430
+ __visit_name__ = "BOOLEAN"
3431
+
3432
+
3433
+ class NullType(TypeEngine[None]):
3434
+ """An unknown type.
3435
+
3436
+ :class:`.NullType` is used as a default type for those cases where
3437
+ a type cannot be determined, including:
3438
+
3439
+ * During table reflection, when the type of a column is not recognized
3440
+ by the :class:`.Dialect`
3441
+ * When constructing SQL expressions using plain Python objects of
3442
+ unknown types (e.g. ``somecolumn == my_special_object``)
3443
+ * When a new :class:`_schema.Column` is created,
3444
+ and the given type is passed
3445
+ as ``None`` or is not passed at all.
3446
+
3447
+ The :class:`.NullType` can be used within SQL expression invocation
3448
+ without issue, it just has no behavior either at the expression
3449
+ construction level or at the bind-parameter/result processing level.
3450
+ :class:`.NullType` will result in a :exc:`.CompileError` if the compiler
3451
+ is asked to render the type itself, such as if it is used in a
3452
+ :func:`.cast` operation or within a schema creation operation such as that
3453
+ invoked by :meth:`_schema.MetaData.create_all` or the
3454
+ :class:`.CreateTable`
3455
+ construct.
3456
+
3457
+ """
3458
+
3459
+ __visit_name__ = "null"
3460
+
3461
+ _isnull = True
3462
+
3463
+ def literal_processor(self, dialect):
3464
+ return None
3465
+
3466
+ class Comparator(TypeEngine.Comparator[_T]):
3467
+ __slots__ = ()
3468
+
3469
+ def _adapt_expression(
3470
+ self,
3471
+ op: OperatorType,
3472
+ other_comparator: TypeEngine.Comparator[Any],
3473
+ ) -> Tuple[OperatorType, TypeEngine[Any]]:
3474
+ if isinstance(
3475
+ other_comparator, NullType.Comparator
3476
+ ) or not operators.is_commutative(op):
3477
+ return op, self.expr.type
3478
+ else:
3479
+ return other_comparator._adapt_expression(op, self)
3480
+
3481
+ comparator_factory = Comparator
3482
+
3483
+
3484
+ class TableValueType(HasCacheKey, TypeEngine[Any]):
3485
+ """Refers to a table value type."""
3486
+
3487
+ _is_table_value = True
3488
+
3489
+ _traverse_internals = [
3490
+ ("_elements", InternalTraversal.dp_clauseelement_list),
3491
+ ]
3492
+
3493
+ def __init__(self, *elements: Union[str, _ColumnExpressionArgument[Any]]):
3494
+ self._elements = [
3495
+ coercions.expect(roles.StrAsPlainColumnRole, elem)
3496
+ for elem in elements
3497
+ ]
3498
+
3499
+
3500
+ class MatchType(Boolean):
3501
+ """Refers to the return type of the MATCH operator.
3502
+
3503
+ As the :meth:`.ColumnOperators.match` is probably the most open-ended
3504
+ operator in generic SQLAlchemy Core, we can't assume the return type
3505
+ at SQL evaluation time, as MySQL returns a floating point, not a boolean,
3506
+ and other backends might do something different. So this type
3507
+ acts as a placeholder, currently subclassing :class:`.Boolean`.
3508
+ The type allows dialects to inject result-processing functionality
3509
+ if needed, and on MySQL will return floating-point values.
3510
+
3511
+ """
3512
+
3513
+
3514
+ _UUID_RETURN = TypeVar("_UUID_RETURN", str, _python_UUID)
3515
+
3516
+
3517
+ class Uuid(Emulated, TypeEngine[_UUID_RETURN]):
3518
+ """Represent a database agnostic UUID datatype.
3519
+
3520
+ For backends that have no "native" UUID datatype, the value will
3521
+ make use of ``CHAR(32)`` and store the UUID as a 32-character alphanumeric
3522
+ hex string.
3523
+
3524
+ For backends which are known to support ``UUID`` directly or a similar
3525
+ uuid-storing datatype such as SQL Server's ``UNIQUEIDENTIFIER``, a
3526
+ "native" mode enabled by default allows these types will be used on those
3527
+ backends.
3528
+
3529
+ In its default mode of use, the :class:`_sqltypes.Uuid` datatype expects
3530
+ **Python uuid objects**, from the Python
3531
+ `uuid <https://docs.python.org/3/library/uuid.html>`_
3532
+ module::
3533
+
3534
+ import uuid
3535
+
3536
+ from sqlalchemy import Uuid
3537
+ from sqlalchemy import Table, Column, MetaData, String
3538
+
3539
+
3540
+ metadata_obj = MetaData()
3541
+
3542
+ t = Table(
3543
+ "t",
3544
+ metadata_obj,
3545
+ Column('uuid_data', Uuid, primary_key=True),
3546
+ Column("other_data", String)
3547
+ )
3548
+
3549
+ with engine.begin() as conn:
3550
+ conn.execute(
3551
+ t.insert(),
3552
+ {"uuid_data": uuid.uuid4(), "other_data", "some data"}
3553
+ )
3554
+
3555
+ To have the :class:`_sqltypes.Uuid` datatype work with string-based
3556
+ Uuids (e.g. 32 character hexadecimal strings), pass the
3557
+ :paramref:`_sqltypes.Uuid.as_uuid` parameter with the value ``False``.
3558
+
3559
+ .. versionadded:: 2.0
3560
+
3561
+ .. seealso::
3562
+
3563
+ :class:`_sqltypes.UUID` - represents exactly the ``UUID`` datatype
3564
+ without any backend-agnostic behaviors.
3565
+
3566
+ """
3567
+
3568
+ __visit_name__ = "uuid"
3569
+
3570
+ collation: Optional[str] = None
3571
+
3572
+ @overload
3573
+ def __init__(
3574
+ self: Uuid[_python_UUID],
3575
+ as_uuid: Literal[True] = ...,
3576
+ native_uuid: bool = ...,
3577
+ ): ...
3578
+
3579
+ @overload
3580
+ def __init__(
3581
+ self: Uuid[str],
3582
+ as_uuid: Literal[False] = ...,
3583
+ native_uuid: bool = ...,
3584
+ ): ...
3585
+
3586
+ def __init__(self, as_uuid: bool = True, native_uuid: bool = True):
3587
+ """Construct a :class:`_sqltypes.Uuid` type.
3588
+
3589
+ :param as_uuid=True: if True, values will be interpreted
3590
+ as Python uuid objects, converting to/from string via the
3591
+ DBAPI.
3592
+
3593
+ .. versionchanged: 2.0 ``as_uuid`` now defaults to ``True``.
3594
+
3595
+ :param native_uuid=True: if True, backends that support either the
3596
+ ``UUID`` datatype directly, or a UUID-storing value
3597
+ (such as SQL Server's ``UNIQUEIDENTIFIER`` will be used by those
3598
+ backends. If False, a ``CHAR(32)`` datatype will be used for
3599
+ all backends regardless of native support.
3600
+
3601
+ """
3602
+ self.as_uuid = as_uuid
3603
+ self.native_uuid = native_uuid
3604
+
3605
+ @property
3606
+ def python_type(self):
3607
+ return _python_UUID if self.as_uuid else str
3608
+
3609
+ @property
3610
+ def native(self):
3611
+ return self.native_uuid
3612
+
3613
+ def coerce_compared_value(self, op, value):
3614
+ """See :meth:`.TypeEngine.coerce_compared_value` for a description."""
3615
+
3616
+ if isinstance(value, str):
3617
+ return self
3618
+ else:
3619
+ return super().coerce_compared_value(op, value)
3620
+
3621
+ def bind_processor(self, dialect):
3622
+ character_based_uuid = (
3623
+ not dialect.supports_native_uuid or not self.native_uuid
3624
+ )
3625
+
3626
+ if character_based_uuid:
3627
+ if self.as_uuid:
3628
+
3629
+ def process(value):
3630
+ if value is not None:
3631
+ value = value.hex
3632
+ return value
3633
+
3634
+ return process
3635
+ else:
3636
+
3637
+ def process(value):
3638
+ if value is not None:
3639
+ value = value.replace("-", "")
3640
+ return value
3641
+
3642
+ return process
3643
+ else:
3644
+ return None
3645
+
3646
+ def result_processor(self, dialect, coltype):
3647
+ character_based_uuid = (
3648
+ not dialect.supports_native_uuid or not self.native_uuid
3649
+ )
3650
+
3651
+ if character_based_uuid:
3652
+ if self.as_uuid:
3653
+
3654
+ def process(value):
3655
+ if value is not None:
3656
+ value = _python_UUID(value)
3657
+ return value
3658
+
3659
+ return process
3660
+ else:
3661
+
3662
+ def process(value):
3663
+ if value is not None:
3664
+ value = str(_python_UUID(value))
3665
+ return value
3666
+
3667
+ return process
3668
+ else:
3669
+ if not self.as_uuid:
3670
+
3671
+ def process(value):
3672
+ if value is not None:
3673
+ value = str(value)
3674
+ return value
3675
+
3676
+ return process
3677
+ else:
3678
+ return None
3679
+
3680
+ def literal_processor(self, dialect):
3681
+ character_based_uuid = (
3682
+ not dialect.supports_native_uuid or not self.native_uuid
3683
+ )
3684
+
3685
+ if not self.as_uuid:
3686
+
3687
+ def process(value):
3688
+ return f"""'{value.replace("-", "").replace("'", "''")}'"""
3689
+
3690
+ return process
3691
+ else:
3692
+ if character_based_uuid:
3693
+
3694
+ def process(value):
3695
+ return f"""'{value.hex}'"""
3696
+
3697
+ return process
3698
+ else:
3699
+
3700
+ def process(value):
3701
+ return f"""'{str(value).replace("'", "''")}'"""
3702
+
3703
+ return process
3704
+
3705
+
3706
+ class UUID(Uuid[_UUID_RETURN], type_api.NativeForEmulated):
3707
+ """Represent the SQL UUID type.
3708
+
3709
+ This is the SQL-native form of the :class:`_types.Uuid` database agnostic
3710
+ datatype, and is backwards compatible with the previous PostgreSQL-only
3711
+ version of ``UUID``.
3712
+
3713
+ The :class:`_sqltypes.UUID` datatype only works on databases that have a
3714
+ SQL datatype named ``UUID``. It will not function for backends which don't
3715
+ have this exact-named type, including SQL Server. For backend-agnostic UUID
3716
+ values with native support, including for SQL Server's ``UNIQUEIDENTIFIER``
3717
+ datatype, use the :class:`_sqltypes.Uuid` datatype.
3718
+
3719
+ .. versionadded:: 2.0
3720
+
3721
+ .. seealso::
3722
+
3723
+ :class:`_sqltypes.Uuid`
3724
+
3725
+ """
3726
+
3727
+ __visit_name__ = "UUID"
3728
+
3729
+ @overload
3730
+ def __init__(self: UUID[_python_UUID], as_uuid: Literal[True] = ...): ...
3731
+
3732
+ @overload
3733
+ def __init__(self: UUID[str], as_uuid: Literal[False] = ...): ...
3734
+
3735
+ def __init__(self, as_uuid: bool = True):
3736
+ """Construct a :class:`_sqltypes.UUID` type.
3737
+
3738
+
3739
+ :param as_uuid=True: if True, values will be interpreted
3740
+ as Python uuid objects, converting to/from string via the
3741
+ DBAPI.
3742
+
3743
+ .. versionchanged: 2.0 ``as_uuid`` now defaults to ``True``.
3744
+
3745
+ """
3746
+ self.as_uuid = as_uuid
3747
+ self.native_uuid = True
3748
+
3749
+ @classmethod
3750
+ def adapt_emulated_to_native(cls, impl, **kw):
3751
+ kw.setdefault("as_uuid", impl.as_uuid)
3752
+ return cls(**kw)
3753
+
3754
+
3755
+ NULLTYPE = NullType()
3756
+ BOOLEANTYPE = Boolean()
3757
+ STRINGTYPE = String()
3758
+ INTEGERTYPE = Integer()
3759
+ NUMERICTYPE: Numeric[decimal.Decimal] = Numeric()
3760
+ MATCHTYPE = MatchType()
3761
+ TABLEVALUE = TableValueType()
3762
+ DATETIME_TIMEZONE = DateTime(timezone=True)
3763
+ TIME_TIMEZONE = Time(timezone=True)
3764
+ _BIGINTEGER = BigInteger()
3765
+ _DATETIME = DateTime()
3766
+ _TIME = Time()
3767
+ _STRING = String()
3768
+ _UNICODE = Unicode()
3769
+
3770
+ _type_map: Dict[Type[Any], TypeEngine[Any]] = {
3771
+ int: Integer(),
3772
+ float: Float(),
3773
+ bool: BOOLEANTYPE,
3774
+ _python_UUID: Uuid(),
3775
+ decimal.Decimal: Numeric(),
3776
+ dt.date: Date(),
3777
+ dt.datetime: _DATETIME,
3778
+ dt.time: _TIME,
3779
+ dt.timedelta: Interval(),
3780
+ type(None): NULLTYPE,
3781
+ bytes: LargeBinary(),
3782
+ str: _STRING,
3783
+ enum.Enum: Enum(enum.Enum),
3784
+ Literal: Enum(enum.Enum), # type: ignore[dict-item]
3785
+ }
3786
+
3787
+
3788
+ _type_map_get = _type_map.get
3789
+
3790
+
3791
+ def _resolve_value_to_type(value: Any) -> TypeEngine[Any]:
3792
+ _result_type = _type_map_get(type(value), False)
3793
+
3794
+ if _result_type is False:
3795
+ _result_type = getattr(value, "__sa_type_engine__", False)
3796
+
3797
+ if _result_type is False:
3798
+ # use inspect() to detect SQLAlchemy built-in
3799
+ # objects.
3800
+ insp = inspection.inspect(value, False)
3801
+ if (
3802
+ insp is not None
3803
+ and
3804
+ # foil mock.Mock() and other impostors by ensuring
3805
+ # the inspection target itself self-inspects
3806
+ insp.__class__ in inspection._registrars
3807
+ ):
3808
+ raise exc.ArgumentError(
3809
+ "Object %r is not legal as a SQL literal value" % (value,)
3810
+ )
3811
+ return NULLTYPE
3812
+ else:
3813
+ return _result_type._resolve_for_literal( # type: ignore [union-attr]
3814
+ value
3815
+ )
3816
+
3817
+
3818
+ # back-assign to type_api
3819
+ type_api.BOOLEANTYPE = BOOLEANTYPE
3820
+ type_api.STRINGTYPE = STRINGTYPE
3821
+ type_api.INTEGERTYPE = INTEGERTYPE
3822
+ type_api.NULLTYPE = NULLTYPE
3823
+ type_api.NUMERICTYPE = NUMERICTYPE
3824
+ type_api.MATCHTYPE = MATCHTYPE
3825
+ type_api.INDEXABLE = INDEXABLE = Indexable
3826
+ type_api.TABLEVALUE = TABLEVALUE
3827
+ type_api._resolve_value_to_type = _resolve_value_to_type