Improved join condition possiblities - arrayrefs of hashrefs now work for OR
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource.pm
index fc2c929..285d10f 100644 (file)
@@ -79,8 +79,8 @@ sub resultset {
   return $self->resultset_class->new($self);
 }
 
-=head2 has_column                                                                
-                                                                                
+=head2 has_column
+
   if ($obj->has_column($col)) { ... }                                           
                                                                                 
 Returns 1 if the source has a column of this name, 0 otherwise.
@@ -242,6 +242,8 @@ sub add_relationship {
 
   return 1;
 
+  # XXX disabled. doesn't work properly currently. skip in tests.
+
   my $f_source = $self->schema->source($f_source_name);
   unless ($f_source) {
     eval "require $f_source_name;";
@@ -287,6 +289,17 @@ sub relationship_info {
   return $self->_relationships->{$rel};
 } 
 
+=head2 has_relationship($rel)
+
+Returns 1 if the source has a relationship of this name, 0 otherwise.
+                                                                                
+=cut                                                                            
+
+sub has_relationship {
+  my ($self, $rel) = @_;
+  return exists $self->_relationships->{$rel};
+}
+
 =head2 resolve_join($relation)
 
 Returns the join structure required for the related result source
@@ -304,19 +317,49 @@ sub resolve_join {
   } elsif (ref $join) {
     die("No idea how to resolve join reftype ".ref $join);
   } else {
-    my $rel_obj = $self->relationship_info($join);
-    #use Data::Dumper; warn Dumper($class->result_source) unless $rel_obj;
-    die("No such relationship ${join}") unless $rel_obj;
-    my $j_class = $self->related_source($join)->result_class;
-    my %join = (_action => 'join',
-         _aliases => { 'self' => $alias, 'foreign' => $join },
-         _classes => { $alias => $self->result_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 ];
+    my $rel_info = $self->relationship_info($join);
+    die("No such relationship ${join}") unless $rel_info;
+    my $type = $rel_info->{attrs}{join_type} || '';
+    return [ { $join => $self->related_source($join)->from,
+               -join_type => $type },
+             $self->resolve_condition($rel_info->{cond}, $join, $alias) ];
   }
 }
 
+=head2 resolve_condition($cond, $rel, $alias|$object)
+
+Resolves the passed condition to a concrete query fragment. If given an alias,
+returns a join condition; if given an object, inverts that object to produce
+a related conditional from that object.
+
+=cut
+
+sub resolve_condition {
+  my ($self, $cond, $rel, $for) = @_;
+  #warn %$cond;
+  if (ref $cond eq 'HASH') {
+    my %ret;
+    while (my ($k, $v) = each %{$cond}) {
+      # XXX should probably check these are valid columns
+      $k =~ s/^foreign\.// || die "Invalid rel cond key ${k}";
+      $v =~ s/^self\.// || die "Invalid rel cond val ${v}";
+      if (ref $for) { # Object
+        #warn "$self $k $for $v";
+        $ret{$k} = $for->get_column($v);
+        #warn %ret;
+      } else {
+        $ret{"${rel}.${k}"} = "${for}.${v}";
+      }
+    }
+    return \%ret;
+  } elsif (ref $cond eq 'ARRAY') {
+    return [ map { $self->resolve_condition($_, $rel, $for) } @$cond ];
+  } else {
+   die("Can't handle this yet :(");
+  }
+}
+
+
 =head2 related_source($relname)
 
 Returns the result source for the given relationship