Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) 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):

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.


In reply to Re: Dynamic DSN w/ Class::DBI Example by thpfft
in thread Dynamic DSN w/ Class::DBI Example by gryphon

Title:
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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (2)
    As of 2021-02-27 04:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?