Hide keyboard shortcuts

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 

4 

5class GUID(TypeDecorator): 

6 """Platform-independent GUID type. 

7 

8 Uses PostgreSQL's UUID type, otherwise uses 

9 CHAR(32), storing as stringified hex values. 

10 

11 """ 

12 impl = CHAR 

13 

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)) 

19 

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 

31 

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