if (defined $result) {
my $author = &getFactInfo($from, "created_by");
- if (&IsFlag("m") or $author =~ /^\Q$who\E\!/i) {
+ if (0 and !&IsFlag("m") or $author !~ /^\Q$who\E\!/i) {
&msg($who, "It's not yours to modify.");
return;
}
&setFactInfo($lhs,"requested_time", time());
&setFactInfo($lhs,"requested_count", $count);
} else {
- &dbReplace("factoids", (
+ &dbReplace("factoids", "factoid_key", (
factoid_key => $lhs,
requested_by => $nuh,
requested_time => time(),
$date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
$reply =~ s/\$date/$date/gi;
$date =~ s/\w+\s+\w+\s+\d+\s+//;
+ # todo: support UTC.
$reply =~ s/\$time/$date/gi;
# dollar variables.
if ($reply =~ /\$rand/) {
my $rand = rand();
- my $randp = int($rand*100);
- $reply =~ s/\$randpercentage/$randp/g; # ???
- # randnick.
+
+ # $randnick.
if ($reply =~ /\$randnick/) {
my @nicks = keys %{ $channels{$chan}{''} };
my $randnick = $nicks[ int($rand*$#nicks) ];
- s/\$randnick/$randnick/;
+ $reply =~ s/\$randnick/$randnick/g;
}
+ # eg: $rand100.3
### TODO: number of digits. 'x.y'
- if ($reply =~ /\$rand(\d+)/) {
- # will this work as it does in C?
- $rand = sprintf("%*f", $1, $rand);
+ # too hard.
+ if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
+ my $max = $1;
+ my $dot = $3 || 0;
+ &status("dot => $dot, max => $max, rand=>$rand");
+ $rand = sprintf("%.*f", $dot, $rand*$max);
+ my $orig = $&;
+
+ &status("swapping $orig to $rand");
+ &status("reply => $reply");
+ $reply =~ s/$orig/$rand/eg;
+ &status("reply => $reply");
}
+
$reply =~ s/\$rand/$rand/g;
}
} else {
### BROKEN!!!
# I'd prefer to use dbReplace but it don't work.
- &dbReplace("factoids", (
+ &dbReplace("factoids", "factoid_key", (
factoid_key => $lhs,
created_by => time(),
factoid_value => $rhs,
$ircstats{'DisconnectTime'} = time();
$ircstats{'DisconnectReason'} = $what;
$ircstats{'DisconnectCount'}++;
- $ircstats{'TotalTime'} += time() - $ircstats{'ConnectTime'};
+ $ircstats{'TotalTime'} += time() - $ircstats{'ConnectTime'}
+ if ($ircstats{'ConnectTime'});
# clear any variables on reconnection.
$nickserv = 0;
);
- &dbReplace("stats", %hash);
+ &dbReplace("stats", "nick", %hash);
# does not work, atleast with old mysql!!! :(
# &dbReplace("stats", (nick => $who, type => $x, -counter => "counter+1") );
}
my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
my $delete = 0;
- if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
- my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
+ if ($param{'DBType'} =~ /^pgsql|mysql/i) {
+ my $query;
+
+ if ($param{'DBType'} =~ /^mysql$/i) {
+ $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
+ "UNIX_TIMESTAMP() - time > $max_time";
+ } else { # pgsql.
+ $query = "SELECT nick,time FROM seen WHERE ".
+ "extract(epoch from timestamp 'now') - time > $max_time";
+ }
+
my $sth = $dbh->prepare($query);
$sth->execute;
}
my $str = join(' ', &ChanConfList("chanlimitcheck") );
- &DEBUG("chanlimitCheck: str => $str");
foreach $chan ( &ChanConfList("chanlimitcheck") ) {
next unless (&validChan($chan));
&status("chanlimit: netsplit; removing it for $chan.");
&rawout("MODE $chan -l");
$cache{chanlimitChange}{$chan} = time();
+ &status("chanlimit: netsplit; removed.");
}
next;
$stats{'new'} = 0;
$stats{'old'} = 0;
- if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
+ if ($param{'DBType'} =~ /^(mysql|pgsql)$/i) {
foreach $nick (keys %seencache) {
- my $retval = &dbReplace("seen", (
+ my $retval = &dbReplace("seen", "nick", (
"nick" => $seencache{$nick}{'nick'},
"time" => $seencache{$nick}{'time'},
"host" => $seencache{$nick}{'host'},
### SQL SPECIFIC.
my ($to_days,$dayname,$monname);
- if ($param{'DBType'} =~ /pg|postgres|mysql/i) {
+ if ($param{'DBType'} =~ /^mysql$/i) {
$to_days = (&dbRawReturn("SELECT TO_DAYS(NOW()) - TO_DAYS('$sqldate')"))[0];
$dayname = (&dbRawReturn("SELECT DAYNAME('$sqldate')"))[0];
$monname = (&dbRawReturn("SELECT MONTHNAME('$sqldate')"))[0];
+
+ } elsif ($param{'DBType'} =~ /^pgsql$/i) {
+ $to_days = (&dbRawReturn("SELECT date_trunc('day',
+ 'now'::timestamp - '$sqldate')"))[0];
+ $dayname = qw(Sun Mon Tue Wed Thu Fri Sat)[(&dbRawReturn("SELECT extract(dow from timestamp '$sqldate')"))[0]];
+ $monname = qw(BAD Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[(&dbRawReturn("SELECT extract(month from timestamp '$sqldate')"))[0]];
+
} elsif ($param{'DBType'} =~ /^dbm$/i) {
&DEBUG("Countdown: FIXME!!!");
# $to_days =
# $dayname =
# $monname =
return 1;
+
} else {
&ERROR("Countdown: invalid DBType?");
return 1;
return;
}
- my $i = 0;
- my %factinfo;
- my @factinfo = &getFactInfo($faqtoid,"*");
- foreach ( &dbGetColInfo("factoids") ) {
- $factinfo{$_} = $factinfo[$i] || '';
- $i++;
- }
+ my %factinfo = &dbGetColNiceHash("factoids", "*", "factoid_key=".&dbQuote($faqtoid));
# factoid does not exist.
- if (scalar @factinfo <= 1) {
+ if (scalar (keys %factinfo) <= 1) {
&performReply("there's no such factoid as \002$faqtoid\002");
return;
}
# set the last refresh time. fixes multiple spawn bug.
&::dbSet("freshmeat",
{ "projectname_short" => "_" },
- { "latest_version" => time()
+ { "latest_version" => time(),
"desc_short" => "" }
);
$pkg{$text} = $string;
if ($expat->depth == 0) {
+
+ # old code.
+ if (0) {
for (my $j=0; $j<scalar @cols; $j++) {
$data[$j] = $pkg{ $cols[$j] };
}
&::dbSetRow("freshmeat", [@data], "DELAY");
undef @data;
+ }
+
+ # new code.
+ $i++;
+ my %data;
+ foreach(@cols) {
+ $data{$_} = $pkg{$_};
+ }
+ &::dbReplace("freshmeat", "projectname_short", %data);
+ undef %data;
+ # end of new code.
+
undef %pkg;
if ($i % 200 == 0 and $i != 0) {
}
}
- if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pg|postgres|dbm)/i) {
+ if (&IsParam("factoids") and $param{'DBType'} =~ /^(mysql|pgsql|dbm)/i) {
&FactoidStuff();
} elsif ($param{'DBType'} =~ /^none$/i) {
return "NO FACTOIDS.";
return;
}
- # todo: get column names, do $hash{$primkey}{blah} = ...
- while (my @row = $sth->fetchrow_array) {
- # reverse it to make it easier to count.
- }
+ %retval = %{ $sth->fetchrow_hashref() };
$sth->finish;
}
#####
-# Usage: &dbReplace($table, %hash);
+# Usage: &dbReplace($table, $key %hash);
# Note: dbReplace does optional dbQuote.
sub dbReplace {
- my ($table, %hash) = @_;
+ my ($table, $key, %hash) = @_;
my (@keys, @vals);
foreach (keys %hash) {
$dbh = DBI->connect($connectstr, $param{'SQLUser'}, $param{'SQLPass'});
if (!$dbh->err) {
- &status("Opened PgSQL connection to $param{'SQLHost'}");
+ &status("Opened pgSQL connection".
+ (exists $param{'SQLHost'} ? " to ".$param{'SQLHost'} : ""));
} else {
&ERROR("cannot connect to $param{'SQLHost'}.");
&ERROR("pgSQL: ".$dbh->errstr);
sub closeDB {
return 0 unless ($dbh);
- &status("Closed pgSQL connection to $param{'SQLHost'}.");
+ &status("Closed pgSQL connection.");
$dbh->disconnect();
return 1;
return;
}
- # todo: get column names, do $hash{$primkey}{blah} = ...
- while (my @row = $sth->fetchrow_array) {
- # todo: reverse it to make it easier to count.
- }
-
+ %retval = %{ $sth->fetchrow_hashref() };
$sth->finish;
return %retval;
sub dbGetColInfo {
my ($table) = @_;
-# my $query = "SELECT * FROM $table LIMIT 1;";
- my $query = "SHOW COLUMNS from $table";
+ my $query = "SELECT * FROM $table LIMIT 1;";
+# my $query = "SHOW COLUMNS from $table";
my %retval;
my $sth = $dbh->prepare($query);
return;
}
- if (0) {
- %retval=%{$sth->fetchrow_hashref()};
- return keys %retval;
- }
-
- my @cols;
- while (my @row = $sth->fetchrow_array) {
- push(@cols, $row[0]);
- }
+ %retval = %{ $sth->fetchrow_hashref() };
$sth->finish;
- return @cols;
+ return keys %retval;
}
#####
#####
# Usage: &dbInsert($table, $primkey, $primval, %hash);
sub dbInsert {
- my ($table, $primkey, $primval, %hash, $delay) = @_;
+ my ($table, $primkey, $primval, %hash) = @_;
my (@keys, @vals);
- my $p = "";
-
- if ($delay) {
- &DEBUG("dbI: delay => $delay");
- $p = " DELAYED";
- }
foreach (keys %hash) {
push(@keys, $_);
push(@vals, &dbQuote($hash{$_}));
}
- &dbRaw("Insert($table)", "INSERT $p INTO $table (".join(',',@keys).
+ &dbRaw("Insert($table)", "INSERT INTO $table (".join(',',@keys).
") VALUES (".join(',',@vals).")"
);
}
#####
-# Usage: &dbReplace($table, %hash);
+# Usage: &dbReplace($table, $key, %hash);
# Note: dbReplace does optional dbQuote.
sub dbReplace {
- my ($table, %hash) = @_;
+ my ($table, $key, %hash) = @_;
my (@keys, @vals);
- my $iquery = "INSERT INTO $table ";
- my $uquery = "UPDATE $table SET ";
+ my $where = "WHERE $key=".&dbQuote($hash{$key});
+ my $squery = "SELECT $key FROM $table $where;";
+ my $iquery = "INSERT INTO $table ";
+ my $uquery = "UPDATE $table SET ";
foreach (keys %hash) {
if (s/^-//) { # as is.
}
$uquery .= "$keys[-1] = $vals[-1], ";
}
- $uquery = ~s/, $/;/;
+ $uquery = ~s/, $/ $where;/;
$iquery .= "(". join(',',@keys) .") VALUES (". join(',',@vals) .");";
- &DEBUG($query) if (0);
+ &DEBUG($squery) if (0);
- if(!&dbRaw("Replace($table)", $iquery)) {
+ if(&dbRawReturn($squery)) {
&dbRaw("Replace($table)", $uquery);
- }
+ } else {
+ &dbRaw("Replace($table)", $iquery);
+ }
+
return 1;
}
-#####
+##### MADE REDUNDANT BY LEAR.
# Usage: &dbSetRow($table, $vref, $delay);
# Note: dbSetRow does dbQuote.
sub dbSetRow ($@$) {
sub randKey {
my ($table, $select) = @_;
my $rand = int(rand(&countKeys($table) - 1));
- my $query = "SELECT $select FROM $table LIMIT $rand,1";
+ my $query = "SELECT $select FROM $table LIMIT 1,$rand";
my $sth = $dbh->prepare($query);
&SQLDebug($query);