[Catalyst] Accessing DB from external model
Octavian Rasnita
octavian.rasnita at ssifbroker.ro
Fri Nov 5 10:10:23 GMT 2010
From: "Mike Raynham" <catalyst at mikeraynham.co.uk>
> Hi,
>
> I am fairly new to all things Perl and Catalyst, and would like to know if
> what I am attempting to do is correct.
>
> I've recently finished reading 'The Definitive Guide to Catalyst', which
> has been very helpful, but I am still a little confused about separating
> my business logic from my Catalyst application, and accessing the database
> connection from my non-Catalyst application.
>
> At the moment, I have my external class in the lib/MyApp directory. It
> uses the connection info found in MyApp::Model::DB, like so:
>
> ---
>
> # lib/MyApp/MyExternalClass.pm
> MyApp::MyExternalClass
>
> use MyApp::Model::DB;
> use MyApp::Schema;
>
> my $connect_info = MyApp::Model::DB->config->{connect_info};
> my $schema = MyApp::Schema->connect( $connect_info );
>
> # Do stuff with $schema...
>
> ---
>
> That works, and I can connect to the database. However, I have searched
> this mailing list, and found this:
>
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/msg00817.html
>
> Here, the connection information is moved from MyApp::Model::DB to
> MyApp::DB, and then Catalyst::Model::Adaptor is used to glue
> MyApp::Model::DB to MyApp::DB. Is it a good idea? MyApp::Model::DB
> appears to be part of the Catalyst application, so if I want to access the
> database from an external model, it makes sense to me to move the
> connection code outside of the Catalyst application.
>
>
> Regards,
>
> Mike
The best idea would be to put the connection information in the
application's config file like in the example below. (This example uses a
Perl data structure, but you can use any configuration type accepted by
Config::Any).
'Model::DB' => {
schema_class => 'MyApp::Schema',
connect_info => {
dsn => 'dbi:Oracle:host=10.10.10.10;port=1521;sid=ora8',
user => 'user',
password => "password",
#name_sep => '.',
LongReadLen => 100*1024*1024,
LongTruncOk => 1,
on_connect_call => 'datetime_setup',
on_connect_do => [
"alter session set NLS_COMP='LINGUISTIC'",
"alter session set NLS_SORT='BINARY_AI'",
],
},
},
The model will access the connection information directly if it is defined
in the config file, and you can use the data from this config file in any
other external program.
You can use the module Config::JFDI for using the Catalyst config file
easier.
Octavian
More information about the Catalyst
mailing list