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);
45 @rootwarn_format = ("nick", "attempt", "time", "host", "channel");
64 my @dbm = ("factoids","freshmeat","rootwarn","seen","stats");
69 next unless (&IsParam($_));
71 my $file = "$param{'DBName'}-$_";
73 if (dbmopen(%{ $_ }, $file, 0666)) {
74 &status("Opened DBM $_ ($file).");
76 &ERROR("Failed open to DBM $_ ($file).");
86 next unless (&IsParam($_));
88 if (dbmclose(%{ $_ })) {
89 &status("Closed DBM $_ successfully.");
92 &ERROR("Failed closing DBM $_.");
97 # Usage: &dbQuote($str);
103 # Usage: &dbGet($table, $select, $where);
105 my ($table, $select, $where) = @_;
106 my ($key, $val) = split('=',$where) if $where =~ /=/;
110 &DEBUG("dbGet($table, $select, $where);");
111 # TODO: support change that's done for db_mysql!
114 if (!scalar @{ "${table}_format" }) {
115 &ERROR("dG: no valid format layout for $table.");
119 if (!defined ${ "$table" }{lc $val}) { # dbm hash exception.
120 &DEBUG("dbGet: '$val' does not exist in $table.");
124 # return the whole row.
125 if ($select eq "*") {
126 return split $;, ${ "$table" }{lc $val};
128 &DEBUG("dbGet: select => '$select'.");
131 my @array = split "$;", ${ "$table" }{lc $val};
132 for (0 .. $#{ "${table}_format" }) {
133 my $str = ${ "${table}_format" }[$_];
134 next unless (grep /^$str$/, split(/\,/, $select));
137 &DEBUG("dG: pushing '$array[$_]'.");
138 push(@retval, $array[$_]);
141 if (scalar @retval > 1) {
143 } elsif (scalar @retval == 1) {
151 # Usage: &dbGetCol();
152 # Usage: &dbGetCol($table, $select, $where, [$type]);
154 my ($table, $select, $where, $type) = @_;
155 &DEBUG("STUB: &dbGetCol($table, $select, $where, $type);");
159 # Usage: &dbGetColNiceHash($table, $select, $where);
160 sub dbGetColNiceHash {
161 my ($table, $select, $where) = @_;
162 &DEBUG("dbGetColNiceHash($table, $select, $where);");
163 my ($key, $val) = split('=',$where) if $where =~ /=/;
165 return unless ${$table}{lc $val};
166 @hash{@{"${table}_format"}} = split $;, ${$table}{lc $val};
171 # Usage: &dbGetColInfo();
175 if (scalar @{ "${table}_format" }) {
176 return @{ "${table}_format" };
178 &ERROR("dbGCI: invalid format name ($table) [${table}_format].");
184 # Usage: &dbInsert($table, $primkey, %hash);
185 # Note: dbInsert should do dbQuote.
187 my ($table, $primkey, %hash) = @_;
189 &DEBUG("dbInsert($table, $primkey, ...)");
191 my $info = ${$table}{lc $primkey} || ''; # primkey or primval?
193 if (!scalar @{ "${table}_format" }) {
194 &ERROR("dbI: array ${table}_format does not exist.");
199 my @array = split $;, $info;
201 delete $hash{${ "${table}_format" }[0]};
202 for $i (1 .. $#{ "${table}_format" }) {
203 my $col = ${ "${table}_format" }[$i];
204 $array[$i]=$hash{$col};
205 $array[$i]='' unless $array[$i];
207 &DEBUG("dbI: setting $table->$primkey\{$col\} => '$array[$i]'.");
210 # foreach (keys %hash) {
211 # my $col = ${ "${table}_format" }[$i];
212 # next unless ($col eq $_);
213 # next unless $hash{$_};
215 # &DEBUG("dbI: setting $table->$primkey\{$col\} => '$hash{$_}'.");
216 # $array[$i] = $hash{$_};
221 if (scalar keys %hash) {
222 &ERROR("dbI: not added...");
223 foreach (keys %hash) {
224 &ERROR("dbI: '$_' => '$hash{$_}'");
229 ${$table}{lc $primkey} = join $;, @array;
235 &FIXME("STUB: &dbUpdate(@_); => somehow use dbInsert!");
239 # Usage: &dbSetRow($table, @values);
241 my ($table, @values) = @_;
242 &DEBUG("dbSetRow(@_);");
243 my $key = lc $values[0];
245 if (!scalar @{ "${table}_format" }) {
246 &ERROR("dbSR: array ${table}_format does not exist.");
250 if (defined ${$table}{$key}) {
251 &WARN("dbSetRow: $table {$key} already exists?");
254 if (scalar @values != scalar @{ "${table}_format" }) {
255 &WARN("dbSetRow: scalar values != scalar ${table}_format.");
258 for (0 .. $#{ "${table}_format" }) {
259 if (defined $array[$_] and $array[$_] ne "") {
260 &DEBUG("dbSetRow: array[$_] != NULL($array[$_]).");
262 $array[$_] = $values[$_];
265 ${$table}{$key} = join $;, @array;
269 # Usage: &dbDel($table, $primkey, $primval, [$key]);
271 my ($table, $primkey, $primval, $key) = @_;
272 &DEBUG("dbDel($table, $primkey, $primval);");
274 if (!scalar @{ "${table}_format" }) {
275 &ERROR("dbD: array ${table}_format does not exist.");
279 if (!defined ${$table}{lc $primval}) {
280 &WARN("dbDel: lc $primval does not exist in $table.");
282 delete ${$table}{lc $primval};
289 # Usage: &dbReplace($table, $key, %hash);
290 # Note: dbReplace does optional dbQuote.
292 my ($table, $key, %hash) = @_;
293 &DEBUG("dbReplace($table, $key, %hash);");
295 &dbDel($table, $key, $hash{$key}, %hash);
296 &dbInsert($table, $hash{$key}, %hash);
301 # Usage: &dbSet($table, $primhash_ref, $hash_ref);
303 my ($table, $phref, $href) = @_;
305 my ($key) = keys %{$phref};
306 my $where = $key . "=" . $phref->{$key};
308 my %hash = &dbGetColNiceHash($table, "*", $where);
309 foreach (keys %{$href}) {
310 &DEBUG("dbSet: setting $_=${$href}{$_}");
311 $hash{$_} = ${$href}{$_};
313 &dbReplace($table, $key, %hash);
316 my $p = join(' AND ', map {
317 $_."=".&dbQuote($href->{$_})
320 &WARN("dbSet not implemented yet $where $p"); return 0;
322 # Usage: &dbSet($table, $primkey, $primval, $key, $val);
323 my ($table, $primkey, $primval, $key, $val) = @_;
325 &DEBUG("dbSet($table, $primkey, $primval, $key, $val);");
327 my $info = ${$table}{lc $primval}; # case insensitive.
328 my @array = ($info) ? split(/$;/, $info) : ();
331 if (!defined ${$table}{lc $primval}) {
332 # we assume primary key as first one. bad!
333 $array[0] = $primval; # case sensitive.
336 for (0 .. $#{ "${table}_format" }) {
337 $array[$_] ||= ''; # from undefined to ''.
338 next unless (${ "${table}_format" }[$_] eq $key);
339 &DEBUG("dbSet: Setting array[$_]($key) to '$val'.");
346 &msg($who,"error: invalid element name \002$type\002.");
350 &DEBUG("setting $primval => '".join('|', @array)."'.");
351 ${$table}{lc $primval} = join $;, @array;
357 &DEBUG("STUB: &dbRaw(@_);");
361 &DEBUG("STUB: &dbRawReturn(@_);");
366 ####################################################################
367 ##### Factoid related stuff...
371 return scalar keys %{$_[0]};
375 &DEBUG("STUB: &getKeys(@_); -- REDUNDANT");
379 &DEBUG("STUB: &randKey(@_);");
382 ##### $select is misleading???
383 # Usage: &searchTable($table, $returnkey, $primkey, $str);
386 my ($table, $primkey, $key, $str) = @_;
387 &DEBUG("searchTable($table, $primkey, $key, $str)");
389 if (!scalar @{ "${table}_format" }) {
390 &ERROR("sT: no valid format layout for $table.");
395 foreach (keys %{$table}) {
396 my $val = &dbGet($table, "NULL", $_, $key) || '';
397 next unless ($val =~ /\Q$str\E/);
401 &DEBUG("sT: ".scalar(@results) );
407 # Usage: &getFactInfo($faqtoid, type);
409 my ($faqtoid, $type) = @_;
411 if (!defined $factoids{$faqtoid}) { # dbm hash exception.
415 if ($type eq "*") { # all.
416 return split /$;/, $factoids{$faqtoid};
420 if (!grep /^$type$/, @factoids_format) {
421 &ERROR("gFI: type '$type' not valid for factoids.");
425 my @array = split /$;/, $factoids{$faqtoid};
426 for (0 .. $#factoids_format) {
427 next unless ($type eq $factoids_format[$_]);
431 &ERROR("gFI: should never happen.");
435 # Usage: &getFactoid($faqtoid);
439 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
440 &WARN("getF: faqtoid == NULL.");
444 if (defined $factoids{$faqtoid}) { # dbm hash exception.
445 # we assume 1 unfortunately.
446 ### TODO: use &getFactInfo() instead?
447 my $retval = (split $;, $factoids{$faqtoid})[1];
449 if (defined $retval) {
450 &DEBUG("getF: returning '$retval' for '$faqtoid'.");
452 &DEBUG("getF: returning NULL for '$faqtoid'.");
461 # Usage: &delFactoid($faqtoid);
465 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
466 &WARN("delF: faqtoid == NULL.");
470 if (defined $factoids{$faqtoid}) { # dbm hash exception.
471 delete $factoids{$faqtoid};
472 &status("DELETED $faqtoid");
474 &WARN("delF: nothing to deleted? ($faqtoid)");
482 # foreach ("factoids", "freshmeat", "rootwarn", "seen", "stats") {
484 # next if (exists $table{$_});
485 # &status(" creating new table $_...");
486 # &dbCreateTable($_);