I've written a couple of subclasses of DBI (after much hair-pulling frustration) -- so far, most everything works like I want it, with the exception of automatic disconnect handling... I'd like to put the disconnect method in a DESTROY, but none of my attempts are working. I tried putting the DESTROY method (calling $self->disconnect) in the specificdb::db class, but I get an error stating "DBI Handle has uncleared implementors data." (among other things... something to do with dbih_clearcom). Here's what I've got so far -- any advice on my approach would be most appreciated, especially if you can help get the automatic disconnect handling working.
package mydbi;
@ISA = qw( DBI );
use DBI;
sub connect {
my( $proto, $dns, $username, $password ) = @_;
my $class = ref( $proto ) || $proto;
$class->init_rootclass;
my $dbh = $class->SUPER::connect( $dns, $username, $password );
$dbh;
}
package mydbi::db;
@ISA = qw( DBI::db );
sub select_value {
my( $self, $st, $attr, @bind_values ) = @_;
my $sth;
if( ref $st ) { $sth = $st }
else { $sth = $self->prepare( $st, $attr ) or return undef }
$sth->execute( @bind_values ) or return undef;
my $value = $sth->fetchrow_array;
}
package mydbi::st;
@ISA = qw( DBI::st );
1;
package specificdb;
use mydbi;
@ISA = qw( mydbi DBI );
use DBI;
sub new {
my( $proto ) = shift;
my $class = ref( $proto ) || $proto;
my $dns = "DBI:mysql:database=XXX";
my $username = "XXX";
my $password = "XXX";
my $self = $class->connect( $dns, $username, $password );
$self;
}
package newbdb::db;
@ISA = qw( mydbi::db );
package newbdb::st;
@ISA = qw( mydbi::st );
1;
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.