[CDBI] RFC: CDBI::Sweet prefetch

Charles Gordon charles.gordon at gmail.com
Tue Feb 7 01:03:19 GMT 2006

I sent an email earlier about using "LEFT JOIN" with CDBI::Sweet's prefetch
attribute. I've hacked together a version that works for me, and retains
backwards compatibility with the existing prefetch attribute. I'd like to
clean it up and submit it as a patch, but I want to make sure the interface
is reasonable first.

Currently, "prefetch" allows you to specify a set of relationships to
automatically populate via an inner join using syntax like this:

    $attr{prefetch} = qw/rel1 rel2 rel3/;

CDBI::Sweet automatically figures out which columns to use for the join and
sets up the FROM and WHERE clauses accordingly. I've made a small change
that allows you to specify the type of join you would like:

    $attr{prefetch} = ['rel1', {name => 'rel2', type => 'left'}, 'rel3'];

The joins are ordered as they will appear in the "FROM" clause, so this
would be:

   FROM me, rel1 left join rel2, rel3

The other small enhancement is that the column returned from the database is
checked for null before being populated. This allows you to use "has_a"
relationships (for example) with null-able keys. If you prefetch using such
a relationship and use a "left" join, you get either "undef" or a reference
to the joined object, depending on whether the column was null or not.

This trivially extends to any other type of join, and essentially gives you
a way to control the "FROM" clause in the same way you can now control the
"WHERE" clause using SQL::Abstract.

I've hacked this in for now, and need to spend some time making it look
nicer, but I'm happy to send a diff to anyone who is curious to see it in
action. I'd love to hear any feedback. It's working for me as-is, but I
could be missing any number of big reasons why it won't work in general. If
you've got one, let me know!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.digitalcraftsmen.net/mailman/private/classdbi/attachments/20060206/37bb9ce0/attachment.html

More information about the ClassDBI mailing list