Some time ago I posted an RFC for a module on [PM] :
http://www.perlmonks.org/?node_id=491418
At first it all may seem somewhat complicated as it's in several files. But
I wrote it once, and just reuse it.
In config file I have something like :
== database.conf ==
connection_management=startup
[autoreorder]
dsn=dbi:mysql:autoreorder
username=alex
name=autoreorder
[oscommerce]
dsn=dbi:mysql:oscommerce
username=alex
name=oscommerce
And all hard work is done by the DBIx::Handy and a bit of code that is
inherited by all modules that contain Runmodes
package Techcode::Foundation::DataBase;
# Package DataBase - inherithed by all other packages
# that need access to database.
use strict;
=pod
This module is ment to be inherited from all modules that contain
Runmodes. But when it's use outside (such as from delete_sessions.pl)
we need to call new and pass different path to the config file.
Config file is same, but their relative location to one another is
different as cronjob set it's own working directory.
=cut
sub new {
my $class = shift;
my $config_file = shift;
my $self = {};
bless($self, $class);
return $self->db_handy($config_file);
}
sub db_handy {
my $self = shift;
my $config_file = shift || die "Config file not specified.";
require Config::Simple;
my $config = Config::Simple->new($config_file);
my @blocks = $config->get_blocks();
my @databases;
my $default = $config->get_block('default');
foreach(@blocks){
if($_ ne 'default'){
my $db = $config->get_block($_);
$db->{database}->{name} = $_;
$db->{database}=$_;
################################################################
$db->{options} = {'RaiseError' => 1};
push (@databases, $db);
}
}
require DBIx::Handy;
my $DB = DBIx::Handy->new(config => $default,
databases => suppressed);
return $DB;
}
1;
============================================================
package Techcode::Foundation::Runmodes;
use strict;
use base 'CGI::Application';
use base 'Techcode::Foundation::DataBase';
use CGI::Application::Plugin::AutoRunmode;
use CGI::Application::Plugin::Session;
sub setup {
my $self = shift;
my $base = $self->param('APP_NAME') || die "APP_NAME parameter not
set.";
$self->param('TMPL_PATH', $base . '/Templates') unless defined
$self->param('TMPL_PATH');
$self->param('CONF_PATH', $base . '/Config/') unless defined
$self->param('CONF_PATH');
##### Set basic runmodes for errors #####
$self->run_modes([qw/
AUTOLOAD
ERROR
UNAUTHORIZED
/]);
$self->error_mode('ERROR');
##### Set basic runmodes for errors #####
##### Load the DB stuff #####
my $DB = $self->db_handy( $self->param('CONF_PATH') .
'database.conf' );
$self->param('DB',$DB);
##### Load the DB stuff #####
##### Set the configs for Sessions #####
$self->session_config(
CGI_SESSION_OPTIONS => ["driver:MySQL;serializer:Default",
$self->query, {Handle => $DB->dbh()}],
DEFAULT_EXPIRY => '+1h',
COOKIE_PARAMS => { -expires => '+1h',
-path => '/',
},
SEND_COOKIE => 1,
);
##### Set the configs for Sessions #####
##### Set the template stuff #####
$self->tmpl_path( $self->param('TMPL_PATH') );
##### Set the template stuff #####
}
==================================================
And sample of ussage ....
package AutoReorder::Runmodes::Product;
use base 'AutoReorder::Foundation::Runmodes';
sub details : StartRunmode {
my $self = shift;
my $form = $self->query_vars();
my $DB = $self->param('DB');
my $SQL = "SELECT p.products_id, p.products_image, pd.products_name,
pd.products_description
FROM products p INNER JOIN products_description pd ON p.products_id =
pd.products_id
WHERE p.products_id = ?";
my $products_data = $DB->execute(sql => $SQL,
data => [$form->{id}],
database =>
'oscommerce')->fetchrow_hashref();
my $template = $self->load_tmpl('Product/details.dwt');
$template->param(%{$products_data});
return $template->output();
}
1;
Beside being able to work with several DB's which you can call by names I
made DBIx::Handy
have few methods for executing SQL. So it could return AoH (and other) which
are nice to be passed
directly to HTML::Template.
Basically it's not realy a DB Abstraction (it does generate simple
INSERT/UPDATE queries). It's more of
DB Management :)
-------------------------------------------------
Aleksandar Petrovic - Alex
email : suppressed
www : http://www.techcode.net
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/suppressed/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.