| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import json |
| 14 |
|
| 15 |
from sqlalchemy.dialects import mysql |
| 16 |
from sqlalchemy.types import Integer, Text, TypeDecorator |
| 17 |
|
| 18 |
|
| 19 |
class JsonEncodedType(TypeDecorator): |
| 20 |
"""Base column type for data serialized as JSON-encoded string in db.""" |
| 21 |
|
| 22 |
type = None |
| 23 |
impl = Text |
| 24 |
cache_ok = True |
| 25 |
"""This type is safe to cache.""" |
| 26 |
|
| 27 |
def __init__(self, mysql_as_long=False, mysql_as_medium=False): |
| 28 |
"""Initialize JSON-encoding type.""" |
| 29 |
super().__init__() |
| 30 |
|
| 31 |
if mysql_as_long and mysql_as_medium: |
| 32 |
raise TypeError("mysql_as_long and mysql_as_medium are mutually " |
| 33 |
"exclusive") |
| 34 |
|
| 35 |
if mysql_as_long: |
| 36 |
self.impl = Text().with_variant(mysql.LONGTEXT(), 'mysql') |
| 37 |
elif mysql_as_medium: |
| 38 |
self.impl = Text().with_variant(mysql.MEDIUMTEXT(), 'mysql') |
| 39 |
|
| 40 |
def process_bind_param(self, value, dialect): |
| 41 |
"""Bind parameters to the process.""" |
| 42 |
if value is None: |
| 43 |
if self.type is not None: |
| 44 |
|
| 45 |
|
| 46 |
value = self.type() |
| 47 |
elif self.type is not None and not isinstance(value, self.type): |
| 48 |
raise TypeError("%s supposes to store %s objects, but %s given" |
| 49 |
% (self.__class__.__name__, |
| 50 |
self.type.__name__, |
| 51 |
type(value).__name__)) |
| 52 |
serialized_value = json.dumps(value) |
| 53 |
return serialized_value |
| 54 |
|
| 55 |
def process_result_value(self, value, dialect): |
| 56 |
"""Process result value.""" |
| 57 |
if value is not None: |
| 58 |
value = json.loads(value) |
| 59 |
return value |
| 60 |
|
| 61 |
|
| 62 |
class JsonEncodedDict(JsonEncodedType): |
| 63 |
"""Represents dict serialized as json-encoded string in db. |
| 64 |
|
| 65 |
Note that this type does NOT track mutations. If you want to update it, you |
| 66 |
have to assign existing value to a temporary variable, update, then assign |
| 67 |
back. See this page for more robust work around: |
| 68 |
http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/mutable.html |
| 69 |
""" |
| 70 |
|
| 71 |
type = dict |
| 72 |
cache_ok = True |
| 73 |
"""This type is safe to cache.""" |
| 74 |
|
| 75 |
|
| 76 |
class JsonEncodedList(JsonEncodedType): |
| 77 |
"""Represents list serialized as json-encoded string in db. |
| 78 |
|
| 79 |
Note that this type does NOT track mutations. If you want to update it, you |
| 80 |
have to assign existing value to a temporary variable, update, then assign |
| 81 |
back. See this page for more robust work around: |
| 82 |
http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/mutable.html |
| 83 |
""" |
| 84 |
|
| 85 |
type = list |
| 86 |
cache_ok = True |
| 87 |
"""This type is safe to cache.""" |
| 88 |
|
| 89 |
|
| 90 |
class SoftDeleteInteger(TypeDecorator): |
| 91 |
"""Coerce a bound param to be a proper integer before passing it to DBAPI. |
| 92 |
|
| 93 |
Some backends like PostgreSQL are very strict about types and do not |
| 94 |
perform automatic type casts, e.g. when trying to INSERT a boolean value |
| 95 |
like ``false`` into an integer column. Coercing of the bound param in DB |
| 96 |
layer by the means of a custom SQLAlchemy type decorator makes sure we |
| 97 |
always pass a proper integer value to a DBAPI implementation. |
| 98 |
|
| 99 |
This is not a general purpose boolean integer type as it specifically |
| 100 |
allows for arbitrary positive integers outside of the boolean int range |
| 101 |
(0, 1, False, True), so that it's possible to have compound unique |
| 102 |
constraints over multiple columns including ``deleted`` (e.g. to |
| 103 |
soft-delete flavors with the same name in Nova without triggering |
| 104 |
a constraint violation): ``deleted`` is set to be equal to a PK |
| 105 |
int value on deletion, 0 denotes a non-deleted row. |
| 106 |
|
| 107 |
""" |
| 108 |
|
| 109 |
impl = Integer |
| 110 |
cache_ok = True |
| 111 |
"""This type is safe to cache.""" |
| 112 |
|
| 113 |
def process_bind_param(self, value, dialect): |
| 114 |
"""Return the binding parameter.""" |
| 115 |
if value is None: |
| 116 |
return None |
| 117 |
return int(value) |