&status("Opened MySQL connection to $param{'SQLHost'}");
} else {
&ERROR("cannot connect to $param{'SQLHost'}.");
+ &ERROR("since mysql is not available, shutting down bot!");
&shutdown();
&closePID();
exit 1;
}
#####
-# Usage: &dbGet($table, $primkey, $primval, $select);
+# Usage: &dbGet($table, $select, $where);
sub dbGet {
- my ($table, $primkey, $primval, $select) = @_;
- my $query = "SELECT $select FROM $table WHERE $primkey=".
- &dbQuote($primval);
+ my ($table, $select, $where) = @_;
+ my $query = "SELECT $select FROM $table";
+ $query .= " WHERE $where" if ($where);
+
+ if (!defined $select) {
+ &WARN("dbGet: select == NULL. table => $table");
+ return;
+ }
my $sth;
if (!($sth = $dbh->prepare($query))) {
- &ERROR("Get: $DBI::errstr");
+ &ERROR("Get: prepare: $DBI::errstr");
return;
}
&SQLDebug($query);
if (!$sth->execute) {
- &ERROR("Get => '$query'");
- &ERROR("Get => $DBI::errstr");
+ &ERROR("Get: execute: '$query'");
$sth->finish;
return 0;
}
}
#####
-# Usage: &dbGetCol($table, $primkey, $key, [$type]);
+# Usage: &dbGetCol($table, $select, $where, [$type]);
sub dbGetCol {
- my ($table, $primkey, $key, $type) = @_;
- my $query = "SELECT $primkey,$key FROM $table WHERE $key IS NOT NULL";
+ my ($table, $select, $where, $type) = @_;
+ my $query = "SELECT $select FROM $table";
+ $query .= " WHERE ".$where if ($where);
my %retval;
+ &DEBUG("dbGetCol: query => '$query'.");
+
my $sth = $dbh->prepare($query);
&SQLDebug($query);
if (!$sth->execute) {
- &ERROR("GetCol => '$query'");
- &ERROR("GetCol => $DBI::errstr");
+ &ERROR("GetCol: execute: '$query'");
+# &ERROR("GetCol => $DBI::errstr");
$sth->finish;
return;
}
- if (defined $type and $type == 1) {
+ if (defined $type and $type == 2) {
+ &DEBUG("dbgetcol: type 2!");
+ while (my @row = $sth->fetchrow_array) {
+ $retval{$row[0]} = join(':', $row[1..$#row]);
+ }
+ &DEBUG("dbgetcol: count => ".scalar(keys %retval) );
+
+ } elsif (defined $type and $type == 1) {
while (my @row = $sth->fetchrow_array) {
# reverse it to make it easier to count.
- $retval{$row[1]}{$row[0]} = 1;
+ if (scalar @row == 2) {
+ $retval{$row[1]}{$row[0]} = 1;
+ } elsif (scalar @row == 3) {
+ $retval{$row[1]}{$row[0]} = 1;
+ }
+ # what to do if there's only one or more than 3?
}
+
} else {
while (my @row = $sth->fetchrow_array) {
$retval{$row[0]} = $row[1];
}
#####
-# Usage: &dbSet($table, $primkey, $primval, $key, $val);
+# Usage: &dbSet($table, $primhash_ref, $hash_ref);
sub dbSet {
- my ($table, $primkey, $primval, $key, $val) = @_;
- my $query;
+ my ($table, $phref, $href) = @_;
+ my $where = join(' AND ', map {
+ $_."=".&dbQuote($phref->{$_})
+ } keys %{$phref}
+ );
+
+ my $result = &dbGet($table, join(',', keys %{$phref}), $where);
+
+ my(@keys,@vals);
+ foreach (keys %{$href}) {
+ push(@keys, $_);
+ push(@vals, &dbQuote($href->{$_}) );
+ }
+
+ if (!@keys or !@vals) {
+ &WARN("dbset: keys or vals is NULL.");
+ return;
+ }
- my $result = &dbGet($table,$primkey,$primval,$primkey);
+ my $query;
if (defined $result) {
- $query = "UPDATE $table SET $key=".&dbQuote($val).
- " WHERE $primkey=".&dbQuote($primval);
+ my @keyval;
+ for(my$i=0; $i<scalar @keys; $i++) {
+ push(@keyval, $keys[$i]."=".$vals[$i] );
+ }
+
+ $query = "UPDATE $table SET ".
+ join(' AND ', @keyval).
+ " WHERE ".$where;
} else {
- $query = "INSERT INTO $table ($primkey,$key) VALUES (".
- &dbQuote($primval).",".&dbQuote($val).")";
+ foreach (keys %{$phref}) {
+ push(@keys, $_);
+ push(@vals, &dbQuote($phref->{$_}) );
+ }
+
+ $query = sprintf("INSERT INTO $table (%s) VALUES (%s)",
+ join(',',@keys), join(',',@vals) );
}
&dbRaw("Set", $query);
}
#####
-# Usage: &dbReplace($table, $primkey, $primval, %hash);
+# Usage: &dbReplace($table, %hash);
sub dbReplace {
- my ($table, $primkey, $primval, %hash) = @_;
+ my ($table, %hash) = @_;
my (@keys, @vals);
foreach (keys %hash) {
- push(@keys, $_);
- push(@vals, &dbQuote($hash{$_}));
+ if (s/^-//) { # as is.
+ push(@keys, $_);
+ push(@vals, $hash{'-'.$_});
+ } else {
+ push(@keys, $_);
+ push(@vals, &dbQuote($hash{$_}));
+ }
+ }
+
+ if (0) {
+ &DEBUG("REPLACE INTO $table (".join(',',@keys).
+ ") VALUES (". join(',',@vals). ")" );
}
&dbRaw("Replace($table)", "REPLACE INTO $table (".join(',',@keys).
return 0;
}
+# &DEBUG("query => '$query'.");
+
&SQLDebug($query);
if (!$sth->execute) {
&ERROR("Raw($prefix): => '$query'");
-# &ERROR("Raw($prefix): $DBI::errstr");
+ # $DBI::errstr is printed as warning automatically.
$sth->finish;
return 0;
}
#####
# Usage: &getFactInfo($faqtoid, type);
sub getFactInfo {
- return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
+ return &dbGet("factoids", $_[1], "factoid_key='$_[0]'");
}
#####
&DEBUG("found!!!");
open(IN, $file);
- $data = <IN>;
+ while (<IN>) {
+ chop;
+ $data .= $_;
+ }
$found++;
last;
&dbRaw("create(db $param{'DBName'})", $query);
}
- foreach ("factoids", "freshmeat", "karma", "rootwarn", "seen",
+ foreach ("factoids", "freshmeat", "rootwarn", "seen", "stats",
) {
next if (exists $db{$_});
&status(" creating new table $_...");