From 172985e2a0fae110cc0cb8de7578dbcd75b75518 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 9 Mar 2007 05:38:44 +0000 Subject: [PATCH] - fix: 7.4 Editing SQL Form seems to break site ...? --- lib/WebGUI/DatabaseLink.pm | 12 ++++---- lib/WebGUI/SQL.pm | 6 ++-- lib/WebGUI/Session.pm | 46 ++++++++++++------------------ lib/WebGUI/Session/DateTime.pm | 2 +- lib/WebGUI/Session/ErrorHandler.pm | 8 ++++-- lib/WebGUI/Session/Http.pm | 2 +- 6 files changed, 33 insertions(+), 43 deletions(-) diff --git a/lib/WebGUI/DatabaseLink.pm b/lib/WebGUI/DatabaseLink.pm index cd7280034..50abe1e51 100644 --- a/lib/WebGUI/DatabaseLink.pm +++ b/lib/WebGUI/DatabaseLink.pm @@ -137,14 +137,12 @@ sub db { $self->{_dbh} = $self->session->db; return $self->{_dbh}; } elsif ($dsn =~ /\DBI\:\w+\:\w+/i) { - eval{ - $self->{_dbh} = WebGUI::SQL->connect($self->session,$dsn,$username,$identifier); - }; - if ($@) { - $self->session->errorHandler->warn("DatabaseLink [".$self->getId."] ".$@); - } else { - return $self->{_dbh}; + my $dbh = WebGUI::SQL->connect($self->session,$dsn,$username,$identifier); + unless (defined $dbh) { + $self->session->errorHandler->arn("Cannot connect to DatabaseLink [".$self->getId."]"); } + $self->{_dbh} = $dbh; + return $self->{_dbh}; } else { $self->session->errorHandler->warn("DatabaseLink [".$self->getId."] The DSN specified is of an improper format."); } diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index f38d5100e..1fe6c7d51 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -317,12 +317,10 @@ sub connect { my $user = shift; my $pass = shift; my $dbh = DBI->connect($dsn,$user,$pass,{RaiseError=>0,AutoCommit=>1 }); - unless (defined $dbh) { - $session->setDbNotAvailable; - $session->errorHandler->fatal("Couldn't connect to database."); + $session->errorHandler->error("Couldn't connect to database: $dsn"); + return undef; } - if ( $dsn =~ /Oracle/ || $dsn =~ /ODBC/ ) { # Set specific attributes for long Oracle and ODBC DSNs $dbh->{LongReadLen} = 512 * 1024; $dbh->{LongTruncOk} = 1; diff --git a/lib/WebGUI/Session.pm b/lib/WebGUI/Session.pm index ab9bc9761..8d006e1db 100644 --- a/lib/WebGUI/Session.pm +++ b/lib/WebGUI/Session.pm @@ -117,7 +117,7 @@ Cleans up a WebGUI session information from memory and disconnects from any reso sub close { my $self = shift; - $self->db->disconnect unless $self->dbNotAvailable; + $self->db->disconnect if (exists $self->{_db}); # Kill circular references. The literal list is so that the order # can be explicitly shuffled as necessary. @@ -158,16 +158,32 @@ sub datetime { #------------------------------------------------------------------- -=head2 db ( ) +=head2 db ( [ skipFatal ] ) Returns a WebGUI::SQL object, which is connected to the WebGUI database. +=head3 skipFatal + +If set to true, we won't throw a fatal error, just return undef. + =cut sub db { my $self = shift; + my $skipFatal = shift; unless (exists $self->{_db}) { - $self->{_db} = WebGUI::SQL->connect($self,$self->config->get("dsn"), $self->config->get("dbuser"), $self->config->get("dbpass")); + my $db = WebGUI::SQL->connect($self,$self->config->get("dsn"), $self->config->get("dbuser"), $self->config->get("dbpass")); + if (defined $db) { + $self->{_db} = $db; + } + else { + if ($skipFatal) { + return undef + } + else { + $self->errorHandler->fatal("Couldn't connect to WebGUI database, and can't continue without it."); + } + } } return $self->{_db}; } @@ -618,28 +634,4 @@ sub var { return $self->{_var}; } -#------------------------------------------------------------------- - -=head2 setDbNotAvailable ( ) - -Sets a flag for this session indicating that database accesses are known to be probably broken and should not be performed. - -=cut - -sub setDbNotAvailable { - my $self = shift; - $self->{_dbNotAvailable} = 1; -} - -=head2 dbNotAvailable ( ) - -Returns true iff there is an error condition such that no database accesses should be attempted for this session whatsoever. - -=cut - -sub dbNotAvailable { - my $self = shift; - return $self->{_dbNotAvailable}; -} - 1; diff --git a/lib/WebGUI/Session/DateTime.pm b/lib/WebGUI/Session/DateTime.pm index 93062e028..a1f1aa288 100644 --- a/lib/WebGUI/Session/DateTime.pm +++ b/lib/WebGUI/Session/DateTime.pm @@ -609,7 +609,7 @@ Returns the timezone for this user, in DateTime::TimeZone format. Checks to mak sub getTimeZone { my $self = shift; - return 'America/Chicago' if $self->session->dbNotAvailable; + return 'America/Chicago' unless defined $self->session->db(1); return $self->session->user->{_timeZone} if $self->session->user->{_timeZone}; my @zones = @{DateTime::TimeZone::all_names()}; my $zone = $self->session->user->profileField('timeZone'); diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index 6134242c9..040994fa6 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -208,15 +208,17 @@ sub fatal { $self->getLogger->debug("Stack trace for FATAL ".$message."\n".$self->getStackTrace()); $self->session->http->sendHeader if ($self->session->request); - if ($self->session->dbNotAvailable) { + if (! defined $self->session->db(1)) { # We can't even _determine_ whether we can show the debug text. Punt. $self->session->output->print("

Fatal Internal Error

"); - } elsif ($self->canShowDebug()) { + } + elsif ($self->canShowDebug()) { $self->session->output->print("

WebGUI Fatal Error

Something unexpected happened that caused this system to fault.

\n",1); $self->session->output->print("

".$message."

\n",1); $self->session->output->print($self->getStackTrace(), 1); $self->session->output->print($self->showDebug(),1); - } else { + } + else { # NOTE: You can't internationalize this because with some types of errors that would cause an infinite loop. $self->session->output->print("

Problem With Request

We have encountered a problem with your request. Please use your back button and try again. diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index e00b7d466..fe384686a 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -247,7 +247,7 @@ Generates and sends HTTP headers for a response. sub sendHeader { my $self = shift; return undef if ($self->{_http}{noHeader}); - return $self->_sendMinimalHeader if $self->session->dbNotAvailable; + return $self->_sendMinimalHeader unless defined $self->session->db(1); my ($request, $datetime, $config, $var) = $self->session->quick(qw(request datetime config var)); return undef unless $request;