From afc36f0ec5e35b9d450855666853cf710ddcc6c1 Mon Sep 17 00:00:00 2001 From: dms Date: Fri, 5 Oct 2001 17:46:25 +0000 Subject: [PATCH] - I borked DBCommon.pl by mistake (setFactInfo duplication). - added more variable checks to db_mysql.pl just in case they're undefined; removed main:: from functions. - nasty eval code in Question.pl simplied. thanks lear! - dbReplace arguments fixed up to match the 3 liner setFactInfo code. git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@520 c11ca15a-4712-0410-83d8-924469b57eb5 --- src/Factoids/DBCommon.pl | 14 ++++---------- src/Factoids/Question.pl | 20 ++++++-------------- src/Factoids/Statement.pl | 6 +++--- src/Factoids/Update.pl | 17 ++++++++++------- src/db_mysql.pl | 26 ++++++++++++++++++++++---- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/Factoids/DBCommon.pl b/src/Factoids/DBCommon.pl index eddd7fa..178847d 100644 --- a/src/Factoids/DBCommon.pl +++ b/src/Factoids/DBCommon.pl @@ -8,7 +8,7 @@ if (&IsParam("useStrict")) { use strict; } ##### -# Usage: &setFactInfo($faqtoid, $type, $primval, $key, $val); +# Usage: &setFactInfo($faqtoid, $key, $val); sub setFactInfo { &dbSet("factoids", { factoid_key => $_[0] }, @@ -18,25 +18,19 @@ sub setFactInfo { ##### # Usage: &getFactInfo($faqtoid, [$what]); -sub main::getFactInfo { +sub getFactInfo { return &dbGet("factoids", $_[1], "factoid_key=".&dbQuote($_[0]) ); } ##### # Usage: &getFactoid($faqtoid); -sub main::getFactoid { +sub getFactoid { return &getFactInfo($_[0], "factoid_value"); } -##### -# Usage: &setFactInfo($faqtoid, $type, $what); -sub main::setFactInfo { - &dbSet("factoids", "factoid_key", $_[0], $_[1], $_[2]); -} - ##### # Usage: &delFactoid($faqtoid); -sub main::delFactoid { +sub delFactoid { my ($faqtoid) = @_; &dbDel("factoids", "factoid_key",$faqtoid); diff --git a/src/Factoids/Question.pl b/src/Factoids/Question.pl index 93725e5..e05fe6a 100644 --- a/src/Factoids/Question.pl +++ b/src/Factoids/Question.pl @@ -184,26 +184,18 @@ sub factoidArgs { s/^CMD: //i; # &DEBUG("factarg: ''$str' =~ /^$_\$/'"); - my @vals; my $arg = $_; # todo: ~punish apt for (Eating) (Parentheses) - # how the hell do I fix the above? + # how the hell do I fix the above? -dms. - # todo: make eval work with $$i's :( - # fuck this is an ugly hack. it works though. + # eval (evil!) code. cleaned up courtesy of lear. + my @vals; eval { - if ($str =~ /^$arg$/i) { - for ($i=1; $i<=5; $i++) { - $val = $$i; - last unless (defined $val); - - push(@vals, $val); - } - } + @vals = ($str =~ /^$arg$/i); }; - if ($@) { # it failed!!! + if ($@) { &WARN("factargs: regex failed! '$str' =~ /^$_\$/"); next; } @@ -229,7 +221,7 @@ sub factoidArgs { # update stats. my $count = &getFactInfo($q, "requested_count") || 0; - $count++; + $count++; &setFactInfo($q, "requested_by", $nuh); &setFactInfo($q, "requested_time", time()); &setFactInfo($q, "requested_count", $count); diff --git a/src/Factoids/Statement.pl b/src/Factoids/Statement.pl index edd69cf..6af6dc3 100644 --- a/src/Factoids/Statement.pl +++ b/src/Factoids/Statement.pl @@ -56,10 +56,10 @@ sub doStatement { my($lhs, $mhs, $rhs) = ($`, $&, $'); # allows factoid arguments to be updated. -lear. - $lhs =~ s/^(CMD: )?(.*)/$1 . lc $2/e; +# $lhs =~ s/^(CMD: )?(.*)/$1 . lc $2/e; - $lhs =~ tr/A-Z/a-z/; - $lhs =~ s/^(the|da|an?)\s+//i; # discard article + # discard article. + $lhs =~ s/^(the|da|an?)\s+//i; # remove excessive initial and final whitespaces. $lhs =~ s/^\s+|\s+$//g; diff --git a/src/Factoids/Update.pl b/src/Factoids/Update.pl index 5df526a..3030bef 100644 --- a/src/Factoids/Update.pl +++ b/src/Factoids/Update.pl @@ -107,16 +107,15 @@ sub update { &performAddressedReply("okay"); - if (1) { # old - &setFactInfo($lhs,"factoid_value", $rhs); - &setFactInfo($lhs,"created_by", $nuh); - &setFactInfo($lhs,"created_time", time()); + if (0) { # old + &setFactInfo($lhs, "factoid_value", $rhs); + &setFactInfo($lhs, "created_by", $nuh); + &setFactInfo($lhs, "created_time", time()); } else { - ### BROKEN!!! - # I'd prefer to use dbReplace but it don't work. &dbReplace("factoids", "factoid_key", ( + created_by => $nuh, + created_time => time(), # modified time. factoid_key => $lhs, - created_by => time(), factoid_value => $rhs, ) ); } @@ -130,6 +129,9 @@ sub update { # factoid exists. if ($exists eq $rhs) { + # this catches the following situation: (right or wrong?) + # "test is test" + # "test is also test" &performAddressedReply("i already had it that way"); return 1; } @@ -221,6 +223,7 @@ sub update { $count{'Update'}++; &status("update: <$who> \'$lhs\' =$mhs=> \'$rhs\'; was \'$exists\'"); + # should dbReplace be used here? &delFactoid($lhs); &setFactInfo($lhs,"created_by", $nuh); &setFactInfo($lhs,"created_time", time()); diff --git a/src/db_mysql.pl b/src/db_mysql.pl index b482051..1b3f3dc 100644 --- a/src/db_mysql.pl +++ b/src/db_mysql.pl @@ -49,8 +49,13 @@ sub dbGet { my $query = "SELECT $select FROM $table"; $query .= " WHERE $where" if ($where); - if (!defined $select) { - &WARN("dbGet: select == NULL. table => $table"); + if (!defined $select or $select =~ /^\s*$/) { + &WARN("dbGet: select == NULL."); + return; + } + + if (!defined $table or $table =~ /^\s*$/) { + &WARN("dbGet: table == NULL."); return; } @@ -134,8 +139,6 @@ sub dbGetColNiceHash { $query .= " WHERE ".$where if ($where); my %retval; - &DEBUG("dbGetColNiceHash: query => '$query'."); - my $sth = $dbh->prepare($query); &SQLDebug($query); if (!$sth->execute) { @@ -188,6 +191,21 @@ sub dbSet { } keys %{$phref} ); + if (!defined $phref) { + &WARN("dbset: phref == NULL."); + return; + } + + if (!defined $href) { + &WARN("dbset: href == NULL."); + return; + } + + if (!defined $table) { + &WARN("dbset: table == NULL."); + return; + } + my $result = &dbGet($table, join(',', keys %{$phref}), $where); my(@keys,@vals); -- 2.39.2