| 1 | package Class::DBI::Relationship::HasMany; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use base 'Class::DBI::Relationship'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | sub remap_arguments { | ||||||||||||||||||||||||||
|
Subroutine
remap_arguments: called CC=13 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||
| 9 | 2 | my ($proto, $class, $accessor, $f_class, $f_key, $args) = @_; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | return $class->_croak($class->name . " needs an accessor name") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | unless $accessor; | ||||||||||||||||||||||||||||
| 13 | 2 | ▶ | return $class->_croak($class->name . " needs a foreign class") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | unless $f_class; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | defined &{"$class\::$accessor"} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | and return $class->_carp("$accessor method already exists in $class\n"); | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | my @f_method = (); | |||||||||||||||||||||||||||
| 23 | 2 | ▶ | if (ref $f_class eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | ($f_class, @f_method) = @$f_class; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 2 | $class->_require_class($f_class); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if (ref $f_key eq "HASH") { # didn't supply f_key, this is really $args | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | $args = $f_key; | |||||||||||||||||||||||||||
| 30 | 0 | $f_key = ""; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | $f_key ||= do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$f_key ||= do {
my $meta = $f_class->meta_info("has_a");
my($col) = grep(($meta->{$_}->foreign_class eq $class), keys %$meta);
$col or $class->table_alias
}
MC/DC0%0/2
$f_key ||= do {
my $meta = $f_class->meta_info("has_a");
my($col) = grep(($meta->{$_}->foreign_class eq $class), keys %$meta);
$col or $class->table_alias
}
$f_key
do {
my $meta = $f_class->meta_info("has_a");
my($col) = grep(($meta->{$_}->foreign_class eq $class), keys %$meta);
$col or $class->table_alias
}
Truth table33%1/3
A ||= B
A= $f_keyB= do {
my $meta = $f_class->meta_info("has_a");
my($col) = grep(($meta->{$_}->foreign_class eq $class), keys %$meta);
$col or $class->table_alias
}
| |||||||||||||||||||||||||||||
| 34 | 2 | my $meta = $f_class->meta_info('has_a'); | |||||||||||||||||||||||||||
| 35 | 2 | my ($col) = grep $meta->{$_}->foreign_class eq $class, keys %$meta; | |||||||||||||||||||||||||||
| 36 | 2 | ▶ | $col || $class->table_alias; | ||||||||||||||||||||||||||
|
Condition66%2/3
$col || $class->table_alias
MC/DC0%0/2
$col || $class->table_alias
$col
$class->table_alias
Truth table66%2/3
A || B
A= $colB= $class->table_alias
| |||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if (ref $f_key eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return $class->_croak("Multi-column foreign keys not supported") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | if @$f_key > 1; | ||||||||||||||||||||||||||||
| 42 | 0 | $f_key = $f_key->[0]; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args ||= {}
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 46 | 2 | $args->{mapping} = \@f_method; | |||||||||||||||||||||||||||
| 47 | 2 | $args->{foreign_key} = $f_key; | |||||||||||||||||||||||||||
| 48 | 2 | ▶ | $args->{order_by} ||= $args->{sort}; # deprecated 0.96 | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"order_by"} ||= $args->{"sort"}
MC/DC0%0/2
$args->{"order_by"} ||= $args->{"sort"}
$args->{"order_by"}
$args->{"sort"}
Truth table33%1/3
A ||= B
A= $args->{"order_by"}B= $args->{"sort"}
| |||||||||||||||||||||||||||||
| 49 | warn "sort argument to has_many deprecated in favour of order_by" | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | if $args->{sort}; # deprecated 0.96 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | return ($class, $accessor, $f_class, $args); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub _set_up_class_data { | ||||||||||||||||||||||||||
|
Subroutine
_set_up_class_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | $self->class->_extend_class_data( | ||||||||||||||||||||||||||||
| 58 | 2 | __hasa_list => $self->foreign_class => $self->args->{foreign_key}); | |||||||||||||||||||||||||||
| 59 | 2 | $self->SUPER::_set_up_class_data; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub triggers { | ||||||||||||||||||||||||||
|
Subroutine
triggers: called CC=5 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | if ($self->args->{no_cascade_delete}) { # old undocumented way | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | warn "no_cascade_delete deprecated in favour of cascade => None"; | |||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my $strategy = $self->args->{cascade} || "Delete"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->args->{'cascade'} || 'Delete'
MC/DC0%0/1
$self->args->{'cascade'} || 'Delete'
$self->args->{'cascade'}
Truth table50%1/2
A
A= $self->args->{'cascade'}
| |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $strategy = "Class::DBI::Cascade::$strategy" unless $strategy =~ /::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | $self->foreign_class->_require_class($strategy); | |||||||||||||||||||||||||||
| 72 | 2 | ▶ | $strategy->can('cascade') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | or return $self->_croak("$strategy is not a valid Cascade Strategy"); | ||||||||||||||||||||||||||||
| 74 | 2 | my $strat_obj = $strategy->new($self); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | return (before_delete => sub { $strat_obj->cascade(@_) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub methods { | ||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 2 | my $accessor = $self->accessor; | |||||||||||||||||||||||||||
| 81 | return ( | ||||||||||||||||||||||||||||
| 82 | 2 | $accessor => $self->_has_many_method, | |||||||||||||||||||||||||||
| 83 | "add_to_$accessor" => $self->_method_add_to, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub _method_add_to { | ||||||||||||||||||||||||||
|
Subroutine
_method_add_to: called CC=1 SCAR=5.5
| |||||||||||||||||||||||||||||
| 88 | 2 | my $rel = shift; | |||||||||||||||||||||||||||
| 89 | 2 | my $accessor = $rel->accessor; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 91 | 0 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $class = ref $self | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | or return $self->_croak("add_to_$accessor called as class method"); | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | return $self->_croak("add_to_$accessor needs data") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | unless ref $data eq "HASH"; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $meta = $class->meta_info($rel->name => $accessor); | |||||||||||||||||||||||||||
| 98 | my ($f_class, $f_key, $args) = | ||||||||||||||||||||||||||||
| 99 | 0 | ($meta->foreign_class, $meta->args->{foreign_key}, $meta->args); | |||||||||||||||||||||||||||
| 100 | 0 | $data->{$f_key} = $self->id; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # See if has_many constraints were defined and auto fill them | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if (defined $args->{constraint} && ref $args->{constraint} eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args->{"constraint"} and ref $args->{"constraint"} eq "HASH"
MC/DC0%0/2
defined $args->{"constraint"} and ref $args->{"constraint"} eq "HASH"
defined $args->{"constraint"}
ref $args->{"constraint"} eq "HASH"
Truth table0%0/3
A and B
A= defined $args->{"constraint"}B= ref $args->{"constraint"} eq "HASH"
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | while (my ($k, $v) = each %{ $args->{constraint} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | $self->_croak( | ||||||||||||||||||||||||||||
| 106 | "Can't add_to_$accessor with $k = $data->{$k} (must be $v)") | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if defined($data->{$k}) && $data->{$k} ne $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $data->{$k} and $data->{$k} ne $v
MC/DC0%0/2
defined $data->{$k} and $data->{$k} ne $v
defined $data->{$k}
$data->{$k} ne $v
Truth table0%0/3
A and B
A= defined $data->{$k}B= $data->{$k} ne $v
| |||||||||||||||||||||||||||||
| 108 | 0 | $data->{$k} = $v; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | $f_class->insert($data); | |||||||||||||||||||||||||||
| 113 | 2 | }; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub _has_many_method { | ||||||||||||||||||||||||||
|
Subroutine
_has_many_method: called CC=2 SCAR=11.2
| |||||||||||||||||||||||||||||
| 117 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 2 | my $run_search = $self->_hm_run_search; | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | my @mapping = @{ $self->args->{mapping} } or return $run_search; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return $run_search->(@_)->set_mapping_method(@mapping) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | unless wantarray; | ||||||||||||||||||||||||||||
| 123 | 0 | my @ret = $run_search->(@_); | |||||||||||||||||||||||||||
| 124 | 0 | foreach my $meth (@mapping) { @ret = map $_->$meth(), @ret } | |||||||||||||||||||||||||||
| 125 | 0 | return @ret; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | } | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | sub _hm_run_search { | ||||||||||||||||||||||||||
|
Subroutine
_hm_run_search: called CC=1 SCAR=5.6
| |||||||||||||||||||||||||||||
| 130 | 2 | my $rel = shift; | |||||||||||||||||||||||||||
| 131 | 2 | my ($class, $accessor) = ($rel->class, $rel->accessor); | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($self, @search_args) = @_; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | @search_args = %{ $search_args[0] } if ref $search_args[0] eq "HASH"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | my $meta = $class->meta_info($rel->name => $accessor); | |||||||||||||||||||||||||||
| 136 | my ($f_class, $f_key, $args) = | ||||||||||||||||||||||||||||
| 137 | 0 | ($meta->foreign_class, $meta->args->{foreign_key}, $meta->args); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if (ref $self) { # For $artist->cds | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | unshift @search_args, %{ $args->{constraint} } | |||||||||||||||||||||||||||
| 140 | 0 | ▶ | if defined($args->{constraint}) && ref $args->{constraint} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args->{"constraint"} and ref $args->{"constraint"} eq "HASH"
MC/DC0%0/2
defined $args->{"constraint"} and ref $args->{"constraint"} eq "HASH"
defined $args->{"constraint"}
ref $args->{"constraint"} eq "HASH"
Truth table0%0/3
A and B
A= defined $args->{"constraint"}B= ref $args->{"constraint"} eq "HASH"
| |||||||||||||||||||||||||||||
| 141 | 0 | unshift @search_args, ($f_key => $self->id); | |||||||||||||||||||||||||||
| 142 | push @search_args, { order_by => $args->{order_by} } | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if defined $args->{order_by}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | }; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 4 | 1; | |||||||||||||||||||||||||||