From 4af71a5a1add30c9150be6bb49b7aafabf17e63a Mon Sep 17 00:00:00 2001 From: dms Date: Fri, 25 May 2001 12:37:34 +0000 Subject: [PATCH] - modified db_mysql to allow eleet usage of dbSet - karma now works! - if there's an attempt to use dbm, warn and prevent usage. git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@496 c11ca15a-4712-0410-83d8-924469b57eb5 --- src/Factoids/DBCommon.pl | 7 ++++-- src/Modules/Freshmeat.pl | 7 ++++-- src/Modules/RootWarn.pl | 15 ++++++++---- src/Process.pl | 6 +++-- src/db_mysql.pl | 52 ++++++++++++++++++++++++++++++---------- src/modules.pl | 6 ++++- 6 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/Factoids/DBCommon.pl b/src/Factoids/DBCommon.pl index fd98ece..3ae3694 100644 --- a/src/Factoids/DBCommon.pl +++ b/src/Factoids/DBCommon.pl @@ -8,9 +8,12 @@ if (&IsParam("useStrict")) { use strict; } ##### -# Usage: &setFactInfo($faqtoid, $type, $what, ?, ?); +# Usage: &setFactInfo($faqtoid, $type, $primval, $key, $val); sub setFactInfo { - &dbSet("factoids", "factoid_key", $_[0], $_[1], $_[2]); + &dbSet("factoids", + { factoid_key => $_[0] }, + { $_[1] => $_[2] } + ); } ##### diff --git a/src/Modules/Freshmeat.pl b/src/Modules/Freshmeat.pl index 62f1952..40c36fa 100644 --- a/src/Modules/Freshmeat.pl +++ b/src/Modules/Freshmeat.pl @@ -183,7 +183,10 @@ sub downloadIndex { ### lets get on with business. # set the last refresh time. fixes multiple spawn bug. - &::dbSet("freshmeat", "projectname_short", "_", "latest_version", time()); + &::dbSet("freshmeat", + { "projectname_short" => "_" }, + { "latest_version" => time() } + ); &::dbRaw("LOCK", "LOCK TABLES freshmeat WRITE"); @cols = &::dbGetColInfo("freshmeat"); @@ -235,7 +238,7 @@ sub downloadIndex { s/·//g; } - if ($str =~ s/\&(\S+);//g) { + if (0 and $str =~ s/\&(\S+?);//g) { &::DEBUG("fm: sarred $1 to ''."); } diff --git a/src/Modules/RootWarn.pl b/src/Modules/RootWarn.pl index 0cdc6a4..185130f 100644 --- a/src/Modules/RootWarn.pl +++ b/src/Modules/RootWarn.pl @@ -46,10 +46,17 @@ sub rootWarn { $attempt++; ### TODO: OPTIMIZE THIS. - &dbSet("rootwarn", "nick", lc($nick), "attempt", $attempt); - &dbSet("rootwarn", "nick", lc($nick), "time", time()); - &dbSet("rootwarn", "nick", lc($nick), "host", $user."\@".$host); - &dbSet("rootwarn", "nick", lc($nick), "channel", $chan); + if (1) { # old + &dbSet("rootwarn", { nick => lc($nick) }, { attempt => $attempt }); + &dbSet("rootwarn", { nick => lc($nick) }, { time => time() }); + &dbSet("rootwarn", { nick => lc($nick) }, { host => $user."\@".$host }); + &dbSet("rootwarn", { nick => lc($nick) }, { channel => $chan }); + } else { # new. replace. + &dbSet("rootwarn", "nick", lc($nick), "attempt", $attempt); + &dbSet("rootwarn", "nick", lc($nick), "time", time()); + &dbSet("rootwarn", "nick", lc($nick), "host", $user."\@".$host); + &dbSet("rootwarn", "nick", lc($nick), "channel", $chan); + } return; } diff --git a/src/Process.pl b/src/Process.pl index a685181..925ae96 100644 --- a/src/Process.pl +++ b/src/Process.pl @@ -299,8 +299,10 @@ sub process { $karma--; } - &dbSet("stats", "nick",$term, "type","karma"); -# &dbSet("stats", "nick",$term, "type","karma"); + &dbSet("stats", + { nick => $term, type => "karma" }, + { counter => $karma } + ); return; } diff --git a/src/db_mysql.pl b/src/db_mysql.pl index d5f7b43..38b1229 100644 --- a/src/db_mysql.pl +++ b/src/db_mysql.pl @@ -48,14 +48,13 @@ sub dbGet { 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; } @@ -83,8 +82,8 @@ sub dbGetCol { 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; } @@ -132,18 +131,45 @@ sub dbGetColInfo { } ##### -# 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->{$_}) ); + } - my $result = &dbGet($table, $primkey, "$primkey='$primval'"); + if (!@keys or !@vals) { + &WARN("dbset: keys or vals is NULL."); + return; + } + + my $query; if (defined $result) { - $query = "UPDATE $table SET $key=".&dbQuote($val). - " WHERE $primkey=".&dbQuote($primval); + my @keyval; + for(my$i=0; $i{$_}) ); + } + + $query = sprintf("INSERT INTO $table (%s) VALUES (%s)", + join(',',@keys), join(',',@vals) ); } &dbRaw("Set", $query); diff --git a/src/modules.pl b/src/modules.pl index 6aea9e0..162279d 100644 --- a/src/modules.pl +++ b/src/modules.pl @@ -110,7 +110,11 @@ sub loadDBModules { } elsif ($param{'DBType'} =~ /^dbm$/i) { &status(" using Berkeley DBM 1.85/2.0 support."); - require "$bot_src_dir/db_dbm.pl"; + &ERROR("dbm support is broken... you want it, you fix it!"); + &shutdown(); + exit 1; + +# require "$bot_src_dir/db_dbm.pl"; } else { &status("DB support DISABLED."); -- 2.39.2