Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Inheriting from DBI

by janDD (Acolyte)
on Mar 02, 2011 at 15:39 UTC ( #891019=perlquestion: print w/replies, xml ) Need Help??
janDD has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I try to derive two classes from DBI with the following code:
use strict; use warnings; use DBI; BEGIN{ package MyDBI; our @ISA = ('DBI','MyDBI::st'); sub connect { my $class=shift; my ($db,$user,$pw) = @_; my $self = DBI->connect($db,$user,$pw, { RaiseError => 1 } ); bless $self; return $self; } } # ---------------------------------------- BEGIN { package MyDBI::st; our @ISA = ('DBI::st'); sub query { my $class = shift; my $self = $class->prepare("SELECT * FROM `table` LIMIT 2"); $self->execute(); bless $self; return $self; } } my $dbh = MyDBI->connect("x","y","z"} ); my $sth = $dbh->query();
and perl gives the answer:
Can't locate auto/MyDBI/ in @INC (@INC contains ....) at lin +e line 29
I tried a couple of things, including using SUPER:: and different settings of @ISA but I don't find a solution. Thanks for your help, Jan

Replies are listed 'Best First'.
Re: Inheriting from DBI
by Corion (Pope) on Mar 02, 2011 at 15:51 UTC

    What problem are you trying to solve by inheriting from DBI?

    I would (if at all) aggregate a DBI object (rep. a database handle, as returned from DBI::connect) into my object. DBI itself provides very little in the way of methods itself, mainly the ->connect method. Most methods (like ->prepare and ->fetch* live in the database drivers (DBD::), and you can't easily inherit them (nor does that make sense to me).

    If you really want a simple way to keep your database handle, look at Class::DBI. A more complex way is DBIx::Class, but there are many other modules that live in the DBIx namespace that provide enhancements or different APIs to DBI.

      Well, the reason I am trying to do that is because I have to connect often to a database which is not very reliable in the sense that only one our of five connections is successful. So I want to enhance the connect, prepare, ... statements with some errorhandling (trying to reconnect several times, writing an error message by mails etc..) I am doing this now with simple functions, but I thought, a class is more appropriate ... J.
        Your class doesn't need to inherit from DBI, it just needs to return a database handle. I've written lots of wrappers that connect to some default database in some default way that goes something like:
        package MyDBI; use DBI; sub connect { ...get args my $dbh; stuff $dbh = DBI->connect(...); more stuff return $dbh; }
Re: Inheriting from DBI
by runrig (Abbot) on Mar 02, 2011 at 15:58 UTC
    Well, for one thing, you're reblessing a database handle (which is not of class DBI, it's something like DBI::db) into a class that inherits from DBI and DBI::st, so that it's not even a database handle anymore.

    See inheritance not working as expected

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://891019]
Approved by sweetblood
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-05-24 03:43 GMT
Find Nodes?
    Voting Booth?