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=27479955522da5769a23937314e0119ec1273e2f;hpb=8918977e98800ece6e7eefcf292d7512138ad48d;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 2747995..8925121 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -3,7 +3,7 @@ package DBIx::Class::Relationship::Base; use strict; use warnings; -use base qw/Class::Data::Inheritable/; +use base qw/DBIx::Class/; __PACKAGE__->mk_classdata('_relationships', { } ); @@ -21,141 +21,63 @@ on searches. =head1 METHODS -=over 4 - -=item add_relationship +=head2 add_relationship __PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs); The condition needs to be an SQL::Abstract-style representation of the -join between the tables - for example if you're creating a rel from Foo to Bar +join between the tables. For example, if you're creating a rel from Foo to Bar, { 'foreign.foo_id' => 'self.id' } -will result in a JOIN clause like +will result in the JOIN clause foo me JOIN bar bar ON bar.foo_id = me.id -=cut +You can specify as many foreign => self mappings as necessary. -sub add_relationship { - my ($class, $rel, $f_class, $cond, $attrs) = @_; - die "Can't create relationship without join condition" unless $cond; - $attrs ||= {}; - eval "require $f_class;"; - my %rels = %{ $class->_relationships }; - $rels{$rel} = { class => $f_class, - cond => $cond, - attrs => $attrs }; - $class->_relationships(\%rels); - #warn %{$f_class->_columns}; - - return unless eval { %{$f_class->_columns}; }; # Foreign class not loaded - eval { $class->_resolve_join($rel, 'me') }; - - if ($@) { # If the resolve failed, back out and re-throw the error - delete $rels{$rel}; # - $class->_relationships(\%rels); - $class->throw("Error creating relationship $rel: $@"); - } - 1; -} +Valid attributes are as follows: -sub _resolve_join { - my ($class, $join, $alias) = @_; - if (ref $join eq 'ARRAY') { - return map { $class->_resolve_join($_, $alias) } @$join; - } elsif (ref $join eq 'HASH') { - return map { $class->_resolve_join($_, $alias), - $class->_relationships->{$_}{class}->_resolve_join($join->{$_}, $_) } - keys %$join; - } elsif (ref $join) { - $class->throw("No idea how to resolve join reftype ".ref $join); - } else { - my $rel_obj = $class->_relationships->{$join}; - $class->throw("No such relationship ${join}") unless $rel_obj; - my $j_class = $rel_obj->{class}; - my %join = (_action => 'join', - _aliases => { 'self' => $alias, 'foreign' => $join }, - _classes => { $alias => $class, $join => $j_class }); - my $j_cond = $j_class->resolve_condition($rel_obj->{cond}, \%join); - return [ { $join => $j_class->_table_name, - -join_type => $rel_obj->{attrs}{join_type} || '' }, $j_cond ]; - } -} +=over 4 -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 :("); - } -} +=item join_type -sub _cond_key { - my ($self, $attrs, $key) = @_; - my $action = $attrs->{_action} || ''; - if ($action eq 'convert') { - unless ($key =~ s/^foreign\.//) { - $self->throw("Unable to convert relationship to WHERE clause: invalid key ${key}"); - } - 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 exists $class->_columns->{$field}; - return join('.', $alias, $field); - } else { - $self->throw( "Unable to resolve type ${type}: only have aliases for ". - join(', ', keys %{$attrs->{_aliases} || {}}) ); - } - } - return $self->NEXT::ACTUAL::_cond_key($attrs, $key); -} +Explicitly specifies the type of join to use in the relationship. Any SQL +join type is valid, e.g. C or C. It will be placed in the SQL +command immediately before C. -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->_columns->{$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 exists $class->_columns->{$field}; - return join('.', $alias, $field); - } else { - $self->throw( "Unable to resolve type ${type}: only have aliases for ". - join(', ', keys %{$attrs->{_aliases} || {}}) ); - } - } - - return $self->NEXT::ACTUAL::_cond_value($attrs, $key, $value) -} +=item proxy + +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/ ] }); + +Then, assuming Bar has an accessor named margle, you can do: + + my $obj = Foo->find(1); + $obj->margle(10); # set margle; Bar object is created if it doesn't exist + +=item accessor + +Specifies the type of accessor that should be created for the relationship. +Valid values are C (for when there is only a single related object), +C (when there can be many), and C (for when there is a single +related object, but you also want the relationship accessor to double as +a column accessor). For C accessors, an add_to_* method is also +created, which calls C for the relationship. + +=back + +=head2 register_relationship($relname, $rel_info) + +Registers a relationship on the class -=item search_related +=cut + +sub register_relationship { } + +=head2 search_related My::Table->search_related('relname', $cond, $attrs); @@ -163,58 +85,43 @@ sub _cond_value { sub search_related { my $self = shift; - return $self->_query_related('search', @_); + my $rel = shift; + 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; + } } -=item count_related +=head2 count_related - My::Table->count_related('relname', $cond, $attrs); + $obj->count_related('relname', $cond, $attrs); =cut sub count_related { my $self = shift; - return $self->_query_related('count', @_); + return $self->search_related(@_)->count; } -sub _query_related { - my $self = shift; - my $meth = shift; - my $rel = shift; - my $attrs = { }; - if (@_ > 1 && ref $_[$#_] eq 'HASH') { - $attrs = { %{ pop(@_) } }; - } - my $rel_obj = $self->_relationships->{$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->resolve_class($rel_obj->{class} - )->$meth($query, $attrs); -} - -=item create_related +=head2 create_related My::Table->create_related('relname', \%col_data); =cut sub create_related { - my $class = shift; - return $class->new_related(@_)->insert; + my $self = shift; + my $rel = shift; + my $obj = $self->search_related($rel)->create(@_); + delete $self->{related_resultsets}->{$rel}; + return $obj; } -=item new_related +=head2 new_related My::Table->new_related('relname', \%col_data); @@ -222,23 +129,10 @@ sub create_related { sub new_related { my ($self, $rel, $values, $attrs) = @_; - $self->throw( "Can't call new_related as class method" ) - unless ref $self; - $self->throw( "new_related needs a hash" ) - unless (ref $values eq 'HASH'); - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - $self->throw( "Can't abstract implicit create for ${rel}, condition not a hash" ) - unless ref $rel_obj->{cond} eq 'HASH'; - $attrs = { %{$rel_obj->{attrs}}, %{$attrs || {}}, _action => 'convert' }; - - my %fields = %{$self->resolve_condition($rel_obj->{cond},$attrs)}; - $fields{$_} = $values->{$_} for keys %$values; - - return $self->resolve_class($rel_obj->{class})->new(\%fields); + return $self->search_related($rel)->new($values, $attrs); } -=item find_related +=head2 find_related My::Table->find_related('relname', @pri_vals | \%pri_vals); @@ -247,20 +141,10 @@ sub new_related { sub find_related { my $self = shift; my $rel = shift; - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - my ($cond) = $self->resolve_condition($rel_obj->{cond}, { _action => 'convert' }); - $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1)); - my $attrs = { }; - if (@_ > 1 && ref $_[$#_] eq 'HASH') { - $attrs = { %{ pop(@_) } }; - } - my $query = ((@_ > 1) ? {@_} : shift); - $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); - return $self->resolve_class($rel_obj->{class})->find($query); + return $self->search_related($rel)->find(@_); } -=item find_or_create_related +=head2 find_or_create_related My::Table->find_or_create_related('relname', \%col_data); @@ -271,7 +155,7 @@ sub find_or_create_related { return $self->find_related(@_) || $self->create_related(@_); } -=item set_from_related +=head2 set_from_related My::Table->set_from_related('relname', $rel_obj); @@ -279,28 +163,22 @@ sub find_or_create_related { sub set_from_related { my ($self, $rel, $f_obj) = @_; - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; + my $rel_obj = $self->relationship_info($rel); + $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->resolve_class($rel_obj->{class}); - $self->throw( "Object $f_obj isn't a ".$f_class ) + my $f_class = $self->result_source->schema->class($rel_obj->{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; } -=item update_from_related +=head2 update_from_related My::Table->update_from_related('relname', $rel_obj); @@ -312,7 +190,7 @@ sub update_from_related { $self->update; } -=item delete_related +=head2 delete_related My::Table->delete_related('relname', $cond, $attrs); @@ -320,12 +198,71 @@ 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; -=back +=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