diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-09-23 12:19:56 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-09-25 19:30:51 -0400 |
commit | 35a1f5481a28837490037a6bd99d63590f748933 (patch) | |
tree | cd42d87738d90c015fdb09a93f2f379234982360 /lib/sqlalchemy/dialects/postgresql/base.py | |
parent | 08430e689f1a0190f671636016b12ff2ca08531f (diff) | |
download | sqlalchemy-35a1f5481a28837490037a6bd99d63590f748933.tar.gz |
Make a common approach for "emulated" types
Internal refinements to the :class:`.Enum`, :class:`.Interval`, and
:class:`.Boolean` types, which now extend a common mixin
:class:`.Emulated` that indicates a type that provides Python-side
emulation of a DB native type, switching out to the DB native type when a
supporting backend is in use. The Postgresql :class:`.INTERVAL` type
when used directly will now include the correct type coercion rules for
SQL expressions that also take effect for :class:`.sqltypes.Interval`
(such as adding a date to an interval yields a datetime).
Change-Id: Ifb9f9d7cbd9f5990dcb2abb583193e9e92b789ad
Fixes: #4088
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 821752870..d3aa1a756 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -878,7 +878,7 @@ from sqlalchemy.sql import elements from ... import sql, schema, exc, util from ...engine import default, reflection from ...sql import compiler, expression -from ... import types as sqltypes +from ...sql import sqltypes try: from uuid import UUID as _python_UUID @@ -963,7 +963,7 @@ class TIME(sqltypes.TIME): self.precision = precision -class INTERVAL(sqltypes.TypeEngine): +class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval): """PostgreSQL INTERVAL type. @@ -972,6 +972,7 @@ class INTERVAL(sqltypes.TypeEngine): """ __visit_name__ = 'INTERVAL' + native = True def __init__(self, precision=None, fields=None): """Construct an INTERVAL. @@ -988,7 +989,7 @@ class INTERVAL(sqltypes.TypeEngine): self.fields = fields @classmethod - def _adapt_from_generic_interval(cls, interval): + def adapt_emulated_to_native(cls, interval, **kw): return INTERVAL(precision=interval.second_precision) @property @@ -1088,7 +1089,7 @@ class TSVECTOR(sqltypes.TypeEngine): __visit_name__ = 'TSVECTOR' -class ENUM(sqltypes.Enum): +class ENUM(sqltypes.NativeForEmulated, sqltypes.Enum): """PostgreSQL ENUM type. @@ -1166,6 +1167,8 @@ class ENUM(sqltypes.Enum): """ + native_enum = True + def __init__(self, *enums, **kw): """Construct an :class:`~.postgresql.ENUM`. @@ -1198,6 +1201,20 @@ class ENUM(sqltypes.Enum): self.create_type = kw.pop("create_type", True) super(ENUM, self).__init__(*enums, **kw) + @classmethod + def adapt_emulated_to_native(cls, impl, **kw): + """Produce a Postgresql native :class:`.postgresql.ENUM` from plain + :class:`.Enum`. + + """ + kw.setdefault("validate_strings", impl.validate_strings) + kw.setdefault('name', impl.name) + kw.setdefault('schema', impl.schema) + kw.setdefault('inherit_schema', impl.inherit_schema) + kw.setdefault('metadata', impl.metadata) + kw.setdefault('_create_events', False) + return cls(**kw) + def create(self, bind=None, checkfirst=True): """Emit ``CREATE TYPE`` for this :class:`~.postgresql.ENUM`. |