Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Class::DBI connect info from file?

by bmoyles (Acolyte)
on Jan 21, 2004 at 21:23 UTC ( [id://323019]=perlquestion: print w/replies, xml ) Need Help??

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

I'd like to start using Class::DBI and CGI::Application for some web projects. Is there a way to get Class::DBI to read connection information from a file or pass that info to Class::DBI rather than hard-coding it in the source for your subclass? Every example I've seen for Class::DBI is in the format:
package My::DBI; use base qw/Class::DBI/; My::DBI->set_db(...
but I'm not too sure how to pass My::DBI params...

Replies are listed 'Best First'.
Re: Class::DBI connect info from file?
by Joost (Canon) on Jan 21, 2004 at 21:34 UTC
    Just do:
    package My::DBI::Config; use base qw/Class::DBI/; My::DBI::Config->set_db { ...
    And for each My::DBI
    package My::DBI; use base qw(My::DBI::Config);
    And you're done!

    See the docs:

    Give it a database connection Class::DBI needs to know how to access the database. It do +es this through a DBI connection which you set up by calling the se +t_db() method. Music::DBI->set_db('Main', 'dbi:mysql:dbname', 'use +r', 'password'); By setting the connection up in your application base class + all the table classes that inherit from it will share the same conn +ection.
Re: Class::DBI connect info from file?
by jest (Pilgrim) on Jan 21, 2004 at 22:15 UTC

    I keep my connection info in a config file, so I'll have something like library.site.conf that includes

    DB_NAME = library DB_HOST = localhost DB_USER = username DB_PASS = password
    Then, in my base DBI package, I have:
    package MyDatabase::DBI; use base qw(Class::DBI::mysql); my %config; use Config::General; my $conf = new Config::General( -ConfigFile => '/usr/local/configfiles/library.site.conf', -InterPolateVars => 1 ); %config = $conf->getall; my $dsn = "DBI:mysql:host=$config{DB_HOST};database=$config{DB_NAME}"; my $db_user = $config{DB_USER}; my $db_pass = $config{DB_PASS}; __PACKAGE__->set_db('Main', $dsn, $db_user, $db_pass); # etc.

    There are a ton of similar ways to do this.

Re: Class::DBI connect info from file?
by PodMaster (Abbot) on Jan 21, 2004 at 21:36 UTC
    ...to Class::DBI rather than hard-coding it in the source for your subclass?
    No, Class::DBI doesn't support reading connection information from a file. The good news is that you don't have to "hard-code" anything. See "set_db" and "Dynamic Database Connections" in the Class::DBI documentation.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Class::DBI connect info from file?
by edoc (Chaplain) on Jan 21, 2004 at 22:45 UTC

    hmm.. there seem to be a lot of complicated answers here.. it's really quite simple.. just move the set_db call, it doesn't have to be in your CDBI modules at all..

    eg in your main script or handler, before you make any calls to CDBI modules..

    my @db_info = get_dbinfo(); My::DBI->set_db('Main', @db_info);

    cheers,

    J

Re: Class::DBI connect info from file?
by hardburn (Abbot) on Jan 21, 2004 at 21:35 UTC

    The Class::DBI documentation covers this. You setup a method called db_Main() which will return a valid DBI handle. All subclasses that don't otherwise override db_Main will use that handle.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      FYI, db_Main doesn't work all that well at the moment. The reason is that unless you pass it an Ima::DBI handle (not vanilla DBI) it will have problems with transactions and with automatic setup classes like Class::DBI::mysql. I think Tony posted a code snippet to the mailing list showing how to open an Ima::DBI handle yourself and pass it through to Class::DBI, which is what you should do if you can't use the set_db method.
Re: Class::DBI connect info from file?
by mattr (Curate) on Jan 22, 2004 at 08:31 UTC
    Great posts here, I just thought I'd mention a possibly problem I had yesterday which might be related to this Main problem mentioned above..

    I wanted to make a Class::DBI object automatically set up by Class::DBI::Pg, but also with mixins of Class::DBI::AsForm and Class::DBI::FromCGI (and CGI qw/:standard/ as per the ::AsForm docs).

    I fiddled for a while and when it took too long, just used the Pg part and ditched the other two modules. I had added a method to the object as in the ::AsForm docs, create_or_edit(). But I kept getting an error to the effect that "Person cannot SELECT" and then a bunch of html tags embedded in the SQL! (e.g. tablename
    and so on).

    Probably I should have persevered and maybe improved my understanding of such things, or.. Is this the problem mentioned about using automatic setup modules above? I thought it would be neat to just use these as my base and have an automatic editting script set up, especially since the other modules I found on cpan to do that seemed buggy.

Re: Class::DBI connect info from file?
by phydeauxarff (Priest) on Jan 21, 2004 at 21:55 UTC
    you mean like this ?
Re: Class::DBI connect info from file?
by Tuppence (Pilgrim) on Jan 22, 2004 at 23:00 UTC
    I use the following, which allows me to use a base class that is defined in a config file - Class::DBI has subclasses for MySQL, pgsql, and others, so I found this to be handy.
    use Mayhem::Config qw/get_config/; my $base_class_name; BEGIN { my $config = get_config(); $base_class_name = $config->perl_object_base_class; eval "use base '$base_class_name'"; eval sprintf("__PACKAGE__->set_db('Main', %s);", $config->db_conn +ect_string); } } 1;
    where $config->perl_object_base_class returns items of the form 'Class::DBI::SQLite' and $config->db_connect_string returns something like '"DBI:sqlite:dbname", "username", "password"' This allows me to run my system against SQLite or mysql or postgres with just changing the config file (and having the databases setup correctly of course ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://323019]
Approved by kutsu
Front-paged by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found