X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FRelationship%2FBase.pm;h=8925121e32de6398c21a2e2b40e8200b0e2c0a5b;hb=64acc2bc801dcc1c982b16fe45434e9b82edcef6;hp=35f906725662ccda689d1216847f8d3a5f5505c3;hpb=87772e46b99bc03f36c5a82ec29282a3228c29c7;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 35f9067..8925121 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -51,7 +51,7 @@ command immediately before C. An arrayref containing a list of accessors in the foreign class to proxy in the main class. If, for example, you do the following: - __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => qw[/ margle /] }); + __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => [ qw/margle/ ] }); Then, assuming Bar has an accessor named margle, you can do: @@ -69,94 +69,13 @@ created, which calls C for the relationship. =back -=cut - -sub add_relationship { - shift->result_source->add_relationship(@_); -} +=head2 register_relationship($relname, $rel_info) -sub relationships { - shift->result_source->relationships(@_); -} - -sub relationship_info { - shift->result_source->relationship_info(@_); -} +Registers a relationship on the class -sub resolve_condition { - my ($self, $cond, $attrs) = @_; - if (ref $cond eq 'HASH') { - my %ret; - foreach my $key (keys %$cond) { - my $val = $cond->{$key}; - if (ref $val) { - $self->throw("Can't handle this yet :("); - } else { - $ret{$self->_cond_key($attrs => $key)} - = $self->_cond_value($attrs => $key => $val); - } - } - return \%ret; - } else { - $self->throw("Can't handle this yet :("); - } -} - -sub _cond_key { - my ($self, $attrs, $key, $alias) = @_; - my $action = $attrs->{_action} || ''; - if ($action eq 'convert') { - unless ($key =~ s/^foreign\.//) { - $self->throw("Unable to convert relationship to WHERE clause: invalid key ${key}"); - } - if (defined (my $alias = $attrs->{_aliases}{foreign})) { - return "${alias}.${key}"; - } else { - return $key; - } - } elsif ($action eq 'join') { - return $key unless $key =~ /\./; - my ($type, $field) = split(/\./, $key); - if (my $alias = $attrs->{_aliases}{$type}) { - my $class = $attrs->{_classes}{$alias}; - $self->throw("Unknown column $field on $class as $alias") - unless $class->has_column($field); - return join('.', $alias, $field); - } else { - $self->throw( "Unable to resolve type ${type}: only have aliases for ". - join(', ', keys %{$attrs->{_aliases} || {}}) ); - } - } - return $self->next::method($attrs, $key); -} +=cut -sub _cond_value { - my ($self, $attrs, $key, $value) = @_; - my $action = $attrs->{_action} || ''; - if ($action eq 'convert') { - unless ($value =~ s/^self\.//) { - $self->throw( "Unable to convert relationship to WHERE clause: invalid value ${value}" ); - } - unless ($self->has_column($value)) { - $self->throw( "Unable to convert relationship to WHERE clause: no such accessor ${value}" ); - } - return $self->get_column($value); - } elsif ($action eq 'join') { - return $key unless $key =~ /\./; - my ($type, $field) = split(/\./, $value); - if (my $alias = $attrs->{_aliases}{$type}) { - my $class = $attrs->{_classes}{$alias}; - $self->throw("Unknown column $field on $class as $alias") - unless $class->has_column($field); - return join('.', $alias, $field); - } else { - $self->throw( "Unable to resolve type ${type}: only have aliases for ". - join(', ', keys %{$attrs->{_aliases} || {}}) ); - } - } - - return $self->next::method($attrs, $key, $value) -} +sub register_relationship { } =head2 search_related @@ -167,31 +86,19 @@ sub _cond_value { sub search_related { my $self = shift; my $rel = shift; - my $attrs = { }; - if (@_ > 1 && ref $_[$#_] eq 'HASH') { - $attrs = { %{ pop(@_) } }; + my $rs = $self->related_resultset($rel); + if( @_ ) { + return $rs->search(@_); + } + else { + # search() returns a new resultset, so related_resultsets would be lost + return wantarray ? $rs->all : $rs; } - my $rel_obj = $self->relationship_info($rel); - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} }; - - $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); - my $query = ((@_ > 1) ? {@_} : shift); - - $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something - # to merge into the AST really? - my ($cond) = $self->resolve_condition($rel_obj->{cond}, $attrs); - $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); - #use Data::Dumper; warn Dumper($query); - #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); - delete $attrs->{_action}; - return $self->result_source->schema->resultset($rel_obj->{class} - )->search($query, $attrs); } =head2 count_related - My::Table->count_related('relname', $cond, $attrs); + $obj->count_related('relname', $cond, $attrs); =cut @@ -207,9 +114,11 @@ sub count_related { =cut sub create_related { - my $class = shift; + my $self = shift; my $rel = shift; - return $class->search_related($rel)->create(@_); + my $obj = $self->search_related($rel)->create(@_); + delete $self->{related_resultsets}->{$rel}; + return $obj; } =head2 new_related @@ -255,23 +164,17 @@ sub find_or_create_related { sub set_from_related { my ($self, $rel, $f_obj) = @_; my $rel_obj = $self->relationship_info($rel); - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; + $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj; my $cond = $rel_obj->{cond}; - $self->throw( "set_from_related can only handle a hash condition; the " + $self->throw_exception( "set_from_related can only handle a hash condition; the " ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar')) unless ref $cond eq 'HASH'; my $f_class = $self->result_source->schema->class($rel_obj->{class}); - $self->throw( "Object $f_obj isn't a ".$f_class ) + $self->throw_exception( "Object $f_obj isn't a ".$f_class ) unless $f_obj->isa($f_class); - foreach my $key (keys %$cond) { - next if ref $cond->{$key}; # Skip literals and complex conditions - $self->throw("set_from_related can't handle $key as key") - unless $key =~ m/^foreign\.([^\.]+)$/; - my $val = $f_obj->get_column($1); - $self->throw("set_from_related can't handle ".$cond->{$key}." as value") - unless $cond->{$key} =~ m/^self\.([^\.]+)$/; - $self->set_column($1 => $val); - } + $self->set_columns( + $self->result_source->resolve_condition( + $rel_obj->{cond}, $f_obj, $rel)); return 1; } @@ -295,11 +198,72 @@ sub update_from_related { sub delete_related { my $self = shift; - return $self->search_related(@_)->delete; + my $obj = $self->search_related(@_)->delete; + delete $self->{related_resultsets}->{$_[0]}; + return $obj; } 1; +=head2 related_resultset($name) + +Returns a L for the relationship named $name. + + $rs = My::Table->related_resultset('related_table'); + +=cut + +sub related_resultset { + my $self = shift; + $self->throw_exception("Can't call *_related as class methods") unless ref $self; + my $rel = shift; + $self->{related_resultsets} ||= {}; + #use Data::Dumper; warn "related_resultsets: ", Dumper $self->{related_resultsets}; + my $resultsets = $self->{related_resultsets}; + if( !exists $resultsets->{$rel} ) { + + #warn "creating related resultset for relation '$rel'", \$self; + my $source = $self->result_source; + # if relation exists but resultset doesn't, create the resultset + + my $attrs = { }; + if (@_ > 1 && ref $_[$#_] eq 'HASH') { + $attrs = { %{ pop(@_) } }; + } + + my $rel_obj = $self->relationship_info($rel); + $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj; + $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} }; + + $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); + my $query = ((@_ > 1) ? {@_} : shift); + + my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self); + if (ref $cond eq 'ARRAY') { + $cond = [ map { my %hash; + foreach my $key (keys %{$_}) { + unless ($key =~ m/\./) { + $hash{"me.$key"} = $_->{$key}; + } else { + $hash{$key} = $_->{$key}; + } + }; \%hash; } @$cond ]; + } else { + foreach my $key (keys %$cond) { + unless ($key =~ m/\./) { + $cond->{"me.$key"} = delete $cond->{$key}; + } + } + } + $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); + #use Data::Dumper; warn Dumper($cond); + #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); + $resultsets->{$rel} = + $self->result_source->related_source($rel)->resultset->search($query, $attrs); + } + return $resultsets->{$rel}; +} + =head1 AUTHORS Matt S. Trout