File Coverage

lib/Oryx/DBI/Parent.pm
Criterion Covered Total %
statement 9 60 15.0
branch 0 8 0.0
condition 0 3 0.0
subroutine 3 9 33.3
pod 6 6 100.0
total 18 86 20.9


line stmt bran cond sub pod time code
1             package Oryx::DBI::Parent;
2              
3 15     15   79 use SQL::Abstract;
  15         28  
  15         114  
4 15     15   383 use Scalar::Util qw(blessed);
  15         32  
  15         1290  
5              
6 15     15   76 use base qw(Oryx::Parent);
  15         53  
  15         6226  
7              
8             sub create {
9 0     0 1   my ($self, $query, $proto) = @_;
10              
11 0           my $sql = SQL::Abstract->new;
12 0           my $lt_name = $self->link_table;
13 0           my $parent = $self->class->create($proto);
14 0           my %fvals = (lc($self->class->name) => $parent->id);
15              
16 0 0         unless ($query->{_seen_parents}) {
17             # insert a new row in the link table
18 0           $fvals{$self->child_field} = $proto->{id};
19 0           my ($stmnt, @bind) = $sql->insert($lt_name, \%fvals);
20 0           my $sth = $self->dbh->prepare($stmnt);
21              
22 0           $sth->execute(@bind);
23 0           $sth->finish;
24 0           $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 0           my %where = ( $self->child_field => $proto->{id} );
29 0           my ($stmnt, @bind) = $sql->update($lt_name, \%fvals, \%where);
30 0           my $sth = $self->dbh->prepare($stmnt);
31              
32 0           $sth->execute(@bind);
33 0           $sth->finish;
34             }
35              
36             }
37              
38 0     0 1   sub retrieve { }
39              
40             sub update {
41 0     0 1   my ($self, $query, $obj) = @_;
42 0           my $parent = $obj->PARENT($self->class);
43 0 0 0       return unless (defined $parent and blessed($parent)); # abstract (no attributes)
44 0           $parent->$_($obj->$_) foreach keys %{$self->class->attributes};
  0            
45 0           $parent->update;
46             }
47              
48             sub delete {
49 0     0 1   my ($self, $query, $obj) = @_;
50              
51 0           my $sql = SQL::Abstract->new;
52 0           my $lt_name = $self->link_table;
53              
54 0           my %where = ($self->child_field => $obj->id);
55 0           my ($stmnt, @bind) = $sql->delete($lt_name, \%where);
56 0           my $sth = $obj->dbh->prepare($stmnt);
57 0           $sth->execute(@bind);
58 0           $sth->finish;
59              
60 0           my $parent = $obj->PARENT($self->class);
61 0 0         $parent->delete() if $parent;
62             }
63              
64 0     0 1   sub search { }
65              
66             sub construct {
67 0     0 1   my ($self, $object) = @_;
68             # copy the attribute values from the parent to the child instance
69              
70 0           my $sql = SQL::Abstract->new;
71 0           my $lt_name = $self->link_table;
72              
73 0           my %where = ($self->child_field => $object->id);
74 0           my @fields = ($self->class->name);
75 0           my ($stmnt, @bind) = $sql->select($lt_name, \@fields, \%where);
76 0           my $sth = $self->dbh->prepare_cached($stmnt);
77              
78 0           $sth->execute(@bind);
79 0           my $row = $sth->fetch;
80 0           my $parent = $self->class->retrieve( $row->[0] );
81 0           $sth->finish;
82 0 0         unless (defined $parent) {
83 0           $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 0           $object->$_($parent->$_) foreach keys %{$self->class->attributes};
  0            
91 0           $object->PARENT($self->class, $parent);
92             }
93              
94             1;
95             __END__