dfd042c7.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. dfd042c7
  3. SQLAlchemy 0.5.8 version of a805d4bd, see the docstring of that module
  4. for an explanation of this workaround.
  5. """
  6. from __future__ import absolute_import
  7. from sqlalchemy.types import PickleType as _PickleType
  8. from sqlalchemy import util
  9. class PickleType(_PickleType):
  10. def process_bind_param(self, value, dialect):
  11. dumps = self.pickler.dumps
  12. protocol = self.protocol
  13. if value is None:
  14. return None
  15. return dumps(value, protocol)
  16. def process_result_value(self, value, dialect):
  17. loads = self.pickler.loads
  18. if value is None:
  19. return None
  20. return loads(str(value))
  21. def copy_value(self, value):
  22. if self.mutable:
  23. return self.pickler.loads(self.pickler.dumps(value, self.protocol))
  24. else:
  25. return value
  26. def compare_values(self, x, y):
  27. if self.comparator:
  28. return self.comparator(x, y)
  29. elif self.mutable and not hasattr(x, '__eq__') and x is not None:
  30. util.warn_deprecated(
  31. "Objects stored with PickleType when mutable=True "
  32. "must implement __eq__() for reliable comparison.")
  33. a = self.pickler.dumps(x, self.protocol)
  34. b = self.pickler.dumps(y, self.protocol)
  35. return a == b
  36. else:
  37. return x == y
  38. def is_mutable(self):
  39. return self.mutable