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);
65 foreach my $table (keys %formats) {
66 next unless (&IsParam($table));
68 my $file = "$param{'DBName'}-$table";
70 if (dbmopen(%{"$table"}, $file, 0666)) {
71 &status("Opened DBM $table ($file).");
73 &ERROR("Failed open to DBM $table ($file).");
81 foreach my $table (keys %formats) {
82 next unless (&IsParam($table));
84 if (dbmclose(%{ $table })) {
85 &status("Closed DBM $table successfully.");
88 &ERROR("Failed closing DBM $table.");
93 # Usage: &dbGetColInfo($table);
97 if (scalar @{$formats{$table}}) {
98 return @{$formats{$table}};
100 &ERROR("dbGCI: no format for table ($table).");
107 # Usage: &dbQuote($str);
113 # Usage: &dbGet($table, $select, $where);
115 my ($table, $select, $where) = @_;
116 my ($key, $val) = split('=',$where) if $where =~ /=/;
120 &DEBUG("dbGet($table, $select, $where);");
123 my @format = &dbGetColInfo($table);
124 if (!scalar @format) {
128 if (!defined ${ "$table" }{lc $val}) { # dbm hash exception.
129 &DEBUG("dbGet: '$val' does not exist in $table.");
133 # return the whole row.
134 if ($select eq "*") {
135 @retval = split $;, ${"$table"}{lc $val};
136 unshift(@retval,$key);
140 &DEBUG("dbGet: select=>'$select'.");
141 my @array = split "$;", ${"$table"}{lc $val};
142 unshift(@array,$val);
143 for (0 .. $#format) {
144 my $str = $format[$_];
145 next unless (grep /^$str$/, split(/\,/, $select));
147 &DEBUG("dG: '$format[$_]'=>'$array[$_]'.");
148 push(@retval, $array[$_]);
151 if (scalar @retval > 1) {
153 } elsif (scalar @retval == 1) {
161 # Usage: &dbGetCol();
162 # Usage: &dbGetCol($table, $select, $where, [$type]);
164 my ($table, $select, $where, $type) = @_;
165 &FIXME("STUB: &dbGetCol($table, $select, $where, $type);");
169 # Usage: &dbGetColNiceHash($table, $select, $where);
170 sub dbGetColNiceHash {
171 my ($table, $select, $where) = @_;
172 &DEBUG("dbGetColNiceHash($table, $select, $where);");
173 my ($key, $val) = split('=',$where) if $where =~ /=/;
174 return unless ${$table}{lc $val};
176 $hash{lc $key} = $val;
177 my (@format) = &dbGetColInfo($table);
179 @hash{@format} = split $;, ${$table}{lc $val};
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 my @format = &dbGetColInfo($table);
194 if (!scalar @format) {
199 my @array = split $;, $info;
200 delete $hash{$format[0]};
201 for $i (1 .. $#format) {
202 my $col = $format[$i];
203 $array[$i - 1]=$hash{$col};
204 $array[$i - 1]='' unless $array[$i - 1];
206 &DEBUG("dbI: '$col'=>'$array[$i - 1]'");
209 if (scalar keys %hash) {
210 &ERROR("dbI: not added...");
211 foreach (keys %hash) {
212 &ERROR("dbI: '$_'=>'$hash{$_}'");
217 ${$table}{lc $primkey} = join $;, @array;
223 &FIXME("STUB: &dbUpdate(@_);=>somehow use dbInsert!");
227 # Usage: &dbSetRow($table, @values);
229 &FIXME("STUB: &dbSetRow(@_)");
233 # Usage: &dbDel($table, $primhash_ref);
234 # Note: dbDel does dbQuote
236 my ($table, $phref) = @_;
237 # FIXME does not really handle more than one key!
238 my $primval = join(':', values %{$phref});
240 if (!defined ${$table}{lc $primval}) {
241 &DEBUG("dbDel: lc $primval does not exist in $table.");
243 delete ${$table}{lc $primval};
250 # Usage: &dbReplace($table, $key, %hash);
251 # Note: dbReplace does optional dbQuote.
253 my ($table, $key, %hash) = @_;
254 &DEBUG("dbReplace($table, $key, %hash);");
256 &dbDel($table, {$key=>$hash{$key}});
257 &dbInsert($table, $hash{$key}, %hash);
262 # Usage: &dbSet($table, $primhash_ref, $hash_ref);
264 my ($table, $phref, $href) = @_;
266 my ($key) = keys %{$phref};
267 my $where = $key . "=" . $phref->{$key};
269 my %hash = &dbGetColNiceHash($table, "*", $where);
270 $hash{$key}=$phref->{$key};
271 foreach (keys %{$href}) {
272 &DEBUG("dbSet: setting $_=${$href}{$_}");
273 $hash{$_} = ${$href}{$_};
275 &dbReplace($table, $key, %hash);
280 &FIXME("STUB: &dbRaw(@_);");
284 &FIXME("STUB: &dbRawReturn(@_);");
289 ####################################################################
290 ##### Factoid related stuff...
294 return scalar keys %{$_[0]};
298 &FIXME("STUB: &getKeys(@_); -- REDUNDANT");
302 &DEBUG("STUB: &randKey(@_);");
303 my ($table, $select) = @_;
304 my @format = &dbGetColInfo($table);
305 if (!scalar @format) {
309 my $rand = int(rand(&countKeys($table) - 1));
310 my @keys = keys %{$table};
311 &dbGet($table, '$select', "$format[0]=$keys[$rand]");
315 # Usage: &deleteTable($table);
318 &FIXME("STUB: deleteTable($table)");
321 ##### $select is misleading???
322 # Usage: &searchTable($table, $returnkey, $primkey, $str);
324 my ($table, $primkey, $key, $str) = @_;
325 &FIXME("STUB: searchTable($table, $primkey, $key, $str)");
327 &DEBUG("searchTable($table, $primkey, $key, $str)");
329 if (!scalar &dbGetColInfo($table)) {
334 foreach (keys %{$table}) {
335 my $val = &dbGet($table, "NULL", $_, $key) || '';
336 next unless ($val =~ /\Q$str\E/);
340 &DEBUG("sT: ".scalar(@results) );
346 # Usage: &getFactInfo($faqtoid, $type);
348 my ($faqtoid, $type) = @_;
350 my @format = &dbGetColInfo("factoids");
351 if (!scalar @format) {
355 if (!defined $factoids{$faqtoid}) { # dbm hash exception.
359 if ($type eq "*") { # all.
360 return split /$;/, $factoids{$faqtoid};
364 if (!grep /^$type$/, @format) {
365 &ERROR("gFI: type '$type' not valid for factoids.");
369 my @array = split /$;/, $factoids{$faqtoid};
370 for (0 .. $#format) {
371 next unless ($type eq $format[$_]);
375 &ERROR("gFI: should never happen.");
379 # Usage: &getFactoid($faqtoid);
383 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
384 &WARN("getF: faqtoid == NULL.");
388 if (defined $factoids{$faqtoid}) { # dbm hash exception.
389 # we assume 1 unfortunately.
390 ### TODO: use &getFactInfo() instead?
391 my $retval = (split $;, $factoids{$faqtoid})[1];
393 if (defined $retval) {
394 &DEBUG("getF: returning '$retval' for '$faqtoid'.");
396 &DEBUG("getF: returning NULL for '$faqtoid'.");
405 # Usage: &delFactoid($faqtoid);
409 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
410 &WARN("delF: faqtoid == NULL.");
414 if (defined $factoids{$faqtoid}) { # dbm hash exception.
415 delete $factoids{$faqtoid};
416 &status("DELETED $faqtoid");
418 &WARN("delF: nothing to deleted? ($faqtoid)");
424 # nothing - DB_FIle will create them on openDB()