Thread: [SQL-CVS] r166 - trunk/SQLObject/sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2004-07-29 17:25:00
|
Author: ianb Date: 2004-07-29 09:18:02 -0400 (Thu, 29 Jul 2004) New Revision: 166 Modified: trunk/SQLObject/sqlobject/main.py Log: * Fix some issues related to subclassing and removing columns=20 from subclasses * Added a new _create method for intercepting row inserts. * Changed insertQueryID usage Modified: trunk/SQLObject/sqlobject/main.py =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/SQLObject/sqlobject/main.py 2004-07-27 16:38:43 UTC (rev 165) +++ trunk/SQLObject/sqlobject/main.py 2004-07-29 13:18:02 UTC (rev 166) @@ -65,12 +65,12 @@ implicitJoins =3D [] for attr, value in d.items(): if isinstance(value, col.Col): - value.setName(attr) + value.name =3D attr implicitColumns.append(value) del d[attr] continue if isinstance(value, joins.Join): - value.setName(attr) + value.joinMethodName =3D attr implicitJoins.append(value) del d[attr] continue @@ -155,7 +155,15 @@ # more. newClass.q =3D sqlbuilder.SQLObjectTable(newClass) =20 - for column in newClass._columns[:]: + # We have to check if there are columns in the inherited + # _columns where the attribute has been set to None in this + # class. If so, then we need to remove that column from + # _columns. + for column in newClass._columns: + if d.has_key(column.name) and d[column.name] is None: + newClass._columns.remove(column) + + for column in newClass._columns: newClass.addColumn(column) if newClass._fromDatabase: newClass.addColumnsFromDatabase() @@ -318,14 +326,17 @@ =20 _lazyUpdate =3D False =20 + # This function is used to coerce IDs into the proper format, + # so you should replace it with str, or another function, if you + # aren't using integer IDs + _idType =3D int + def get(cls, id, connection=3DNone, selectResults=3DNone): =20 assert id is not None, 'None is not a possible id for %s' % cls.= __name + =20 + id =3D cls._idType(id) =20 - # Some databases annoyingly return longs for INT - if isinstance(id, long): - id =3D int(id) - if connection is None: cache =3D cls._connection.cache else: @@ -417,7 +428,7 @@ if cls._cacheValues: # self._SO_class_className is a reference # to the class in question. - getter =3D eval('lambda self: self._SO_foreignKey(self.%= s, self._SO_class_%s)' % (instanceName(name), column.foreignKey)) + getter =3D eval('lambda self: self._SO_foreignKey(self._= SO_loadValue(%r), self._SO_class_%s)' % (instanceName(name), column.forei= gnKey)) else: # Same non-caching version as above. getter =3D eval('lambda self: self._SO_foreignKey(self._= SO_getValue(%s), self._SO_class_%s)' % (repr(name), column.foreignKey)) @@ -810,11 +821,15 @@ self._SO_writeLock =3D threading.Lock() =20 if kw.has_key('id'): - id =3D kw['id'] + id =3D self._idType(kw['id']) del kw['id'] else: id =3D None =20 + self._create(id, **kw) + + def _create(self, id, **kw): + self._SO_creating =3D True self._SO_createValues =3D {} self._SO_validatorState =3D SQLObjectState(self) @@ -861,7 +876,10 @@ getattr(self.__class__, name) except AttributeError: raise TypeError, "%s.new() got an unexpected keyword arg= ument %s" % (self.__class__.__name__, name) - setattr(self, name, value) + try: + setattr(self, name, value) + except AttributeError, e: + raise AttributeError, '%s (with attribute %r)' % (e, nam= e) =20 # Then we finalize the process: self._SO_finishCreate(id) @@ -887,7 +905,7 @@ # Do the insert -- most of the SQL in this case is left # up to DBConnection, since getting a new ID is # non-standard. - id =3D self._connection.queryInsertID(self._table, self._idName, + id =3D self._connection.queryInsertID(self, id, names, values) cache =3D self._connection.cache cache.created(id, self.__class__, self) @@ -1043,6 +1061,14 @@ =20 sqlrepr =3D classmethod(sqlrepr) =20 + def coerceID(cls, value): + if isinstance(value, cls): + return value.id + else: + return self._idType(value) + + coerceID =3D classmethod(coerceID) + def _reprItems(self): items =3D [] for col in self._SO_columns: @@ -1246,7 +1272,10 @@ elif type(obj) is type(1L): return int(obj) elif type(obj) is type(""): - return int(obj) + try: + return int(obj) + except ValueError: + return obj elif obj is None: return None =20 |