| 1 | package Oryx::DBI::Association::Hash; | ||
| 2 | |||
| 3 | use Oryx::DBI::Association::Reference; | ||
| 4 | |||
| 5 | use base qw(Oryx::Association::Hash); | ||
| 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 | |||
| 20 | my $sql = SQL::Abstract->new; | ||
| 21 | |||
| 22 | my $lt_name = $self->link_table; | ||
| 23 | my @lt_flds = $self->link_fields; | ||
| 24 | |||
| 25 | my (@bind, %lt_fieldvals, %lt_where, $stmnt, $sth); | ||
| 26 | if (%{tied(%$value)->deleted}) { | ||
| 27 | %lt_where = ($lt_flds[0] => $obj->id, $lt_flds[2] => ''); | ||
| 28 | |||
| 29 | $stmnt = $sql->delete($lt_name, \%lt_where); | ||
| 30 | $sth = $obj->dbh->prepare($stmnt); | ||
| 31 | |||
| 32 | while (my ($key, $thing) = each %{tied(%$value)->deleted}) { | ||
| 33 | $lt_where{$lt_flds[2]} = $key; | ||
| 34 | @bind = $sql->values(\%lt_where); | ||
| 35 | $sth->execute(@bind); | ||
| 36 | } | ||
| 37 | |||
| 38 | $sth->finish; | ||
| 39 | tied(%$value)->deleted({}); | ||
| 40 | } | ||
| 41 | |||
| 42 | if (%{tied(%$value)->created}) { | ||
| 43 | @lt_fieldvals{@lt_flds} = ($obj->id, '', ''); | ||
| 44 | |||
| 45 | $stmnt = $sql->insert($lt_name, \%lt_fieldvals); | ||
| 46 | $sth = $obj->dbh->prepare($stmnt); | ||
| 47 | |||
| 48 | while (my ($key, $thing) = each %{tied(%$value)->created}) { | ||
| 49 | $lt_fieldvals{$lt_flds[1]} = defined $thing ? $thing->{id} : undef; | ||
| 50 | $lt_fieldvals{$lt_flds[2]} = $key; | ||
| 51 | @bind = $sql->values(\%lt_fieldvals); | ||
| 52 | $sth->execute(@bind); | ||
| 53 | } | ||
| 54 | |||
| 55 | $sth->finish; | ||
| 56 | tied(%$value)->created({}); | ||
| 57 | } | ||
| 58 | |||
| 59 | if (%{tied(%$value)->updated}) { | ||
| 60 | %lt_where = ( $lt_flds[0] => $obj->id, $lt_flds[2] => '' ); | ||
| 61 | %lt_fieldvals = ( $lt_flds[1] => '' ); | ||
| 62 | |||
| 63 | $stmnt = $sql->update($lt_name, \%lt_fieldvals, \%lt_where); | ||
| 64 | $sth = $obj->dbh->prepare($stmnt); | ||
| 65 | |||
| 66 | while (my ($key, $thing) = each %{tied(%$value)->updated}) { | ||
| 67 | $lt_fieldvals{$lt_flds[1]} = defined $thing ? $thing->id : undef; | ||
| 68 | $lt_where{$lt_flds[2]} = $key; | ||
| 69 | @bind = $sql->values(\%lt_fieldvals); | ||
| 70 | push @bind, $sql->values(\%lt_where); | ||
| 71 | $sth->execute(@bind); | ||
| 72 | } | ||
| 73 | |||
| 74 | $sth->finish; | ||
| 75 | tied(%$value)->updated({}); | ||
| 76 | } | ||
| 77 | |||
| 78 | $self->update_backrefs($obj, values %$value); | ||
| 79 | |||
| 80 | $obj->dbh->commit; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub delete { | ||
| 84 | my $self = shift; | ||
| 85 | my ($query, $obj) = @_; | ||
| 86 | my $accessor = $self->role; | ||
| 87 | my $value = $obj->$accessor; | ||
| 88 | |||
| 89 | if ($self->constraint eq 'Composition') { | ||
| 90 | # composition, so cascade the delete | ||
| 91 | foreach my $thing (values %$value) { | ||
| 92 | $thing->delete; | ||
| 93 | } | ||
| 94 | } elsif ($self->constraint eq 'Aggregation') { | ||
| 95 | # aggregation so just clear the Hash | ||
| 96 | %$value = (); | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->update(@_); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub search { | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub construct { | ||
| 107 | my ($self, $obj) = @_; | ||
| 108 | my $assoc_name = $self->role; | ||
| 109 | my @args = ($self, $obj); | ||
| 110 | |||
| 111 | my %hash; | ||
| 112 | if ($obj->{$assoc_name}) { | ||
| 113 | %hash = %{$obj->{$assoc_name}}; | ||
| 114 | } | ||
| 115 | |||
| 116 | $obj->{$assoc_name} = { } unless $obj->{$assoc_name}; | ||
| 117 | tie %{$obj->{$assoc_name}}, __PACKAGE__, @args; | ||
| 118 | |||
| 119 | if (%hash) { | ||
| 120 | warn "got prefil hash"; | ||
| 121 | while (my ($k, $v) = each %hash) { | ||
| 122 | warn "set_created $k => $v"; | ||
| 123 | tied(%{$obj->{$assoc_name}})->_set_created($k, $v); | ||
| 124 | } | ||
| 125 | |||
| 126 | my $tieobj = tied(%{$obj->{$assoc_name}}); | ||
| 127 | my $sql = SQL::Abstract->new; | ||
| 128 | my $lt_name = $self->link_table; | ||
| 129 | my @lt_flds = $self->link_fields; | ||
| 130 | |||
| 131 | my %lt_where = ($lt_flds[0] => $obj->id); | ||
| 132 | my $stmnt = $sql->delete($lt_name, \%lt_where); | ||
| 133 | my $sth = $obj->dbh->prepare($stmnt); | ||
| 134 | my @bind = $sql->values(\%lt_where); | ||
| 135 | $sth->execute(@bind); | ||
| 136 | $sth->finish; | ||
| 137 | |||
| 138 | $tieobj->deleted({}); | ||
| 139 | $tieobj->updated({}); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | sub load { | ||
| 144 | my ($self, $owner) = @_; | ||
| 145 | |||
| 146 | my $lt_name = $self->link_table; | ||
| 147 | my ($source_id, $target_id, $_key) = $self->link_fields; | ||
| 148 | |||
| 149 | my (@fields, %where); | ||
| 150 | @fields = ($target_id, $_key); | ||
| 151 | |||
| 152 | $DEBUG && $self->_carp("load : OWNER => $owner, ID => ".$owner->id); | ||
| 153 | |||
| 154 | $where{$source_id} = $owner->id; | ||
| 155 | |||
| 156 | my $sql = SQL::Abstract->new; | ||
| 157 | my ($stmnt, @bind) = $sql->select($lt_name, \@fields, \%where); | ||
| 158 | |||
| 159 | my $sth = $owner->dbh->prepare_cached($stmnt); | ||
| 160 | $sth->execute(@bind); | ||
| 161 | |||
| 162 | my $Hash = { }; my @args; | ||
| 163 | my @ids_keys = $sth->fetchall; | ||
| 164 | for (my $x = 0; $x < @ids_keys; $x++) { | ||
| 165 | @args = ($self, $ids_keys[$x]->[0]); | ||
| 166 | tie $Hash->{$ids_keys[$x]->[1]}, | ||
| 167 | 'Oryx::DBI::Association::Reference', @args; | ||
| 168 | } | ||
| 169 | $sth->finish; | ||
| 170 | |||
| 171 | return $Hash; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | sub fetch { | ||
| 176 | my ($self, $thing, $owner) = @_; | ||
| 177 | return $thing; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub store { | ||
| 181 | my ($self, $thing, $owner) = @_; | ||
| 182 | return $thing; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub link_fields { | ||
| 186 | my $self = shift; | ||
| 187 | return ("source_id", "target_id", '_key'); | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | __END__ |