spq has asked for the wisdom of the Perl Monks concerning the following question:
I wrote my first module wrapping DBI years ago, and have used it on a few projects. Basically I've done this for two general objectives. The first is to wrap the DBI->connect call with a connect of my own, using a different name (such as db_connect). The purpose of custom connect isn't to muddle with DBI's internal connection mechanics, but just to handle things like providing some default values and/or retry connecting a few times. Inside my connect method I use DBI->connect and then store the $dbh as an attribute in the the object I bless into my own class and then return. The second reason is to provide a few special methods which all operate on a database. In no place am I trying to override existing DBI methods, just add new ones.
So far I have accomplished this fine by writing my custom DB class as a wrapper. In my custom methods I would use $self->{DBH}->prepare for example, and I use AUTOLOAD to simply pass any call not recognized in my module on to DBI:
sub AUTOLOAD { my $self = shift; my $call = $AUTOLOAD; $call =~ s/.*:://; return $self->{DBH}->$call(@_); } # AUTOLOAD
But I find myself in the middle of a moderate rewrite of an old project too long neglected. While the rest of the modules in the project are becoming more carefully designed classes, I feel an urge to do the same with my custom DBI wrapper, making it instead a subclass of DBI. This would allow me drop AUTOLOAD, but how would it effect my custom methods? If I read the docs correctly, $self->{DBH}->prepare() would just become $self->prepare(); OK, minor improvement.
Which leaves making the database connection, and this is where I found the docs (or mostly the ancillary articles on the subject) a bit confusing, giving some dire words of caution. ... Can I continue to use my own connect method, leaving DBI's available to the user if needed, and just replace $self{DBH} = DBI->connect(...) with $dbh = DBI->connect(..., { RootClass => 'MySubDBI' }) and return that connection handle instead of blessing it myself? Would it be more prudent to override DBI's connect method, calling SUPER::connect internally?
So fellow monks, besides hoping someone with experience could provide advice or sample snippets clarifying my questions above, I humbly ask you: Are the gains of sublassing DBI over wrapping it worth the effort given my simple needs?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Subclassing DBI instead of wrapping
by samtregar (Abbot) on Aug 22, 2006 at 17:33 UTC | |
|
Re: Subclassing DBI instead of wrapping
by jeffa (Bishop) on Aug 22, 2006 at 17:45 UTC | |
|
Re: Subclassing DBI instead of wrapping
by renodino (Curate) on Aug 22, 2006 at 18:52 UTC | |
by spq (Friar) on Aug 23, 2006 at 02:14 UTC |