liz has asked for the wisdom of the Perl Monks concerning the following question:
Suppose I have a class method in an external module:
package External; sub dbh { $External::dbh ||= DBI->connect( 'DBI:mysql:test','root','' +) } #dbh
Now, after I obtain the database handle, I want to know which subclass of my own module I would need to bless an object with. Ideally:
package Foo; sub new { my $dbh = External->dbh; # how we get the $dbh is not the question + here my $driver = $dbh->SOMETHING; # now set to 'mysql', but what is "S +OMETHING" ? bless {},__PACKAGE__.'::'.$driver; } sub general { same for all database drivers }
and of course, I would then have a module Foo::mysql:
package Foo::mysql; @ISA = qw(Foo); sub specific { stuff specific to MySQL }
I can't find any easy way to do this, apart from possibly trying to do a query of some sort and inspect the class of the statement handle. If that would to be the way to do it, what would be an SQL statement that would work with all possible database drivers?
Liz
Update:
Thanks to 3dan and Abigail-II:
$dbh->{Driver}{Name}; # gives "mysql" $dbh->get_info( 17 ); # gives "MySQL"
The appropriate doc (which is indeed in DBI, but which I didn't find):
$dbh->{Driver}->{Name}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI driver name from database handle
by edan (Curate) on Oct 20, 2003 at 15:00 UTC | |
|
Re: DBI driver name from database handle
by Abigail-II (Bishop) on Oct 20, 2003 at 15:02 UTC | |
by liz (Monsignor) on Oct 20, 2003 at 15:17 UTC | |
by Anonymous Monk on Oct 20, 2003 at 15:18 UTC | |
|
Re: DBI driver name from database handle
by Anonymous Monk on Mar 05, 2009 at 11:28 UTC |