| 1 | package Oryx::DBM::Class; | ||
| 2 | |||
| 3 | use DBM::Deep; | ||
| 4 | use Oryx::DBM::Association; | ||
| 5 | use Oryx::DBM::Attribute; | ||
| 6 | use Oryx::DBM::Method; | ||
| 7 | use Oryx::DBM::Parent; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | use base qw(Oryx::MetaClass); | ||
| 11 | |||
| 12 | our $DEBUG = 0; | ||
| 13 | |||
| 14 | __PACKAGE__->mk_classdata('_dbm'); | ||
| 15 | |||
| 16 | sub dbh { $_[0]->storage } | ||
| 17 | |||
| 18 | sub dbm { | ||
| 19 | my $class = ref $_[0] || $_[0]; | ||
| 20 | unless ($class->_dbm) { | ||
| 21 | Carp::confess('no catalog entry for table : '.$class->table) | ||
| 22 | unless defined $class->dbh->catalog->get($class->table); | ||
| 23 | $class->_dbm( | ||
| 24 | DBM::Deep->new(%{ $class->dbh->catalog->get($class->table) }) | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | $class->_dbm; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub create { | ||
| 31 | my ($class, $param) = @_; | ||
| 32 | |||
| 33 | $param->{id} = $class->nextId(); | ||
| 34 | $param->{_isa} ||= $class; | ||
| 35 | |||
| 36 | $class->notify_observers('before_create', { param => $param }); | ||
| 37 | $_->create($param) foreach $class->members; | ||
| 38 | $_->create($param) foreach @{$class->parents}; | ||
| 39 | |||
| 40 | # grab out the attributes that this class knows about | ||
| 41 | my @keys = ('id', keys %{$class->attributes}); | ||
| 42 | push @keys, '_isa' if $class->is_abstract; | ||
| 43 | push @keys, '_parent_ids' if @{ $class->parents }; | ||
| 44 | |||
| 45 | my $proto = { }; | ||
| 46 | @$proto{@keys} = @$param{@keys}; | ||
| 47 | |||
| 48 | $class->dbm->push( $proto ); | ||
| 49 | |||
| 50 | $class->notify_observers('after_create', { param => $param, proto => $proto }); | ||
| 51 | |||
| 52 | return $class->construct($proto); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub retrieve { | ||
| 56 | my ($class, $id) = @_; | ||
| 57 | |||
| 58 | # fetch the object from the cache if it exists | ||
| 59 | my $key = $class->_mk_cache_key($id); | ||
| 60 | my $object; | ||
| 61 | return $object if ($object = $Live_Objects{$key}); | ||
| 62 | |||
| 63 | $DEBUG && $class->_carp("retrieve : id => $id"); | ||
| 64 | my $proto = $class->dbm->get( $id ); | ||
| 65 | return undef unless $proto; | ||
| 66 | $proto = $proto->export; | ||
| 67 | |||
| 68 | $class->notify_observers('before_retrieve', { proto => $proto, id => $id }); | ||
| 69 | $_->retrieve($proto, $id) foreach $class->members; | ||
| 70 | $_->retrieve($proto, $id) foreach @{$class->parents}; | ||
| 71 | |||
| 72 | if ($proto) { | ||
| 73 | if ($class->is_abstract and $proto->{_isa} ne $class) { | ||
| 74 | # abstract classes are never instantiated directly, so we | ||
| 75 | # need to retrieve the decendant instead. The descendant's | ||
| 76 | # ID is the same as the abstract class' ID because we used | ||
| 77 | # the abstract class' sequence when the decendant instance | ||
| 78 | # was created... | ||
| 79 | $DEBUG>1 && $class->_carp("RETRIEVE subclass : " | ||
| 80 | .$proto->{_isa}." for abstract class : $class"); | ||
| 81 | eval "use ".$proto->{_isa}; | ||
| 82 | $class->_croak($@) if $@; | ||
| 83 | return $proto->{_isa}->retrieve($proto->{id}); | ||
| 84 | } | ||
| 85 | $class->notify_observers('after_retrieve', { proto => $proto, id => $id }); | ||
| 86 | return $class->construct($proto); | ||
| 87 | } else { | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub update { | ||
| 93 | my ($self) = @_; | ||
| 94 | return if $self->is_abstract; | ||
| 95 | |||
| 96 | #$self->dbm->lock; | ||
| 97 | |||
| 98 | my $proto = $self->dbm->get( $self->id ); | ||
| 99 | return undef unless $proto; | ||
| 100 | $proto = $proto->export; | ||
| 101 | |||
| 102 | $self->notify_observers('before_update', { proto => $proto }); | ||
| 103 | $_->update($proto, $self) foreach $self->members; | ||
| 104 | $_->update($proto, $self) foreach @{$self->parents}; | ||
| 105 | $self->dbm->put( $self->id, $proto ); | ||
| 106 | |||
| 107 | $self->notify_observers('after_update'); | ||
| 108 | #$self->dbm->unlock; | ||
| 109 | |||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub delete { | ||
| 114 | my ($self) = @_; | ||
| 115 | my $proto = $self->dbm->get( $self->id )->export; | ||
| 116 | $self->notify_observers('before_delete', { proto => $proto }); | ||
| 117 | $_->delete($proto, $self) foreach $self->members; | ||
| 118 | $_->delete($proto, $self) foreach @{$self->parents}; | ||
| 119 | $self->dbm->delete($self->id); | ||
| 120 | $self->remove_from_cache; | ||
| 121 | $self->notify_observers('after_delete'); | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub search { | ||
| 126 | my ($class, $param, $order, $limit, $offset) = @_; | ||
| 127 | $class->notify_observers('before_search', { | ||
| 128 | param => $param, | ||
| 129 | order => $order, | ||
| 130 | limit => $limit | ||
| 131 | } | ||
| 132 | ); | ||
| 133 | |||
| 134 | my ($found, @objs); | ||
| 135 | SEARCH: foreach my $proto (@{ $class->dbm }) { | ||
| 136 | next unless defined $proto->{id}; | ||
| 137 | |||
| 138 | $found = 1; | ||
| 139 | foreach my $field (keys %$param) { | ||
| 140 | next SEARCH if ref $proto->{$field}; | ||
| 141 | |||
| 142 | my $value = $param->{$field}; | ||
| 143 | $value =~ /%?([^%]*)%?/; | ||
| 144 | unless ($proto->{$field} =~ /$1/) { | ||
| 145 | $found = 0; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | push @objs, $class->construct($proto->export) if $found; | ||
| 149 | } | ||
| 150 | if ($order) { | ||
| 151 | foreach my $field (@$order) { | ||
| 152 | @objs = sort { | ||
| 153 | $a->$field cmp $b->$field | ||
| 154 | } @objs; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | shift @objs while ( $offset-- > 0 ); | ||
| 159 | pop @objs while ( defined $limit and (@objs > $limit) ); | ||
| 160 | |||
| 161 | $class->notify_observers('after_search', { | ||
| 162 | param => $param, | ||
| 163 | order => $order, | ||
| 164 | limit => $limit, | ||
| 165 | objects => \@objs | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | return @objs; | ||
| 169 | } | ||
| 170 | |||
| 171 | # next id in sequence | ||
| 172 | sub nextId { | ||
| 173 | my $nextId = $_[0]->dbm->length; | ||
| 174 | $DEBUG && $_[0]->_carp("next id => $nextId"); | ||
| 175 | return $nextId; | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; | ||
| 179 | __END__ |