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

Dear monks,

Say you already have a sub which creates a database handle e.g.

sub connect { my $dbh = DBI->connect( $dsn, $user, $passwd, {PrintError => 0} ) or Exception::Database->throw($DBI::errstr); $dbh->{HandleError} = sub { Exception::Database->throw($_[0]) }; return $dbh; }
, is there a way to use that when setting up Class::DBI classes?

thanks.

Replies are listed 'Best First'.
•Re: Setting up Class::DBI classes with existing $dbh
by merlyn (Sage) on Dec 09, 2003 at 21:44 UTC
    From the Class::DBI manpage:
    Dynamic Database Connections It is sometimes desirable to generate your database connection +informa- tion dynamically, for example, to allow multiple databases with + the same schema to not have to duplicate an entire class hierarchy. The preferred method for doing this is to supply your own db_Ma +in() method rather than calling set_db(). This method should return +a valid database handle. Note however that this is class data, and that changing it may +have unexpected behaviour for instances of the class already in exis +tence.
    I've used this for my own code so that forked kids get their own $dbh, for example.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Setting up Class::DBI classes with existing $dbh
by hanenkamp (Pilgrim) on Dec 09, 2003 at 21:46 UTC

    You might want to look through the Class::DBI and Ima::DBI documentation, specifically the section called "Dynamic Database Connections" in Class::DBI. You can define your own db_Main() method that returns your DBI connection rather than using set_db() directly. I've never done this, but this should work without a hitch. That is,

    package MyClass; use base 'Class::DBI'; # Stuff.... sub connect { # .... } sub db_Main() { return connect; }
Re: Setting up Class::DBI classes with existing $dbh
by jeffa (Bishop) on Dec 09, 2003 at 21:48 UTC
    You can use the code, but i don't think you can use the sub like that. I think you have to set up a DBI class first, maybe something like this:
    package Foo::DBI; use base qw(Class::DBI); use Exception::Database; Foo::DBI->set_db( qw(Main DBI:vendor:database:host user pass), { RaiseError => 1, PrintError => 0, HandleError => sub {Exception::Database->throw($_[0])}, }, ); 1;
    will work for you. I haven't used Exception::Database, however. But i think that will work.

    UPDATE:
    fixed some code typos from cut-n-paste (must preview more ...)
    looks like i was wrong ... but i would create a class anyway ... "when in Rome"

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Setting up Class::DBI classes with existing $dbh
by perrin (Chancellor) on Dec 10, 2003 at 17:31 UTC
    Be careful. Although the other answers here are correct, it is easy to run into trouble if the $dbh you provide is not a subclass of Ima::DBI. For example, calling dbi_commit() on your Class::DBI class will not work with the current release of Class::DBI unless your $dbh is an Ima::DBI subclass.