--- DatabasePlugin.pm.orig Mon May 5 10:30:37 2003 +++ DatabasePlugin.pm Tue Jul 20 16:12:53 2004 @@ -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' ); + # ========================= sub initPlugin { @@ -157,14 +162,18 @@ 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 = 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'"); } 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; + 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,7 +262,7 @@ 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 = 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'"); } @@ -268,7 +277,11 @@ my $cmd = "SELECT $col FROM $table"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; my $sth = $db->prepare($cmd); - $sth->execute; + eval { $sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } + while (my @row = $sth->fetchrow_array()) { my $row = "| "; for my $c (@row) { @@ -326,14 +339,17 @@ 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 = 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'"); } my $cmd = "$command"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; my $sth = $db->prepare($cmd); - $sth->execute; + 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,7 +414,7 @@ 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 = 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'"); } @@ -407,7 +423,11 @@ my $cmd = "SELECT $col FROM $table"; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; my $sth = $db->prepare($cmd); - $sth->execute; + 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 +494,18 @@ 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 = 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'"); } my $cmd = $command; &TWiki::Func::writeDebug( "- TWiki::Plugins::DatabasePlugin [$cmd]") if $debug; my $sth = $db->prepare("$cmd"); - $sth->execute; + eval {$sth->execute;}; + if ($@) { + return make_error($DBI::errstr); + } + my $line; while (my @row = $sth->fetchrow_array()) { @@ -539,19 +563,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 = 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'"; } 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; + eval {$sth->execute; }; + if ($@) { + return make_error($DBI::errstr); + } my @columns; while (my @row = $sth->fetchrow_array()) { push (@columns, $row[0]);