[SQL-CVS] r821 - trunk/SQLObject/sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-06-20 11:38:23
|
Author: phd Date: 2005-06-20 11:38:19 +0000 (Mon, 20 Jun 2005) New Revision: 821 Modified: trunk/SQLObject/sqlobject/col.py Log: Split SOStringCol into SOStringLikeCol; used SOStringLikeCol as a common ancestor for SOUncideCol; this is to allow UnicodeCol avoid having StringValidator in its validators list. Modified: trunk/SQLObject/sqlobject/col.py =================================================================== --- trunk/SQLObject/sqlobject/col.py 2005-06-20 11:07:50 UTC (rev 820) +++ trunk/SQLObject/sqlobject/col.py 2005-06-20 11:38:19 UTC (rev 821) @@ -374,28 +374,9 @@ def withClass(self, soClass): return self.baseClass(soClass=soClass, name=self._name, **self.kw) -class StringValidator(validators.Validator): - def toPython(self, value, state): - if value is None: - return None - if isinstance(value, unicode): - return value.encode("ascii") - return value - - def fromPython(self, value, state): - if value is None: - return None - if isinstance(value, str): - return value - if isinstance(value, unicode): - return value.encode("ascii") - return value - -class SOStringCol(SOCol): - - # 3-03 @@: What about BLOB? - +class SOStringLikeCol(SOCol): + """A common ancestor for SOStringCol and SOUnicodeCol""" def __init__(self, **kw): self.length = popKey(kw, 'length') self.varchar = popKey(kw, 'varchar', 'auto') @@ -409,10 +390,6 @@ SOCol.__init__(self, **kw) - def createValidators(self): - return [StringValidator()] + \ - super(SOStringCol, self).createValidators() - def autoConstraints(self): constraints = [consts.isString] if self.length is not None: @@ -441,11 +418,11 @@ def _postgresType(self): self._check_case_sensitive("PostgreSQL") - return super(SOStringCol, self)._postgresType() + return super(SOStringLikeCol, self)._postgresType() def _sqliteType(self): self._check_case_sensitive("SQLite") - return super(SOStringCol, self)._sqliteType() + return super(SOStringLikeCol, self)._sqliteType() def _sybaseType(self): self._check_case_sensitive("SYBASE") @@ -468,6 +445,31 @@ else: return self._sqlType() + +class StringValidator(validators.Validator): + + def toPython(self, value, state): + if value is None: + return None + if isinstance(value, unicode): + return value.encode("ascii") + return value + + def fromPython(self, value, state): + if value is None: + return None + if isinstance(value, str): + return value + if isinstance(value, unicode): + return value.encode("ascii") + return value + +class SOStringCol(SOStringLikeCol): + + def createValidators(self): + return [StringValidator()] + \ + super(SOStringCol, self).createValidators() + class StringCol(Col): baseClass = SOStringCol @@ -487,10 +489,10 @@ return value return value.encode(self.db_encoding) -class SOUnicodeCol(SOStringCol): +class SOUnicodeCol(SOStringLikeCol): def __init__(self, **kw): self.dbEncoding = popKey(kw, 'dbEncoding', 'UTF-8') - SOStringCol.__init__(self, **kw) + super(SOUnicodeCol, self).__init__(**kw) def createValidators(self): return [UnicodeStringValidator(db_encoding=self.dbEncoding)] + \ |