]> git.donarmstrong.com Git - infobot.git/blobdiff - src/dbi.pl
factstats author
[infobot.git] / src / dbi.pl
index 893d80aba79d9bb0c8869b5f30fb64342edcd815..f10845a2b59cfa0648456f1e4655d5ca671fba40 100644 (file)
@@ -39,8 +39,8 @@ sub sqlOpenDB {
     if ($dbh && !$dbh->err) {
        &status("Opened $type connection$hoststr");
     } else {
-       &ERROR("cannot connect$hoststr.");
-       &ERROR("since $type is not available, shutting down bot!");
+       &ERROR("Cannot connect$hoststr.");
+       &ERROR("Since $type is not available, shutting down bot!");
        &ERROR( $dbh->errstr ) if ($dbh);
        &closePID();
        &closeSHM($shm);
@@ -88,9 +88,12 @@ sub sqlSelectMany {
        return;
     }
 
-    my $where = ($where_href) ? &hashref2where($where_href) : "";
-    $query .= " WHERE $where"  if ($where);
-    $query .= "$other"         if $other;
+    $query .= " WHERE " if (($where_href) || ($other));
+    if ($where_href) {
+       my $where = &hashref2where($where_href);
+       $query .= "$where" if ($where);
+    }
+    $query .= " $other"                if $other;
 
     if (!($sth = $dbh->prepare($query))) {
        &ERROR("sqlSelectMany: prepare: $DBI::errstr");
@@ -130,13 +133,33 @@ sub sqlSelect {
 }
 
 #####
-#  Usage: &sqlSelectColHash($table, $select, [$where_href], [$type]);
+#  Usage: &sqlSelectColArray($table, $select, [$where_href], [$other]);
+# Return: array.
+sub sqlSelectColArray {
+    my $sth    = &sqlSelectMany(@_);
+    my @retval;
+
+    if (!defined $sth) {
+       &WARN("sqlSelect failed.");
+       return;
+    }
+
+    while (my @row = $sth->fetchrow_array) {
+       push(@retval, $row[0]);
+    }
+    $sth->finish;
+
+    return @retval;
+}
+
+#####
+#  Usage: &sqlSelectColHash($table, $select, [$where_href], [$other], [$type]);
 # Return: type = 1: $retval{ col2 }{ col1 } = 1;
 # Return: no  type: $retval{ col1 } = col2;
 #   Note: does not support $other, yet.
 sub sqlSelectColHash {
-    my ($table, $select, $where_href, $type) = @_;
-    my $sth    = &sqlSelectMany($table, $select, $where_href);
+    my ($table, $select, $where_href, $other, $type) = @_;
+    my $sth    = &sqlSelectMany($table, $select, $where_href, $other);
     if (!defined $sth) {
        &WARN("sqlSelectColhash failed.");
        return;
@@ -212,15 +235,14 @@ sub sqlSet {
        return;
     }
 
-    my $k = (keys %{ $where_href })[0];
+    # any column can be NULL... so just get them all.
+    my $k = join(',', keys %{ $where_href } );
     my $result = &sqlSelect($table, $k, $where_href);
-    &DEBUG("result is not defined :(") if (!defined $result);
+#    &DEBUG("result is not defined :(") if (!defined $result);
 
     if (1 or defined $result) {
        &sqlUpdate($table, $data_href, $where_href);
     } else {
-       &DEBUG("doing insert...");
-
        # hack.
        my %hash = %{ $where_href };
        # add data_href values...
@@ -379,7 +401,6 @@ sub sqlRawReturn {
        return 0;
     }
 
-
     while (my @row = $sth->fetchrow_array) {
        push(@retval, $row[0]);
     }
@@ -559,7 +580,7 @@ sub searchTable {
     return @results;
 }
 
-sub dbCreateTable {
+sub sqlCreateTable {
     my($table) = @_;
     my(@path)  = ($bot_data_dir, ".","..","../..");
     my $found  = 0;
@@ -585,7 +606,7 @@ sub dbCreateTable {
     if (!$found) {
        return 0;
     } else {
-       &sqlRaw("dbcreateTable($table)", $data);
+       &sqlRaw("sqlCreateTable($table)", $data);
        return 1;
     }
 }