in reply to RFC: DBIx::Iterator

A better example may help. Here is some actual pseudo code (that is, some actual code stripped down and with names changed to protect the innocent). Notice how different iterators can share the same lookup cache per database handle, and other iterators can be conditionally injected into the stream.

for my $dbh ( @db_handles) { my ( $crud_cache, $crud_stuff_cache, $stuff_name_cache) = ({},{},{}) +; my @filter_foo_name; if ($do_some_filtering) { @filter_foo_name = ( \&filter_foo_name, { FILTER => $this } ); } my $f = mk_iterator( { DBH => $dbh, BIND => SQL_INTEGER }, \&get_foo, { WHERE_CLAUSE => $where{$dbname} }, \&get_wibble, \&get_stuff, \&get_crud, [ \&get_more_important_stuff, \&get_foo_wibble_id, \&get_bar_wibble_id, \&get_foo_name, @filter_foo_name, \&get_bar_name, \&get_foo_wibble_thingy_id, \&get_bar_wibble_thingy_id, \&get_foo_crud_id, \&get_bar_crud_id, \&get_foo_crud_name, { CACHE => $crud_cache }, \&get_bar_crud_name, { CACHE => $crud_cache }, \&get_foo_crud_stuff, { CACHE => $crud_stuff_cache }, \&get_bar_crud_stuff, { CACHE => $crud_stuff_cache }, ], [ \&get_stuff_name, \&get_stuff_type, \&get_stuff_this_and_that, \&get_stuff_this_name, { OUTER => 1, BIND => SQL_VARCHAR, CACHE => $stuff_name_cache +}, \&get_stuff_that_name, { OUTER => 1, BIND => SQL_VARCHAR, CACHE => $stuff_name_cache +}, \&get_foo_crud_stuff, { OUTER => 1, CACHE => $crud_stuff_cache } +, \&get_bar_crud_stuff, { OUTER => 1, CACHE => $crud_stuff_cache } +, ], ); while ( my $row = $f->() ) { # do stuff w/$row which is a hashref # w/all the keys from the SELECT array refs (see # sql_iterator() example below). # # Update: Actually, since there are array refs # in the iterator constructor, the actual href here # will contain all of the keys from the first # four iterators, and for each of those values, # it will iterate through values containing # keys from the first array ref, then through # values containing keys from the second array ref print join(",", $@row{qw(FOO_WIBBLE_ID FOO_NAME BAR_WIBBLE_ID BAR_ +NAME ETC ETC)}), "\n"; } }

And many of the iterator generator functions share the same code, with only the SELECT and ARG list differing, e.g.:

sub get_foo_wibble_id { sql_iterator( SQL => "select WIBBLE_ID\n". "from WIBBLE_TABLE\n". "where STUFF_ID = ?\n". "and IMPORTANT_ID = ?\n", SELECT => ['FOO_WIBBLE_ID'], ARGS => [qw(STUFF_ID FOO_THINGY_ID)], @_, ) } sub get_bar_wibble_id { get_foo_wibble_id( SELECT => ['BAR_WIBBLE_ID'], ARGS => [qw(STUFF_ID BAR_THINGY_ID)], @_, ) }
Updated: per below. probably not overly clearly, but it's something for now :-)

More update: Since the iterator is basically the same as a DBI fetchrow_hashref(), the "# do stuff" is whatever you would do after fetching a row from a database. But instead of doing one big SQL statement that joins many tables, I have many separate iterators each selecting from a (usually) single table with the iterators joined together.

Why do it this way? In my case, it was because I was working with a completely undocumented and confusing database schema. I was constantly commenting out some of the inner loops/joins and seeing if the outer loops were returning what I wanted. In one big SQL statement with joins, this would require commenting out columns in the SELECT clause, the tables in the FROM clause, and expressions in the WHERE clause. Using the above scheme, I could comment out just ONE LINE to eliminate an inner join...usually commenting out one line at a time from the bottom up in the mk_iterator call. Using the TRANSFORM option, I could do things that SQL alone wouldn't do, or would be hard to do. Though using this, you are limited in some ways, most notably in how useful any ORDER BY clause is. Hope that explains things :-)

Replies are listed 'Best First'.
Re^2: RFC: DBIx::Iterator (stuff)
by tye (Sage) on May 12, 2007 at 06:05 UTC

    I think this example would be clearer if you gave some details about what values $row transitions through here. That is, what might the code that replaces "# do stuff" look like?

    - tye        

Re^2: RFC: DBIx::Iterator
by runrig (Abbot) on Jul 12, 2007 at 21:38 UTC
    Just for fun, here is a completely runnable abstract example that doesn't use SQL at all, we are instead iterating over various lists that sort of simulate selecting a single column from several joined tables (No, you probably wouldn't really want to do this sort of hard coded list iterator this way, but it would be a good way to splice in hardcoded lists as sort of a Cartesian join on some SQL when combined with the SQL iterators):
    use strict; use warnings; use DBIx::Iterator qw(mk_iterator list_iterator); my $iter = mk_iterator( \&list_123, [ \&list_abc, \&list_def, ], [ \&list_456, \&list_789, ], ); use Data::Dumper qw(Dumper); while ( my $r = $iter->() ) { print Dumper($r); } sub list_ab { my $x = shift; my $y = shift; list_iterator( LIST => [ map {[$_]} $x..$y ], SELECT => [ "$x$y" ], @_, ) } sub list_123 { list_ab(1,3,@_) } sub list_456 { list_ab(4,6,@_) } sub list_789 { list_ab(7,9,@_) } sub list_abc { list_ab("a","c",@_) } sub list_def { list_ab("d","f",@_) }