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 :-)


In reply to Re: RFC: DBIx::Iterator by runrig
in thread RFC: DBIx::Iterator by runrig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.