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