2 # db_dbm.pl: Extension on the factoid database.
3 # OrigAuthor: Kevin Lenzo (c) 1997
4 # CurrAuthor: dms <dms@users.sourceforge.net>
5 # Version: v0.6 (20000707)
11 if (&IsParam("useStrict")) { use strict; }
13 use vars qw(%factoids %freshmeat %seen %rootwarn); # db hash.
14 use vars qw(@factoids_format @rootwarn_format @seen_format);
49 @rootwarn_format = ("nick", "attempt", "time", "host", "channel");
62 my @dbm = ("factoids","freshmeat","rootwarn","seen");
67 next unless (&IsParam($_));
69 my $file = "$param{'DBName'}-$_";
71 if (dbmopen(%{ $_ }, $file, 0644)) {
72 &status("Opened DBM $_ ($file).");
74 &ERROR("Failed open to DBM $_ ($file).");
84 next unless (&IsParam($_));
86 if (dbmclose(%{ $_ })) {
87 &status("Closed DBM $_ successfully.");
90 &ERROR("Failed closing DBM $_.");
95 # Usage: &dbGet($table, $primkey, $primval, $select);
97 my ($db, $key, $val, $select) = @_;
101 &DEBUG("dbGet($db, $key, $val, $select);");
103 if (!scalar @{ "${db}_format" }) {
104 &ERROR("dG: no valid format layout for $db.");
108 if (!defined ${ "$db" }{lc $val}) { # dbm hash exception.
109 &DEBUG("dbGet: '$val' does not exist in $db.");
113 # return the whole row.
114 if ($select eq "*") {
115 return split $;, ${ "$db" }{lc $val};
117 &DEBUG("dbGet: select => '$select'.");
120 my @array = split "$;", ${ "$db" }{lc $val};
121 for (0 .. $#{ "${db}_format" }) {
122 my $str = ${ "${db}_format" }[$_];
123 next unless (grep /^$str$/, split(/\,/, $select));
126 &DEBUG("dG: pushing '$array[$_]'.");
127 push(@retval, $array[$_]);
130 if (scalar @retval > 1) {
132 } elsif (scalar @retval == 1) {
140 # Usage: &dbGetCol();
142 &DEBUG("STUB: &dbGetCol();");
146 # Usage: &dbGetRowInfo();
150 if (scalar @{ "${db}_format" }) {
151 return @{ "${db}_format" };
153 &ERROR("dbGCI: invalid format name ($db) [${db}_format].");
159 # Usage: &dbSet($db, $primkey, $primval, $key, $val);
161 my ($db, $primkey, $primval, $key, $val) = @_;
163 &DEBUG("dbSet($db, $primkey, $primval, $key, $val);");
165 my $info = ${$db}{lc $primval}; # case insensitive.
166 my @array = ($info) ? split(/$;/, $info) : ();
169 if (!defined ${$db}{lc $primval}) {
170 # we assume primary key as first one. bad!
171 $array[0] = $primval; # case sensitive.
174 for (0 .. $#{ "${db}_format" }) {
175 $array[$_] ||= ''; # from undefined to ''.
176 next unless (${ "${db}_format" }[$_] eq $key);
177 &DEBUG("dbSet: Setting array[$_]($key) to '$val'.");
184 &msg($who,"error: invalid element name \002$type\002.");
188 &DEBUG("setting $primval => '".join('|', @array)."'.");
189 ${$db}{lc $primval} = join $;, @array;
195 &FIXME("STUB: &dbUpdate(@_); => somehow use dbInsert!");
199 my ($db, $primkey, %hash) = @_;
202 my $info = ${$db}{lc $primkey} || ''; # primkey or primval?
204 if (!scalar @{ "${db}_format" }) {
205 &ERROR("dbI: array ${db}_format does not exist.");
210 my @array = split $;, $info;
211 for $i (0 .. $#{ "${db}_format" }) {
214 foreach (keys %hash) {
215 my $col = ${ "${db}_format" }[$i];
216 next unless ($col eq $_);
218 &DEBUG("dbI: setting $db->$primkey\{$col} => '$hash{$_}'.");
219 $array[$i] = $hash{$_};
224 if (scalar keys %hash) {
225 &ERROR("dbI: not added...");
226 foreach (keys %hash) {
227 &ERROR("dbI: '$_' => '$hash{$_}'");
232 ${$db}{lc $primkey} = join $;, @array;
238 # Usage: &dbSetRow($db, @values);
240 my ($db, @values) = @_;
241 my $key = lc $values[0];
243 if (!scalar @{ "${db}_format" }) {
244 &ERROR("dbSR: array ${db}_format does not exist.");
248 if (defined ${$db}{$key}) {
249 &WARN("dbSetRow: $db {$key} already exists?");
252 if (scalar @values != scalar @{ "${db}_format" }) {
253 &WARN("dbSetRow: scalar values != scalar ${db}_format.");
256 for (0 .. $#{ "${db}_format" }) {
257 if (defined $array[$_] and $array[$_] ne "") {
258 &DEBUG("dbSetRow: array[$_] != NULL($array[$_]).");
260 $array[$_] = $values[$_];
263 ${$db}{$key} = join $;, @array;
265 &DEBUG("STUB: &dbSetRow(@_);");
269 # Usage: &dbDel($db, NULL, $primval);
271 my ($db, $primkey, $primval) = @_;
273 if (!scalar @{ "${db}_format" }) {
274 &ERROR("dbD: array ${db}_format does not exist.");
278 if (!defined ${$db}{lc $primval}) {
279 &WARN("dbDel: lc $primval does not exist in $db.");
281 delete ${$db}{lc $primval};
288 &DEBUG("STUB: &dbRaw(@_);");
292 &DEBUG("STUB: &dbRawReturn(@_);");
297 ####################################################################
298 ##### Factoid related stuff...
302 return scalar keys %{$_[0]};
306 &DEBUG("STUB: &getKeys(@_); -- REDUNDANT");
310 &DEBUG("STUB: &randKey(@_);");
313 ##### $select is misleading???
314 # Usage: &searchTable($db, $returnkey, $primkey, $str);
316 my ($db, $primkey, $key, $str) = @_;
318 if (!scalar @{ "${db}_format" }) {
319 &ERROR("sT: no valid format layout for $db.");
324 foreach (keys %{$db}) {
325 my $val = &dbGet($db, "NULL", $_, $key) || '';
326 next unless ($val =~ /\Q$str\E/);
330 &DEBUG("sT: ".scalar(@results) );
336 # Usage: &getFactInfo($faqtoid, type);
338 my ($faqtoid, $type) = @_;
340 if (!defined $factoids{$faqtoid}) { # dbm hash exception.
344 if ($type eq "*") { # all.
345 return split /$;/, $factoids{$faqtoid};
349 if (!grep /^$type$/, @factoids_format) {
350 &ERROR("gFI: type '$type' not valid for factoids.");
354 my @array = split /$;/, $factoids{$faqtoid};
355 for (0 .. $#factoids_format) {
356 next unless ($type eq $factoids_format[$_]);
360 &ERROR("gFI: should never happen.");
364 # Usage: &getFactoid($faqtoid);
368 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
369 &WARN("getF: faqtoid == NULL.");
373 if (defined $factoids{$faqtoid}) { # dbm hash exception.
374 # we assume 1 unfortunately.
375 ### TODO: use &getFactInfo() instead?
376 my $retval = (split $;, $factoids{$faqtoid})[1];
378 if (defined $retval) {
379 &DEBUG("getF: returning '$retval' for '$faqtoid'.");
381 &DEBUG("getF: returning NULL for '$faqtoid'.");
390 # Usage: &delFactoid($faqtoid);
394 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
395 &WARN("delF: faqtoid == NULL.");
399 if (defined $factoids{$faqtoid}) { # dbm hash exception.
400 delete $factoids{$faqtoid};
401 &status("DELETED $faqtoid");
403 &WARN("delF: nothing to deleted? ($faqtoid)");