[CDBI] Why does CDBI not populate the object on insert

Edward J. Sabol Edward.J.Sabol at nasa.gov
Thu Mar 5 18:58:13 GMT 2009


> This list is pretty quiet these days. I'm looking at some old code and
> feeling a bit rusty with CDBI and wondering about something. My guess
> is I've just forgot the reasoning here:
>
> If I do:
>
>     my $artist = Artist->insert({ name => 'Artist Name' });
>
> then $artist->name doesn't exist yet in the object. That means, of
> course, that $artist->name must do another fetch of the database.
>
> I suppose there could be times when "name" comes back out of the
> database different than it went in, but most of the time not. Just
> seems a bit inefficient.

That's exactly the reason. Triggers (both the database kind and possibly the
CDBI kind) might change the record values after it is inserted into the
database. The primary key columns should be intact, I believe, but the other
columns are flushed and all "essential" columns reloaded upon access.

> In other words, I wonder what would break if after _insert() was called
> the object got set. That is, effectively:

Well, if you don't have any triggers or sequences or anything like that, then
you can probably override the insert method to do what you're suggesting. Of
course there's no guarantee that setting $country->{world} will DTRT. That's
not part of the approved API, but it hasn't changed in many years. I wouldn't
advise it, but if you *really* feel like you need more speed....

CDBI wasn't optimized to be efficient in the number of database accesses.
That wasn't a design goal, I'm sure.

Hope this helps,
Ed



More information about the ClassDBI mailing list