Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/json.py : 38%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# mysql/json.py
2# Copyright (C) 2005-2020 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: http://www.opensource.org/licenses/mit-license.php
8from __future__ import absolute_import
10from ... import types as sqltypes
13class JSON(sqltypes.JSON):
14 """MySQL JSON type.
16 MySQL supports JSON as of version 5.7. Note that MariaDB does **not**
17 support JSON at the time of this writing.
19 The :class:`.mysql.JSON` type supports persistence of JSON values
20 as well as the core index operations provided by :class:`_types.JSON`
21 datatype, by adapting the operations to render the ``JSON_EXTRACT``
22 function at the database level.
24 .. versionadded:: 1.1
26 """
28 pass
31class _FormatTypeMixin(object):
32 def _format_value(self, value):
33 raise NotImplementedError()
35 def bind_processor(self, dialect):
36 super_proc = self.string_bind_processor(dialect)
38 def process(value):
39 value = self._format_value(value)
40 if super_proc:
41 value = super_proc(value)
42 return value
44 return process
46 def literal_processor(self, dialect):
47 super_proc = self.string_literal_processor(dialect)
49 def process(value):
50 value = self._format_value(value)
51 if super_proc:
52 value = super_proc(value)
53 return value
55 return process
58class JSONIndexType(_FormatTypeMixin, sqltypes.JSON.JSONIndexType):
59 def _format_value(self, value):
60 if isinstance(value, int):
61 value = "$[%s]" % value
62 else:
63 value = '$."%s"' % value
64 return value
67class JSONPathType(_FormatTypeMixin, sqltypes.JSON.JSONPathType):
68 def _format_value(self, value):
69 return "$%s" % (
70 "".join(
71 [
72 "[%s]" % elem if isinstance(elem, int) else '."%s"' % elem
73 for elem in value
74 ]
75 )
76 )