from sqlalchemy import Integer from sqlalchemy.dialects.mysql import BIT from sqlalchemy.types import TypeDecorator __all__ = ["BooleanBit", "IntEnum"] class BooleanBit(TypeDecorator): impl = BIT cache_ok = True def __init__(self, *args, **kwargs): super().__init__(length=1) def process_bind_param(self, value, dialect): if value is None: return None return int(value) def process_result_value(self, value, dialect): if value is None: return None return bool(value) class IntEnum(TypeDecorator): impl = Integer cache_ok = True def __init__(self, enumtype, *args, **kwargs): super().__init__(*args, **kwargs) self._enumtype = enumtype def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self._enumtype(value)