in reply to Inheriting from DBI

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.

Replies are listed 'Best First'.
Re^2: Inheriting from DBI
by janDD (Acolyte) on Mar 02, 2011 at 18:50 UTC
    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; ...do stuff $dbh = DBI->connect(...); ...do more stuff return $dbh; }