This isn't verbatim what I'm doing, since posting a webapp in here doesn't really make sense. But I'll try to be true to the problem I'm trying to solve. Please forgive me any typos.
What I would like to do is use CGI::Application::Plugin::DBH to provide access to my database in conjunction with CGI::Ajax. When the page loads normally, the CGI::Application setup sub is called and Plugin::DBH can do its thing. However, when 'BUILD_CHART' is called from the the javascript function 'GET_CHART', it doesn't go through the setup routine. Can I still access the database config set up Plugin::DBH when executing a the 'BUILD_CHART' sub? How?
package MyWebApp;
use base 'CGI::Application';
use strict;
use CGI;
use CGI::Ajax;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Application::Plugin::DBH qw(dbh_config dbh);
use DBI qw(:sql_types);
use DBIx::Chart;
sub setup{
my $self = shift;
$self->start_mode('mode1');
$self->mode_param('rm');
$self->run_modes(
'mode1' => 'runMode1',
'mode2' => 'runMode2'
);
$self->dbh_config('dbh1', ['DBI:DB2:DS1', 'usr', 'pwd', {RaiseError
+=> 0, PrintError => 0}]);
}
sub runMode1 {
my $q = new CGI;
my $pjx = new CGI::Ajax('GET_CHART' => \&BUILD_CHART, 'skip_header'
+=> 1);
open(SRC, "pageSrc.htm") or die $!;
my $src = <SRC>;
close(SRC);
my $output = $q->start_html;
$output .= $src;
$output .= $q->end_html;
return $pjx->build_html($q, $output);
}
sub BUILD_CHART {
my ($chartSelection, $screenWidth) = @_;
my $chartWidth = $screenWidth / 2;
#this is where I want to take advantage of CGI::Application::Plugin:
+:DBH -- so that I don't have to build this connection every time an a
+jax call is made to this subroutine
my $dbh = DBIx::Chart->connect('DBI:DB2:DS1', 'usr', 'pwd', {RaiseEr
+ror => 0, PrintError => 0}) or die $DBI::errstr;
my $qryStatement = "SELECT SOMETHING FROM SOMEWHERE";
my $chartStatement = "RETURNING BARCHART(*), IMAGEMAP WHERE ...";
my $sth = $dbh->prepare("$qryStatement $chartStatement");
unless(eval{$sth->execute();}){
die "Failed to execute!";
}
my $tempChartRef = $sth->fetchrow_arrayref or die "Failed to fetch!"
+;
$sth->finish;
my $imageData = $$tempChartRef[0];
my $imageMap = $$tempChartRef[1];
my $imageName = &WRITE_IMAGE_TO_DISK($imageData, 'PNG', 'barchart');
my $html = "<IMG SRC=\"/$imageName\" USEMAP=\"#barMap\">";
$html .= $imageMap;
return $html;
}
|