Update of /cvsroot/sqlobject/SQLObject/SQLObject
In directory sc8-pr-cvs1:/tmp/cvs-serv1915/SQLObject
Modified Files:
Col.py Converters.py DBConnection.py SQLObject.py
Log Message:
* Fixed validation -- fromPython and toPython were mixed up in places.
* Made sure validation/conversion happened everywhere it should.
* Fixed BoolCol to work like it should.
Index: Col.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/Col.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** Col.py 25 Sep 2003 20:40:25 -0000 1.27
--- Col.py 26 Sep 2003 07:12:59 -0000 1.28
***************
*** 103,113 ****
self.foreignName = None
! if validator:
! self.toPython = validator.toPython
! self.fromPython = validator.fromPython
else:
self.toPython = None
self.fromPython = None
def autoConstraints(self):
return []
--- 103,122 ----
self.foreignName = None
! self.validator = validator
!
! def _set_validator(self, value):
! self._validator = value
! if self._validator:
! self.toPython = self._validator.toPython
! self.fromPython = self._validator.fromPython
else:
self.toPython = None
self.fromPython = None
+ def _get_validator(self):
+ return self._validator
+
+ validator = property(_get_validator, _set_validator)
+
def autoConstraints(self):
return []
***************
*** 252,262 ****
baseClass = SOIntCol
class SOBoolCol(SOCol):
def autoConstraints(self):
return [Constraints.isBool]
! def _sqlType(self):
return 'BOOL'
class BoolCol(Col):
--- 261,286 ----
baseClass = SOIntCol
+ class BoolValidator(Validator.Validator):
+
+ def fromPython(self, value, state):
+ if value:
+ return 't'
+ else:
+ return 'f'
+
class SOBoolCol(SOCol):
+ def __init__(self, **kw):
+ SOCol.__init__(self, **kw)
+ self.validator = Validator.All.join(BoolValidator(), self.validator)
+
def autoConstraints(self):
return [Constraints.isBool]
! def _postgresType(self):
return 'BOOL'
+
+ def _mysqlType(self):
+ return "ENUM('t', 'f')"
class BoolCol(Col):
Index: Converters.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/Converters.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Converters.py 25 Sep 2003 20:40:25 -0000 1.5
--- Converters.py 26 Sep 2003 07:12:59 -0000 1.6
***************
*** 80,86 ****
def BoolConverter(value):
! return repr(bool(value))
! registerConverter(type(True), BoolConverter)
def FloatConverter(value):
--- 80,90 ----
def BoolConverter(value):
! if value:
! return '1'
! else:
! return '0'
! if type(True) != type(1):
! registerConverter(type(True), BoolConverter)
def FloatConverter(value):
Index: DBConnection.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/DBConnection.py,v
retrieving revision 1.48
retrieving revision 1.49
diff -C2 -d -r1.48 -r1.49
*** DBConnection.py 25 Sep 2003 20:40:25 -0000 1.48
--- DBConnection.py 26 Sep 2003 07:12:59 -0000 1.49
***************
*** 661,665 ****
return Col.StringCol, {'length': int(t[t.index('(')+1:-1]),
'varchar': False}
! elif t=='text':
return Col.StringCol, {}
elif t.startswith('datetime'):
--- 661,665 ----
return Col.StringCol, {'length': int(t[t.index('(')+1:-1]),
'varchar': False}
! elif t == 'text':
return Col.StringCol, {}
elif t.startswith('datetime'):
Index: SQLObject.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/SQLObject.py,v
retrieving revision 1.58
retrieving revision 1.59
diff -C2 -d -r1.58 -r1.59
*** SQLObject.py 24 Sep 2003 12:18:15 -0000 1.58
--- SQLObject.py 26 Sep 2003 07:12:59 -0000 1.59
***************
*** 380,383 ****
--- 380,384 ----
inst = object.__new__(cls)
inst._SO_creating = True
+ inst._SO_validatorState = SQLObjectState(inst)
# This is a dictionary of column-names to
# column-values for the new row:
***************
*** 403,406 ****
--- 404,408 ----
try:
val = object.__new__(cls)
+ val._SO_validatorState = SQLObjectState(val)
val._init(id, connection, selectResults)
cache.put(id, cls, val)
***************
*** 454,459 ****
if not column.immutable:
# We start by just using the _SO_setValue method
! setter = eval('lambda self, val: self._SO_setValue(%s, val, self.%s)' % (repr(name), '_SO_toPython_%s' % name))
! setattr(cls, '_SO_toPython_%s' % name, column.toPython)
setattr(cls, rawSetterName(name), setter)
# Then do the aliasing
--- 456,461 ----
if not column.immutable:
# We start by just using the _SO_setValue method
! setter = eval('lambda self, val: self._SO_setValue(%s, val, self.%s)' % (repr(name), '_SO_fromPython_%s' % name))
! setattr(cls, '_SO_fromPython_%s' % name, column.fromPython)
setattr(cls, rawSetterName(name), setter)
# Then do the aliasing
***************
*** 661,666 ****
self._SO_perConnection = True
- self._SO_validatorState = SQLObjectState(self)
-
if not selectResults:
dbNames = [col.dbName for col in self._SO_columns]
--- 663,666 ----
***************
*** 721,725 ****
self._SO_writeLock.release()
! def _SO_setValue(self, name, value, toPython):
# This is the place where we actually update the
# database.
--- 721,725 ----
self._SO_writeLock.release()
! def _SO_setValue(self, name, value, fromPython):
# This is the place where we actually update the
# database.
***************
*** 729,734 ****
# the parts are set. So we just keep them in a
# dictionary until later:
! if toPython:
! value = toPython(value, self._SO_validatorState)
if self._SO_creating:
self._SO_createValues[name] = value
--- 729,734 ----
# the parts are set. So we just keep them in a
# dictionary until later:
! if fromPython:
! value = fromPython(value, self._SO_validatorState)
if self._SO_creating:
self._SO_createValues[name] = value
***************
*** 748,751 ****
--- 748,755 ----
# _SO_creating is special, see _SO_setValue
if self._SO_creating:
+ for name, value in kw.items():
+ fromPython = getattr(self, '_SO_fromPython_%s' % name)
+ if fromPython:
+ kw[name] = fromPython(value, self._SO_validatorState)
self._SO_createValues.update(kw)
return
***************
*** 764,767 ****
--- 768,774 ----
for name, value in kw.items():
if self._SO_plainSetters.has_key(name):
+ fromPython = getattr(self, '_SO_fromPython_%s' % name)
+ if fromPython:
+ value = fromPython(value, self._SO_validatorState)
toUpdate[name] = value
if self._cacheValues:
***************
*** 776,781 ****
def _SO_selectInit(self, row):
for col, colValue in zip(self._SO_columns, row):
! if col.fromPython:
! colValue = col.fromPython(colValue, self._SO_validatorState)
setattr(self, instanceName(col.name), colValue)
--- 783,788 ----
def _SO_selectInit(self, row):
for col, colValue in zip(self._SO_columns, row):
! if col.toPython:
! colValue = col.toPython(colValue, self._SO_validatorState)
setattr(self, instanceName(col.name), colValue)
|