in reply to Getting db handle from statement handle with DBI

No, but what you might try is writing a subclass of DBI:
package myDBI; use DBI; our @ISA = qw(DBI); sub connect { my $proto = shift; my $class = ref($proto) || $proto; my $dbh = $class->SUPER::connect(@_); bless $dbh, "${class}::db"; } package myDBI::db; our @ISA = qw(DBI::db); sub prepare { my $dbh = shift; my $sth = $dbh->SUPER::prepare(@_); $sth->{private_MyDBI_dbh} = $dbh; $sth; } package main; my $dbh = myDBI->connect('dbi:Drivername:dbname', 'user','passwd',{RaiseError=>1}); my $sth = $dbh->prepare("select stuff from table"); my $dbh_copy = $sth->{'private_MyDBI_dbh'};
Then you would also have to write a prepare() method to save the db handles in the statement handles using a 'private_*' attribute name (see the DBI docs). You could make it transparent except for the initial connect. Don't ask me if this'll work though, I've never tried it :)

Update: Fixed code. DBI->connect actually returns a DBI::db object. Oh what the heck, I wrote the prepare method also. All completely untested.

  • Comment on Re: Help me to not have to rewrite everything I've done for the past few weeks
  • Download Code