hi,
I've a script which inserts (correctly) into several tables. I'm trying to get errors from dbi so that I can rollback, and then display a more user-friendly error message than the standard database error if something goes wrong (usually the form not filled out correctly).
On an error my script will end up in the if($@) section and so rollsback, but I can't get the errors to print out. Any ideas?
Thanks,
Fraser
eval
{
### check that there are no empty fields in the required arrays
### (null values are set for those without competencies/compliances)
if ((checkForNULLs(\@competencies) + checkForNULLs(\@compliances)) > 0)
{
die "Not all required fields were filled";
}
### have all required data, we can start adding to database
if ($team ne '-' && $team ne '')
{
if (!defined($isStrat) || $isStrat eq '')
{
$isStrat = 'N';
}
my $teamSQL = "INSERT INTO aat_agenthasteam "
. "(ahtagentid, ahtmonth, ahtisstrat, ahtteamname)"
. "VALUES "
. "('$id', TO_DATE('$month', 'FMMonth YYYY'), '$isStrat', '$team')";
$dbh->do($teamSQL) or die $dbh->errstr();
}
### insert report variables into summary table
my $qSummary = $dbh->quote($summary);
my $sumSQL = "INSERT INTO aat_agentmonthlyassessment "
. "(amaagentid, amamonth, amasummary, amadateadded, amaenteredby) "
. "VALUES "
. "('$id', TO_DATE('$month', 'FMMonth YYYY'), $qSummary, getrmctime, '$ldapID')";
$dbh->do($sumSQL) or die $dbh->errstr();
### insert competencies into database
for (my $i=0; $i<@compIDs; $i++)
{
my $qComment;
my $compNo = $compIDs[$i];
if ($comments[$i] eq '')
{
$qComment = 'NULL';
}
else
{
$qComment = $dbh->quote($comments[$i]);
}
my $sql = "INSERT INTO aat_agenthascompetency "
. "(ahcagentid, ahccompid, ahcmonth, ahcenteredby, ahccompetency, ahccompliance, ahcnotes) "
. "VALUES "
. "('$id', $compNo, TO_DATE('$month', 'FMMonth YYYY'), '$ldapID', $competencies[$i], $compliances[$i], $qComment)";
$dbh->do($sql) or die $dbh->errstr();
}
}; ### end of outer eval
### commit if no problems, else rollback
if ($@)
{
$dbh->do('rollback');
print $@; #doesn't work
if ($@ =~ /CK_AHCCOMPETENCY/)
{
print "<P>All competencies must have value 0, 1, 2, 3, 4 or 5.</P>";
}
### print error message
print "<P>No change has been made to the database</P>";
displayEnterReportForm($dbh, $id, $month, $ldapID, $team, $isStrat, \@competencies, \@compliances, \@comments, $summary);
}
else
{
$dbh->do('commit');
print "<P>All entries have been added to the database.</P>";
}
I've a script which inserts (correctly) into several tables. I'm trying to get errors from dbi so that I can rollback, and then display a more user-friendly error message than the standard database error if something goes wrong (usually the form not filled out correctly).
On an error my script will end up in the if($@) section and so rollsback, but I can't get the errors to print out. Any ideas?
Thanks,
Fraser
eval
{
### check that there are no empty fields in the required arrays
### (null values are set for those without competencies/compliances)
if ((checkForNULLs(\@competencies) + checkForNULLs(\@compliances)) > 0)
{
die "Not all required fields were filled";
}
### have all required data, we can start adding to database
if ($team ne '-' && $team ne '')
{
if (!defined($isStrat) || $isStrat eq '')
{
$isStrat = 'N';
}
my $teamSQL = "INSERT INTO aat_agenthasteam "
. "(ahtagentid, ahtmonth, ahtisstrat, ahtteamname)"
. "VALUES "
. "('$id', TO_DATE('$month', 'FMMonth YYYY'), '$isStrat', '$team')";
$dbh->do($teamSQL) or die $dbh->errstr();
}
### insert report variables into summary table
my $qSummary = $dbh->quote($summary);
my $sumSQL = "INSERT INTO aat_agentmonthlyassessment "
. "(amaagentid, amamonth, amasummary, amadateadded, amaenteredby) "
. "VALUES "
. "('$id', TO_DATE('$month', 'FMMonth YYYY'), $qSummary, getrmctime, '$ldapID')";
$dbh->do($sumSQL) or die $dbh->errstr();
### insert competencies into database
for (my $i=0; $i<@compIDs; $i++)
{
my $qComment;
my $compNo = $compIDs[$i];
if ($comments[$i] eq '')
{
$qComment = 'NULL';
}
else
{
$qComment = $dbh->quote($comments[$i]);
}
my $sql = "INSERT INTO aat_agenthascompetency "
. "(ahcagentid, ahccompid, ahcmonth, ahcenteredby, ahccompetency, ahccompliance, ahcnotes) "
. "VALUES "
. "('$id', $compNo, TO_DATE('$month', 'FMMonth YYYY'), '$ldapID', $competencies[$i], $compliances[$i], $qComment)";
$dbh->do($sql) or die $dbh->errstr();
}
}; ### end of outer eval
### commit if no problems, else rollback
if ($@)
{
$dbh->do('rollback');
print $@; #doesn't work
if ($@ =~ /CK_AHCCOMPETENCY/)
{
print "<P>All competencies must have value 0, 1, 2, 3, 4 or 5.</P>";
}
### print error message
print "<P>No change has been made to the database</P>";
displayEnterReportForm($dbh, $id, $month, $ldapID, $team, $isStrat, \@competencies, \@compliances, \@comments, $summary);
}
else
{
$dbh->do('commit');
print "<P>All entries have been added to the database.</P>";
}