Re: willChange - under the hood

Sebastien Bigaret <[email protected]> 20 Feb 2004 16:40:48 +0100
Newsgroups gmane.comp.python.modeling
Message-ID <[email protected]>
        Hi Tom,

Tom Hallman <[email protected]> wrote:
> Greetings!
> 
>    I am just about to start doing some work with Modeling, and I'm very
> excited about its capabilities! It looks very promising, to say the least.
> 
>    One question I have is on what "willChange" actually does. As I understand
> it, Modeling captures a single "snapshot" of some fetched rows (objects), and
> whenever "willChange" is called on a particular object's attribute, that
> attribute is made "dirty", such that when "ec.saveChanges()" is called, only
> "dirty" attributes get updated. Is this accurate?

Absolutely, except one detail: willChange() marks the whole object as
updated (not a particular object's property); the object is then
examined when the EC saves changes. The snapshots are registered in the
central Database object, and when the EC saves changes, the objects that
were marked as updated (the "dirty" ones) are compared to their
snapshots and the observed changes are propagated to the database.

Hence, a "dirty" object that in fact did not change will not cause any
SQL UPDATE to be sent to the database --you can verify that by setting
MDL_ENABLE_DATABASE_LOGGING to "yes" and observing the sql statements
(not) sent to the database. For example, using the AuthorBooks test
model:

>>> from AuthorBooks import Book
>>> from Modeling.EditingContext import EditingContext
>>> ec=EditingContext()
>>> b=ec.fetch('Book')[0] # get whatever book
>>> b
<AuthorBooks.Book.Book object at 0x85060fc>
>>> b.willChange()
>>> ec.allUpdatedObjects()
[<AuthorBooks.Book.Book object at 0x85060fc>]
>>> ec.saveChanges() # does not send any sql statement to the db
>>> # same if you modify an obj. property then revert to the original value:
... fetched_title=b.getTitle()
>>> b.setTitle("this is a new title, not the fetched one")
>>> b.setTitle(fetched_title) # restore the original value
>>> ec.allUpdatedObjects()
[<AuthorBooks.Book.Book object at 0x85060fc>]
>>> ec.saveChanges() # does not send any sql statement to the db either

  Wishing you a nice trip with the framework!)

-- Sébastien.



-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click