Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Is there an official way to obtain the driver name from a DBI database handle (object of type "DBI::db")?

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?


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):

"Driver" (handle)
Holds the handle of the parent driver. The only recommended use for this is to find the name of the driver using:

"Name" (string)
Holds the "name" of the database. Usually (and recommended to be) the same as the ""dbi:DriverName:..."" string used to connect to the database, but with the leading ""dbi:DriverName:"" removed.

In reply to DBI driver name from database handle by liz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (4)
    As of 2018-06-24 00:44 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.