Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Deriving a class from DBI.

by ikegami (Patriarch)
on Mar 23, 2010 at 17:24 UTC ( [id://830345]=note: print w/replies, xml ) Need Help??


in reply to Deriving a class from DBI.

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.

Replies are listed 'Best First'.
Re^2: Deriving a class from DBI.
by clueless newbie (Curate) on Mar 23, 2010 at 18:14 UTC

    This points out why my attempts failed! Three classes not just one!

    Thank you, ikegami, thank you!

      Database handles and statement handles have different methods, so they are two different classes (::db and ::st). DBI itself provides global static methods. There's a fourth class (::dr) for database drivers.

      DBI determines the name of the database handle class and the statement handle class based on the package used to create the connection ($pkg->connect), so there's no need to tell DBI about your ::db and ::st class. On the flip side, you need to create all three subclasses even if you only want to subclass one.

        Found the documentation, thank you, here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://830345]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found