I wanted to add a few convenience methods to statement handles (as if it didn't have enough already). Specifically, I wanted to be able to do
$sth->selectrow_arrayref(...)
when the existing convention is
$dbh->selectrow_arrayref($sth, ...)

Here's the approach I took:

use strict; use warnings; use DBI qw( ); # ---------------------------------------- BEGIN { package MyDBI; our @ISA = 'DBI'; } # ---------------------------------------- BEGIN { package MyDBI::db; our @ISA = 'DBI::db'; } # ---------------------------------------- BEGIN { package MyDBI::st; our @ISA = 'DBI::st'; sub bind_params { my $sth = $_[0]; for my $i (1..$#_) { $sth->bind_param($i, $_[$i]) or return; } return 1; } sub _do_selectrow { my ($method, $sth, $attr) = splice(@_, 0, 3); $sth->execute(@_) or return; my $row = $sth->$method() or return; $sth->finish(); return $row; } sub _do_firstrow { my ($method, $sth, $attr) = splice(@_, 0, 3); $sth->execute(@_) or return; my $row = $sth->$method() or return $sth->set_err($DBI::stderr, "No rows returned"); $sth->finish(); return $row; } sub selectrow_hashref { return _do_selectrow('fetchrow_hashref', @ +_); } sub firstrow_hashref { return _do_firstrow ('fetchrow_hashref', @ +_); } sub selectrow_arrayref { return _do_selectrow('fetchrow_arrayref', +@_); } sub firstrow_arrayref { return _do_firstrow ('fetchrow_arrayref', +@_); } sub selectrow_array { my $row = _do_selectrow('fetchrow_arrayref +', @_) or return; return wantarray ? @$row : $row->[0]; } sub firstrow_array { my $row = _do_firstrow ('fetchrow_arrayref +', @_) or return; return wantarray ? @$row : $row->[0]; } } # ---------------------------------------- 1;

You could surely override do using this method. Create the three classes, setup their inheritance relationship, and override do in ::db.


In reply to Re: Deriving a class from DBI. by ikegami
in thread Deriving a class from DBI. by clueless newbie

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.