2 # 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)
14 use vars qw(%factoids %param);
17 # FIXME we don't handle multiply indexes tables
18 # perhaps we should combine the keys with a ':' or something?
19 # the spaces below separate the keys from the rest
21 # Tim Riker thinks that freshmeat below should be a single index
81 foreach my $table (keys %formats) {
82 next unless (&IsParam($table));
84 my $file = "$param{'DBName'}-$table";
86 if (dbmopen(%{"$table"}, $file, 0666)) {
87 &status("Opened DBM $table ($file).");
89 &ERROR("Failed open to DBM $table ($file).");
97 foreach my $table (keys %formats) {
98 next unless (&IsParam($table));
100 if (dbmclose(%{ $table })) {
101 &status("Closed DBM $table successfully.");
104 &ERROR("Failed closing DBM $table.");
109 # Usage: &dbGetColInfo($table);
113 if (scalar @{$formats{$table}}) {
114 return @{$formats{$table}};
116 &ERROR("dbGCI: no format for table ($table).");
123 # Usage: &dbQuote($str);
129 # Usage: &dbGet($table, $select, $where);
131 my ($table, $select, $where) = @_;
132 my ($key, $val) = split('=',$where) if $where =~ /=/;
136 &DEBUG("dbGet($table, $select, $where);");
139 my @format = &dbGetColInfo($table);
140 if (!scalar @format) {
144 if (!defined ${ "$table" }{lc $val}) { # dbm hash exception.
145 &DEBUG("dbGet: '$val' does not exist in $table.");
149 # return the whole row.
150 if ($select eq "*") {
151 @retval = split $;, ${"$table"}{lc $val};
152 unshift(@retval,$key);
156 &DEBUG("dbGet: select=>'$select'.");
157 my @array = split "$;", ${"$table"}{lc $val};
158 unshift(@array,$val);
159 for (0 .. $#format) {
160 my $str = $format[$_];
161 next unless (grep /^$str$/, split(/\,/, $select));
163 &DEBUG("dG: '$format[$_]'=>'$array[$_]'.");
164 push(@retval, $array[$_]);
167 if (scalar @retval > 1) {
169 } elsif (scalar @retval == 1) {
177 # Usage: &dbGetCol();
178 # Usage: &dbGetCol($table, $select, $where, [$type]);
180 my ($table, $select, $where, $type) = @_;
181 &FIXME("STUB: &dbGetCol($table, $select, $where, $type);");
185 # Usage: &dbGetColNiceHash($table, $select, $where);
186 sub dbGetColNiceHash {
187 my ($table, $select, $where) = @_;
188 &DEBUG("dbGetColNiceHash($table, $select, $where);");
189 my ($key, $val) = split('=',$where) if $where =~ /=/;
190 return unless ${$table}{lc $val};
192 $hash{lc $key} = $val;
193 my (@format) = &dbGetColInfo($table);
195 @hash{@format} = split $;, ${$table}{lc $val};
200 # Usage: &dbInsert($table, $primkey, %hash);
201 # Note: dbInsert should do dbQuote.
203 my ($table, $primkey, %hash) = @_;
205 &DEBUG("dbInsert($table, $primkey, ...)");
207 my $info = ${$table}{lc $primkey} || ''; # primkey or primval?
209 my @format = &dbGetColInfo($table);
210 if (!scalar @format) {
215 my @array = split $;, $info;
216 delete $hash{$format[0]};
217 for $i (1 .. $#format) {
218 my $col = $format[$i];
219 $array[$i - 1]=$hash{$col};
220 $array[$i - 1]='' unless $array[$i - 1];
222 &DEBUG("dbI: '$col'=>'$array[$i - 1]'");
225 if (scalar keys %hash) {
226 &ERROR("dbI: not added...");
227 foreach (keys %hash) {
228 &ERROR("dbI: '$_'=>'$hash{$_}'");
233 ${$table}{lc $primkey} = join $;, @array;
239 &FIXME("STUB: &dbUpdate(@_);=>somehow use dbInsert!");
243 # Usage: &dbSetRow($table, @values);
245 &FIXME("STUB: &dbSetRow(@_)");
249 # Usage: &dbDel($table, $primhash_ref);
250 # Note: dbDel does dbQuote
252 my ($table, $phref) = @_;
253 # FIXME does not really handle more than one key!
254 my $primval = join(':', values %{$phref});
256 if (!defined ${$table}{lc $primval}) {
257 &DEBUG("dbDel: lc $primval does not exist in $table.");
259 delete ${$table}{lc $primval};
266 # Usage: &dbReplace($table, $key, %hash);
267 # Note: dbReplace does optional dbQuote.
269 my ($table, $key, %hash) = @_;
270 &DEBUG("dbReplace($table, $key, %hash);");
272 &dbDel($table, {$key=>$hash{$key}});
273 &dbInsert($table, $hash{$key}, %hash);
278 # Usage: &dbSet($table, $primhash_ref, $hash_ref);
280 my ($table, $phref, $href) = @_;
282 my ($key) = keys %{$phref};
283 my $where = $key . "=" . $phref->{$key};
285 my %hash = &dbGetColNiceHash($table, "*", $where);
286 $hash{$key}=$phref->{$key};
287 foreach (keys %{$href}) {
288 &DEBUG("dbSet: setting $_=${$href}{$_}");
289 $hash{$_} = ${$href}{$_};
291 &dbReplace($table, $key, %hash);
296 &FIXME("STUB: &dbRaw(@_);");
300 &FIXME("STUB: &dbRawReturn(@_);");
305 ####################################################################
306 ##### Factoid related stuff...
310 return scalar keys %{$_[0]};
314 &FIXME("STUB: &getKeys(@_); -- REDUNDANT");
318 &DEBUG("STUB: &randKey(@_);");
319 my ($table, $select) = @_;
320 my @format = &dbGetColInfo($table);
321 if (!scalar @format) {
325 my $rand = int(rand(&countKeys($table) - 1));
326 my @keys = keys %{$table};
327 &dbGet($table, '$select', "$format[0]=$keys[$rand]");
331 # Usage: &deleteTable($table);
334 &FIXME("STUB: deleteTable($table)");
337 ##### $select is misleading???
338 # Usage: &searchTable($table, $returnkey, $primkey, $str);
340 my ($table, $primkey, $key, $str) = @_;
341 &FIXME("STUB: searchTable($table, $primkey, $key, $str)");
343 &DEBUG("searchTable($table, $primkey, $key, $str)");
345 if (!scalar &dbGetColInfo($table)) {
350 foreach (keys %{$table}) {
351 my $val = &dbGet($table, "NULL", $_, $key) || '';
352 next unless ($val =~ /\Q$str\E/);
356 &DEBUG("sT: ".scalar(@results) );
362 # Usage: &getFactInfo($faqtoid, $type);
364 my ($faqtoid, $type) = @_;
366 my @format = &dbGetColInfo("factoids");
367 if (!scalar @format) {
371 if (!defined $factoids{$faqtoid}) { # dbm hash exception.
375 if ($type eq "*") { # all.
376 return split /$;/, $factoids{$faqtoid};
380 if (!grep /^$type$/, @format) {
381 &ERROR("gFI: type '$type' not valid for factoids.");
385 my @array = split /$;/, $factoids{$faqtoid};
386 for (0 .. $#format) {
387 next unless ($type eq $format[$_]);
391 &ERROR("gFI: should never happen.");
395 # Usage: &getFactoid($faqtoid);
399 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
400 &WARN("getF: faqtoid == NULL.");
404 if (defined $factoids{$faqtoid}) { # dbm hash exception.
405 # we assume 1 unfortunately.
406 ### TODO: use &getFactInfo() instead?
407 my $retval = (split $;, $factoids{$faqtoid})[1];
409 if (defined $retval) {
410 &DEBUG("getF: returning '$retval' for '$faqtoid'.");
412 &DEBUG("getF: returning NULL for '$faqtoid'.");
421 # Usage: &delFactoid($faqtoid);
425 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
426 &WARN("delF: faqtoid == NULL.");
430 if (defined $factoids{$faqtoid}) { # dbm hash exception.
431 delete $factoids{$faqtoid};
432 &status("DELETED $faqtoid");
434 &WARN("delF: nothing to deleted? ($faqtoid)");
440 # nothing - DB_FIle will create them on openDB()