Thread: [SQL-CVS] SQLObject/SQLObject Col.py,1.25,1.26 SQLObject.py,1.55,1.56
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <ian...@us...> - 2003-09-07 22:35:17
|
Update of /cvsroot/sqlobject/SQLObject/SQLObject In directory sc8-pr-cvs1:/tmp/cvs-serv5224/SQLObject Modified Files: Col.py SQLObject.py Log Message: Added conversion/validation to columns Index: Col.py =================================================================== RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/Col.py,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** Col.py 6 Sep 2003 02:33:17 -0000 1.25 --- Col.py 7 Sep 2003 22:35:09 -0000 1.26 *************** *** 6,9 **** --- 6,10 ---- import re import Constraints + from include import Validator NoDefault = SQLBuilder.NoDefault *************** *** 31,39 **** unique=NoDefault, sqlType=None, ! columnDef=None): ! # This isn't strictly true, since we *could* use backquotes ! # around column names, but why would anyone *want* to ! # use a name like that? # @@: I suppose we could actually add backquotes to the # dbName if we needed to... --- 32,42 ---- unique=NoDefault, sqlType=None, ! columnDef=None, ! validator=None, ! immutable=False): ! # This isn't strictly true, since we *could* use backquotes or ! # " or something (database-specific) around column names, but ! # why would anyone *want* to use a name like that? # @@: I suppose we could actually add backquotes to the # dbName if we needed to... *************** *** 45,48 **** --- 48,53 ---- self.columnDef = columnDef + self.immutable = immutable + if type(constraints) not in (type([]), type(())): constraints = [constraints] *************** *** 97,100 **** --- 102,112 ---- else: self.foreignName = None + + if validator: + self.toPython = validator.toPython + self.fromPython = validator.fromPython + else: + self.toPython = None + self.fromPython = None def autoConstraints(self): Index: SQLObject.py =================================================================== RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/SQLObject.py,v retrieving revision 1.55 retrieving revision 1.56 diff -C2 -d -r1.55 -r1.56 *** SQLObject.py 7 Sep 2003 18:06:21 -0000 1.55 --- SQLObject.py 7 Sep 2003 22:35:09 -0000 1.56 *************** *** 427,434 **** # no superclass that defines the database access. if cls._cacheValues: - # We create a method here, which is just a function # that takes "self" as the first argument. getter = eval('lambda self: self._SO_loadValue(%s)' % repr(instanceName(name))) else: # If we aren't caching values, we just call the --- 427,434 ---- # no superclass that defines the database access. if cls._cacheValues: # We create a method here, which is just a function # that takes "self" as the first argument. getter = eval('lambda self: self._SO_loadValue(%s)' % repr(instanceName(name))) + else: # If we aren't caching values, we just call the *************** *** 452,466 **** # those methods themself. ! # We start by just using the _SO_setValue method ! setter = eval('lambda self, val: self._SO_setValue(%s, val)' % repr(name)) ! setattr(cls, rawSetterName(name), setter) ! # Then do the aliasing ! if not hasattr(cls, setterName(name)): ! setattr(cls, setterName(name), setter) ! # We keep track of setters that haven't been ! # overridden, because we can combine these ! # set columns into one SQL UPDATE query. ! cls._SO_plainSetters[name] = 1 ! ################################################## --- 452,467 ---- # those methods themself. ! 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 ! if not hasattr(cls, setterName(name)): ! setattr(cls, setterName(name), setter) ! # We keep track of setters that haven't been ! # overridden, because we can combine these ! # set columns into one SQL UPDATE query. ! cls._SO_plainSetters[name] = 1 ################################################## *************** *** 490,500 **** cls._SO_plainForeignGetters[name[:-2]] = 1 ! # The setter just gets the ID of the object, ! # and then sets the real column. ! setter = eval('lambda self, val: setattr(self, %s, self._SO_getID(val))' % (repr(name))) ! setattr(cls, rawSetterName(name)[:-2], setter) ! if not hasattr(cls, setterName(name)[:-2]): ! setattr(cls, setterName(name)[:-2], setter) ! cls._SO_plainForeignSetters[name[:-2]] = 1 # We'll need to put in a real reference at --- 491,502 ---- cls._SO_plainForeignGetters[name[:-2]] = 1 ! if not column.immutable: ! # The setter just gets the ID of the object, ! # and then sets the real column. ! setter = eval('lambda self, val: setattr(self, %s, self._SO_getID(val))' % (repr(name))) ! setattr(cls, rawSetterName(name)[:-2], setter) ! if not hasattr(cls, setterName(name)[:-2]): ! setattr(cls, setterName(name)[:-2], setter) ! cls._SO_plainForeignSetters[name[:-2]] = 1 # We'll need to put in a real reference at *************** *** 659,662 **** --- 661,666 ---- self._SO_perConnection = True + self._SO_validatorState = SQLObjectState(self) + if not selectResults: dbNames = [col.dbName for col in self._SO_columns] *************** *** 717,721 **** self._SO_writeLock.release() ! def _SO_setValue(self, name, value): # This is the place where we actually update the # database. --- 721,725 ---- self._SO_writeLock.release() ! def _SO_setValue(self, name, value, toPython): # This is the place where we actually update the # database. *************** *** 725,728 **** --- 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 *************** *** 770,773 **** --- 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) *************** *** 778,786 **** # @@: do we really need this lock? #self._SO_writeLock.acquire() ! results = self._connection._SO_selectOne(self, [self._SO_columnDict[name].dbName]) #self._SO_writeLock.release() assert results != None, "%s with id %s is not in the database" \ % (self.__class__.__name__, self.id) ! return results[0] def _SO_foreignKey(self, id, joinClass): --- 786,798 ---- # @@: do we really need this lock? #self._SO_writeLock.acquire() ! column = self._SO_columnDict[name] ! results = self._connection._SO_selectOne(self, [column.dbName]) #self._SO_writeLock.release() assert results != None, "%s with id %s is not in the database" \ % (self.__class__.__name__, self.id) ! value = results[0] ! if column.toPython: ! value = column.toPython(value, self._SO_validatorState) ! return value def _SO_foreignKey(self, id, joinClass): *************** *** 1150,1154 **** --- 1162,1170 ---- return count + class SQLObjectState(object): + def __init__(self, soObject): + self.soObject = soObject + self.protocol = 'sql' |