| 1 | package Oryx::DBI::Class; | ||
| 2 | |||
| 3 | use SQL::Abstract; | ||
| 4 | |||
| 5 | use Oryx::DBI::Association; | ||
| 6 | use Oryx::DBI::Attribute; | ||
| 7 | use Oryx::DBI::Method; | ||
| 8 | use Oryx::DBI::Parent; | ||
| 9 | |||
| 10 | use base qw(Oryx::MetaClass); | ||
| 11 | |||
| 12 | # Other MetaClass constructs are true instances and save their meta | ||
| 13 | # data as $self->{meta}. Class meta objects are different because | ||
| 14 | # their state is saved as class data instead of as instances of the | ||
| 15 | # MetaClass class. | ||
| 16 | |||
| 17 | # make some noise | ||
| 18 | our $DEBUG = 0; | ||
| 19 | |||
| 20 | sub dbh { $_[0]->storage->dbh } | ||
| 21 | |||
| 22 | sub create { | ||
| 23 | my ($class, $param) = @_; | ||
| 24 | my %query = ( table => $class->table ); | ||
| 25 | $param->{_isa} ||= $class; | ||
| 26 | |||
| 27 | $class->notify_observers('before_create', { param => $param, query => \%query }); | ||
| 28 | |||
| 29 | $_->create(\%query, $param) foreach $class->members; | ||
| 30 | |||
| 31 | # grab out the attributes that this class knows about | ||
| 32 | my @keys = (keys %{$class->attributes}); | ||
| 33 | push @keys, '_isa' if $class->is_abstract; | ||
| 34 | my $proto = { }; | ||
| 35 | @$proto{@keys} = @$param{@keys}; | ||
| 36 | |||
| 37 | my $sql = SQL::Abstract->new; | ||
| 38 | my ($stmnt, @bind) = $sql->insert($query{table}, $proto); | ||
| 39 | |||
| 40 | my $sth; | ||
| 41 | eval { $sth = $class->dbh->prepare_cached($stmnt) }; | ||
| 42 | die "ERROR: statement $stmnt $@" if $@; | ||
| 43 | $sth->execute(@bind); | ||
| 44 | $sth->finish; | ||
| 45 | |||
| 46 | $param->{id} = $class->lastId(); | ||
| 47 | $proto->{id} = $class->lastId(); | ||
| 48 | |||
| 49 | $_->create(\%query, $param) foreach @{$class->parents}; | ||
| 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 | my %query = ( | ||
| 64 | table => $class->table, | ||
| 65 | fields => [ 'id' ], | ||
| 66 | where => { id => $id }, | ||
| 67 | ); | ||
| 68 | |||
| 69 | if ($class->is_abstract) { | ||
| 70 | $DEBUG && $class->_carp("ABSTRACT CLASS retrieve $class"); | ||
| 71 | push @{$query{fields}}, '_isa'; | ||
| 72 | } | ||
| 73 | $DEBUG && $class->_carp("retrieve : id => $id"); | ||
| 74 | $class->notify_observers('before_retrieve', { query => \%query, id => $id }); | ||
| 75 | $_->retrieve(\%query, $id) foreach $class->members; | ||
| 76 | $_->retrieve(\%query, $id) foreach @{$class->parents}; | ||
| 77 | |||
| 78 | my $sql = SQL::Abstract->new; | ||
| 79 | my ($stmnt, @bind) = $sql->select(@query{ | ||
| 80 | qw(table fields where order) | ||
| 81 | }); | ||
| 82 | my $sth = $class->dbh->prepare_cached($stmnt); | ||
| 83 | |||
| 84 | eval { $sth->execute(@bind) }; | ||
| 85 | $class->_croak("execute failed [$stmnt], bind => " | ||
| 86 | .join(", ", @bind)." $@") if $@; | ||
| 87 | |||
| 88 | my $values = $sth->fetch; | ||
| 89 | $sth->finish; | ||
| 90 | |||
| 91 | if ($values and @$values) { | ||
| 92 | my $proto = $class->row2proto($query{fields}, $values); | ||
| 93 | |||
| 94 | if ($class->is_abstract and $proto->{_isa} ne $class) { | ||
| 95 | # abstract classes are never instantiated directly, so we | ||
| 96 | # need to retrieve the decendant instead. The descendant's | ||
| 97 | # ID is the same as the abstract class' ID because we used | ||
| 98 | # the abstract class' sequence when the decendant instance | ||
| 99 | # was created... so no need for a JOIN here | ||
| 100 | $DEBUG>1 && $class->_carp("RETRIEVE subclass : " | ||
| 101 | .$proto->{_isa}." for abstract class : $class"); | ||
| 102 | eval "use ".$proto->{_isa}; | ||
| 103 | $class->_croak($@) if $@; | ||
| 104 | return $proto->{_isa}->retrieve($proto->{id}); | ||
| 105 | } | ||
| 106 | $class->notify_observers('after_retrieve', { proto => $proto }); | ||
| 107 | return $class->construct($proto); | ||
| 108 | } else { | ||
| 109 | return undef; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub update { | ||
| 114 | my ($self) = @_; | ||
| 115 | return if $self->is_abstract; | ||
| 116 | my %query = ( | ||
| 117 | table => $self->table, | ||
| 118 | fieldvals => { }, | ||
| 119 | where => { id => $self->id }, | ||
| 120 | ); | ||
| 121 | $self->notify_observers('before_update', { query => \%query }); | ||
| 122 | $_->update(\%query, $self) foreach $self->members; | ||
| 123 | $_->update(\%query, $self) foreach @{$self->parents}; | ||
| 124 | |||
| 125 | my $sql = SQL::Abstract->new; | ||
| 126 | my ($stmnt, @bind) = $sql->update(@query{ | ||
| 127 | qw(table fieldvals where) | ||
| 128 | }); | ||
| 129 | my $sth = $self->dbh->prepare_cached($stmnt); | ||
| 130 | |||
| 131 | eval { $sth->execute(@bind) }; | ||
| 132 | $self->_croak("execute failed for $stmnt, bind => " | ||
| 133 | .join(", ", @bind)." $@") if $@; | ||
| 134 | |||
| 135 | $sth->finish; | ||
| 136 | |||
| 137 | $self->notify_observers('after_update'); | ||
| 138 | return $self; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub delete { | ||
| 142 | my ($self) = @_; | ||
| 143 | my %query = ( | ||
| 144 | table => $self->table, | ||
| 145 | where => { id => $self->id }, | ||
| 146 | ); | ||
| 147 | $self->notify_observers('before_delete', { query => \%query }); | ||
| 148 | $_->delete(\%query, $self) foreach $self->members; | ||
| 149 | $_->delete(\%query, $self) foreach @{$self->parents}; | ||
| 150 | |||
| 151 | my $sql = SQL::Abstract->new; | ||
| 152 | my ($stmnt, @bind) = $sql->delete(@query{qw(table where)}); | ||
| 153 | my $sth = $self->dbh->prepare_cached($stmnt); | ||
| 154 | |||
| 155 | $sth->execute(@bind); | ||
| 156 | $sth->finish; | ||
| 157 | |||
| 158 | $self->remove_from_cache; | ||
| 159 | $self->notify_observers('after_delete'); | ||
| 160 | return $self; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub search { | ||
| 164 | my ($class, $param, $order, $limit, $offset) = @_; | ||
| 165 | my %query = ( | ||
| 166 | table => $class->table, | ||
| 167 | fields => [ 'id' ], | ||
| 168 | where => $param, | ||
| 169 | order => $order || [ ], | ||
| 170 | ); | ||
| 171 | $limit = -1 unless defined $limit; | ||
| 172 | |||
| 173 | push @{$query{fields}}, '_isa' if $class->is_abstract; | ||
| 174 | |||
| 175 | $class->notify_observers('before_search', { | ||
| 176 | query => \%query, | ||
| 177 | param => $param, | ||
| 178 | order => $order, | ||
| 179 | limit => $limit, | ||
| 180 | } | ||
| 181 | ); | ||
| 182 | |||
| 183 | $_->search(\%query) foreach $class->members; | ||
| 184 | $_->search(\%query) foreach @{$class->parents}; | ||
| 185 | |||
| 186 | my $sql = SQL::Abstract->new(cmp => 'like'); | ||
| 187 | my ($stmnt, @bind) = $sql->select(@query{ | ||
| 188 | qw(table fields where order) | ||
| 189 | }); | ||
| 190 | #warn 'SEARCH STATEMENT => '.$stmnt; | ||
| 191 | my $sth = $class->dbh->prepare_cached($stmnt); | ||
| 192 | $sth->execute(@bind); | ||
| 193 | |||
| 194 | my (@objs, @row); | ||
| 195 | if (defined $offset) { | ||
| 196 | @row = $sth->fetch while ($offset-- > 0); | ||
| 197 | } | ||
| 198 | while ($limit-- and (@row = $sth->fetch)) { | ||
| 199 | my $proto = $class->row2proto($query{fields}, \@row); | ||
| 200 | push @objs, $class->construct($proto); | ||
| 201 | } | ||
| 202 | $sth->finish; | ||
| 203 | |||
| 204 | $class->notify_observers('after_search', { | ||
| 205 | query => \%query, | ||
| 206 | param => $param, | ||
| 207 | order => $order, | ||
| 208 | limit => $limit, | ||
| 209 | objects => \@objs | ||
| 210 | } | ||
| 211 | ); | ||
| 212 | |||
| 213 | return @objs; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub row2proto { | ||
| 217 | my ($class, $fields, $values) = @_; | ||
| 218 | my $proto = { }; | ||
| 219 | @$proto{ @$fields } = @$values; | ||
| 220 | return $proto; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub lastId { | ||
| 224 | my $class = shift; | ||
| 225 | $class->storage->util->lastval($class->dbh, $class->table); | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | 1; | ||
| 230 | __END__ |