File: BelongsTo.pm

package info (click to toggle)
libdbix-class-perl 0.08196-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,424 kB
  • sloc: perl: 22,328; sql: 362; makefile: 10
file content (106 lines) | stat: -rw-r--r-- 2,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package # hide from PAUSE
    DBIx::Class::Relationship::BelongsTo;

# Documentation for these methods can be found in
# DBIx::Class::Relationship

use strict;
use warnings;
use Try::Tiny;
use namespace::clean;

our %_pod_inherit_config =
  (
   class_map => { 'DBIx::Class::Relationship::BelongsTo' => 'DBIx::Class::Relationship' }
  );

sub belongs_to {
  my ($class, $rel, $f_class, $cond, $attrs) = @_;

  # assume a foreign key contraint unless defined otherwise
  $attrs->{is_foreign_key_constraint} = 1
    if not exists $attrs->{is_foreign_key_constraint};
  $attrs->{undef_on_null_fk} = 1
    if not exists $attrs->{undef_on_null_fk};

  # no join condition or just a column name
  if (!ref $cond) {
    $class->ensure_class_loaded($f_class);
    my %f_primaries = map { $_ => 1 } try { $f_class->_pri_cols }
      catch {
        $class->throw_exception( "Can't infer join condition for ${rel} on ${class}: $_");
      };

    my ($pri, $too_many) = keys %f_primaries;
    $class->throw_exception(
      "Can't infer join condition for ${rel} on ${class}; ".
      "${f_class} has multiple primary keys"
    ) if $too_many;

    my $fk = defined $cond ? $cond : $rel;
    $class->throw_exception(
      "Can't infer join condition for ${rel} on ${class}; ".
      "$fk is not a column of $class"
    ) unless $class->has_column($fk);

    $cond = { "foreign.${pri}" => "self.${fk}" };

  }
  # explicit join condition
  elsif (ref $cond) {
    if (ref $cond eq 'HASH') { # ARRAY is also valid
      my $cond_rel;
      for (keys %$cond) {
        if (m/\./) { # Explicit join condition
          $cond_rel = $cond;
          last;
        }
        $cond_rel->{"foreign.$_"} = "self.".$cond->{$_};
      }
      $cond = $cond_rel;
    }
  }
  # dunno
  else {
    $class->throw_exception(
      'third argument for belongs_to must be undef, a column name, '.
      'or a join condition'
    );
  }

  my $acc_type = (
    ref $cond eq 'HASH'
      and
    keys %$cond == 1
      and
    $class->has_column($rel)
  ) ? 'filter' : 'single';

  my $fk_columns = ($acc_type eq 'single' and ref $cond eq 'HASH')
    ? { map { $_ =~ /^self\.(.+)/ ? ( $1 => 1 ) : () } (values %$cond ) }
    : undef
  ;

  $class->add_relationship($rel, $f_class,
    $cond,
    {
      accessor => $acc_type,
      $fk_columns ? ( fk_columns => $fk_columns ) : (),
      %{$attrs || {}}
    }
  );

  return 1;
}

# Attempt to remove the POD so it (maybe) falls off the indexer

#=head1 AUTHORS
#
#Alexander Hartmaier <Alexander.Hartmaier@t-systems.at>
#
#Matt S. Trout <mst@shadowcatsystems.co.uk>
#
#=cut

1;