|
From: Eduardo E. <eel...@na...> - 2004-05-11 22:50:39
|
Luke Opperman wrote:
> It's been a while since I've done this (ie, a couple revs of SQLObject=20
> ago),
> but the limitations on inheritance in SQLObject have always been about
> concrete classes, and since 0.4 or so, abstract columns could be=20
> inherited. It
> is quite possible to define common elements in abstract base classes,=20
> I use it
> regularly to have a common _connection. Again, the subclassing=20
> limitation is
> that every subclass table will be expected to actually have those=20
> columns, but
> I think that's exactly your situation.
That's right.
>
> So (untested) I think this will do what you want for *create*:
>
> class MainObject(SQLObject):
> createdBy =3D StringCol(default=3D"me")
> createdOn =3D DateTimeCol(default=3Dnow)
>
> class MyObjOne(MainObject):
> otherColumn =3D ...
>
> class MyObjTwo(MainObject):
> someColumn =3D ...
>
>
> (Note the default=3Dnow, not default=3Dnow() - SQLObject will use it as=
a=20
> callback
> since you don't want the value of now() as of creation time, but at=20
> insert).
> Shouldn't have to override anything else, the default will be inserted=20
> if you
> ignore those column names for your subclasses.
Ok.
>
> 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=20
> treat all
> _SO_setValue calls as effectively set() calls, since we now have multip=
le
> values.
>
>
> class MainObject(SQLObject):
> createdBy =3D StringCol(default=3D"me")
> createdOn =3D DateTimeCol(default=3Dnow)
> updateBy =3D StringCol(default=3D"me")
> updateOn =3D DateTimeCol(default=3Dnow)
>
> def _SO_setValue(self, name, value, fromPython):
> if fromPython:
> value =3D fromPython(value, self._SO_validatorState)
> updates =3D {'updateBy': "me", 'updateOn': now(), name: value}
> self.set(**updates)
>
> def set(self, **kw):
> if not kw.get('updateOn',None):
> kw['updateOn'] =3D now()
> if not kw.get('updateBy',None):
> kw['updateBy'] =3D "me"
> super(MainObject, self).set(**kw)
>
>
> Disclaimer again: this is looking at SO 0.5, I'm not sure I really=20
> 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=20
> mentioned on
> the list a long time ago, nothing really came of it).
>
I understand this may not work. I'll see if it applies to the release I=20
have. However, I'm wondering why you have to override _SO_setValue and=20
set. I think set does all the job.
And second, from that code It seems to me that the call to self.onUpdate=20
should be made in the set method, because (I think) every field update=20
goes through this code. Obviously, it'd be better to have that event=20
function called once, right before sending the sql statment to the=20
database, but this would be enough for me, at least by now.
> And my example may not work at all because of something I've forgotten.
> Hopefully it gets you on a workable track though.
>
> - Luke
>
Thank you very much.
Ed.
> 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 =3D StringCol()
>> onInsert =3D MyOnInsert(self)
>> onUpdate =3D MyOnUpdate(self)
>>
>> def MySQLObject2 (SQLObject):
>> myField =3D IntCol()
>> onInsert =3D MyOnInsert(self)
>> onUpdate =3D MyOnUpdate(self)
>>
>> What I need is to find the place to insert the self.onUpdate(self) cal=
l.
>> 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=20
>>> 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=20
>>>>> in my rudimentary ORM superclass.
>>>>>
>>>>> I've seen in the FAQ/docs that SQLObject classes are not meant to=20
>>>>> be subclassed
>>>>
>>>>
>>>>
>>>>
>>>> There was a Daniel Savard's inheritance patch. I cannot find it o=
n
>>>> 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=20
>>> deliver higher performing products faster, at low TCO.
>>> http://www.sleepycat.com/telcomwpreg.php?From=3Dosdnemail3
>>
>>
>>
>>
>> -------------------------------------------------------
>> 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=3Dosdnemail3
>> _______________________________________________
>> sqlobject-discuss mailing list
>> sql...@li...
>> https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
>
>
>
>
> --=20
> The Pursuit of Counterfactual Histories
>
--=20
Eduardo Elgueta
Senior Consultant
Navix
correo/email: eel...@na...
tel=E9fono/phone: +56 (2) 204-5823
celular/mobile: +56 (9) 821-0033
web: www.navix.cl
Av. Pedro de Valdivia 555 Of. 216
Providencia 7500913
Santiago, Chile
|