Author: phd
Date: 2005-01-05 12:24:45 +0000 (Wed, 05 Jan 2005)
New Revision: 512
Modified:
trunk/SQLObject/sqlobject/col.py
Log:
Added IntValidator that tests if an IntCol got only int or long;
raises TypeError if not.
Modified: trunk/SQLObject/sqlobject/col.py
===================================================================
--- trunk/SQLObject/sqlobject/col.py 2005-01-05 12:13:34 UTC (rev 511)
+++ trunk/SQLObject/sqlobject/col.py 2005-01-05 12:24:45 UTC (rev 512)
@@ -363,8 +363,7 @@
def __init__(self, **kw):
self.dbEncoding = popKey(kw, 'dbEncoding', 'UTF-8')
SOStringCol.__init__(self, **kw)
- self.validator = validators.All.join(
- self.createValidator(), self.validator)
+ self.validator = validators.All.join(self.createValidator(), self.validator)
def createValidator(self):
"""Create a validator for the column. Can be overriden in descendants."""
@@ -373,13 +372,41 @@
class UnicodeCol(Col):
baseClass = SOUnicodeCol
+
+class IntValidator(validators.Validator):
+
+ def fromPython(self, value, state):
+ if value is None:
+ return None
+ if not isinstance(value, (int, long)):
+ raise TypeError, "expected an int in the IntCol '%s', got %s instead" % \
+ (self.name, type(value))
+ return value
+
+ def toPython(self, value, state):
+ if value is None:
+ return None
+ if not isinstance(value, (int, long)):
+ raise TypeError, "expected an int in the IntCol '%s', got %s instead" % \
+ (self.name, type(value))
+ return value
+
class SOIntCol(SOCol):
+ validatorClass = IntValidator # can be overriden in descendants
# 3-03 @@: support precision, maybe max and min directly
+ def __init__(self, **kw):
+ SOCol.__init__(self, **kw)
+ self.validator = validators.All.join(self.createValidator(), self.validator)
+
def autoConstraints(self):
return [consts.isInt]
+ def createValidator(self):
+ """Create a validator for the column. Can be overriden in descendants."""
+ return self.validatorClass(name=self.name)
+
def _sqlType(self):
return 'INT'
|