From 45c02798fea7575debbe3d8d2f9dafa2c3291e5f Mon Sep 17 00:00:00 2001 From: timriker Date: Wed, 20 Nov 2002 18:41:01 +0000 Subject: [PATCH] allow use as a module git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@634 c11ca15a-4712-0410-83d8-924469b57eb5 --- src/db_dbm.pl | 95 ++++++++++++++++++++++++------------------------ src/db_sqlite.pl | 82 ++++++++++++++++++++--------------------- 2 files changed, 88 insertions(+), 89 deletions(-) diff --git a/src/db_dbm.pl b/src/db_dbm.pl index 1242cea..7d97f5a 100644 --- a/src/db_dbm.pl +++ b/src/db_dbm.pl @@ -6,9 +6,9 @@ # FModified: 19991020 # -package main; +#package main; -if (&IsParam('useStrict')) { use strict;} +if (&::IsParam('useStrict')) { use strict;} use vars qw(%factoids %freshmeat %seen %rootwarn); # db hash. @@ -60,17 +60,18 @@ use vars qw(%factoids %freshmeat %seen %rootwarn); # db hash. ); sub openDB { + my ($dbname) = @_; use DB_File; foreach (keys %formats) { - next unless (&IsParam($_)); + next unless (&::IsParam($_)); - my $file = "$param{'DBName'}-$_"; + my $file = "$dbname-$_"; if (dbmopen(%{ $_ }, $file, 0666)) { - &status("Opened DBM $_ ($file)."); + &::status("Opened DBM $_ ($file)."); } else { - &ERROR("Failed open to DBM $_ ($file)."); - &shutdown(); + &::ERROR("Failed open to DBM $_ ($file)."); + &::shutdown(); exit 1; } } @@ -78,13 +79,13 @@ use vars qw(%factoids %freshmeat %seen %rootwarn); # db hash. sub closeDB { foreach (keys %formats) { - next unless (&IsParam($_)); + next unless (&::IsParam($_)); if (dbmclose(%{ $_ })) { - &status("Closed DBM $_ successfully."); + &::status("Closed DBM $_ successfully."); next; } - &ERROR("Failed closing DBM $_."); + &::ERROR("Failed closing DBM $_."); } } @@ -96,7 +97,7 @@ use vars qw(%factoids %freshmeat %seen %rootwarn); # db hash. if (scalar @{$formats{$table}}) { return @{$formats{$table}}; } else { - &ERROR("dbGCI: no format for table ($table)."); + &::ERROR("dbGCI: no format for table ($table)."); return; } } @@ -116,7 +117,7 @@ sub dbGet { my $found = 0; my @retval; my $i; - &DEBUG("dbGet($table, $select, $where);"); + &::DEBUG("dbGet($table, $select, $where);"); return unless $key; my @format = &dbGetColInfo($table); @@ -125,7 +126,7 @@ sub dbGet { } if (!defined ${ "$table" }{lc $val}) { # dbm hash exception. - &DEBUG("dbGet: '$val' does not exist in $table."); + &::DEBUG("dbGet: '$val' does not exist in $table."); return; } @@ -136,14 +137,14 @@ sub dbGet { return(@retval); } - &DEBUG("dbGet: select=>'$select'."); + &::DEBUG("dbGet: select=>'$select'."); my @array = split "$;", ${"$table"}{lc $val}; unshift(@array,$val); for (0 .. $#format) { my $str = $format[$_]; next unless (grep /^$str$/, split(/\,/, $select)); $array[$_] ||= ''; - &DEBUG("dG: '$format[$_]'=>'$array[$_]'."); + &::DEBUG("dG: '$format[$_]'=>'$array[$_]'."); push(@retval, $array[$_]); } @@ -161,14 +162,14 @@ sub dbGet { # Usage: &dbGetCol($table, $select, $where, [$type]); sub dbGetCol { my ($table, $select, $where, $type) = @_; - &FIXME("STUB: &dbGetCol($table, $select, $where, $type);"); + &::FIXME("STUB: &dbGetCol($table, $select, $where, $type);"); } ##### # Usage: &dbGetColNiceHash($table, $select, $where); sub dbGetColNiceHash { my ($table, $select, $where) = @_; - &DEBUG("dbGetColNiceHash($table, $select, $where);"); + &::DEBUG("dbGetColNiceHash($table, $select, $where);"); my ($key, $val) = split('=',$where) if $where =~ /=/; return unless ${$table}{lc $val}; my (%hash) = (); @@ -185,7 +186,7 @@ sub dbGetColNiceHash { sub dbInsert { my ($table, $primkey, %hash) = @_; my $found = 0; - &DEBUG("dbInsert($table, $primkey, ...)"); + &::DEBUG("dbInsert($table, $primkey, ...)"); my $info = ${$table}{lc $primkey} || ''; # primkey or primval? @@ -202,13 +203,13 @@ sub dbInsert { $array[$i - 1]=$hash{$col}; $array[$i - 1]='' unless $array[$i - 1]; delete $hash{$col}; - &DEBUG("dbI: '$col'=>'$array[$i - 1]'"); + &::DEBUG("dbI: '$col'=>'$array[$i - 1]'"); } if (scalar keys %hash) { - &ERROR("dbI: not added..."); + &::ERROR("dbI: not added..."); foreach (keys %hash) { - &ERROR("dbI: '$_'=>'$hash{$_}'"); + &::ERROR("dbI: '$_'=>'$hash{$_}'"); } return 0; } @@ -219,14 +220,14 @@ sub dbInsert { } sub dbUpdate { - &FIXME("STUB: &dbUpdate(@_);=>somehow use dbInsert!"); + &::FIXME("STUB: &dbUpdate(@_);=>somehow use dbInsert!"); } ##### # Usage: &dbSetRow($table, @values); sub dbSetRow { my ($table, @values) = @_; - &DEBUG("dbSetRow(@_);"); + &::DEBUG("dbSetRow(@_);"); my $key = lc $values[0]; my @format = &dbGetColInfo($table); @@ -235,16 +236,16 @@ sub dbSetRow { } if (defined ${$table}{$key}) { - &WARN("dbSetRow: $table {$key} already exists?"); + &::WARN("dbSetRow: $table {$key} already exists?"); } if (scalar @values != scalar @format) { - &WARN("dbSetRow: scalar values != scalar ${table} format."); + &::WARN("dbSetRow: scalar values != scalar ${table} format."); } for (0 .. $#format) { if (defined $array[$_] and $array[$_] ne "") { - &DEBUG("dbSetRow: array[$_] != NULL($array[$_])."); + &::DEBUG("dbSetRow: array[$_] != NULL($array[$_])."); } $array[$_] = $values[$_]; } @@ -256,10 +257,10 @@ sub dbSetRow { # Usage: &dbDel($table, $primkey, $primval, [$key]); sub dbDel { my ($table, $primkey, $primval, $key) = @_; - &DEBUG("dbDel($table, $primkey, $primval);"); + &::DEBUG("dbDel($table, $primkey, $primval);"); if (!defined ${$table}{lc $primval}) { - &DEBUG("dbDel: lc $primval does not exist in $table."); + &::DEBUG("dbDel: lc $primval does not exist in $table."); } else { delete ${$table}{lc $primval}; } @@ -272,7 +273,7 @@ sub dbDel { # Note: dbReplace does optional dbQuote. sub dbReplace { my ($table, $key, %hash) = @_; - &DEBUG("dbReplace($table, $key, %hash);"); + &::DEBUG("dbReplace($table, $key, %hash);"); &dbDel($table, $key, $hash{$key}, %hash); &dbInsert($table, $hash{$key}, %hash); @@ -283,14 +284,14 @@ sub dbReplace { # Usage: &dbSet($table, $primhash_ref, $hash_ref); sub dbSet { my ($table, $phref, $href) = @_; - &DEBUG("dbSet(@_)"); + &::DEBUG("dbSet(@_)"); my ($key) = keys %{$phref}; my $where = $key . "=" . $phref->{$key}; my %hash = &dbGetColNiceHash($table, "*", $where); $hash{$key}=$phref->{$key}; foreach (keys %{$href}) { - &DEBUG("dbSet: setting $_=${$href}{$_}"); + &::DEBUG("dbSet: setting $_=${$href}{$_}"); $hash{$_} = ${$href}{$_}; } &dbReplace($table, $key, %hash); @@ -298,11 +299,11 @@ sub dbSet { } sub dbRaw { - &FIXME("STUB: &dbRaw(@_);"); + &::FIXME("STUB: &dbRaw(@_);"); } sub dbRawReturn { - &FIXME("STUB: &dbRawReturn(@_);"); + &::FIXME("STUB: &dbRawReturn(@_);"); } @@ -316,11 +317,11 @@ sub countKeys { } sub getKeys { - &FIXME("STUB: &getKeys(@_); -- REDUNDANT"); + return keys %{$_[0]}; } sub randKey { - &DEBUG("STUB: &randKey(@_);"); + &::DEBUG("STUB: &randKey(@_);"); my ($table, $select) = @_; my @format = &dbGetColInfo($table); if (!scalar @format) { @@ -336,16 +337,16 @@ sub randKey { # Usage: &deleteTable($table); sub deleteTable { my ($table) = @_; - &FIXME("STUB: deleteTable($table)"); + &::FIXME("STUB: deleteTable($table)"); } ##### $select is misleading??? # Usage: &searchTable($table, $returnkey, $primkey, $str); sub searchTable { my ($table, $primkey, $key, $str) = @_; - &FIXME("STUB: searchTable($table, $primkey, $key, $str)"); + &::FIXME("STUB: searchTable($table, $primkey, $key, $str)"); return; - &DEBUG("searchTable($table, $primkey, $key, $str)"); + &::DEBUG("searchTable($table, $primkey, $key, $str)"); if (!scalar &dbGetColInfo($table)) { return; @@ -358,7 +359,7 @@ sub searchTable { push(@results, $_); } - &DEBUG("sT: ".scalar(@results) ); + &::DEBUG("sT: ".scalar(@results) ); @results; } @@ -383,7 +384,7 @@ sub getFactInfo { # specific. if (!grep /^$type$/, @format) { - &ERROR("gFI: type '$type' not valid for factoids."); + &::ERROR("gFI: type '$type' not valid for factoids."); return; } @@ -393,7 +394,7 @@ sub getFactInfo { return $array[$_]; } - &ERROR("gFI: should never happen."); + &::ERROR("gFI: should never happen."); } ##### @@ -402,7 +403,7 @@ sub getFactoid { my ($faqtoid) = @_; if (!defined $faqtoid or $faqtoid =~ /^\s*$/) { - &WARN("getF: faqtoid == NULL."); + &::WARN("getF: faqtoid == NULL."); return; } @@ -412,9 +413,9 @@ sub getFactoid { my $retval = (split $;, $factoids{$faqtoid})[1]; if (defined $retval) { - &DEBUG("getF: returning '$retval' for '$faqtoid'."); + &::DEBUG("getF: returning '$retval' for '$faqtoid'."); } else { - &DEBUG("getF: returning NULL for '$faqtoid'."); + &::DEBUG("getF: returning NULL for '$faqtoid'."); } return $retval; } else { @@ -428,15 +429,15 @@ sub delFactoid { my ($faqtoid) = @_; if (!defined $faqtoid or $faqtoid =~ /^\s*$/) { - &WARN("delF: faqtoid == NULL."); + &::WARN("delF: faqtoid == NULL."); return; } if (defined $factoids{$faqtoid}) { # dbm hash exception. delete $factoids{$faqtoid}; - &status("DELETED $faqtoid"); + &::status("DELETED $faqtoid"); } else { - &WARN("delF: nothing to deleted? ($faqtoid)"); + &::WARN("delF: nothing to deleted? ($faqtoid)"); return; } } diff --git a/src/db_sqlite.pl b/src/db_sqlite.pl index eeaf32a..cf00865 100644 --- a/src/db_sqlite.pl +++ b/src/db_sqlite.pl @@ -5,10 +5,10 @@ # Created: 20021101 # -package main; +#package main; eval "use DBI"; -if (&IsParam("useStrict")) { use strict; } +if (&::IsParam("useStrict")) { use strict; } ##### # &openDB($dbname, $sqluser, $sqlpass, $nofail); @@ -18,10 +18,10 @@ sub openDB { $dbh = DBI->connect($dsn,$user,$pass); if ($dbh) { - &status("Opened SQLite connection $dsn"); + &::status("Opened SQLite connection $dsn"); } else { - &ERROR("cannot connect $dsn."); - &ERROR("since SQLite is not available, shutting down bot!"); + &::ERROR("cannot connect $dsn."); + &::ERROR("since SQLite is not available, shutting down bot!"); &closePID(); &closeSHM($shm); &closeLog(); @@ -38,7 +38,7 @@ sub closeDB { my $hoststr = ""; $hoststr = " to $param{'SQLHost'}" if (exists $param{'SQLHost'}); - &status("Closed SQLite connection$hoststr."); + &::status("Closed SQLite connection$hoststr."); $dbh->disconnect(); return 1; @@ -58,24 +58,24 @@ sub dbGet { $query .= " WHERE $where" if ($where); if (!defined $select or $select =~ /^\s*$/) { - &WARN("dbGet: select == NULL."); + &::WARN("dbGet: select == NULL."); return; } if (!defined $table or $table =~ /^\s*$/) { - &WARN("dbGet: table == NULL."); + &::WARN("dbGet: table == NULL."); return; } my $sth; if (!($sth = $dbh->prepare($query))) { - &ERROR("Get: prepare: $DBI::errstr"); + &::ERROR("Get: prepare: $DBI::errstr"); return; } - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &ERROR("Get: execute: '$query'"); + &::ERROR("Get: execute: '$query'"); $sth->finish; return 0; } @@ -102,9 +102,9 @@ sub dbGetCol { my %retval; my $sth = $dbh->prepare($query); - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &ERROR("GetCol: execute: '$query'"); + &::ERROR("GetCol: execute: '$query'"); $sth->finish; return; } @@ -148,10 +148,10 @@ sub dbGetColNiceHash { my %retval; my $sth = $dbh->prepare($query); - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &ERROR("GetColNiceHash: execute: '$query'"); -# &ERROR("GetCol => $DBI::errstr"); + &::ERROR("GetColNiceHash: execute: '$query'"); +# &::ERROR("GetCol => $DBI::errstr"); $sth->finish; return; } @@ -172,10 +172,10 @@ sub dbGetColInfo { my %retval; my $sth = $dbh->prepare($query); - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &ERROR("GRI => '$query'"); - &ERROR("GRI => $DBI::errstr"); + &::ERROR("GRI => '$query'"); + &::ERROR("GRI => $DBI::errstr"); $sth->finish; return; } @@ -200,17 +200,17 @@ sub dbSet { ); if (!defined $phref) { - &WARN("dbset: phref == NULL."); + &::WARN("dbset: phref == NULL."); return; } if (!defined $href) { - &WARN("dbset: href == NULL."); + &::WARN("dbset: href == NULL."); return; } if (!defined $table) { - &WARN("dbset: table == NULL."); + &::WARN("dbset: table == NULL."); return; } @@ -223,7 +223,7 @@ sub dbSet { } if (!@keys or !@vals) { - &WARN("dbset: keys or vals is NULL."); + &::WARN("dbset: keys or vals is NULL."); return; } @@ -233,10 +233,9 @@ sub dbSet { for(my$i=0; $iprepare($query))) { - &ERROR("Raw($prefix): $DBI::errstr"); + &::ERROR("Raw($prefix): $DBI::errstr"); return 0; } - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &ERROR("Raw($prefix): => '$query'"); + &::ERROR("Raw($prefix): => '$query'"); # $DBI::errstr is printed as warning automatically. $sth->finish; return 0; @@ -388,8 +386,8 @@ sub dbRawReturn { my @retval; my $sth = $dbh->prepare($query); - &SQLDebug($query); - &ERROR("RawReturn => '$query'.") unless $sth->execute; + &::SQLDebug($query); + &::ERROR("RawReturn => '$query'.") unless $sth->execute; while (my @row = $sth->fetchrow_array) { push(@retval, $row[0]); } @@ -427,8 +425,8 @@ sub randKey { my $query = "SELECT $select FROM $table LIMIT $rand,1"; my $sth = $dbh->prepare($query); - &SQLDebug($query); - &WARN("randKey($query)") unless $sth->execute; + &::SQLDebug($query); + &::WARN("randKey($query)") unless $sth->execute; my @retval = $sth->fetchrow_array; $sth->finish; @@ -467,9 +465,9 @@ sub searchTable { my $query = "SELECT $select FROM $table WHERE $key LIKE ". &dbQuote($str); my $sth = $dbh->prepare($query); - &SQLDebug($query); + &::SQLDebug($query); if (!$sth->execute) { - &WARN("Search($query)"); + &::WARN("Search($query)"); return; } @@ -522,7 +520,7 @@ sub checkTables { foreach ("factoids", "freshmeat", "rootwarn", "seen", "stats") { next if (exists $db{$_}); - &status("checkTables: creating $_..."); + &::status("checkTables: creating $_..."); &dbCreateTable($_); } -- 2.39.5