From: Luke O. <lu...@me...> - 2004-05-11 21:50:49
|
It's been a while since I've done this (ie, a couple revs of SQLObject ago), but the limitations on inheritance in SQLObject have always been about concrete classes, and since 0.4 or so, abstract columns could be inherited. It is quite possible to define common elements in abstract base classes, I use it regularly to have a common _connection. Again, the subclassing limitation is that every subclass table will be expected to actually have those columns, but I think that's exactly your situation. So (untested) I think this will do what you want for *create*: class MainObject(SQLObject): createdBy = StringCol(default="me") createdOn = DateTimeCol(default=now) class MyObjOne(MainObject): otherColumn = ... class MyObjTwo(MainObject): someColumn = ... (Note the default=now, not default=now() - SQLObject will use it as a callback since you don't want the value of now() as of creation time, but at insert). Shouldn't have to override anything else, the default will be inserted if you ignore those column names for your subclasses. Now, for update you'd have to override _SO_setValue() and set(). (Again, untested, and based on 0.5 that I have in front of me.) I'm going to treat all _SO_setValue calls as effectively set() calls, since we now have multiple values. class MainObject(SQLObject): createdBy = StringCol(default="me") createdOn = DateTimeCol(default=now) updateBy = StringCol(default="me") updateOn = DateTimeCol(default=now) def _SO_setValue(self, name, value, fromPython): if fromPython: value = fromPython(value, self._SO_validatorState) updates = {'updateBy': "me", 'updateOn': now(), name: value} self.set(**updates) def set(self, **kw): if not kw.get('updateOn',None): kw['updateOn'] = now() if not kw.get('updateBy',None): kw['updateBy'] = "me" super(MainObject, self).set(**kw) Disclaimer again: this is looking at SO 0.5, I'm not sure I really like it, it would be nice to write a database-agnostic trigger system as part of SQLObject. But I certainly don't have the time for it (it was was mentioned on the list a long time ago, nothing really came of it). And my example may not work at all because of something I've forgotten. Hopefully it gets you on a workable track though. - Luke Quoting Eduardo Elgueta <eel...@na...>: > Oleg and anyone else, > > I tried Daniel Savard's patch and I can see I could use it to achieve > what I want, but creating an unnatural hierarchy. > > What I would like to have is something like this: > > def MyOnUpdate (sqlObj): > sqlObj.addColumn(StringCol("updateBy", "me"); > sqlObj.addColumn(DateTimeCol("updateOn", now()); > > def MyOnInsert (sqlObj): > sqlObj.addColumn(StringCol("createdBy", "me"); > sqlObj.addColumn(DateTimeCol("createdOn", now()); > MyOnUpdate(sqlObj) > > def MySQLObject1 (SQLObject): > myField = StringCol() > onInsert = MyOnInsert(self) > onUpdate = MyOnUpdate(self) > > def MySQLObject2 (SQLObject): > myField = IntCol() > onInsert = MyOnInsert(self) > onUpdate = MyOnUpdate(self) > > What I need is to find the place to insert the self.onUpdate(self) call. > Any suggestion? > > TIA, > > Ed. > > PS: In other words, I need triggers which MySQL (my database backend) > does not provide. > > Eduardo Elgueta wrote: >> Oleg, >> >> Thank you for your answer. >> >> I'm not sure inheritance is what I need, but I'll certainly give it >> a try. Where can I get that patch? >> >> Ed. >> >> Oleg Broytmann wrote: >> >>> On Mon, May 10, 2004 at 05:37:21PM -0400, Eduardo Elgueta wrote: >>> >>>> I have some basic audit fields in my tables, that I used to update >>>> in my rudimentary ORM superclass. >>>> >>>> I've seen in the FAQ/docs that SQLObject classes are not meant to >>>> be subclassed >>> >>> >>> >>> There was a Daniel Savard's inheritance patch. I cannot find it on >>> the web, but I have patches for SQLObject 0.5.1 and 0.5.2. >>> >>> Oleg. >> >> >> >> >> ------------------------------------------------------- >> This SF.Net email is sponsored by Sleepycat Software >> Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to >> deliver higher performing products faster, at low TCO. >> http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3 > > > > ------------------------------------------------------- > This SF.Net email is sponsored by Sleepycat Software > Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to > deliver higher performing products faster, at low TCO. > http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3 > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss -- The Pursuit of Counterfactual Histories |