2024-08-13 14:44:10 +08:00
|
|
|
from sqlalchemy import CHAR, TypeDecorator
|
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
|
|
|
|
|
|
|
|
class StringUUID(TypeDecorator):
|
|
|
|
impl = CHAR
|
|
|
|
cache_ok = True
|
|
|
|
|
|
|
|
def process_bind_param(self, value, dialect):
|
|
|
|
if value is None:
|
|
|
|
return value
|
2024-09-10 17:08:06 +08:00
|
|
|
elif dialect.name == "postgresql":
|
2024-08-13 14:44:10 +08:00
|
|
|
return str(value)
|
|
|
|
else:
|
|
|
|
return value.hex
|
|
|
|
|
|
|
|
def load_dialect_impl(self, dialect):
|
2024-09-10 17:08:06 +08:00
|
|
|
if dialect.name == "postgresql":
|
2024-08-13 14:44:10 +08:00
|
|
|
return dialect.type_descriptor(UUID())
|
|
|
|
else:
|
|
|
|
return dialect.type_descriptor(CHAR(36))
|
|
|
|
|
|
|
|
def process_result_value(self, value, dialect):
|
|
|
|
if value is None:
|
|
|
|
return value
|
2024-09-10 17:08:06 +08:00
|
|
|
return str(value)
|