| 1 | package Oryx::DBI::Association::Array; | ||
| 2 | |||
| 3 | use Oryx::DBI::Association::Reference; | ||
| 4 | |||
| 5 | use base qw(Oryx::Association::Array); | ||
| 6 | |||
| 7 | sub create { | ||
| 8 | my ($self, $query, $proto) = @_; | ||
| 9 | } | ||
| 10 | |||
| 11 | sub retrieve { | ||
| 12 | my ($self, $query, $id) = @_; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub update { | ||
| 16 | my ($self, $query, $obj) = @_; | ||
| 17 | my $accessor = $self->role; | ||
| 18 | my $value = $obj->$accessor; | ||
| 19 | my $sql = SQL::Abstract->new; | ||
| 20 | |||
| 21 | my $lt_name = $self->link_table; | ||
| 22 | my @lt_flds = $self->link_fields; | ||
| 23 | |||
| 24 | unless (tied(@$value)) { | ||
| 25 | my @list = @$value; | ||
| 26 | $self->construct($obj); | ||
| 27 | my $i = 0; | ||
| 28 | grep { tied(@$value)->_set_created($i++, $_) } @list; | ||
| 29 | my %lt_where = ($lt_flds[0] => $obj->id); | ||
| 30 | my $stmnt = $sql->delete($lt_name, \%lt_where); | ||
| 31 | my $sth = $obj->dbh->prepare($stmnt); | ||
| 32 | my @bind = $sql->values(\%lt_where); | ||
| 33 | $sth->execute(@bind); | ||
| 34 | $sth->finish; | ||
| 35 | |||
| 36 | tied(@$value)->deleted({}); | ||
| 37 | tied(@$value)->updated({}); | ||
| 38 | } | ||
| 39 | |||
| 40 | my (@bind, %lt_fieldvals, %lt_where, $stmnt, $sth); | ||
| 41 | if (%{tied(@$value)->deleted}) { | ||
| 42 | %lt_where = ($lt_flds[0] => $obj->id, $lt_flds[2] => ''); | ||
| 43 | |||
| 44 | $stmnt = $sql->delete($lt_name, \%lt_where); | ||
| 45 | $sth = $obj->dbh->prepare($stmnt); | ||
| 46 | |||
| 47 | while (my ($index, $thing) = each %{tied(@$value)->deleted}) { | ||
| 48 | $lt_where{$lt_flds[2]} = $index; | ||
| 49 | @bind = $sql->values(\%lt_where); | ||
| 50 | $sth->execute(@bind); | ||
| 51 | } | ||
| 52 | |||
| 53 | $sth->finish; | ||
| 54 | tied(@$value)->deleted({}); | ||
| 55 | } | ||
| 56 | |||
| 57 | if (%{tied(@$value)->created}) { | ||
| 58 | @lt_fieldvals{@lt_flds} = ($obj->id, '', ''); | ||
| 59 | |||
| 60 | $stmnt = $sql->insert($lt_name, \%lt_fieldvals); | ||
| 61 | $sth = $obj->dbh->prepare($stmnt); | ||
| 62 | |||
| 63 | while (my ($index, $thing) = each %{tied(@$value)->created}) { | ||
| 64 | $lt_fieldvals{$lt_flds[1]} = defined $thing ? $thing->{id} : undef; | ||
| 65 | $lt_fieldvals{$lt_flds[2]} = $index; | ||
| 66 | @bind = $sql->values(\%lt_fieldvals); | ||
| 67 | $sth->execute(@bind); | ||
| 68 | } | ||
| 69 | |||
| 70 | $sth->finish; | ||
| 71 | tied(@$value)->created({}); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (%{tied(@$value)->updated}) { | ||
| 75 | %lt_where = ( $lt_flds[0] => $obj->id, $lt_flds[2] => '' ); | ||
| 76 | %lt_fieldvals = ( $lt_flds[1] => '' ); | ||
| 77 | |||
| 78 | $stmnt = $sql->update($lt_name, \%lt_fieldvals, \%lt_where); | ||
| 79 | $sth = $obj->dbh->prepare($stmnt); | ||
| 80 | |||
| 81 | while (my ($index, $thing) = each %{tied(@$value)->updated}) { | ||
| 82 | $lt_fieldvals{$lt_flds[1]} = defined $thing ? $thing->{id} : undef; | ||
| 83 | $lt_where{$lt_flds[2]} = $index; | ||
| 84 | @bind = $sql->values(\%lt_fieldvals); | ||
| 85 | push @bind, $sql->values(\%lt_where); | ||
| 86 | $sth->execute(@bind); | ||
| 87 | } | ||
| 88 | |||
| 89 | $sth->finish; | ||
| 90 | tied(@$value)->updated({}); | ||
| 91 | } | ||
| 92 | |||
| 93 | $self->update_backrefs($obj, @$value); | ||
| 94 | |||
| 95 | $obj->dbh->commit; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub delete { | ||
| 99 | my $self = shift; | ||
| 100 | my ($query, $obj) = @_; | ||
| 101 | my $accessor = $self->role; | ||
| 102 | my $value = $obj->$accessor; | ||
| 103 | |||
| 104 | if ($self->constraint eq 'Composition') { | ||
| 105 | # cascade the delete | ||
| 106 | while (my $thing = pop @$value) { | ||
| 107 | $thing->delete; | ||
| 108 | } | ||
| 109 | } elsif ($self->constraint eq 'Aggregation') { | ||
| 110 | # just clear the Array | ||
| 111 | @$value = (); | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->update(@_); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub search { | ||
| 118 | my ($self, $query) = @_; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub construct { | ||
| 122 | my ($self, $obj) = @_; | ||
| 123 | my $assoc_name = $self->role; | ||
| 124 | my @args = ($self, $obj); | ||
| 125 | |||
| 126 | my @list; | ||
| 127 | if ($obj->{$assoc_name}) { | ||
| 128 | @list = @{$obj->{$assoc_name}}; | ||
| 129 | } | ||
| 130 | |||
| 131 | $obj->{$assoc_name} = [ ] unless $obj->{$assoc_name}; | ||
| 132 | tie @{$obj->{$assoc_name}}, __PACKAGE__, @args; | ||
| 133 | |||
| 134 | if (@list) { | ||
| 135 | my $i = 0; | ||
| 136 | grep { tied(@{$obj->{$assoc_name}})->_set_created($i++, $_) } @list; | ||
| 137 | |||
| 138 | my $tieobj = tied(@{$obj->{$assoc_name}}); | ||
| 139 | my $sql = SQL::Abstract->new; | ||
| 140 | my $lt_name = $self->link_table; | ||
| 141 | my @lt_flds = $self->link_fields; | ||
| 142 | |||
| 143 | my %lt_where = ($lt_flds[0] => $obj->id); | ||
| 144 | my $stmnt = $sql->delete($lt_name, \%lt_where); | ||
| 145 | my $sth = $obj->dbh->prepare($stmnt); | ||
| 146 | my @bind = $sql->values(\%lt_where); | ||
| 147 | $sth->execute(@bind); | ||
| 148 | $sth->finish; | ||
| 149 | |||
| 150 | $tieobj->deleted({}); | ||
| 151 | $tieobj->updated({}); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # Fill an array ref with ids from the link table and order by 'meta'. | ||
| 156 | # The ids are each tied to a Reference value type which will retrieve | ||
| 157 | # the referenced object (lazy loading) | ||
| 158 | sub load { | ||
| 159 | my ($self, $owner) = @_; | ||
| 160 | |||
| 161 | my $lt_name = $self->link_table; | ||
| 162 | my ($s_id_field, $t_id_field, $meta_field) = $self->link_fields; | ||
| 163 | |||
| 164 | my (@fields, %where, @order); | ||
| 165 | @fields = ($t_id_field, '_seq'); | ||
| 166 | $where{$s_id_field} = $owner->id; | ||
| 167 | @order = ('_seq'); | ||
| 168 | |||
| 169 | my $sql = SQL::Abstract->new; | ||
| 170 | my ($stmnt, @bind) = $sql->select($lt_name, \@fields, \%where, \@order); | ||
| 171 | |||
| 172 | my $sth = $owner->dbh->prepare_cached($stmnt); | ||
| 173 | $sth->execute(@bind); | ||
| 174 | |||
| 175 | my $Array = [ ]; | ||
| 176 | my ($oid, $idx, @args); | ||
| 177 | my @ids_seq = $sth->fetchall; | ||
| 178 | for (my $x = 0; $x < @ids_seq; $x++) { | ||
| 179 | $oid = $ids_seq[$x]->[0]; | ||
| 180 | $idx = $ids_seq[$x]->[1]; | ||
| 181 | @args = ($self, $oid); | ||
| 182 | $Array->[$idx] = Oryx::DBI::Association::Reference->TIESCALAR(@args); | ||
| 183 | } | ||
| 184 | |||
| 185 | $sth->finish; | ||
| 186 | return $Array; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub fetch { | ||
| 190 | my ($self, $thing, $owner) = @_; | ||
| 191 | if (ref $thing eq 'Oryx::DBI::Association::Reference') { | ||
| 192 | return $thing->FETCH(); | ||
| 193 | } | ||
| 194 | return $thing; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub store { | ||
| 198 | my ($self, $thing, $owner) = @_; | ||
| 199 | return $thing; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub link_fields { | ||
| 203 | my $self = shift; | ||
| 204 | return ("source_id", "target_id", '_seq'); | ||
| 205 | } | ||
| 206 | |||
| 207 | 1; | ||
| 208 | __END__ |