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

Ladies and Gents,

I am writing a module that performs db ops. I am given a database handle on the constructor, let's say $self->{'dbh'} = $param{'dbh'};

Since there will be many kinds of db out there (oracle, mysql, etc), how do I check the the $self->{'dbh'} what kind of db I am dealing with.

Example: I want Oracle. How do I check it's Oracle handle is being passed on?

Best regards,
Songahji

Replies are listed 'Best First'.
Re: Getting database type from db handle
by ikegami (Patriarch) on Sep 22, 2009 at 13:44 UTC

    Looking at the docs, database handles have a three attributes that sound useful. It turns out that all three are useful. I don't know how you could have missed them all.

    $dbh->{Name}
    $dbh->get_info( $GetInfoType{SQL_DBMS_NAME} );
    $dbh->{Driver}->{Name}

      The DBI docs are pretty long, though.. It's tempting when first using it, to just read what you 'think' is essential to you, and skip all the 'extra' stuff.

      Still no excuse for RTFM! But.. I'm feeling a little leeway here on not catching that.. (no?)

        It's tempting when first using it, to just read what you 'think' is essential to you

        It sounds like you're saying that's a bad thing, but that's exactly how I got the answer.

        The DBI docs are pretty long, though..

        When wanting a property of a database handle, look at the index for database handles.

        * Database Handle Methods o clone o data_sources o do o last_insert_id o selectrow_array o selectrow_arrayref o selectrow_hashref o selectall_arrayref o selectall_hashref o selectcol_arrayref o prepare o prepare_cached o commit o rollback o begin_work o disconnect o ping o get_info o table_info o column_info o primary_key_info o primary_key o foreign_key_info o statistics_info o tables o type_info_all o type_info o quote o quote_identifier o take_imp_data * Database Handle Attributes o AutoCommit (boolean) o Driver (handle) o Name (string) o Statement (string, read-only) o RowCacheSize (integer) o Username (string)

        I got three possible answers from that. Three quick clicks indicates all three fulfill the OP's requirements.

        That's not long. That's among the shortest amount of reading I've had to do to find a function. And I found three!

      many thanks. ++