| 1 | package DBstorage::RDB; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | DBstorage::RDB - dbedit driver for /rdb tables | ||
| 5 | |||
| 6 | DBstorage::RandRDB - dbedit driver for free version of rdb written by RANDD | ||
| 7 | |||
| 8 | =head1 DESCRIPTIONN | ||
| 9 | This is the RDB driver for dbedit | ||
| 10 | |||
| 11 | =head1 TODO | ||
| 12 | Should encapsulate file headers in objects | ||
| 13 | |||
| 14 | =head1 LICENSE | ||
| 15 | |||
| 16 | Copyright (C) 2002 Globewide Network Academy | ||
| 17 | Relased under the SCHEME license see LICENSE.SCHEME.txt for details | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use DBstorage; | ||
| 22 | use DBfilelock; | ||
| 23 | use Symbol; | ||
| 24 | use Carp; | ||
| 25 | |||
| 26 | @DBstorage::RDB::ISA = qw(DBstorage); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $proto = shift; | ||
| 30 | my $class = ref($proto) || $proto; | ||
| 31 | my $self = {}; | ||
| 32 | $self->{'FILEHANDLE'} = undef; | ||
| 33 | $self->{'FIELDS'} = []; | ||
| 34 | $self->{'TYPE'} = {}; | ||
| 35 | |||
| 36 | $self->{'CO'} = `which co`; | ||
| 37 | $self->{'CO'} =~ s/\n//g; | ||
| 38 | $self->{'CI'} = `which ci`; | ||
| 39 | $self->{'CI'} =~ s/\n//g; | ||
| 40 | |||
| 41 | $self->{'tabletolist'} = "tabletolist"; | ||
| 42 | $self->{'listtotable'} = "listtotable"; | ||
| 43 | $self->{'firstline'} = 0; | ||
| 44 | bless ($self, $class); | ||
| 45 | if (@_) {$self->open(@_)}; | ||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub find { | ||
| 50 | my $self = shift; | ||
| 51 | my $filename = shift; | ||
| 52 | my $keyref = shift; | ||
| 53 | my $foundref = shift; | ||
| 54 | my $lastref = shift; | ||
| 55 | my ($key, $value); | ||
| 56 | my ($record, %cookie); | ||
| 57 | local ($_); | ||
| 58 | local (*FILE); | ||
| 59 | |||
| 60 | if ($filename ne $cookie{'filename'}) { | ||
| 61 | %cookie = (); | ||
| 62 | } | ||
| 63 | |||
| 64 | ${$lastref} = 0; | ||
| 65 | $record = 0; | ||
| 66 | $self->open($filename, "FILE") || croak; | ||
| 67 | loop: | ||
| 68 | while ($self->read($foundref)) { | ||
| 69 | ${$lastref}++; | ||
| 70 | my ($restart) = keys %{$keyref}; | ||
| 71 | while (($key, $value) = each %{$keyref}) { | ||
| 72 | ($value ne $foundref->{$key}) && next loop; | ||
| 73 | } | ||
| 74 | |||
| 75 | $record = ${$lastref}; | ||
| 76 | last; | ||
| 77 | } | ||
| 78 | # if (defined($cookie{'nrecords'})) { | ||
| 79 | # ${$lastref} = $cookie{'nrecords'}; | ||
| 80 | # } else { | ||
| 81 | while (<FILE>) { | ||
| 82 | ($_ ne "\n") && ${$lastref}++; | ||
| 83 | } | ||
| 84 | # } | ||
| 85 | $self->close(); | ||
| 86 | # If we didn't find anything, erase the array | ||
| 87 | $record || (%{$foundref} = ()); | ||
| 88 | return $record; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | sub get_nth { | ||
| 93 | my $self = shift; | ||
| 94 | my $filename = shift; | ||
| 95 | my $record = shift; | ||
| 96 | my $foundref = shift; | ||
| 97 | my $lastref = shift; | ||
| 98 | my $last; | ||
| 99 | |||
| 100 | local ($_); | ||
| 101 | $self->open($filename, "FILE") || return 0; | ||
| 102 | $last = 1; | ||
| 103 | |||
| 104 | while ($last < $record) { | ||
| 105 | $_ = <FILE>; | ||
| 106 | ($_ ne "\n") && $last++; | ||
| 107 | } | ||
| 108 | |||
| 109 | $self->read($foundref) || ($record = 0); | ||
| 110 | |||
| 111 | while(<FILE>) { | ||
| 112 | ($_ ne "\n") && $last++; | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->close(); | ||
| 116 | ${$lastref} = $last; | ||
| 117 | return $record; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub attrib { | ||
| 121 | my $self = shift; | ||
| 122 | my $file = shift; | ||
| 123 | my (%type, @fields); | ||
| 124 | local(*TYPE); | ||
| 125 | open(TYPE, $file) || croak; | ||
| 126 | $_ = <TYPE>; | ||
| 127 | chop; | ||
| 128 | if ($_ =~ /^\s*$/) { | ||
| 129 | while(<TYPE>) { | ||
| 130 | chop; | ||
| 131 | /^\s*$/ && last; | ||
| 132 | /^([^\t]+)/ && push(@fields, $1); | ||
| 133 | } | ||
| 134 | if (@fields == 0) { | ||
| 135 | $type{"type"} = "none"; | ||
| 136 | } else { | ||
| 137 | $type{"type"} = "list"; | ||
| 138 | $type{'fields'} = \@fields; | ||
| 139 | } | ||
| 140 | return %type; | ||
| 141 | } else { | ||
| 142 | @fields = split(/\t/); | ||
| 143 | $type{'fields'} = \@fields; | ||
| 144 | $type{"type"} ="table"; | ||
| 145 | } | ||
| 146 | close(TYPE); | ||
| 147 | $self->{'TYPE'} = \%type; | ||
| 148 | |||
| 149 | return %type; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub delete { | ||
| 153 | my $self = shift; | ||
| 154 | my $filename = shift; | ||
| 155 | my $keyref = shift; | ||
| 156 | my $delete_all = shift; | ||
| 157 | |||
| 158 | my(%keys) = %{$keyref}; | ||
| 159 | my(@keys) = keys %keys; | ||
| 160 | my (%type, @fields, %f); | ||
| 161 | local ($_); | ||
| 162 | |||
| 163 | my $lock = DBfilelock->new("${filename}.lck"); | ||
| 164 | $self->open($filename, "INPUT_TABLE"); | ||
| 165 | if ($self->type() eq "table") { | ||
| 166 | open (OUTPUT_TABLE, ">/tmp/delete1.$$"); | ||
| 167 | } elsif ($self->type() eq "list") { | ||
| 168 | open (OUTPUT_TABLE, "| $self->{'tabletolist'} > /tmp/delete1.$$"); | ||
| 169 | } else { | ||
| 170 | print "Unknown table type $self->type()!!!!"; | ||
| 171 | exit; | ||
| 172 | } | ||
| 173 | @fields = @{$self->{'FIELDS'}}; | ||
| 174 | |||
| 175 | print OUTPUT_TABLE $self->table_header($self->{'FIELDS'}); | ||
| 176 | loop: | ||
| 177 | while ($self->read(\%f)) { | ||
| 178 | foreach (@keys) { | ||
| 179 | if ($keys{$_} ne $f{$_}) { | ||
| 180 | print OUTPUT_TABLE join("\t", @f{@fields}), "\n"; | ||
| 181 | next loop; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | $delete_all || last loop; | ||
| 185 | } | ||
| 186 | while(<INPUT_TABLE>) { | ||
| 187 | print OUTPUT_TABLE; | ||
| 188 | } | ||
| 189 | close(OUTPUT_TABLE); | ||
| 190 | |||
| 191 | $self->close(); | ||
| 192 | $self->commit($filename, "/tmp/delete1.$$"); | ||
| 193 | $lock->release(); | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | sub append { | ||
| 199 | my $self = shift; | ||
| 200 | my $table = shift; | ||
| 201 | my $hashref = shift; | ||
| 202 | |||
| 203 | my %inarray = %{$hashref}; | ||
| 204 | |||
| 205 | my (%type, @fields, $output); | ||
| 206 | |||
| 207 | foreach (keys %inarray) { | ||
| 208 | # remove forward and trailing space | ||
| 209 | $inarray{$_} =~ s/^\s+(.*)/$1/g; | ||
| 210 | $inarray{$_} =~ s/(.*)\s+$/$1/g; | ||
| 211 | $inarray{$_} =~ s/\s+/ /g; | ||
| 212 | } | ||
| 213 | |||
| 214 | my $lock = DBfilelock->new("${table}.lck"); | ||
| 215 | |||
| 216 | if (!-e $table || -z $table) { | ||
| 217 | open("FILE", ">$table") || croak "Cannot open"; | ||
| 218 | $_ = join("\t", grep(!/^RDB/, sort (keys %inarray))); | ||
| 219 | print FILE "$_\n"; | ||
| 220 | s/[^\t]/\-/g; | ||
| 221 | print FILE "$_\n"; | ||
| 222 | close FILE; | ||
| 223 | } | ||
| 224 | |||
| 225 | %type = $self->attrib($table); | ||
| 226 | @fields = @{$type{"fields"}}; | ||
| 227 | if ($type{"type"} eq "list") { | ||
| 228 | open (FILE, ">/tmp/file.$$"); | ||
| 229 | open (INPUT_FILE, $table); | ||
| 230 | while (<INPUT_FILE>) { | ||
| 231 | print FILE $_; | ||
| 232 | $output = $_; | ||
| 233 | } | ||
| 234 | close(INPUT_FILE); | ||
| 235 | if ($output !~ /^\s*$/) { | ||
| 236 | print FILE "\n"; | ||
| 237 | } | ||
| 238 | foreach (@fields) { | ||
| 239 | print FILE "$_\t$inarray{$_}\n"; | ||
| 240 | } | ||
| 241 | close (FILE); | ||
| 242 | } elsif($type{"type"} eq "table") { | ||
| 243 | `cp $table /tmp/file.$$`; | ||
| 244 | chmod 0664, "/tmp/file.$$"; | ||
| 245 | open (FILE ,">>/tmp/file.$$"); | ||
| 246 | print FILE join("\t", @inarray{@fields}) . "\n"; | ||
| 247 | close (FILE); | ||
| 248 | } else { | ||
| 249 | $lock->release(); | ||
| 250 | croak; | ||
| 251 | } | ||
| 252 | $self->commit($table, "/tmp/file.$$"); | ||
| 253 | $lock->release(); | ||
| 254 | %{$hashref} = %inarray; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub replace { | ||
| 258 | my $self = shift; | ||
| 259 | my $filename = shift; | ||
| 260 | my $keyref = shift; | ||
| 261 | my $replace_ref = shift; | ||
| 262 | my $replace_all = shift; | ||
| 263 | |||
| 264 | my %keys = %{$keyref}; | ||
| 265 | my %replace_values = %{$replace_ref}; | ||
| 266 | |||
| 267 | |||
| 268 | my (@keys) = keys %keys; | ||
| 269 | my (@replace_keys) = keys %replace_values; | ||
| 270 | my (%type, @fields, %f, $invalue, $input_line, %cookie); | ||
| 271 | local ($_); | ||
| 272 | my ($nrecords); | ||
| 273 | |||
| 274 | my ($lock) = DBfilelock->new("$filename.lck"); | ||
| 275 | |||
| 276 | $self->open($filename, "INPUT_TABLE"); | ||
| 277 | if ($self->type() eq "table") { | ||
| 278 | open (OUTPUT_TABLE, ">/tmp/replace1.$$"); | ||
| 279 | } elsif ($self->type() eq "list") { | ||
| 280 | open (OUTPUT_TABLE, "| $self->{'tabletolist'} > /tmp/replace1.$$"); | ||
| 281 | } else { | ||
| 282 | print "Unknown table type $type{'type'}!!!!"; | ||
| 283 | exit; | ||
| 284 | } | ||
| 285 | print OUTPUT_TABLE $self->table_header($self->{'FIELDS'}); | ||
| 286 | @fields = @{$self->{'FIELDS'}}; | ||
| 287 | loop: | ||
| 288 | |||
| 289 | # Break an abstraction barrier for speed | ||
| 290 | while ($input_line = <INPUT_TABLE>) { | ||
| 291 | chop $input_line; | ||
| 292 | if ($input_line ne "") { | ||
| 293 | @f{@fields} = split("\t", $input_line); | ||
| 294 | $nrecords++; | ||
| 295 | } else { | ||
| 296 | next loop; | ||
| 297 | } | ||
| 298 | |||
| 299 | foreach (@keys) { | ||
| 300 | if ($keys{$_} ne $f{$_}) { | ||
| 301 | print OUTPUT_TABLE $input_line, "\n"; | ||
| 302 | next loop; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | foreach (@replace_keys) { | ||
| 308 | $invalue = $replace_values{$_}; | ||
| 309 | $invalue =~ s/^\s+//; | ||
| 310 | $invalue =~ s/\s+$//; | ||
| 311 | $invalue =~ s/\s+/ /g; | ||
| 312 | $f{$_} = $invalue; | ||
| 313 | } | ||
| 314 | # if ($cookie{'seek'} == 0) { | ||
| 315 | # $cookie{'seek'} = tell(INPUT_TABLE) - length($input_line) - 1; | ||
| 316 | # if ($cookie{'seek'} < 0) { | ||
| 317 | # $cookie{'seek'} = 0; | ||
| 318 | # } | ||
| 319 | # $cookie{'record_num'} = $nrecords; | ||
| 320 | # } | ||
| 321 | print OUTPUT_TABLE join("\t", @f{@fields}), "\n"; | ||
| 322 | $replace_all || last loop; | ||
| 323 | } | ||
| 324 | while(<INPUT_TABLE>) { | ||
| 325 | if ($_ ne "\n") { | ||
| 326 | print OUTPUT_TABLE; | ||
| 327 | $nrecords++; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | $cookie{'nrecords'} = $nrecords; | ||
| 331 | |||
| 332 | close(OUTPUT_TABLE); | ||
| 333 | $self->close(); | ||
| 334 | $self->commit($filename, "/tmp/replace1.$$"); | ||
| 335 | $lock->release(); | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | sub replace_regexp { | ||
| 340 | my $self = shift; | ||
| 341 | my $filename = shift; | ||
| 342 | my $keyref = shift; | ||
| 343 | my $replace_ref = shift; | ||
| 344 | my $replace_all = shift; | ||
| 345 | |||
| 346 | my %keys = %{$keyref}; | ||
| 347 | my %replace_values = %{$replace_ref}; | ||
| 348 | |||
| 349 | |||
| 350 | my (@keys) = keys %keys; | ||
| 351 | my (@replace_keys) = keys %replace_values; | ||
| 352 | my (%type, @fields, %f, $invalue, $input_line); | ||
| 353 | local ($_); | ||
| 354 | my ($nrecords); | ||
| 355 | |||
| 356 | my(%cookie) = (); | ||
| 357 | |||
| 358 | my ($lock) = DBfilelock->new("$filename.lck"); | ||
| 359 | $self->open($filename, "INPUT_TABLE"); | ||
| 360 | if ($self->type() eq "table") { | ||
| 361 | open (OUTPUT_TABLE, ">/tmp/replace1.$$"); | ||
| 362 | } elsif ($self->type() eq "list") { | ||
| 363 | open (OUTPUT_TABLE, "| $self->{'tabletolist'} > /tmp/replace1.$$"); | ||
| 364 | } else { | ||
| 365 | print "Unknown table type $type{'type'}!!!!"; | ||
| 366 | exit; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | print OUTPUT_TABLE $self->table_header($self->{'FIELDS'}); | ||
| 371 | @fields = @{$self->{'FIELDS'}}; | ||
| 372 | loop: | ||
| 373 | # Break an abstraction barrier for speed | ||
| 374 | while ($input_line = <INPUT_TABLE>) { | ||
| 375 | chop $input_line; | ||
| 376 | if ($input_line ne "") { | ||
| 377 | @f{$self->fields()} = split("\t", $input_line); | ||
| 378 | $nrecords++; | ||
| 379 | } else { | ||
| 380 | next loop; | ||
| 381 | } | ||
| 382 | |||
| 383 | foreach (@keys) { | ||
| 384 | if ($f{$_} !~ $keys{$_}) { | ||
| 385 | print OUTPUT_TABLE $input_line, "\n"; | ||
| 386 | next loop; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | foreach (@replace_keys) { | ||
| 391 | $invalue = $replace_values{$_}; | ||
| 392 | $invalue =~ s/^\s+//; | ||
| 393 | $invalue =~ s/\s+$//; | ||
| 394 | $invalue =~ s/\s+/ /g; | ||
| 395 | if ($keys{$_} ne "") { | ||
| 396 | eval "\$f{$_} =~ s/$keys{$_}/$invalue/"; | ||
| 397 | } else { | ||
| 398 | $f{$_} = $invalue; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | print OUTPUT_TABLE join("\t", @f{@fields}), "\n"; | ||
| 402 | $replace_all || last loop; | ||
| 403 | } | ||
| 404 | while(<INPUT_TABLE>) { | ||
| 405 | if ($_ ne "\n") { | ||
| 406 | print OUTPUT_TABLE; | ||
| 407 | $nrecords++; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | $cookie{'nrecords'} = $nrecords; | ||
| 411 | |||
| 412 | close(OUTPUT_TABLE); | ||
| 413 | $self->close(); | ||
| 414 | $self->commit($filename, "/tmp/replace1.$$"); | ||
| 415 | $lock->release(); | ||
| 416 | } | ||
| 417 | |||
| 418 | sub commit { | ||
| 419 | my $self = shift; | ||
| 420 | my $filename = shift; | ||
| 421 | my $newfilename = shift; | ||
| 422 | my($has_rcs, $checkin, $checkout); | ||
| 423 | my(@list) = split(/\//, $filename); | ||
| 424 | my($file) = pop @list; | ||
| 425 | my($dir) = join("/", @list); | ||
| 426 | my($rcsfile) = "$dir/RCS/$file,v"; | ||
| 427 | |||
| 428 | if (-z $newfilename) { | ||
| 429 | print "$newfilename is a zero length file. Aborting...."; | ||
| 430 | croak; | ||
| 431 | } | ||
| 432 | |||
| 433 | $has_rcs = ((-e "$filename,v") || (-e $rcsfile)); | ||
| 434 | |||
| 435 | if (!-e $filename) { | ||
| 436 | `mv $newfilename $filename`; | ||
| 437 | } | ||
| 438 | |||
| 439 | if (!-w $filename && $has_rcs) { | ||
| 440 | $checkout = $self->checkout($filename, "-l"); | ||
| 441 | $checkin = $self->checkin($filename, | ||
| 442 | "-u", "Checked in by $ENV{'REMOTE_USER'}"); | ||
| 443 | `$checkout | ||
| 444 | cp $filename $filename.bak | ||
| 445 | mv $newfilename $filename | ||
| 446 | $checkin &`; | ||
| 447 | } elsif (-w $filename && $has_rcs) { | ||
| 448 | $checkin = $self->checkin($filename, | ||
| 449 | "-l", "Checked in by $ENV{'REMOTE_USER'}"); | ||
| 450 | `cp $filename $filename.bak | ||
| 451 | mv $newfilename $filename | ||
| 452 | $checkin &`; | ||
| 453 | } elsif (-w $filename && !$has_rcs) { | ||
| 454 | `cp $filename $filename.bak | ||
| 455 | mv $newfilename $filename`; | ||
| 456 | } else { | ||
| 457 | print "Error cannot write to file $filename"; | ||
| 458 | croak; | ||
| 459 | } | ||
| 460 | if (-e "${filename}.bak") { | ||
| 461 | chmod 0666, "${filename}.bak"; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | sub checkout { | ||
| 466 | my ($self, $filename, $options) = @_; | ||
| 467 | return "$self->{'CO'} $options $filename 2> /dev/null"; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub checkin { | ||
| 471 | my ($self, $filename, $options, $message) = @_; | ||
| 472 | return "echo '$message' | $self->{'CI'} $options $filename 2> /dev/null"; | ||
| 473 | } | ||
| 474 | |||
| 475 | sub create { | ||
| 476 | my ($self, $filename, $fieldref) = @_; | ||
| 477 | local (*FILE); | ||
| 478 | if (!-e $filename) { | ||
| 479 | open(FILE, ">$filename"); | ||
| 480 | print FILE $self->table_header($fieldref); | ||
| 481 | close(FILE); | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | sub table_header { | ||
| 486 | my($self, $fieldref) = @_; | ||
| 487 | if (!defined($fieldref)) { | ||
| 488 | $fieldref = $self->{'FIELDS'}; | ||
| 489 | } | ||
| 490 | my($header) = join("\t", @{$fieldref}); | ||
| 491 | my($return_value) = $header; | ||
| 492 | $return_value .= "\n"; | ||
| 493 | $header =~ s/\S/\-/g; | ||
| 494 | $return_value .= $header; | ||
| 495 | $return_value .= "\n"; | ||
| 496 | return $return_value; | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | sub open { | ||
| 501 | my $self = shift; | ||
| 502 | my $filename = shift; | ||
| 503 | my $filehandle = shift; | ||
| 504 | my $line; | ||
| 505 | |||
| 506 | if (!defined($filehandle)) { | ||
| 507 | $filehandle = gensym; | ||
| 508 | } | ||
| 509 | |||
| 510 | ($filehandle ne "STDIN") && | ||
| 511 | (open($filehandle, $filename) || return 0); | ||
| 512 | $line = readline(*$filehandle); | ||
| 513 | if ($line =~ /^\s*$/) { | ||
| 514 | close($filehandle); | ||
| 515 | ($filehandle ne "STDIN") && | ||
| 516 | (open($filehandle, "$self->{'listtotable'} < $filename |") || | ||
| 517 | return 0); | ||
| 518 | $line = <$filehandle>; | ||
| 519 | chop; | ||
| 520 | $self->{'TYPE'}->{"type"} = "list"; | ||
| 521 | } else { | ||
| 522 | $self->{'TYPE'}->{"type"} = "table"; | ||
| 523 | } | ||
| 524 | |||
| 525 | $self->{'FILEHANDLE'} = $filehandle; | ||
| 526 | chop $line; | ||
| 527 | my @fields = split("\t", $line); | ||
| 528 | $self->{'FIELDS'} = \@fields; | ||
| 529 | $self->{'firstline'} = 1; | ||
| 530 | return 1; | ||
| 531 | } | ||
| 532 | |||
| 533 | sub read { | ||
| 534 | my $self = shift; | ||
| 535 | my $hashref = shift; | ||
| 536 | my $line; | ||
| 537 | my $filehandle = $self->{'FILEHANDLE'}; | ||
| 538 | |||
| 539 | while ($line = readline(*$filehandle)) { | ||
| 540 | if ($self->{'firstline'}) { | ||
| 541 | $self->{'firstline'} = 0; | ||
| 542 | if ($line =~ /^\-/) { | ||
| 543 | next; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | chop $line; | ||
| 547 | if ($line ne "") { | ||
| 548 | @{$hashref}{@{$self->{'FIELDS'}}} = split("\t", $line); | ||
| 549 | return 1; | ||
| 550 | } | ||
| 551 | } | ||
| 552 | return 0; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub close { | ||
| 556 | my $self = shift; | ||
| 557 | if ($self->{'FILEHANDLE'} ne "STDIN") { | ||
| 558 | close $self->{'FILEHANDLE'}; | ||
| 559 | } | ||
| 560 | $self->{'FILEHANDLE'} = undef; | ||
| 561 | } | ||
| 562 | |||
| 563 | sub fields { | ||
| 564 | my $self = shift; | ||
| 565 | return @{$self->{'FIELDS'}}; | ||
| 566 | } | ||
| 567 | |||
| 568 | sub type { | ||
| 569 | my $self = shift; | ||
| 570 | return $self->{'TYPE'}->{"type"}; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub exists { | ||
| 574 | my $self = shift; | ||
| 575 | my $filename = shift; | ||
| 576 | return (-r $filename); | ||
| 577 | } | ||
| 578 | |||
| 579 | package DBstorage::RandRDB; | ||
| 580 | @DBstorage::RandRDB::ISA = qw(DBstorage::RDB DBstorage); | ||
| 581 | |||
| 582 | sub new { | ||
| 583 | my $proto = shift; | ||
| 584 | my $class = ref($proto) || $proto; | ||
| 585 | my $self = {}; | ||
| 586 | $self->{'FILEHANDLE'} = undef; | ||
| 587 | $self->{'FIELDS'} = []; | ||
| 588 | $self->{'TYPE'} = {}; | ||
| 589 | |||
| 590 | $self->{'tabletolist'} = "tbl2lst"; | ||
| 591 | $self->{'listtotable'} = "lst2tbl"; | ||
| 592 | bless ($self, $class); | ||
| 593 | if (@_) {$self->open(@_)}; | ||
| 594 | return $self; | ||
| 595 | } | ||
| 596 | |||
| 597 | 1; | ||
| 598 | |||
| 599 | |||
| 600 | |||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | |||
| 605 |