Author: phd
Date: 2005-01-14 14:28:29 +0000 (Fri, 14 Jan 2005)
New Revision: 544
Modified:
home/phd/SQLObject/inheritance/sqlobject/col.py
home/phd/SQLObject/inheritance/tests/test_sqlobject.py
Log:
Merged patch from revision 543: added PickleCol, PickleValidator and tests.
Modified: home/phd/SQLObject/inheritance/sqlobject/col.py
===================================================================
--- home/phd/SQLObject/inheritance/sqlobject/col.py 2005-01-14 14:24:18 UTC (rev 543)
+++ home/phd/SQLObject/inheritance/sqlobject/col.py 2005-01-14 14:28:29 UTC (rev 544)
@@ -19,6 +19,10 @@
"""
import re, time
+try:
+ import cPickle as pickle
+except ImportError:
+ import pickle
import sqlbuilder
# Sadly the name "constraints" conflicts with many of the function
# arguments in this module, so we rename it:
@@ -885,6 +889,37 @@
baseClass = SOBLOBCol
+class PickleValidator(BinaryValidator):
+ """
+ Validator for pickle types. A pickle type is simply a binary type with
+ hidden pickling, so that we can simply store any kind of stuff in a
+ particular column.
+
+ The support for this relies directly on the support for binary for your
+ database.
+ """
+
+ def toPython(self, value, state):
+ if value is None:
+ return None
+ if isinstance(value, str):
+ return pickle.loads(value)
+ return value
+
+ def fromPython(self, value, state):
+ if value is None:
+ return None
+ pickled = pickle.dumps(value)
+ return BinaryValidator.fromPython(self, pickled, state)
+
+
+class SOPickleCol(SOBLOBCol):
+ validatorClass = PickleValidator # can be overriden in descendants
+
+class PickleCol(BLOBCol):
+ baseClass = SOPickleCol
+
+
def popKey(kw, name, default=None):
if not kw.has_key(name):
return default
Modified: home/phd/SQLObject/inheritance/tests/test_sqlobject.py
===================================================================
--- home/phd/SQLObject/inheritance/tests/test_sqlobject.py 2005-01-14 14:24:18 UTC (rev 543)
+++ home/phd/SQLObject/inheritance/tests/test_sqlobject.py 2005-01-14 14:28:29 UTC (rev 544)
@@ -1342,25 +1342,61 @@
## BLOB columns
########################################
-class Profile(SQLObject):
+class ImageData(SQLObject):
image = BLOBCol(default='emptydata', length=65535)
class BLOBColTest(SQLObjectTest):
- classes = [Profile]
+ classes = [ImageData]
def testBLOBCol(self):
data = ''.join([chr(x) for x in range(256)])
- prof = Profile()
+ prof = ImageData()
prof.image = data
iid = prof.id
connection().cache.clear()
- prof2 = Profile.get(iid)
- assert prof2.image == data
+ prof2 = ImageData.get(iid)
+ self.assertEqual(prof2.image, data)
########################################
+## Pickle columns
+########################################
+
+class PickleData:
+ pi = 3.14156
+ def __init__(self):
+ self.question = 'The Ulimate Question of Life, the Universe and Everything'
+ self.answer = 42
+
+class PickleContainer(SQLObject):
+ pickledata = PickleCol(default=None, length=65535)
+
+class PickleColTest(SQLObjectTest):
+ classes = [PickleContainer]
+
+ def testPickleCol(self):
+ mypickledata = PickleData()
+
+ ctnr = PickleContainer(pickledata=mypickledata)
+ iid = ctnr.id
+
+ connection().cache.clear()
+
+ ctnr2 = PickleContainer.get(iid)
+ s2 = ctnr2.pickledata
+
+ self.failUnless(isinstance(s2, PickleData))
+ self.failUnless(isinstance(s2.pi, float))
+ self.failUnless(isinstance(s2.question, str))
+ self.failUnless(isinstance(s2.answer, int))
+ self.assertEqual(s2.pi, mypickledata.pi)
+ self.assertEqual(s2.question, mypickledata.question)
+ self.assertEqual(s2.answer, mypickledata.answer)
+
+
+########################################
## Run from command-line:
########################################
|