--- DatabasePlugin.pm.orig 2003-05-05 10:30:37.000000000 -0700 +++ DatabasePlugin.pm 2005-09-22 12:57:27.101256000 -0700 @@ -113,11 +113,16 @@ use vars qw( $web $topic $user $installWeb $VERSION $debug $security_on $security_message $description %db_driver %db_name %db_sid %db_table - %db_username %db_password %db_hostname + %db_username %db_password %db_hostname %driver_db_string ); $VERSION = '1.3'; +# Initialize the hash used to insert the appropriate +# field name string in the DSN for DBI->connect calls +# based on the driver being used +%driver_db_string = ( mysql => 'database', Pg => 'dbname', Oracle => 'database', ODBC => 'dsn' ); + # ========================= sub initPlugin { @@ -157,14 +162,24 @@ if ($DatabasePluginConfig::db_sid ne "") { $sid = ";sid=$DatabasePluginConfig::db_sid" } - my $db = DBI->connect("DBI:$DatabasePluginConfig::db_driver:database=$DatabasePluginConfig::db_database;host=$DatabasePluginConfig::db_hostname$sid", $DatabasePluginConfig::db_username, $DatabasePluginConfig::db_password, {PrintError=>1, RaiseError=>0}); + + my $db = eval { + DBI->connect("DBI:$DatabasePluginConfig::db_driver:$driver_db_string{$DatabasePluginConfig::db_driver}=$DatabasePluginConfig::db_database;host=$DatabasePluginConfig::db_hostname$sid", $DatabasePluginConfig::db_username, $DatabasePluginConfig::db_password, {PrintError=>1, RaiseError=>0}) + }; if (! $db ) { - return make_error("Can't open initialization DatabasePlugin database '$DatabasePluginConfig::db_database'"); + return make_error("Can't open initialization DatabasePlugin database '$DatabasePluginConfig::db_database'" - $DBI::errstr); } my $cmd = "SELECT description, driver, db_name, db_sid, table_name, ro_username, ro_password, hostname FROM $DatabasePluginConfig::db_table"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare($cmd); - $sth->execute; + my $sth = eval { $db->prepare($cmd); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval {$sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } + # Fill hashes with the database information. while (my @row = $sth->fetchrow_array()) { $db_driver{$row[0]} = $row[1]; @@ -253,9 +268,12 @@ if ($db_sid ne "") { $sid = ";sid=$db_sid"; } - my $db = DBI->connect("DBI:$db_driver:database=$database;host=$hostname$sid", $user, $password, {PrintError=>1, RaiseError=>1}); + my $db = eval { + DBI->connect("DBI:$db_driver:$driver_db_string{$db_driver}=$database;host=$hostname$sid", + $user, $password, {PrintError=>1, RaiseError=>1}) + }; if (! $db ) { - return make_error("Can't open database specified by description '$description'"); + return make_error("Can't open database specified by description '$description'" . $DBI::errstr); } # Generate table header using the table column names @@ -267,8 +285,15 @@ my $col = join(", ", @columns); my $cmd = "SELECT $col FROM $table"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare($cmd); - $sth->execute; + my $sth = eval { $db->prepare($cmd); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval { $sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } + while (my @row = $sth->fetchrow_array()) { my $row = "| "; for my $c (@row) { @@ -326,14 +351,23 @@ if ($db_sid ne "") { $sid = ";sid=$db_sid"; } - my $db = DBI->connect("DBI:$db_driver:database=$database;host=$hostname$sid", $user, $password, {PrintError=>1, RaiseError=>1}); + my $db = eval { + DBI->connect("DBI:$db_driver:$driver_db_string{$db_driver}=$database;host=$hostname$sid", + $user, $password, {PrintError=>1, RaiseError=>1}) + }; if (! $db ) { - return make_error("Can't open database specified by description '$description'"); + return make_error("Can't open database specified by description '$description'" . $DBI::errstr); } my $cmd = "$command"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare($cmd); - $sth->execute; + my $sth = eval { $db->prepare($cmd); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval {$sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } # Generate table header using the 'headers' values for column names my $line = "| "; for my $c (@headers) { @@ -398,16 +432,27 @@ if ($db_sid ne "") { $sid = ";sid=$db_sid"; } - my $db = DBI->connect("DBI:$db_driver:database=$database;host=$hostname$sid", $user, $password, {PrintError=>1, RaiseError=>1}); + + my $db = eval { + DBI->connect("DBI:$db_driver:$driver_db_string{$db_driver}=$database;host=$hostname$sid", + $user, $password, {PrintError=>1, RaiseError=>1}) + }; if (! $db ) { - return make_error("Can't open database specified by description '$description'"); + return make_error("Can't open database specified by description '$description' - " . $DBI::errstr); } my $col = join(", ", @columns); my $cmd = "SELECT $col FROM $table"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare($cmd); - $sth->execute; + my $sth = eval { $db->prepare($cmd); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval { $sth->execute;}; + if ($@) { + return make_error($DBI::errstr); + } + my $line; while (my @row = $sth->fetchrow_array()) { # Now for each row in the database, we attempt to perform any @@ -474,14 +519,24 @@ if ($db_sid ne "") { $sid = ";sid=$db_sid"; } - my $db = DBI->connect("DBI:$db_driver:database=$database;host=$hostname$sid", $user, $password, {PrintError=>1, RaiseError=>1}); + my $db = eval { + DBI->connect("DBI:$db_driver:$driver_db_string{$db_driver}=$database;host=$hostname$sid", + $user, $password, {PrintError=>1, RaiseError=>1}) + }; if (! $db ) { - return make_error("Can't open database specified by description '$description'"); + return make_error("Can't open database specified by description '$description'" . $DBI::errstr); } my $cmd = $command; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare("$cmd"); - $sth->execute; + my $sth = eval { $db->prepare("$cmd"); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval {$sth->execute;}; + if ($@) { + return make_error($DBI::errstr); + } + my $line; while (my @row = $sth->fetchrow_array()) { @@ -539,19 +648,30 @@ if ($db_sid ne "") { $sid = ";sid=$db_sid"; } - my $db = DBI->connect("DBI:$db_driver:database=$database;host=$hostname$sid", $user, $password, {PrintError=>1, RaiseError=>1}); + my $db = eval { + DBI->connect("DBI:$db_driver:$driver_db_string{$db_driver}=$database;host=$hostname$sid", + $user, $password, {PrintError=>1, RaiseError=>1}) + }; if (! $db ) { - return "Can't open database specified by description '$description'"; + return "Can't open database specified by description '$description' - " . $DBI::errstr; } my $cmd; if ($db_driver eq "Oracle") { $cmd = "SELECT COLUMN_NAME FROM all_tab_columns WHERE TABLE_NAME = '$table'"; + } elsif ($db_driver eq "Pg") { + $cmd = "SELECT attname FROM pg_attribute WHERE attrelid=(SELECT oid FROM pg_class WHERE relname='$table') AND attnum>0"; } else { $cmd = "DESCRIBE $table"; } &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; - my $sth = $db->prepare($cmd); - $sth->execute; + my $sth = eval { $db->prepare($cmd); }; + if (!$sth) { + return make_error($DBI::errstr); + } + eval {$sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } my @columns; while (my @row = $sth->fetchrow_array()) { push (@columns, $row[0]);