changes in 0.05 dist that mst forgot to commit
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Base.pm
index 0986be8..ddc11b5 100644 (file)
@@ -51,7 +51,7 @@ command immediately before C<JOIN>.
 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,130 +69,13 @@ created, which calls C<create_related> for the relationship.
 
 =back
 
-=cut
-
-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;";
-  if ($@) {
-    $class->throw($@) unless $@ =~ /Can't locate/;
-  }
-  my %rels = %{ $class->_relationships };
-  $rels{$rel} = { class => $f_class,
-                  cond  => $cond,
-                  attrs => $attrs };
-  $class->_relationships(\%rels);
-
-  return unless eval { $f_class->can('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;
-}
-
-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 ];
-  }
-}
+=head2 register_relationship($relname, $rel_info)
 
-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 :(");
-  }
-}
+Registers a relationship on the class
 
-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
 
@@ -202,32 +85,30 @@ sub _cond_value {
 
 sub search_related {
   my $self = shift;
+  die "Can't call *_related as class methods" unless ref $self;
   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;
+  my $rel_obj = $self->relationship_info($rel);
+  $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
   $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
 
-  $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
+  $self->throw_exception( "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);
+  my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self);
   $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
-  #use Data::Dumper; warn Dumper($query);
+  #use Data::Dumper; warn Dumper($cond);
   #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]});
-  delete $attrs->{_action};
-  return $self->resolve_class($rel_obj->{class}
-           )->search($query, $attrs);
+  return $self->result_source->related_source($rel
+           )->resultset->search($query, $attrs);
 }
 
 =head2 count_related
 
-  My::Table->count_related('relname', $cond, $attrs);
+  $obj->count_related('relname', $cond, $attrs);
 
 =cut
 
@@ -243,8 +124,9 @@ sub count_related {
 =cut
 
 sub create_related {
-  my $class = shift;
-  return $class->new_related(@_)->insert;
+  my $self = shift;
+  my $rel = shift;
+  return $self->search_related($rel)->create(@_);
 }
 
 =head2 new_related
@@ -255,20 +137,7 @@ 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);
 }
 
 =head2 find_related
@@ -280,17 +149,7 @@ 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(@_);
 }
 
 =head2 find_or_create_related
@@ -312,21 +171,21 @@ 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")
+    $self->throw_exception("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")
+    $self->throw_exception("set_from_related can't handle ".$cond->{$key}." as value")
       unless $cond->{$key} =~ m/^self\.([^\.]+)$/;
     $self->set_column($1 => $val);
   }