Coverage for src/utils/guid.py : 91%
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
1from sqlalchemy.types import TypeDecorator, CHAR
2from sqlalchemy.dialects.postgresql import UUID
3import uuid
5class GUID(TypeDecorator):
6 """Platform-independent GUID type.
8 Uses PostgreSQL's UUID type, otherwise uses
9 CHAR(32), storing as stringified hex values.
11 """
12 impl = CHAR
14 def load_dialect_impl(self, dialect):
15 if dialect.name == 'postgresql':
16 return dialect.type_descriptor(UUID())
17 else:
18 return dialect.type_descriptor(CHAR(32))
20 def process_bind_param(self, value, dialect):
21 if value is None:
22 return value
23 elif dialect.name == 'postgresql':
24 return str(value)
25 else:
26 if not isinstance(value, uuid.UUID):
27 return "%.32x" % uuid.UUID(value).int
28 else:
29 # hexstring
30 return "%.32x" % value.int
32 def process_result_value(self, value, dialect):
33 if value is None:
34 return value
35 else:
36 if not isinstance(value, uuid.UUID):
37 value = uuid.UUID(value)
38 return value