| 1 | package Oryx::DBI::Parent; | ||
| 2 | |||
| 3 | use SQL::Abstract; | ||
| 4 | use Scalar::Util qw(blessed); | ||
| 5 | |||
| 6 | use base qw(Oryx::Parent); | ||
| 7 | |||
| 8 | sub create { | ||
| 9 | my ($self, $query, $proto) = @_; | ||
| 10 | |||
| 11 | my $sql = SQL::Abstract->new; | ||
| 12 | my $lt_name = $self->link_table; | ||
| 13 | my $parent = $self->class->create($proto); | ||
| 14 | my %fvals = (lc($self->class->name) => $parent->id); | ||
| 15 | |||
| 16 | unless ($query->{_seen_parents}) { | ||
| 17 | # insert a new row in the link table | ||
| 18 | $fvals{$self->child_field} = $proto->{id}; | ||
| 19 | my ($stmnt, @bind) = $sql->insert($lt_name, \%fvals); | ||
| 20 | my $sth = $self->dbh->prepare($stmnt); | ||
| 21 | |||
| 22 | $sth->execute(@bind); | ||
| 23 | $sth->finish; | ||
| 24 | $query->{_seen_parents}++; | ||
| 25 | } else { | ||
| 26 | # the row in the link table has already been created by | ||
| 27 | # another superclass instance, so just update link table | ||
| 28 | my %where = ( $self->child_field => $proto->{id} ); | ||
| 29 | my ($stmnt, @bind) = $sql->update($lt_name, \%fvals, \%where); | ||
| 30 | my $sth = $self->dbh->prepare($stmnt); | ||
| 31 | |||
| 32 | $sth->execute(@bind); | ||
| 33 | $sth->finish; | ||
| 34 | } | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | sub retrieve { } | ||
| 39 | |||
| 40 | sub update { | ||
| 41 | my ($self, $query, $obj) = @_; | ||
| 42 | my $parent = $obj->PARENT($self->class); | ||
| 43 | return unless (defined $parent and blessed($parent)); # abstract (no attributes) | ||
| 44 | $parent->$_($obj->$_) foreach keys %{$self->class->attributes}; | ||
| 45 | $parent->update; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub delete { | ||
| 49 | my ($self, $query, $obj) = @_; | ||
| 50 | |||
| 51 | my $sql = SQL::Abstract->new; | ||
| 52 | my $lt_name = $self->link_table; | ||
| 53 | |||
| 54 | my %where = ($self->child_field => $obj->id); | ||
| 55 | my ($stmnt, @bind) = $sql->delete($lt_name, \%where); | ||
| 56 | my $sth = $obj->dbh->prepare($stmnt); | ||
| 57 | $sth->execute(@bind); | ||
| 58 | $sth->finish; | ||
| 59 | |||
| 60 | my $parent = $obj->PARENT($self->class); | ||
| 61 | $parent->delete() if $parent; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub search { } | ||
| 65 | |||
| 66 | sub construct { | ||
| 67 | my ($self, $object) = @_; | ||
| 68 | # copy the attribute values from the parent to the child instance | ||
| 69 | |||
| 70 | my $sql = SQL::Abstract->new; | ||
| 71 | my $lt_name = $self->link_table; | ||
| 72 | |||
| 73 | my %where = ($self->child_field => $object->id); | ||
| 74 | my @fields = ($self->class->name); | ||
| 75 | my ($stmnt, @bind) = $sql->select($lt_name, \@fields, \%where); | ||
| 76 | my $sth = $self->dbh->prepare_cached($stmnt); | ||
| 77 | |||
| 78 | $sth->execute(@bind); | ||
| 79 | my $row = $sth->fetch; | ||
| 80 | my $parent = $self->class->retrieve( $row->[0] ); | ||
| 81 | $sth->finish; | ||
| 82 | unless (defined $parent) { | ||
| 83 | $self->_croak( | ||
| 84 | "undefined parent for $object [".$object->id. | ||
| 85 | "], you may have a diamond inheritance involving". | ||
| 86 | "a common, abstract super class" | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | $object->$_($parent->$_) foreach keys %{$self->class->attributes}; | ||
| 91 | $object->PARENT($self->class, $parent); | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | __END__ |