Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Dynamic DSN w/ Class::DBI Example

by thpfft (Chaplain)
on Apr 24, 2004 at 11:34 UTC ( #347841=note: print w/replies, xml ) Need Help??

in reply to Dynamic DSN w/ Class::DBI Example

Every Class::DBI class uses the class method db_Main() to grab its database handle. That sub is normally a closure created in the dark and twisted heart of Ima::DBI, and it embodies various assumptions that are unhelpful when you're working with multiple databases.

The good news is that you can very easily override it: as long as calling My::Class->db_Main() returns an Ima::DBI handle connected to the right database, it doesn't matter how it does it.

You don't need to use any complicated loader mechanisms, in other words. Just something like this (schematic code):

package Stuff::DBI; use base qw(Class::DBI); sub db_Main { return shift->choose_dbh; } sub choose_dbh { my $self = shift; my $dsn = $self->get_dsn; my ($dbuser, $dbpass) = $self->get_db_id; return Ima::DBI->connect_cached($dsn, $dbuser, $dbass); }

The get_dsn() and get_db_id() methods could use %ENV variables, configuration values, command line parameters, session variables or whatever you like, and because you're intervening in the normal handle-retrieval process, there are none of the problems with execution-order that you can get with other approaches.

My (blush) Class::DBI::Factory will do this work for you, but it also does a lot of other stuff that you might not want, and has plenty of assumptions of its own. If I were you I'd use just the basic Class::DBI to begin with, build with it until you're familiar with how it works, and only then start to try out the various helpers and binders you'll find on CPAN.

As perrin says, Class::DBI is really very easy, especially if you begin by conforming to its assumptions and work out from there. The mailing list is active and helpful, and the wiki has begun to get useful. Good luck.

ps. oops. link fixed. thanks.

Replies are listed 'Best First'.
Re: Re: Dynamic DSN w/ Class::DBI Example
by biosysadmin (Deacon) on Apr 24, 2004 at 19:13 UTC
    Good post, I'll actually look into using Class::DBI myself (I'm a longtime user of the "pure" DBI).

    Also, your link to the wiki is just a tad broken. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347841]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2021-02-27 03:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found