use DBIx::Class::Carp;
use DBIx::Class::_Util 'UNRESOLVABLE_CONDITION';
+use SQL::Abstract 'is_literal_value';
use Devel::GlobalDestruction;
use Try::Tiny;
use List::Util 'first';
}
}
- $self->throw_exception('No practical way to resolve a relationship between two structures')
- if $is_objlike[0] and $is_objlike[1];
-
my $args = {
condition => $cond,
# where-is-waldo block guesses relname, then further down we override it if available
(
- $is_objlike[1] ? ( rel_name => $res_args[0], self_alias => $res_args[0], foreign_alias => 'me', self_resultobj => $res_args[1] )
- : $is_objlike[0] ? ( rel_name => $res_args[1], self_alias => 'me', foreign_alias => $res_args[1], foreign_resultobj => $res_args[0] )
+ $is_objlike[1] ? ( rel_name => $res_args[0], self_alias => $res_args[0], foreign_alias => 'me', self_result_object => $res_args[1] )
+ : $is_objlike[0] ? ( rel_name => $res_args[1], self_alias => 'me', foreign_alias => $res_args[1], foreign_result_object => $res_args[0] )
: ( rel_name => $res_args[0], self_alias => $res_args[1], foreign_alias => $res_args[0] )
),
## self-explanatory API, modeled on the custom cond coderef:
# rel_name
# foreign_alias
-# foreign_resultobj
+# foreign_result_object
# self_alias
-# self_resultobj
+# self_result_object
# require_join_free_condition
# infer_values_based_on (optional, mandatory hashref argument)
# condition (optional, derived from $self->rel_info(rel_name))
#
## returns a hash
# condition
-# join_free_condition (maybe undef)
-# inferred_values (maybe undef, always complete or empty)
+# identity_map
+# join_free_condition (maybe unset)
+# inferred_values (always either complete or unset)
#
sub _resolve_relationship_condition {
my $self = shift;
my $args = { ref $_[0] eq 'HASH' ? %{ $_[0] } : @_ };
for ( qw( rel_name self_alias foreign_alias ) ) {
- $self->throw_exception("Mandatory argument '$_' is not a plain string")
+ $self->throw_exception("Mandatory argument '$_' to _resolve_relationship_condition() is not a plain string")
if !defined $args->{$_} or length ref $args->{$_};
}
- $self->throw_exception('No practical way to resolve a relationship between two objects')
- if defined $args->{self_resultobj} and defined $args->{foreign_resultobj};
+ $self->throw_exception("Arguments 'self_alias' and 'foreign_alias' may not be identical")
+ if $args->{self_alias} eq $args->{foreign_alias};
- my $rel_info = $self->relationship_info($args->{rel_name});
- # or $self->throw_exception( "No such relationship '$args->{rel_name}'" );
+ my $exception_rel_id = "relationship '$args->{rel_name}' on source '@{[ $self->source_name ]}'";
- $self->throw_exception( "Object '$args->{foreign_resultobj}' must be of class '$rel_info->{class}'" )
- if defined blessed $args->{foreign_resultobj} and ! $args->{foreign_resultobj}->isa($rel_info->{class});
+ my $rel_info = $self->relationship_info($args->{rel_name})
+ or $self->throw_exception( "No such $exception_rel_id" );
- $args->{condition} ||= $rel_info->{cond};
+ $self->throw_exception("No practical way to resolve $exception_rel_id between two data structures")
+ if defined $args->{self_result_object} and defined $args->{foreign_result_object};
$self->throw_exception( "Argument to infer_values_based_on must be a hash" )
if exists $args->{infer_values_based_on} and ref $args->{infer_values_based_on} ne 'HASH';
$args->{require_join_free_condition} ||= !!$args->{infer_values_based_on};
+ $args->{condition} ||= $rel_info->{cond};
+
+ if (exists $args->{self_result_object}) {
+ if (defined blessed $args->{self_result_object}) {
+ $self->throw_exception( "Object '$args->{self_result_object}' must be of class '@{[ $self->result_class ]}'" )
+ unless $args->{self_result_object}->isa($self->result_class);
+ }
+ else {
+ $args->{self_result_object} = DBIx::Class::Core->new({
+ -result_source => $self,
+ %{ $args->{self_result_object}||{} }
+ });
+ }
+ }
+
+ if (exists $args->{foreign_result_object}) {
+ if (defined blessed $args->{foreign_result_object}) {
+ $self->throw_exception( "Object '$args->{foreign_result_object}' must be of class '$rel_info->{class}'" )
+ unless $args->{foreign_result_object}->isa($rel_info->{class});
+ }
+ else {
+ $args->{foreign_result_object} = DBIx::Class::Core->new({
+ -result_source => $self->related_source($args->{rel_name}),
+ %{ $args->{foreign_result_object}||{} }
+ });
+ }
+ }
+
my $ret;
if (ref $args->{condition} eq 'CODE') {
foreign_alias => $args->{foreign_alias},
( map
{ (exists $args->{$_}) ? ( $_ => $args->{$_} ) : () }
- qw( self_resultobj foreign_resultobj )
+ qw( self_result_object foreign_result_object )
),
};
# legacy - never remove these!!!
$cref_args->{foreign_relname} = $cref_args->{rel_name};
- $cref_args->{self_rowobj} = $cref_args->{self_resultobj}
- if exists $cref_args->{self_resultobj};
+ $cref_args->{self_rowobj} = $cref_args->{self_result_object}
+ if exists $cref_args->{self_result_object};
($ret->{condition}, $ret->{join_free_condition}, my @extra) = $args->{condition}->($cref_args);
if (my $jfc = $ret->{join_free_condition}) {
$self->throw_exception (
- "The join-free condition returned for relationship '$args->{rel_name}' must be a hash reference"
+ "The join-free condition returned for $exception_rel_id must be a hash reference"
) unless ref $jfc eq 'HASH';
my ($joinfree_alias, $joinfree_source);
- if (defined $args->{self_resultobj}) {
+ if (defined $args->{self_result_object}) {
$joinfree_alias = $args->{foreign_alias};
$joinfree_source = $self->related_source($args->{rel_name});
}
- elsif (defined $args->{foreign_resultobj}) {
+ elsif (defined $args->{foreign_result_object}) {
$joinfree_alias = $args->{self_alias};
$joinfree_source = $self;
}
# FIXME sanity check until things stabilize, remove at some point
$self->throw_exception (
- "A join-free condition returned for relationship '$args->{rel_name}' without a result object to chain from"
+ "A join-free condition returned for $exception_rel_id without a result object to chain from"
) unless $joinfree_alias;
my $fq_col_list = { map
};
$fq_col_list->{$_} or $self->throw_exception (
- "The join-free condition returned for relationship '$args->{rel_name}' may only "
+ "The join-free condition returned for $exception_rel_id may only "
. 'contain keys that are fully qualified column names of the corresponding source'
) for keys %$jfc;
push @l_cols, $lc;
}
- # construct the crosstable condition
- $ret->{condition} = { map
- {( "$args->{foreign_alias}.$f_cols[$_]" => { -ident => "$args->{self_alias}.$l_cols[$_]" } )}
- (0..$#f_cols)
+ # construct the crosstable condition and the identity map
+ for (0..$#f_cols) {
+ $ret->{condition}{"$args->{foreign_alias}.$f_cols[$_]"} = { -ident => "$args->{self_alias}.$l_cols[$_]" };
+ $ret->{identity_map}{$l_cols[$_]} = $f_cols[$_];
};
- if (exists $args->{self_resultobj} or exists $args->{foreign_resultobj}) {
+ if (exists $args->{self_result_object} or exists $args->{foreign_result_object}) {
- my ($obj, $obj_alias, $plain_alias, $obj_cols, $plain_cols) = defined $args->{self_resultobj}
- ? ( @{$args}{qw( self_resultobj self_alias foreign_alias )}, \@l_cols, \@f_cols )
- : ( @{$args}{qw( foreign_resultobj foreign_alias self_alias )}, \@f_cols, \@l_cols )
+ my ($obj, $obj_alias, $plain_alias, $obj_cols, $plain_cols) = defined $args->{self_result_object}
+ ? ( @{$args}{qw( self_result_object self_alias foreign_alias )}, \@l_cols, \@f_cols )
+ : ( @{$args}{qw( foreign_result_object foreign_alias self_alias )}, \@f_cols, \@l_cols )
;
for my $i (0..$#$obj_cols) {
- # FIXME - temp shim
- if (! blessed $obj) {
- $ret->{join_free_condition}{"$plain_alias.$plain_cols->[$i]"} = $obj->{$obj_cols->[$i]};
- }
- elsif (
- defined $args->{self_resultobj}
+ if (
+ defined $args->{self_result_object}
and
! $obj->has_column_loaded($obj_cols->[$i])
) {
}
}
else {
- $self->throw_exception ("Can't handle condition $args->{condition} for relationship '$args->{rel_name}' yet :(");
+ $self->throw_exception ("Can't handle condition $args->{condition} for $exception_rel_id yet :(");
}
- $self->throw_exception("Relationship '$args->{rel_name}' does not resolve to a join-free condition fragment") if (
+ $self->throw_exception(ucfirst "$exception_rel_id does not resolve to a join-free condition fragment") if (
$args->{require_join_free_condition}
and
( ! $ret->{join_free_condition} or $ret->{join_free_condition} eq UNRESOLVABLE_CONDITION )
if ($args->{infer_values_based_on}) {
$self->throw_exception(sprintf (
- "Unable to complete value inferrence - custom relationship '%s' returns conditions instead of values for column(s): %s",
- $args->{rel_name},
+ "Unable to complete value inferrence - custom $exception_rel_id returns conditions instead of values for column(s): %s",
map { "'$_'" } @nonvalues
)) if @nonvalues;
for keys %{$args->{infer_values_based_on}};
}
+ # add the identities based on the main condition
+ # (may already be there, since easy to calculate on the fly in the HASH case)
+ if ( ! $ret->{identity_map} ) {
+
+ my $col_eqs = $self->schema->storage->_extract_fixed_condition_columns($ret->{condition});
+
+ my $colinfos;
+ for my $lhs (keys %$col_eqs) {
+
+ next if $col_eqs->{$lhs} eq UNRESOLVABLE_CONDITION;
+ my ($rhs) = @{ is_literal_value( $ret->{condition}{$lhs} ) || next };
+
+ # there is no way to know who is right and who is left
+ # therefore the ugly scan below
+ $colinfos ||= $self->schema->storage->_resolve_column_info([
+ { -alias => $args->{self_alias}, -rsrc => $self },
+ { -alias => $args->{foreign_alias}, -rsrc => $self->related_source($args->{rel_name}) },
+ ]);
+
+ my ($l_col, $l_alias, $r_col, $r_alias) = map {
+ ( reverse $_ =~ / ^ (?: ([^\.]+) $ | ([^\.]+) \. (.+) ) /x )[0,1]
+ } ($lhs, $rhs);
+
+ if (
+ $colinfos->{$l_col}
+ and
+ $colinfos->{$r_col}
+ and
+ $colinfos->{$l_col}{-source_alias} ne $colinfos->{$r_col}{-source_alias}
+ ) {
+ ( $colinfos->{$l_col}{-source_alias} eq $args->{self_alias} )
+ ? ( $ret->{identity_map}{$l_col} = $r_col )
+ : ( $ret->{identity_map}{$r_col} = $l_col )
+ ;
+ }
+ }
+ }
+
$ret
}