|XP is just a number|
Re: Dynamic DSN w/ Class::DBI Exampleby thpfft (Chaplain)
|on Apr 24, 2004 at 11:34 UTC||Need Help??|
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):
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.