drobnox has asked for the wisdom of the Perl Monks concerning the following question:

This a a quicky DBI question: Given a module that's been passed a DBI statement handle, is there a method that can be called on said handle that will return the database handle the statement handle was derived from?

I suspect I'm out of luck...but it'sd be nice if there WAS such a beast.

(Note: the DBD driver I'm using is for PostgreSQL, if that makes a difference.)
--

"Der Fueher war ein armes Schwein.
    Er hatte keinen Fuehrerschein."
                                --Werner

Gustafson
                                                    ericg@drobnox.com

Edit kudra, 2001-08-28 Changed title

  • Comment on Getting db handle from statement handle with DBI

Replies are listed 'Best First'.
Re: Help me to not have to rewrite everything I've done for the past few weeks
by runrig (Abbot) on Aug 25, 2001 at 03:01 UTC
    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.