As many may know, fetchall_hashref was never implemented for DBI, although fetchall_arrayref has been implemented. I've been focusing on strategies to create my own cookie cutter subroutine, which will let me fetch a value like so: my @recs = subroutine("with certain input that will be specified for each sub");, and to return a row of data a user would use: $recs[0 .. last row - 1]->{FIELD}; I've also decided that field should be case sensitive, and I hope that other perl monks will give sugestions to optimizing these versions, or another (faster hopefully) version (maybe implementing fetchall_arrayref, or some other optimization method that I'm not aware of?)

The 3 queries that I have written rely on two differant DBI queries: fetchrow_hashref, and bind_columns/fetchrow_arrayref. The bind_hash_query, and bind_hash_query_arraydec subroutines are very similiar, but deal with binding two differant ways (per the sugestion of perl.com, and perldoc.com). So, here's the three implementations of fetchall_hashref I have, and what exactly is needed for each to function:
Notice: The code is not all my own, in fact it comes from multiple sources, with a few spoofs and spiffs by myself to create hashes, etc.
$moo = "3"; my @recs; Functions- my @fields = qw( MEH MOOSE ); bind_hash_query_arraydec: @recs = bind_hash_query_arraydec('BLEH', \@f +ields, 'WHERE pissant = ?',$moo); bind_hash_query: @recs = bind_hash_query('SELECT MEH, MOOSE FROM BLEH +WHERE pissant = ?',$moo); doUpdate: @recs = doQuery('SELECT MEH, MOOSE FROM BLEH WHERE pissant += ?',$moo);
The queries themselves:
sub bind_hash_query_arraydec { my $table = shift; my $fields = shift; my $extra = shift; my @fields = @{$fields}; my $sql = 'SELECT ' . join(', ', @fields) . " FROM $table $extra"; my $sth = $db->prepare($sql); $sth->execute(@_); my %results; my @recs; @results{@fields} = (); $sth->bind_columns(map { \$results{$_} } @fields); while ($sth->fetch()) { push @recs, { %results }; } return @recs; } sub bind_hash_query { my $sql = shift; my $sth = $db->prepare($sql); $sth->execute(@_); my %results; my @recs; $sth->bind_columns( \( @results{ @{$sth->{NAME} } } )); while ($sth->fetch()) { push @recs, { %results }; } return @recs; } sub doQuery { my @recs; my $sql = shift; my $st = $db->prepare($sql); my $rc = $st->execute(@_); my $rec; while ($rec = $st->fetchrow_hashref) { push @recs, $rec; } $st->finish; return @recs; }
If Time Bunce is around, his help would be most appreciated ;)
Gyan Kapur
gyan.kapur@rhhllp.com

In reply to Implementation Of Fetchall_hashref by Revelation

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.