[Dbix-class] auto-prefetch?

Mark Hedges hedges at ucsd.edu
Tue Mar 28 04:00:15 CEST 2006


DBIC is really impressive.  I'm only a newbie but I'm already
leaping through work that would have taken me longer.  It seems
easier to wrap my head around than Class::DBI for some reason too.
I wish 3 months ago it had functioned as well as now and had 
crossed my radar.

Here's an idea I came across from my own meanderings in POOP.
It always bothered me that if I say $foo->bar->biz->baz where
each is 1:1 it results in 3 queries.  And what if each is 1:M,
shouldn't I be able to say @{ $foo->bar->biz->baz } and get
a unique set of all baz's from all biz's from all bars of foo?
But something like that could result in 10,000 queries.

There is a way to "overload" the -> dereference operator by a 
trick.  (Thanks to Damian Conway for this hint.)  In the AUTO 
method, use Want, then if want('OBJREF'), you know there is 
another -> ahead in the chain.  Push the method call name to an 
array on a special "call stack" object and pass this object 
along to the next method call.  Then, when the auto method 
want('SCALAR') or want('ARRAY'), figure out the prefetch from 
the array of method names called (selecting everything as a 
single join), populate all objects, and pass back the right one.

1:M's might be complicated to implement in the way theorized 
above, but it's food for thought for a chain of 1:1 relationships.

Mark



More information about the Dbix-class mailing list