File | /wise/base/static/lib/perl5/site_perl/5.10.0/DBIx/Class/Relationship/BelongsTo.pm | Statements Executed | 7 | Total Time | 0.000444 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine | |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | DBIx::Class::Relationship::BelongsTo:: | BEGIN |
0 | 0 | 0 | 0 | 0 | DBIx::Class::Relationship::BelongsTo:: | belongs_to |
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | package # hide from PAUSE | |||
2 | DBIx::Class::Relationship::BelongsTo; | |||
3 | ||||
4 | # Documentation for these methods can be found in | |||
5 | # DBIx::Class::Relationship | |||
6 | ||||
7 | 3 | 3.5e-5 | 1.2e-5 | use strict; # spent 10µs making 1 call to strict::import |
8 | 3 | 0.00041 | 0.00014 | use warnings; # spent 19µs making 1 call to warnings::import |
9 | ||||
10 | sub belongs_to { | |||
11 | my ($class, $rel, $f_class, $cond, $attrs) = @_; | |||
12 | # no join condition or just a column name | |||
13 | if (!ref $cond) { | |||
14 | $class->ensure_class_loaded($f_class); | |||
15 | my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns }; | |||
16 | $class->throw_exception( | |||
17 | "Can't infer join condition for ${rel} on ${class}; ". | |||
18 | "unable to load ${f_class}: $@" | |||
19 | ) if $@; | |||
20 | ||||
21 | my ($pri, $too_many) = keys %f_primaries; | |||
22 | $class->throw_exception( | |||
23 | "Can't infer join condition for ${rel} on ${class}; ". | |||
24 | "${f_class} has no primary keys" | |||
25 | ) unless defined $pri; | |||
26 | $class->throw_exception( | |||
27 | "Can't infer join condition for ${rel} on ${class}; ". | |||
28 | "${f_class} has multiple primary keys" | |||
29 | ) if $too_many; | |||
30 | ||||
31 | my $fk = defined $cond ? $cond : $rel; | |||
32 | $class->throw_exception( | |||
33 | "Can't infer join condition for ${rel} on ${class}; ". | |||
34 | "$fk is not a column of $class" | |||
35 | ) unless $class->has_column($fk); | |||
36 | ||||
37 | my $acc_type = $class->has_column($rel) ? 'filter' : 'single'; | |||
38 | $class->add_relationship($rel, $f_class, | |||
39 | { "foreign.${pri}" => "self.${fk}" }, | |||
40 | { accessor => $acc_type, %{$attrs || {}} } | |||
41 | ); | |||
42 | } | |||
43 | # explicit join condition | |||
44 | elsif (ref $cond) { | |||
45 | if (ref $cond eq 'HASH') { # ARRAY is also valid | |||
46 | my $cond_rel; | |||
47 | for (keys %$cond) { | |||
48 | if (m/\./) { # Explicit join condition | |||
49 | $cond_rel = $cond; | |||
50 | last; | |||
51 | } | |||
52 | $cond_rel->{"foreign.$_"} = "self.".$cond->{$_}; | |||
53 | } | |||
54 | $cond = $cond_rel; | |||
55 | } | |||
56 | my $acc_type = ((ref $cond eq 'HASH') | |||
57 | && keys %$cond == 1 | |||
58 | && $class->has_column($rel)) | |||
59 | ? 'filter' | |||
60 | : 'single'; | |||
61 | $class->add_relationship($rel, $f_class, | |||
62 | $cond, | |||
63 | { accessor => $acc_type, %{$attrs || {}} } | |||
64 | ); | |||
65 | } | |||
66 | else { | |||
67 | $class->throw_exception( | |||
68 | 'third argument for belongs_to must be undef, a column name, '. | |||
69 | 'or a join condition' | |||
70 | ); | |||
71 | } | |||
72 | return 1; | |||
73 | } | |||
74 | ||||
75 | =head1 AUTHORS | |||
76 | ||||
77 | Alexander Hartmaier <Alexander.Hartmaier@t-systems.at> | |||
78 | ||||
79 | Matt S. Trout <mst@shadowcatsystems.co.uk> | |||
80 | ||||
81 | =cut | |||
82 | ||||
83 | 1 | 3.0e-6 | 3.0e-6 | 1; |