Source code for simpleml.orm.sqlalchemy_types

"""
Platform independent sqlalchemy types
"""

import uuid

from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.types import CHAR
from sqlalchemy.types import JSON as SQLJSON
from sqlalchemy.types import Text, TypeDecorator
from sqlalchemy_json import mutable_json_type


[docs]class GUID(TypeDecorator): """Platform-independent GUID type. Uses PostgreSQL's UUID type, otherwise uses CHAR(32), storing as stringified hex values. http://docs.sqlalchemy.org/en/latest/core/custom_types.html """
[docs] impl = CHAR
[docs] def load_dialect_impl(self, dialect): if dialect.name == "postgresql": return dialect.type_descriptor(UUID()) else: return dialect.type_descriptor(CHAR(32))
[docs] def process_bind_param(self, value, dialect): if value is None: return value elif dialect.name == "postgresql": return str(value) else: if not isinstance(value, uuid.UUID): return "%.32x" % uuid.UUID(value).int else: # hexstring return "%.32x" % value.int
[docs] def process_result_value(self, value, dialect): if value is None: return value else: if not isinstance(value, uuid.UUID): value = uuid.UUID(value) return value
[docs]class JSON(TypeDecorator): """ Platform-independent JSON type Uses PostgreSQL's JSONB type, otherwise falls back to standard JSON """
[docs] impl = SQLJSON
[docs] def load_dialect_impl(self, dialect): if dialect.name == "postgresql": return dialect.type_descriptor(JSONB(astext_type=Text())) else: return dialect.type_descriptor(SQLJSON())
# Mutable version of JSON field # https://docs.sqlalchemy.org/en/13/core/type_basics.html?highlight=json#sqlalchemy.types.JSON
[docs]MutableJSON = mutable_json_type(dbtype=JSON, nested=True)