diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-08-04 11:56:31 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-08-04 12:38:58 -0400 |
commit | f2fa9d000b44a54b0fd3ae6114eb5d53ef20c3b8 (patch) | |
tree | d7cec44ced27243d7f0d4a62831e5f4de9903bdc /lib/sqlalchemy/dialects/mysql/json.py | |
parent | af6f4ab938f1ef66491cf239c91ffff393275d95 (diff) | |
download | sqlalchemy-f2fa9d000b44a54b0fd3ae6114eb5d53ef20c3b8.tar.gz |
Build string/int processors for JSONIndexType, JSONPathType
Fixed regression in JSON datatypes where the "literal processor" for
a JSON index value, that needs to take effect for example within DDL,
would not be invoked for the value. The native String and Integer
datatypes are now called upon from within the JSONIndexType
and JSONPathType. This is applied to the generic, Postgresql, and
MySQL JSON types.
Change-Id: Ifa5f2acfeee57a79d01d7fc85d265a37bd27c716
Fixes: #3765
Diffstat (limited to 'lib/sqlalchemy/dialects/mysql/json.py')
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/json.py | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/json.py b/lib/sqlalchemy/dialects/mysql/json.py index 3840a7cd6..8dd99bd45 100644 --- a/lib/sqlalchemy/dialects/mysql/json.py +++ b/lib/sqlalchemy/dialects/mysql/json.py @@ -31,25 +31,49 @@ class JSON(sqltypes.JSON): pass -class JSONIndexType(sqltypes.JSON.JSONIndexType): + +class _FormatTypeMixin(object): + def _format_value(self, value): + raise NotImplementedError() + def bind_processor(self, dialect): + super_proc = self.string_bind_processor(dialect) + def process(value): - if isinstance(value, int): - return "$[%s]" % value - else: - return '$."%s"' % value + value = self._format_value(value) + if super_proc: + value = super_proc(value) + return value return process + def literal_processor(self, dialect): + super_proc = self.string_literal_processor(dialect) -class JSONPathType(sqltypes.JSON.JSONPathType): - def bind_processor(self, dialect): def process(value): - return "$%s" % ( - "".join([ - "[%s]" % elem if isinstance(elem, int) - else '."%s"' % elem for elem in value - ]) - ) + value = self._format_value(value) + if super_proc: + value = super_proc(value) + return value return process + + +class JSONIndexType(_FormatTypeMixin, sqltypes.JSON.JSONIndexType): + + def _format_value(self, value): + if isinstance(value, int): + value = "$[%s]" % value + else: + value = '$."%s"' % value + return value + + +class JSONPathType(_FormatTypeMixin, sqltypes.JSON.JSONPathType): + def _format_value(self, value): + return "$%s" % ( + "".join([ + "[%s]" % elem if isinstance(elem, int) + else '."%s"' % elem for elem in value + ]) + ) |