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, $primkey, $primval, [$key]);
235 my ($table, $primkey, $primval, $key) = @_;
236 &DEBUG("dbDel($table, $primkey, $primval);");
238 if (!defined ${$table}{lc $primval}) {
239 &DEBUG("dbDel: lc $primval does not exist in $table.");
241 delete ${$table}{lc $primval};
248 # Usage: &dbReplace($table, $key, %hash);
249 # Note: dbReplace does optional dbQuote.
251 my ($table, $key, %hash) = @_;
252 &DEBUG("dbReplace($table, $key, %hash);");
254 &dbDel($table, $key, $hash{$key}, %hash);
255 &dbInsert($table, $hash{$key}, %hash);
260 # Usage: &dbSet($table, $primhash_ref, $hash_ref);
262 my ($table, $phref, $href) = @_;
264 my ($key) = keys %{$phref};
265 my $where = $key . "=" . $phref->{$key};
267 my %hash = &dbGetColNiceHash($table, "*", $where);
268 $hash{$key}=$phref->{$key};
269 foreach (keys %{$href}) {
270 &DEBUG("dbSet: setting $_=${$href}{$_}");
271 $hash{$_} = ${$href}{$_};
273 &dbReplace($table, $key, %hash);
278 &FIXME("STUB: &dbRaw(@_);");
282 &FIXME("STUB: &dbRawReturn(@_);");
287 ####################################################################
288 ##### Factoid related stuff...
292 return scalar keys %{$_[0]};
296 &FIXME("STUB: &getKeys(@_); -- REDUNDANT");
300 &DEBUG("STUB: &randKey(@_);");
301 my ($table, $select) = @_;
302 my @format = &dbGetColInfo($table);
303 if (!scalar @format) {
307 my $rand = int(rand(&countKeys($table) - 1));
308 my @keys = keys %{$table};
309 &dbGet($table, '$select', "$format[0]=$keys[$rand]");
313 # Usage: &deleteTable($table);
316 &FIXME("STUB: deleteTable($table)");
319 ##### $select is misleading???
320 # Usage: &searchTable($table, $returnkey, $primkey, $str);
322 my ($table, $primkey, $key, $str) = @_;
323 &FIXME("STUB: searchTable($table, $primkey, $key, $str)");
325 &DEBUG("searchTable($table, $primkey, $key, $str)");
327 if (!scalar &dbGetColInfo($table)) {
332 foreach (keys %{$table}) {
333 my $val = &dbGet($table, "NULL", $_, $key) || '';
334 next unless ($val =~ /\Q$str\E/);
338 &DEBUG("sT: ".scalar(@results) );
344 # Usage: &getFactInfo($faqtoid, $type);
346 my ($faqtoid, $type) = @_;
348 my @format = &dbGetColInfo("factoids");
349 if (!scalar @format) {
353 if (!defined $factoids{$faqtoid}) { # dbm hash exception.
357 if ($type eq "*") { # all.
358 return split /$;/, $factoids{$faqtoid};
362 if (!grep /^$type$/, @format) {
363 &ERROR("gFI: type '$type' not valid for factoids.");
367 my @array = split /$;/, $factoids{$faqtoid};
368 for (0 .. $#format) {
369 next unless ($type eq $format[$_]);
373 &ERROR("gFI: should never happen.");
377 # Usage: &getFactoid($faqtoid);
381 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
382 &WARN("getF: faqtoid == NULL.");
386 if (defined $factoids{$faqtoid}) { # dbm hash exception.
387 # we assume 1 unfortunately.
388 ### TODO: use &getFactInfo() instead?
389 my $retval = (split $;, $factoids{$faqtoid})[1];
391 if (defined $retval) {
392 &DEBUG("getF: returning '$retval' for '$faqtoid'.");
394 &DEBUG("getF: returning NULL for '$faqtoid'.");
403 # Usage: &delFactoid($faqtoid);
407 if (!defined $faqtoid or $faqtoid =~ /^\s*$/) {
408 &WARN("delF: faqtoid == NULL.");
412 if (defined $factoids{$faqtoid}) { # dbm hash exception.
413 delete $factoids{$faqtoid};
414 &status("DELETED $faqtoid");
416 &WARN("delF: nothing to deleted? ($faqtoid)");
422 # nothing - DB_FIle will create them on openDB()