File Coverage

blib/lib/Class/DBI/Relationship/HasMany.pm
Criterion Covered Total %
statement 57 101 56.4
branch 13 44 29.5
condition 4 19 21.0
subroutine 11 15 73.3
pod 3 3 100.0
total 88 182 48.3


line stmt bran cond sub pod time code
1             package Class::DBI::Relationship::HasMany;
2              
3 4     4   23 use strict;
  4         8  
  4         145  
4 4     4   22 use warnings;
  4         7  
  4         107  
5              
6 4     4   21 use base 'Class::DBI::Relationship';
  4         6  
  4         1081  
7              
8             sub remap_arguments {
9 2     2 1 6 my ($proto, $class, $accessor, $f_class, $f_key, $args) = @_;
10              
11 2 50       19 return $class->_croak($class->name . " needs an accessor name")
12             unless $accessor;
13 2 50       7 return $class->_croak($class->name . " needs a foreign class")
14             unless $f_class;
15              
16             {
17 4     4   23 no strict 'refs';
  4         6  
  4         5943  
  2         2  
18 2 50       5 defined &{"$class\::$accessor"}
  2         66  
19             and return $class->_carp("$accessor method already exists in $class\n");
20             }
21              
22 2         6 my @f_method = ();
23 2 50       8 if (ref $f_class eq "ARRAY") {
24 0         0 ($f_class, @f_method) = @$f_class;
25             }
26 2         18 $class->_require_class($f_class);
27              
28 2 50       7 if (ref $f_key eq "HASH") { # didn't supply f_key, this is really $args
29 0         0 $args = $f_key;
30 0         0 $f_key = "";
31             }
32              
33 2   33     18 $f_key ||= do {
34 2         53 my $meta = $f_class->meta_info('has_a');
35 2         11 my ($col) = grep $meta->{$_}->foreign_class eq $class, keys %$meta;
36 2 100       24 $col || $class->table_alias;
37             };
38              
39 2 50       85 if (ref $f_key eq "ARRAY") {
40 0 0       0 return $class->_croak("Multi-column foreign keys not supported")
41             if @$f_key > 1;
42 0         0 $f_key = $f_key->[0];
43             }
44              
45 2   50     13 $args ||= {};
46 2         6 $args->{mapping} = \@f_method;
47 2         7 $args->{foreign_key} = $f_key;
48 2   33     13 $args->{order_by} ||= $args->{sort}; # deprecated 0.96
49 2 50       8 warn "sort argument to has_many deprecated in favour of order_by"
50             if $args->{sort}; # deprecated 0.96
51              
52 2         10 return ($class, $accessor, $f_class, $args);
53             }
54              
55             sub _set_up_class_data {
56 2     2   4 my $self = shift;
57 2         13 $self->class->_extend_class_data(
58             __hasa_list => $self->foreign_class => $self->args->{foreign_key});
59 2         72 $self->SUPER::_set_up_class_data;
60             }
61              
62             sub triggers {
63 2     2 1 5 my $self = shift;
64 2 50       8 if ($self->args->{no_cascade_delete}) { # old undocumented way
65 0         0 warn "no_cascade_delete deprecated in favour of cascade => None";
66 0         0 return;
67             }
68 2   50     22 my $strategy = $self->args->{cascade} || "Delete";
69 2 50       34 $strategy = "Class::DBI::Cascade::$strategy" unless $strategy =~ /::/;
70              
71 2         9 $self->foreign_class->_require_class($strategy);
72 2 50       27 $strategy->can('cascade')
73             or return $self->_croak("$strategy is not a valid Cascade Strategy");
74 2         14 my $strat_obj = $strategy->new($self);
75 2     0   17 return (before_delete => sub { $strat_obj->cascade(@_) });
  0         0  
76             }
77              
78             sub methods {
79 2     2 1 5 my $self = shift;
80 2         9 my $accessor = $self->accessor;
81             return (
82 2         14 $accessor => $self->_has_many_method,
83             "add_to_$accessor" => $self->_method_add_to,
84             );
85             }
86              
87             sub _method_add_to {
88 2     2   22 my $rel = shift;
89 2         7 my $accessor = $rel->accessor;
90             return sub {
91 0     0   0 my ($self, $data) = @_;
92 0 0       0 my $class = ref $self
93             or return $self->_croak("add_to_$accessor called as class method");
94 0 0       0 return $self->_croak("add_to_$accessor needs data")
95             unless ref $data eq "HASH";
96              
97 0         0 my $meta = $class->meta_info($rel->name => $accessor);
98 0         0 my ($f_class, $f_key, $args) =
99             ($meta->foreign_class, $meta->args->{foreign_key}, $meta->args);
100 0         0 $data->{$f_key} = $self->id;
101              
102             # See if has_many constraints were defined and auto fill them
103 0 0 0     0 if (defined $args->{constraint} && ref $args->{constraint} eq 'HASH') {
104 0         0 while (my ($k, $v) = each %{ $args->{constraint} }) {
  0         0  
105 0 0 0     0 $self->_croak(
106             "Can't add_to_$accessor with $k = $data->{$k} (must be $v)")
107             if defined($data->{$k}) && $data->{$k} ne $v;
108 0         0 $data->{$k} = $v;
109             }
110             }
111              
112 0         0 $f_class->insert($data);
113 2         28 };
114             }
115              
116             sub _has_many_method {
117 2     2   4 my $self = shift;
118 2         7 my $run_search = $self->_hm_run_search;
119 2 50       4 my @mapping = @{ $self->args->{mapping} } or return $run_search;
  2         8  
120             return sub {
121 0 0   0   0 return $run_search->(@_)->set_mapping_method(@mapping)
122             unless wantarray;
123 0         0 my @ret = $run_search->(@_);
124 0         0 foreach my $meth (@mapping) { @ret = map $_->$meth(), @ret }
  0         0  
125 0         0 return @ret;
126             }
127 0         0 }
128              
129             sub _hm_run_search {
130 2     2   63 my $rel = shift;
131 2         8 my ($class, $accessor) = ($rel->class, $rel->accessor);
132             return sub {
133 0     0     my ($self, @search_args) = @_;
134 0 0         @search_args = %{ $search_args[0] } if ref $search_args[0] eq "HASH";
  0            
135 0           my $meta = $class->meta_info($rel->name => $accessor);
136 0           my ($f_class, $f_key, $args) =
137             ($meta->foreign_class, $meta->args->{foreign_key}, $meta->args);
138 0 0         if (ref $self) { # For $artist->cds
139 0 0 0       unshift @search_args, %{ $args->{constraint} }
  0            
140             if defined($args->{constraint}) && ref $args->{constraint} eq 'HASH';
141 0           unshift @search_args, ($f_key => $self->id);
142 0 0         push @search_args, { order_by => $args->{order_by} }
143             if defined $args->{order_by};
144 0           return $f_class->search(@search_args);
145             } else { # For Artist->cds
146             # Cross-table join as class method
147             # This stuff is highly experimental and will probably change beyond
148             # recognition. Use at your own risk...
149 0           my %kv = @search_args;
150 0           my $query = Class::DBI::Query->new({ owner => $f_class });
151 0           $query->kings($class, $f_class);
152 0           $query->add_restriction(sprintf "%s.%s = %s.%s",
153             $f_class->table_alias, $f_key, $class->table_alias,
154             $class->primary_column);
155 0           $query->add_restriction("$_ = ?") for keys %kv;
156 0           my $sth = $query->run(values %kv);
157 0           return $f_class->sth_to_objects($sth);
158             }
159 2         28 };
160             }
161              
162             1;