Test for an insane yet valid rel condition with subquery
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / BelongsTo.pm
CommitLineData
9b83fccd 1package # hide from PAUSE
2 DBIx::Class::Relationship::BelongsTo;
3
4# Documentation for these methods can be found in
5# DBIx::Class::Relationship
07037f89 6
7use strict;
8use warnings;
ed7ab0f4 9use Try::Tiny;
fd323bf1 10use namespace::clean;
07037f89 11
fd323bf1 12our %_pod_inherit_config =
044e70c7 13 (
14 class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
15 );
16
07037f89 17sub belongs_to {
18 my ($class, $rel, $f_class, $cond, $attrs) = @_;
6bf6ba2f 19
20 # assume a foreign key contraint unless defined otherwise
fd323bf1 21 $attrs->{is_foreign_key_constraint} = 1
6bf6ba2f 22 if not exists $attrs->{is_foreign_key_constraint};
cef1bdda 23 $attrs->{undef_on_null_fk} = 1
24 if not exists $attrs->{undef_on_null_fk};
6bf6ba2f 25
aeb1bf75 26 # no join condition or just a column name
7b601771 27 if (!ref $cond) {
fd4df975 28 $class->ensure_class_loaded($f_class);
ed7ab0f4 29 my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols }
30 catch {
31 $class->throw_exception( "Can't infer join condition for ${rel} on ${class}: $_");
32 };
1e3bc087 33
34 my ($pri, $too_many) = keys %f_primaries;
fd4df975 35 $class->throw_exception(
36 "Can't infer join condition for ${rel} on ${class}; ".
fd4df975 37 "${f_class} has multiple primary keys"
38 ) if $too_many;
1e3bc087 39
40 my $fk = defined $cond ? $cond : $rel;
fd4df975 41 $class->throw_exception(
42 "Can't infer join condition for ${rel} on ${class}; ".
43 "$fk is not a column of $class"
44 ) unless $class->has_column($fk);
1e3bc087 45
35f5c265 46 $cond = { "foreign.${pri}" => "self.${fk}" };
47
07037f89 48 }
aeb1bf75 49 # explicit join condition
4ff1b819 50 elsif (ref $cond) {
51 if (ref $cond eq 'HASH') { # ARRAY is also valid
52 my $cond_rel;
53 for (keys %$cond) {
54 if (m/\./) { # Explicit join condition
55 $cond_rel = $cond;
56 last;
57 }
58 $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
99be059e 59 }
4ff1b819 60 $cond = $cond_rel;
07037f89 61 }
07037f89 62 }
35f5c265 63 # dunno
7b601771 64 else {
fd4df975 65 $class->throw_exception(
66 'third argument for belongs_to must be undef, a column name, '.
67 'or a join condition'
68 );
7b601771 69 }
35f5c265 70
71 my $acc_type = (
72 ref $cond eq 'HASH'
73 and
74 keys %$cond == 1
75 and
76 $class->has_column($rel)
77 ) ? 'filter' : 'single';
78
79 my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
80 ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
81 : undef
82 ;
83
84 $class->add_relationship($rel, $f_class,
85 $cond,
86 {
87 accessor => $acc_type,
88 $fk_columns ? ( fk_columns => $fk_columns ) : (),
89 %{$attrs || {}}
90 }
91 );
92
07037f89 93 return 1;
94}
95
e09b8a14 96# Attempt to remove the POD so it (maybe) falls off the indexer
503536d5 97
e09b8a14 98#=head1 AUTHORS
99#
100#Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
101#
102#Matt S. Trout <mst@shadowcatsystems.co.uk>
103#
104#=cut
503536d5 105
07037f89 1061;