]> git.donarmstrong.com Git - infobot.git/blobdiff - src/db_mysql.pl
fixed the following bugs:
[infobot.git] / src / db_mysql.pl
index e8055569fba60964b27aa603b4530dec3a30e9d1..754bcdab70513de474e122caec07c0e1f1fb0fdf 100644 (file)
@@ -25,10 +25,7 @@ sub openDB {
 }
 
 sub closeDB {
-    if (!$dbh) {
-       &WARN("closeDB: connection already closed?");
-       return 0;
-    }
+    return 0 unless ($dbh);
 
     &status("Closed MySQL connection to $param{'SQLHost'}.");
     $dbh->disconnect();
@@ -58,7 +55,8 @@ sub dbGet {
     if (!$sth->execute) {
        &ERROR("Get => '$query'");
        &ERROR("Get => $DBI::errstr");
-       return;
+       $sth->finish;
+       return 0;
     }
 
     my @retval = $sth->fetchrow_array;
@@ -86,6 +84,7 @@ sub dbGetCol {
     if (!$sth->execute) {
        &ERROR("GetCol => '$query'");
        &ERROR("GetCol => $DBI::errstr");
+       $sth->finish;
        return;
     }
 
@@ -118,6 +117,8 @@ sub dbGetRowInfo {
     if (!$sth->execute) {
        &ERROR("GRI => '$query'");
        &ERROR("GRI => $DBI::errstr");
+       $sth->finish;
+       return;
     }
 
     my @cols;
@@ -184,6 +185,26 @@ sub dbInsert {
     return 1;
 }
 
+#####
+# Usage: &dbReplace($table, $primkey, $primval, %hash);
+sub dbReplace {
+    my ($table, $primkey, $primval, %hash) = @_;
+    my (@keys, @vals);
+
+    foreach (keys %hash) {
+       &DEBUG("hash{$_} => $hash{$_}");
+       push(@keys, $_);
+       push(@vals, &dbQuote($hash{$_}));
+    }
+
+    &dbRaw("Replace($table)", "REPLACE INTO $table (".join(',',@keys).
+               ") VALUES (".join(',',@vals).") WHERE $primkey=".
+               &dbQuote($primval)
+    );
+
+    return 1;
+}
+
 #####
 # Usage: &dbSetRow($table, @values);
 sub dbSetRow {
@@ -222,7 +243,8 @@ sub dbRaw {
     &SQLDebug($query);
     if (!$sth->execute) {
        &ERROR("Raw($prefix): => '$query'");
-       &ERROR("Raw($prefix): $DBI::errstr");
+#      &ERROR("Raw($prefix): $DBI::errstr");
+       $sth->finish;
        return 0;
     }
 
@@ -357,7 +379,7 @@ sub delFactoid {
     my ($faqtoid) = @_;
 
     &dbDel("factoids", "factoid_key",$faqtoid);
-    &status("DELETED $faqtoid");
+    &status("DELETED '$faqtoid'");
 
     return 1;
 }