| 1 | #!/usr/bin/perl -I.. | ||
| 2 | ## | ||
| 3 | ## Persistence::Database -- Persistent Database. | ||
| 4 | ## | ||
| 5 | ## $Date: 1999/08/02 22:01:18 $ | ||
| 6 | ## $Revision: 0.14 $ | ||
| 7 | ## $State: Exp $ | ||
| 8 | ## $Author: root $ | ||
| 9 | ## | ||
| 10 | ## Copyright (c) 1998, Vipul Ved Prakash. All rights reserved. | ||
| 11 | ## This code is free software; you can redistribute it and/or modify | ||
| 12 | ## it under the same terms as Perl itself. | ||
| 13 | |||
| 14 | package Persistence::Database; | ||
| 15 | use Persistence::Object::Simple; | ||
| 16 | use Data::Dumper; | ||
| 17 | use Carp; | ||
| 18 | |||
| 19 | @ISA = qw( Persistence::Object::Simple ); | ||
| 20 | ( $VERSION ) = '$Revision: 0.14 $' =~ /\s(\d+\.\d+)\s/; | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | |||
| 24 | my ( $class, %args ) = @_; | ||
| 25 | my ( $id ); | ||
| 26 | my $table = $args{ Table } || $args{ __Dope }; | ||
| 27 | my $self = {}; | ||
| 28 | |||
| 29 | if ( $args{ __Fn } ) { | ||
| 30 | my ($tab) = $args{ __Fn }; ($tab) = $tab =~ m:(.*)/:; | ||
| 31 | my ($id) = $args{ __Fn }; $id =~ s:.*/::; | ||
| 32 | $table = $tab; $args{ Id } = $id; | ||
| 33 | } | ||
| 34 | |||
| 35 | croak "Table $table doesn't exist." unless -d $table; | ||
| 36 | |||
| 37 | if ( $args{ Id } ) { | ||
| 38 | $id = $args{ Id }; | ||
| 39 | $self = $class->SUPER::new ( __Fn => "$table/$id" ); | ||
| 40 | $self->{ Id } = $id; | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | $self->{ Table } = $table; | ||
| 45 | return bless $self, $class; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | sub search { | ||
| 50 | |||
| 51 | my ( $self, $key, $regex ) = @_; | ||
| 52 | my $class = ref $self; | ||
| 53 | my $table = $self->{ Table }; | ||
| 54 | my @found; | ||
| 55 | $key =~ s/^(.)/uc $1/e; | ||
| 56 | |||
| 57 | croak "Unidentified Id." unless $table; | ||
| 58 | |||
| 59 | opendir( GROUP, $table ) || croak "Unidentified Table: $table."; | ||
| 60 | my @users = grep { !(-d "$table/$_") } readdir( GROUP ); | ||
| 61 | closedir GROUP; | ||
| 62 | |||
| 63 | for ( @users ) { | ||
| 64 | my $user = $class->new ( Id => $_ , Table => $table ); | ||
| 65 | my $data = $user->{ $key }; | ||
| 66 | push @found, $user if $data =~ /$regex/i; | ||
| 67 | } | ||
| 68 | |||
| 69 | return \@found; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | sub commit { | ||
| 74 | |||
| 75 | my ( $self, %args ) = @_; | ||
| 76 | return $self->SUPER::commit ( %args ); | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | sub load { | ||
| 81 | |||
| 82 | my ( $class, %args ) = @_; | ||
| 83 | my $object = $class->SUPER::load ( %args ); | ||
| 84 | return $object; | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | sub AUTOLOAD { | ||
| 89 | |||
| 90 | my ( $self, $value ) = @_; | ||
| 91 | |||
| 92 | my $key = $AUTOLOAD; $key =~ s/.*://; | ||
| 93 | $key =~ s/^(.)/uc $1/e; | ||
| 94 | |||
| 95 | if ( $value ) { | ||
| 96 | $self->{ $key } = $value; | ||
| 97 | $self->commit (); | ||
| 98 | } | ||
| 99 | |||
| 100 | return $self->{ $key }; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 'True Value' | ||
| 106 | |||
| 107 |