1 package DBIx::Class::Relationship::Base;
6 use base qw/Class::Data::Inheritable/;
8 __PACKAGE__->mk_classdata('_relationships', { } );
12 DBIx::Class::Relationship - Inter-table relationships
18 This class handles relationships between the tables in your database
19 model. It allows your to set up relationships, and to perform joins
28 sub add_relationship {
29 my ($class, $rel, $f_class, $cond, $attrs) = @_;
30 die "Can't create relationship without join condition" unless $cond;
33 my %rels = %{ $class->_relationships };
34 $rels{$rel} = { class => $f_class,
37 $class->_relationships(\%rels);
38 #warn %{$f_class->_columns};
40 return unless eval { %{$f_class->_columns}; }; # Foreign class not loaded
41 eval { $class->_resolve_join($rel, 'me') };
43 if ($@) { # If the resolve failed, back out and re-throw the error
45 $class->_relationships(\%rels);
46 $class->throw("Error creating relationship $rel: $@");
52 my ($class, $join, $alias) = @_;
53 if (ref $join eq 'ARRAY') {
54 return map { $class->_resolve_join($_, $alias) } @$join;
55 } elsif (ref $join eq 'HASH') {
56 return map { $class->_resolve_join($_, $alias),
57 $class->_relationships->{$_}{class}->_resolve_join($join->{$_}, $_) }
60 $class->throw("No idea how to resolve join reftype ".ref $join);
62 my $rel_obj = $class->_relationships->{$join};
63 $class->throw("No such relationship ${join}") unless $rel_obj;
64 my $j_class = $rel_obj->{class};
65 my %join = (_action => 'join',
66 _aliases => { 'self' => $alias, 'foreign' => $join },
67 _classes => { $alias => $class, $join => $j_class });
68 my $j_cond = $j_class->resolve_condition($rel_obj->{cond}, \%join);
69 return [ { $join => $j_class->_table_name,
70 -join_type => $rel_obj->{attrs}{join_type} || '' }, $j_cond ];
74 sub resolve_condition {
75 my ($self, $cond, $attrs) = @_;
76 if (ref $cond eq 'HASH') {
78 foreach my $key (keys %$cond) {
79 my $val = $cond->{$key};
81 $self->throw("Can't handle this yet :(");
83 $ret{$self->_cond_key($attrs => $key)}
84 = $self->_cond_value($attrs => $key => $val);
89 $self->throw("Can't handle this yet :(");
94 my ($self, $attrs, $key) = @_;
95 my $action = $attrs->{_action} || '';
96 if ($action eq 'convert') {
97 unless ($key =~ s/^foreign\.//) {
98 $self->throw("Unable to convert relationship to WHERE clause: invalid key ${key}");
101 } elsif ($action eq 'join') {
102 my ($type, $field) = split(/\./, $key);
103 if (my $alias = $attrs->{_aliases}{$type}) {
104 my $class = $attrs->{_classes}{$alias};
105 $self->throw("Unknown column $field on $class as $alias")
106 unless exists $class->_columns->{$field};
107 return join('.', $alias, $field);
109 $self->throw( "Unable to resolve type ${type}: only have aliases for ".
110 join(', ', keys %{$attrs->{_aliases} || {}}) );
113 return $self->NEXT::ACTUAL::_cond_key($attrs, $key);
117 my ($self, $attrs, $key, $value) = @_;
118 my $action = $attrs->{_action} || '';
119 if ($action eq 'convert') {
120 unless ($value =~ s/^self\.//) {
121 $self->throw( "Unable to convert relationship to WHERE clause: invalid value ${value}" );
123 unless ($self->_columns->{$value}) {
124 $self->throw( "Unable to convert relationship to WHERE clause: no such accessor ${value}" );
126 return $self->get_column($value);
127 } elsif ($action eq 'join') {
128 my ($type, $field) = split(/\./, $value);
129 if (my $alias = $attrs->{_aliases}{$type}) {
130 my $class = $attrs->{_classes}{$alias};
131 $self->throw("Unknown column $field on $class as $alias")
132 unless exists $class->_columns->{$field};
133 return join('.', $alias, $field);
135 $self->throw( "Unable to resolve type ${type}: only have aliases for ".
136 join(', ', keys %{$attrs->{_aliases} || {}}) );
140 return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value)
145 return $self->_query_related('search', @_);
150 return $self->_query_related('count', @_);
158 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
159 $attrs = { %{ pop(@_) } };
161 my $rel_obj = $self->_relationships->{$rel};
162 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
163 $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
165 $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
166 my $query = ((@_ > 1) ? {@_} : shift);
168 $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something
169 # to merge into the AST really?
170 my ($cond) = $self->resolve_condition($rel_obj->{cond}, $attrs);
171 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
172 #use Data::Dumper; warn Dumper($query);
173 #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]});
174 delete $attrs->{_action};
175 return $self->resolve_class($rel_obj->{class}
176 )->$meth($query, $attrs);
181 return $class->new_related(@_)->insert;
185 my ($self, $rel, $values, $attrs) = @_;
186 $self->throw( "Can't call new_related as class method" )
188 $self->throw( "new_related needs a hash" )
189 unless (ref $values eq 'HASH');
190 my $rel_obj = $self->_relationships->{$rel};
191 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
192 $self->throw( "Can't abstract implicit create for ${rel}, condition not a hash" )
193 unless ref $rel_obj->{cond} eq 'HASH';
194 $attrs = { %{$rel_obj->{attrs}}, %{$attrs || {}}, _action => 'convert' };
196 my %fields = %{$self->resolve_condition($rel_obj->{cond},$attrs)};
197 $fields{$_} = $values->{$_} for keys %$values;
199 return $self->resolve_class($rel_obj->{class})->new(\%fields);
205 my $rel_obj = $self->_relationships->{$rel};
206 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
207 my ($cond) = $self->resolve_condition($rel_obj->{cond}, { _action => 'convert' });
208 $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
210 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
211 $attrs = { %{ pop(@_) } };
213 my $query = ((@_ > 1) ? {@_} : shift);
214 $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
215 return $self->resolve_class($rel_obj->{class})->find($query);
218 sub find_or_create_related {
220 return $self->find_related(@_) || $self->create_related(@_);
223 sub set_from_related {
224 my ($self, $rel, $f_obj) = @_;
225 my $rel_obj = $self->_relationships->{$rel};
226 $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
227 my $cond = $rel_obj->{cond};
228 $self->throw( "set_from_related can only handle a hash condition; the "
229 ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
230 unless ref $cond eq 'HASH';
231 my $f_class = $self->resolve_class($rel_obj->{class});
232 $self->throw( "Object $f_obj isn't a ".$f_class )
233 unless $f_obj->isa($f_class);
234 foreach my $key (keys %$cond) {
235 next if ref $cond->{$key}; # Skip literals and complex conditions
236 $self->throw("set_from_related can't handle $key as key")
237 unless $key =~ m/^foreign\.([^\.]+)$/;
238 my $val = $f_obj->get_column($1);
239 $self->throw("set_from_related can't handle ".$cond->{$key}." as value")
240 unless $cond->{$key} =~ m/^self\.([^\.]+)$/;
241 $self->set_column($1 => $val);
246 sub update_from_related {
248 $self->set_from_related(@_);
254 return $self->search_related(@_)->delete;
263 Matt S. Trout <mst@shadowcatsystems.co.uk>
267 You may distribute this code under the same terms as Perl itself.