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/mysql/enumerated.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/mysql/enumerated.py')
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/enumerated.py | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/enumerated.py b/lib/sqlalchemy/dialects/mysql/enumerated.py index 495bee5a8..e67177b2f 100644 --- a/lib/sqlalchemy/dialects/mysql/enumerated.py +++ b/lib/sqlalchemy/dialects/mysql/enumerated.py @@ -9,7 +9,7 @@ import re from .types import _StringType from ... import exc, sql, util -from ... import types as sqltypes +from ...sql import sqltypes class _EnumeratedValues(_StringType): @@ -55,11 +55,13 @@ class _EnumeratedValues(_StringType): return strip_values -class ENUM(sqltypes.Enum, _EnumeratedValues): +class ENUM(sqltypes.NativeForEmulated, sqltypes.Enum, _EnumeratedValues): """MySQL ENUM type.""" __visit_name__ = 'ENUM' + native_enum = True + def __init__(self, *enums, **kw): """Construct an ENUM. @@ -114,21 +116,21 @@ class ENUM(sqltypes.Enum, _EnumeratedValues): """ kw.pop('strict', None) - validate_strings = kw.pop("validate_strings", False) - sqltypes.Enum.__init__( - self, validate_strings=validate_strings, *enums) - kw.pop('metadata', None) - kw.pop('schema', None) - kw.pop('name', None) - kw.pop('quote', None) - kw.pop('native_enum', None) - kw.pop('inherit_schema', None) - kw.pop('_create_events', None) + self._enum_init(enums, kw) _StringType.__init__(self, length=self.length, **kw) + @classmethod + def adapt_emulated_to_native(cls, impl, **kw): + """Produce a MySQL native :class:`.mysql.ENUM` from plain + :class:`.Enum`. + + """ + kw.setdefault("validate_strings", impl.validate_strings) + return cls(**kw) + def _setup_for_values(self, values, objects, kw): values, length = self._init_values(values, kw) - return sqltypes.Enum._setup_for_values(self, values, objects, kw) + return super(ENUM, self)._setup_for_values(values, objects, kw) def _object_value_for_elem(self, elem): # mysql sends back a blank string for any value that @@ -144,9 +146,6 @@ class ENUM(sqltypes.Enum, _EnumeratedValues): return util.generic_repr( self, to_inspect=[ENUM, _StringType, sqltypes.Enum]) - def adapt(self, cls, **kw): - return sqltypes.Enum.adapt(self, cls, **kw) - class SET(_EnumeratedValues): """MySQL SET type.""" |