]> git.donarmstrong.com Git - infobot.git/blobdiff - src/db_mysql.pl
- fix for null factoids in factinfo.
[infobot.git] / src / db_mysql.pl
index 3cf0433d4e48457d582fd887f8e67aaf607132ad..545abf1cb3bd49370d5ea0e8cc59f167363c0516 100644 (file)
@@ -1,6 +1,6 @@
 #
 # db_mysql.pl: MySQL database frontend.
-#      Author: xk <xk@leguin.openprojects.net>
+#      Author: dms
 #     Version: v0.2c (19991224)
 #     Created: 19991203
 #
@@ -10,8 +10,9 @@ package main;
 if (&IsParam("useStrict")) { use strict; }
 
 sub openDB {
-    my $dsn = "DBI:mysql:$param{'DBName'}:$param{'SQLHost'}";
-    $dbh    = DBI->connect($dsn, $param{'SQLUser'}, $param{'SQLPass'});
+    my ($db, $user, $pass) = @_;
+    my $dsn = "DBI:mysql:$db:$param{'SQLHost'}";
+    $dbh    = DBI->connect($dsn, $user, $pass);
 
     if ($dbh) {
        &status("Opened MySQL connection to $param{'SQLHost'}");
@@ -53,10 +54,12 @@ sub dbGet {
        return;
     }
 
+    &SQLDebug($query);
     if (!$sth->execute) {
        &ERROR("Get => '$query'");
        &ERROR("Get => $DBI::errstr");
-       return;
+       $sth->finish;
+       return 0;
     }
 
     my @retval = $sth->fetchrow_array;
@@ -80,7 +83,13 @@ sub dbGetCol {
     my %retval;
 
     my $sth = $dbh->prepare($query);
-    &ERROR("GetCol => '$query'") unless $sth->execute;
+    &SQLDebug($query);
+    if (!$sth->execute) {
+       &ERROR("GetCol => '$query'");
+       &ERROR("GetCol => $DBI::errstr");
+       $sth->finish;
+       return;
+    }
 
     if (defined $type and $type == 1) {
        while (my @row = $sth->fetchrow_array) {
@@ -101,7 +110,27 @@ sub dbGetCol {
 ####
 # Usage: &dbGetRowInfo($table);
 sub dbGetRowInfo {
-    &DEBUG("STUB: dbGetRowInfo().");
+    my ($table) = @_;
+
+    my $query = "SHOW COLUMNS from $table";
+    my %retval;
+
+    my $sth = $dbh->prepare($query);
+    &SQLDebug($query);
+    if (!$sth->execute) {
+       &ERROR("GRI => '$query'");
+       &ERROR("GRI => $DBI::errstr");
+       $sth->finish;
+       return;
+    }
+
+    my @cols;
+    while (my @row = $sth->fetchrow_array) {
+       push(@cols, $row[0]);
+    }
+    $sth->finish;
+
+    return @cols;
 }
 
 #####
@@ -194,9 +223,11 @@ sub dbRaw {
        return 0;
     }
 
+    &SQLDebug($query);
     if (!$sth->execute) {
        &ERROR("Raw($prefix): => '$query'");
        &ERROR("Raw($prefix): $DBI::errstr");
+       $sth->finish;
        return 0;
     }
 
@@ -211,6 +242,7 @@ sub dbRawReturn {
     my @retval;
 
     my $sth = $dbh->prepare($query);
+    &SQLDebug($query);
     &ERROR("RawReturn => '$query'.") unless $sth->execute;
     while (my @row = $sth->fetchrow_array) {
        push(@retval, $row[0]);
@@ -241,7 +273,9 @@ sub getKeys {
     my $query  = "SELECT $primkey FROM $table";
     my $sth    = $dbh->prepare($query);
 
-    $sth->execute;
+    &SQLDebug($query);
+    &WARN("ERROR: getKeys($query)") unless $sth->execute;
+
     while (my @row = $sth->fetchrow_array) {
        push(@retval, $row[0]);
     }
@@ -258,7 +292,8 @@ sub randKey {
     my $query  = "SELECT $select FROM $table LIMIT $rand,1";
 
     my $sth    = $dbh->prepare($query);
-    $sth->execute;
+    &SQLDebug($query);
+    &WARN("randKey($query)") unless $sth->execute;
     my @retval = $sth->fetchrow_array;
     $sth->finish;
 
@@ -294,7 +329,8 @@ sub searchTable {
     my $query = "SELECT $select FROM $table WHERE $key LIKE ". 
                &dbQuote($str);
     my $sth = $dbh->prepare($query);
-    $sth->execute;
+    &SQLDebug($query);
+    &WARN("Search($query)") unless $sth->execute;
 
     while (my @row = $sth->fetchrow_array) {
        push(@results, $row[0]);
@@ -331,4 +367,12 @@ sub delFactoid {
     return 1;
 }
 
+sub SQLDebug {
+    return unless (&IsParam("SQLDebug"));
+
+    return if (!fileno SQLDEBUG);
+
+    print SQLDEBUG $_[0]."\n";
+}
+
 1;