| 1 | package DBIx::Class::Valiant::Result; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use base 'DBIx::Class'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Role::Tiny::With; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Valiant::Util 'debug'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use namespace::autoclean -also => ['debug']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use DBIx::Class::Valiant::Util::Exception::TooManyRows; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use DBIx::Class::Valiant::Util::Exception::BadParameterFK; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use DBIx::Class::Valiant::Util::Exception::BadParameters; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | with 'DBIx::Class::Valiant::Validates'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 12 | with 'Valiant::Filterable'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | use DBIx::Class::Candy::Exports; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | export_methods ['filters', 'validates', 'filters_with', 'validates_with', 'accept_nested_for', 'auto_validation']; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | __PACKAGE__->mk_classdata( _m2m_metadata => {} ); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | __PACKAGE__->mk_classdata( auto_validation => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | __PACKAGE__->mk_classdata( _nested => [] ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | __PACKAGE__->mk_classdata( _src_info => '' ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | sub many_to_many { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
many_to_many: called CC=4 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | my ($meth_name, $link, $far_side) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | my $store = $class->_m2m_metadata; | |||||||||||||||||||||||||||||||||||||||||
| 33 | warn("You are overwritting another relationship's metadata") | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | ▶ | if exists $store->{$meth_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | my $attrs = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | accessor => $meth_name, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | relation => $link, #"link" table or immediate relation | ||||||||||||||||||||||||||||||||||||||||||
| 39 | foreign_relation => $far_side, #'far' table or foreign relation | ||||||||||||||||||||||||||||||||||||||||||
| 40 | (@_ > 3 ? (attrs => $_[3]) : ()), #only store if exist | ||||||||||||||||||||||||||||||||||||||||||
| 41 | rs_method => "${meth_name}_rs", #for completeness.. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | add_method => "add_to_${meth_name}", | ||||||||||||||||||||||||||||||||||||||||||
| 43 | set_method => "set_${meth_name}", | ||||||||||||||||||||||||||||||||||||||||||
| 44 | remove_method => "remove_from_${meth_name}", | ||||||||||||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | my $pk_meth = qq[ | |||||||||||||||||||||||||||||||||||||||||
| 48 | package $class; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub ${meth_name}_pks { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | my \$self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | my \@pks = \$self->related_resultset("${link}")->related_resultset("${far_side}")->result_source->primary_columns; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | return map { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | my \$row = \$_; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | +{ map { \$_ => \$row->\$_ } \@pks }; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } \$self->\$meth_name->all; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | eval $pk_meth; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
roles_pks: not called CC=1 SCAR=4.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | #inheritable data workaround | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | $class->_m2m_metadata({ $meth_name => $attrs, %$store}); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | $class->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub BUILDARGS { } # The filter role wants this (for now) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 204 | ▶ | sub new { # also support for the filter role | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 204 | my ($class, $attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 204 | my @columns = $class->columns; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 204 | my %columns = (); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 204 | foreach my $column (@columns) { #strip $attrs on non column stuff | |||||||||||||||||||||||||||||||||||||||||
| 75 | 683 | ▶ | if(exists($attrs->{$column})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 238 | $columns{$column} = $attrs->{$column}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 204 | my %filtered = (%$attrs, %{$class->_process_filters(\%columns)}); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 204 | my $self = $class->next::method(\%filtered); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 204 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 946 | ▶ | sub accept_nested_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
accept_nested_for: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 946 | ▶ | my $class = blessed($_[0]) ? ref(shift) : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 946 | my %default_config = ( | |||||||||||||||||||||||||||||||||||||||||
| 87 | allow_destroy => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | delete_omitted => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | reject_if => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | limit => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | update_only => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | find_with_uniques => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 946 | my @existing = @{$class->_nested}; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 946 | ▶ | my %m2m_names = map { $_=>1 } keys %{ $class->_m2m_metadata ||+{} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$class->_m2m_metadata || {}
MC/DC0%0/1
$class->_m2m_metadata || {}
$class->_m2m_metadata
Truth table50%1/2
A
A= $class->_m2m_metadata
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 946 | my $changed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 946 | ▶ | while(my $attribute = shift) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 106 | ▶ | my $config = (ref($_[0])||'') eq 'HASH' ? shift : +{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $_[0] || ''
MC/DC100%1/1
ref $_[0] || ''
ref $_[0]
Truth table100%2/2
A
A= ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 106 | push @existing, $attribute; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 106 | push @existing, +{ %default_config, %$config }; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 106 | $changed = 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | ## Handed m2m, ugg | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 106 | ▶ | if($m2m_names{$attribute}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | # treat it like a set | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 6 | $class->validates($attribute => (result_set=>+{validations=>1} )); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | ## Add validation | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 100 | my $rel_data = $class->relationship_info($attribute); | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 100 | ▶ | die "'$attribute' does not seem to be a relationship. There's a typo in your source or you've placed your 'accept_nested_for' statement before the relationship it's associated with (has_many, belongs_to, etc)." unless $rel_data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 100 | my $rel_type = $rel_data->{attrs}{accessor}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 100 | ▶ | if($rel_type eq 'single') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 71 | $class->validates($attribute => (result=>+{validations=>1} )); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 29 | $class->validates($attribute => (result_set=>+{validations=>1} )); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 946 | ▶ | $class->_nested(\@existing) if $changed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 946 | return @existing; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 156 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=19 SCAR=36.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 156 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 156 | ▶ | my %args = %{ $self->{__VALIANT_CREATE_ARGS} ||+{} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'__VALIANT_CREATE_ARGS'} || {}
MC/DC100%1/1
$self->{'__VALIANT_CREATE_ARGS'} || {}
$self->{'__VALIANT_CREATE_ARGS'}
Truth table100%2/2
A
A= $self->{'__VALIANT_CREATE_ARGS'}
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 156 | ▶ | my $context = $args{context}||[]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'context'} || []
MC/DC100%1/1
$args{'context'} || []
$args{'context'}
Truth table100%2/2
A
A= $args{'context'}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 156 | ▶ | my @context = ref($context)||'' eq 'ARRAY' ? @$context : ($context); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $context or
MC/DC100%1/1
ref $context or
ref $context
Truth table100%2/2
A
A= ref $context
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 156 | ▶ | push @context, 'create' unless grep { $_ eq 'create' } @context; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # Add in any extra or new contexts passed as ->insert({__context=>...}) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 156 | ▶ | if( (ref($args[0])||'') eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $args[0] || ''
MC/DC0%0/1
ref $args[0] || ''
ref $args[0]
Truth table50%1/2
A
A= ref $args[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $ctx = delete($args[0]->{__context})||[]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $args[0]{'__context'} || []
MC/DC0%0/1
delete $args[0]{'__context'} || []
delete $args[0]{'__context'}
Truth table0%0/2
A
A= delete $args[0]{'__context'}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my @ctx = ref($ctx)||'' eq 'ARRAY' ? @$ctx : ($ctx); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $ctx or
MC/DC0%0/1
ref $ctx or
ref $ctx
Truth table0%0/2
A
A= ref $ctx
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | push @context, @ctx unless grep { $_ eq 'update' } @context; ## ?? IS this a bug? Why update | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 156 | $args{context} = \@context; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 156 | debug 2, "Checking if row for insert is marked for deletion @{[$self]}"; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 156 | ▶ | if($self->is_marked_for_deletion) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | debug 2, "Skipping insert for @{[$self]} because its marked for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 156 | debug 2, "About to run validations for @{[$self]} on insert"; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 156 | ▶ | if($self->auto_validation) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 155 | ▶ | $self->validate(%args) unless $self->{__valiant_donot_insert} || $self->{__valiant_add}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
MC/DC0%0/2
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
$self->{"__valiant_donot_insert"}
$self->{"__valiant_add"}
Truth table33%1/3
A or B
A= $self->{"__valiant_donot_insert"}B= $self->{"__valiant_add"}
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 156 | ▶ | if($self->errors->size) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 24 | debug 2, "Skipping insert for @{[$self]} because its invalid"; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 24 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 132 | debug 2, "proceeding to insert @{[$self]} because its valid"; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 132 | ▶ | if($self->{__valiant_donot_insert} || $self->{__valiant_add}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
MC/DC0%0/2
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
$self->{"__valiant_donot_insert"}
$self->{"__valiant_add"}
Truth table33%1/3
A or B
A= $self->{"__valiant_donot_insert"}B= $self->{"__valiant_add"}
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | debug 2, "Skipping insert for @{[$self]} because its probably and _add"; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 132 | my $ret = $self->next::method(@args); | |||||||||||||||||||||||||||||||||||||||||
| 167 | # TODO could probably do the merge errors from related here | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 132 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 261 | ▶ | sub _nested_info_for_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nested_info_for_related: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 261 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 261 | my %nested = $self->result_class->accept_nested_for; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 261 | ▶ | my %info = %{ $nested{$related}||+{} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$nested{$related} || {}
MC/DC0%0/1
$nested{$related} || {}
$nested{$related}
Truth table50%1/2
A
A= $nested{$related}
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 261 | return %info; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 150 | ▶ | sub _related_allow_destroy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_related_allow_destroy: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 150 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 150 | my %info = $self->_nested_info_for_related($related); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 150 | ▶ | if(my $proto = $info{allow_destroy}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 56 | ▶ | my $allow_or_not = (ref($proto)||'' eq 'CODE') ? $proto->($self) : $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $proto or
MC/DC0%0/1
ref $proto or
ref $proto
Truth table50%1/2
A
A= ref $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 56 | return $allow_or_not; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 94 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 59 | ▶ | sub _related_delete_omitted { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_related_delete_omitted: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 59 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 59 | my %info = $self->_nested_info_for_related($related); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 59 | ▶ | if(my $proto = $info{delete_omitted}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 36 | ▶ | my $delete_or_not = (ref($proto)||'' eq 'CODE') ? $proto->($self) : $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $proto or
MC/DC100%1/1
ref $proto or
ref $proto
Truth table100%2/2
A
A= ref $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 36 | return $delete_or_not; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 23 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 52 | ▶ | sub _related_limit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_related_limit: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 52 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 52 | my %info = $self->_nested_info_for_related($related); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 52 | ▶ | if(my $limit_proto = $info{limit}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | ▶ | my $limit = (ref($limit_proto)||'' eq 'CODE') ? $limit_proto->($self) : $limit_proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $limit_proto or
MC/DC0%0/1
ref $limit_proto or
ref $limit_proto
Truth table50%1/2
A
A= ref $limit_proto
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | return 1, $limit; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 50 | return 0, undef; | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 110 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=24 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 110 | my ($self, $upd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 110 | ▶ | my $context = delete($upd->{__context})||[]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $upd->{'__context'} || []
MC/DC100%1/1
delete $upd->{'__context'} || []
delete $upd->{'__context'}
Truth table100%2/2
A
A= delete $upd->{'__context'}
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 110 | ▶ | my @context = ref($context)||'' eq 'ARRAY' ? @$context : ($context); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $context or
MC/DC100%1/1
ref $context or
ref $context
Truth table100%2/2
A
A= ref $context
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 110 | ▶ | push @context, 'update' unless grep { $_ eq 'update' } @context; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 110 | my %related = (); | |||||||||||||||||||||||||||||||||||||||||
| 215 | 110 | my %nested = $self->result_class->accept_nested_for; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 110 | foreach my $associated(keys %nested) { | |||||||||||||||||||||||||||||||||||||||||
| 218 | $related{$associated} = delete($upd->{$associated}) | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 137 | ▶ | if exists($upd->{$associated}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | # Remove any relationed keys we didn't find with the allows nested | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 110 | my @rel_names = $self->result_source->relationships(); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 110 | ▶ | my @m2m_names = keys %{ $self->result_class->_m2m_metadata ||+{} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->result_class->_m2m_metadata || {}
MC/DC0%0/1
$self->result_class->_m2m_metadata || {}
$self->result_class->_m2m_metadata
Truth table50%1/2
A
A= $self->result_class->_m2m_metadata
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 110 | debug 1, "Found related for @{[ ref $self ]} of @{[ join ',', @rel_names ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 110 | my %found = map { $_ => delete($upd->{$_}) } @rel_names, @m2m_names; # backcompat with old perl | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 110 | ▶ | if(grep { defined $_ } values %found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $related = join(', ', grep { $found{$_} } keys %found); | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | die "You are trying to create a relationship ($related) on @{[ $self ]} without setting 'accept_nested_for'"; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 110 | ▶ | my %validate_args = (context => \@context) if @context; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 110 | ▶ | $self->set_inflated_columns($upd) if $upd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 110 | foreach my $related(keys %related) { | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 52 | ▶ | if(my $cb = $nested{$related}->{reject_if}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | next if $cb->($self, $related{$related}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 52 | my ($has_limit, $limit) = $self->_related_limit($related); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 52 | ▶ | if($has_limit) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | my $num = scalar @{$related{$related}}; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | ▶ | DBIx::Class::Valiant::Util::Exception::TooManyRows | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | ->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 249 | limit=>$limit, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | attempted=>$num, | ||||||||||||||||||||||||||||||||||||||||||
| 251 | related=>$related, | ||||||||||||||||||||||||||||||||||||||||||
| 252 | me=>$self->result_source->name, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | ) if $num > $limit; | ||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 52 | debug 2, "Setting related '$related' for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | #$self->update_or_create_related($related, $related{$related}); | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 52 | $self->{_valiant_nested_info} = $nested{$related}; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 52 | $self->set_related_from_params($related, $related{$related}); | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 48 | delete $self->{_valiant_nested_info}; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 106 | debug 2, "Checking if row for update is marked for deletion @{[$self]}"; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 106 | ▶ | if($self->is_marked_for_deletion) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 1 | debug 2, "Skipping update for @{[$self]} because its marked for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 105 | debug 2, "About to run validations for @{[$self]} on update"; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 105 | ▶ | if($self->auto_validation) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 105 | ▶ | $self->validate(%validate_args) unless $self->{__valiant_donot_insert} || $self->{__valiant_add}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
MC/DC0%0/2
$self->{"__valiant_donot_insert"} or $self->{"__valiant_add"}
$self->{"__valiant_donot_insert"}
$self->{"__valiant_add"}
Truth table33%1/3
A or B
A= $self->{"__valiant_donot_insert"}B= $self->{"__valiant_add"}
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 105 | ▶ | if($self->has_errors) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 32 | debug 2, "Validation errors found, skipping mutate for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 32 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 73 | debug 2, "No validation issues found, proceeding to mutate @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | # wrap it all in a transaction to undo if issues. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 73 | my $txn_guard = $self->result_source->schema->txn_scope_guard; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 73 | foreach my $related(keys %nested) { | |||||||||||||||||||||||||||||||||||||||||
| 286 | 75 | ▶ | if(my $cb = $nested{$related}->{reject_if}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 9 | ▶ | next if $cb->($self, $related{$related}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 75 | debug 3, "mutating related $related for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 75 | $self->_mutate_related($related); | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 73 | my $result = $self->next::method(); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 73 | $txn_guard->commit; | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 73 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 444 | ▶ | sub register_column { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_column: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 444 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 444 | my ($column, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 444 | ▶ | if(my $validates = delete($info->{validates})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | debug 1, "Found validation info inside column '$column' definition"; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | $class->validates($column, @$validates); | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 444 | ▶ | if(my $filters = delete($info->{filters})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | debug 1, "Found filter info inside column '$column' definition"; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $class->filters($column, @$filters); | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 444 | $class->next::method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | # Gotta jump thru these hoops because of the way the Catalyst | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # DBIC model messes with the result namespace but not the schema | ||||||||||||||||||||||||||||||||||||||||||
| 318 | # namespace | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 5155 | ▶ | sub namespace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
namespace: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 5155 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 5155 | ▶ | my $class = ref($self) ? ref($self) : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 5155 | my ($ns) = ($class =~m/^(.+)::.+$/); | |||||||||||||||||||||||||||||||||||||||||
| 324 | 5155 | return $ns; | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | # Trouble here is you can only inject one attribute per model. Will be an | ||||||||||||||||||||||||||||||||||||||||||
| 328 | # issue if you have more than one confirmation validation on the model. Should be an easy | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # fix, we just need to track incoming attributes so 'new' knows how to init | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # all of them | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 4 | ▶ | sub inject_attribute { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inject_attribute: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 4 | my ($class, $attribute_to_inject) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 4 | my $injection = " | |||||||||||||||||||||||||||||||||||||||||
| 335 | package $class; | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | __PACKAGE__->mk_group_accessors(simple => '$attribute_to_inject'); | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 340 | my (\$class, \$args) = \@_; | ||||||||||||||||||||||||||||||||||||||||||
| 341 | my \$val = delete \$args->{$attribute_to_inject}; | ||||||||||||||||||||||||||||||||||||||||||
| 342 | my \$new = \$class->next::method(\$args); | ||||||||||||||||||||||||||||||||||||||||||
| 343 | \$new->$attribute_to_inject(\$val); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | return \$new; | ||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | "; | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | 4 | ▶ | eval $injection; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 4 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | sub is_attribute_changed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_attribute_changed: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | my ($self, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | return $self->is_column_changed($attr); | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | # We override here because we really want the uninflated values for the columns. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # Otherwise if we try to inflate first we can get an error since the value has not | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # been validated and may not inflate. We see this very commonly on date type columns | ||||||||||||||||||||||||||||||||||||||||||
| 360 | # when the developer is using the DateTime inflation components. | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 3265 | ▶ | sub read_attribute_for_validation { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_attribute_for_validation: called CC=9 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 3265 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 3265 | ▶ | return unless defined $attribute; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 3265 | ▶ | return $self->get_column($attribute) if $self->result_source->has_column($attribute); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 862 | ▶ | if($self->has_relationship($attribute)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 659 | my $rel_data = $self->relationship_info($attribute); | |||||||||||||||||||||||||||||||||||||||||
| 369 | 659 | my $rel_type = $rel_data->{attrs}{accessor}; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 659 | ▶ | if($rel_type eq 'single') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 414 | return $self->$attribute; | |||||||||||||||||||||||||||||||||||||||||
| 372 | #return $self->related_resultset($attribute)->single; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | } elsif($rel_type eq 'multi') { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 245 | return $self->related_resultset($attribute); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | die "Cannot read_attribute_for_validation for '$attribute' of rel_type '$rel_type' in @{[ref $self]}"; | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 203 | debug 1, "Failing back to accessor for 'read_attribute_for_validation' for object @{[ ref $self ]} attribute $attribute"; | |||||||||||||||||||||||||||||||||||||||||
| 381 | 203 | ▶ | debug 2, "Failing back @{[ $self->can($attribute) ? 'succeeds':'failed' ]}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 203 | ▶ | return $self->$attribute if $self->can($attribute); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 98 | my %columns = $self->get_columns; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 98 | ▶ | return $columns{$attribute} if exists($columns{$attribute}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub get_attribute_for_json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_attribute_for_json: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | return $self->is_marked_for_deletion if $attribute eq '_delete'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | return 1 if $attribute eq '_add'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | return $self->read_attribute_for_validation($attribute); | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | # Provide basic uniqueness checking for columns. This is basically a dumb DB lookup. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # Its probably fine for light work but you'll need something more performant when your | ||||||||||||||||||||||||||||||||||||||||||
| 399 | # table gets big. | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 116 | ▶ | sub is_unique { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_unique: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 116 | my ($self, $attribute_name, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 403 | # Don't do this check unless the user is actually trying to change the | ||||||||||||||||||||||||||||||||||||||||||
| 404 | # value (otherwise it will fail all the time). | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 116 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 64 | ▶ | return 1 unless $self->is_column_changed($attribute_name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | 67 | my $found = $self->result_source->resultset->single({$attribute_name=>$value}); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 67 | ▶ | return $found ? 0:1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 26 | ▶ | sub mark_for_deletion { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mark_for_deletion: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 26 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 26 | ▶ | return unless $self->{__valiant_allow_destroy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 25 | $self->{__valiant_kiss_of_death} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | sub unmark_for_deletion { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unmark_for_deletion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | $self->{__valiant_kiss_of_death} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 821 | ▶ | sub is_marked_for_deletion { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_marked_for_deletion: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | 821 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 425 | 821 | ▶ | return $self->{__valiant_kiss_of_death} ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 18 | ▶ | sub delete_if_in_storage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_if_in_storage: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 18 | ▶ | $self->delete if $self->in_storage; #TODO some sort of relationship handling... | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | #### | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | sub build_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_related: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | my ($self, $related, $attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | debug 2, "Building related entity '$related' for @{[ ref $self ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | my $related_obj = ($attrs && scalar(%$attrs)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$attrs and scalar %$attrs
MC/DC0%0/1
$attrs and scalar %$attrs
$attrs
Truth table0%0/2
A
A= $attrs
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | ? $self->find_or_new_related($related, $attrs) | ||||||||||||||||||||||||||||||||||||||||||
| 441 | : $self->new_related($related, +{}); | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | return if $related_obj->in_storage; #I think we can skip if its found | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | # TODO do this dance need to go into other places??? | ||||||||||||||||||||||||||||||||||||||||||
| 446 | # TODO do I need some set_from_related or something here to get everthing into _relationship_data ??? | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | my $relinfo = $self->relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | if ($relinfo->{attrs}{accessor} eq 'single') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | $self->{_relationship_data}{$related} = $related_obj; | |||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||
| 451 | elsif ($relinfo->{attrs}{accessor} eq 'filter') { | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | $self->{_inflated_column}{$related} = $related_obj; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | my @current_cache = @{ $self->related_resultset($related)->get_cache ||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->related_resultset($related)->get_cache || []
MC/DC0%0/1
$self->related_resultset($related)->get_cache || []
$self->related_resultset($related)->get_cache
Truth table0%0/2
A
A= $self->related_resultset($related)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $self->related_resultset($related)->set_cache([@current_cache, $related_obj]); | |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | return $related_obj; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | sub build_related_if_empty { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_related_if_empty: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | my ($self, $related, $attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | debug 2, "Build related entity '$related' for @{[ ref $self ]} if empty"; | |||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my $rel_data = $self->relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | ▶ | unless($rel_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | if(my $rel_data = $self->_m2m_metadata->{$related}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | my $relation = $rel_data->{relation}; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | my $foreign_relation = $rel_data->{foreign_relation}; | |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | return if @{ $self->related_resultset($relation)->get_cache ||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->related_resultset($relation)->get_cache || []
MC/DC0%0/1
$self->related_resultset($relation)->get_cache || []
$self->related_resultset($relation)->get_cache
Truth table0%0/2
A
A= $self->related_resultset($relation)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | my $obj = $self->build_related_if_empty($relation, $attrs); | |||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | return if @{ $obj->related_resultset($foreign_relation)->get_cache ||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$obj->related_resultset($foreign_relation)->get_cache || []
MC/DC0%0/1
$obj->related_resultset($foreign_relation)->get_cache || []
$obj->related_resultset($foreign_relation)->get_cache
Truth table0%0/2
A
A= $obj->related_resultset($foreign_relation)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | return $obj->build_related_if_empty($foreign_relation); | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | return if @{ $self->related_resultset($related)->get_cache ||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->related_resultset($related)->get_cache || []
MC/DC0%0/1
$self->related_resultset($related)->get_cache || []
$self->related_resultset($related)->get_cache
Truth table0%0/2
A
A= $self->related_resultset($related)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | return $self->build_related($related, $attrs); | |||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | # When this param is an array we only use the last value, this is to support | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # how checkboxes in HTML work (unchecked submits no value). | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 4 | ▶ | sub _param_is_delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_param_is_delete: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 4 | my ($value_proto) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 486 | 4 | ▶ | my $value = ref($value_proto) ? $value_proto->[-1] : $value_proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 4 | ▶ | return $value ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | 9 | ▶ | sub set_columns_recursively { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_columns_recursively: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 9 | my ($self, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 492 | 9 | $self->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 493 | 9 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | 191 | ▶ | sub set_from_params_recursively { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_from_params_recursively: called CC=22 SCAR=50.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 191 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 191 | debug 2, "Starting 'set_from_params_recursively' for @{[ ref $self ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 191 | foreach my $param (keys %params) { # probably needs to be sorted so we get specials (_destroy) first | |||||||||||||||||||||||||||||||||||||||||
| 500 | 297 | debug 3, "Starting param $param"; | |||||||||||||||||||||||||||||||||||||||||
| 501 | # Spot to normalize serialized params (like for dates, etc). | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 297 | ▶ | if($self->has_column($param)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 252 | $self->set_column($param => $params{$param}); | |||||||||||||||||||||||||||||||||||||||||
| 504 | } elsif($self->has_relationship($param)) { | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 41 | my %nested = $self->result_class->accept_nested_for; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 41 | ▶ | if(my $cb = $nested{$param}->{reject_if}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | 5 | ▶ | next if $cb->($self, $params{$param}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 40 | debug 3, "set_related_from_params for @{[ ref $self ]}, related $param"; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 40 | $self->set_related_from_params($param, $params{$param}); | |||||||||||||||||||||||||||||||||||||||||
| 511 | } elsif($self->can($param)) { | ||||||||||||||||||||||||||||||||||||||||||
| 512 | # Right now this is only used by confirmation stuff | ||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | $self->$param($params{$param}); | |||||||||||||||||||||||||||||||||||||||||
| 514 | } elsif( $param eq '_delete') { | ||||||||||||||||||||||||||||||||||||||||||
| 515 | 4 | ▶ | if( _param_is_delete($params{$param}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | 4 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | 4 | debug 2, "Marking record @{[ ref $self ]}, id @{[ $self->id ]} for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 4 | $self->mark_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 519 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | debug 2, "Marking unsaved record @{[ ref $self ]}, id @{[ $self->id||'NA' ]} for deletion (wil be NOP)"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->id || 'NA'
MC/DC0%0/1
$self->id || 'NA'
$self->id
Truth table0%0/2
A
A= $self->id
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | $self->mark_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | } elsif( $param eq '_nop') { | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | if($params{$param}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | debug 3, "Found _nop"; | |||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | delete $params{$param}; | |||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | $self->mark_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||||||
| 530 | ▶ | } elsif($param eq '_restore' && $params{$param}) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$param eq "_restore" and $params{$param}
MC/DC0%0/2
$param eq "_restore" and $params{$param}
$param eq "_restore"
$params{$param}
Truth table0%0/3
A and B
A= $param eq "_restore"B= $params{$param}
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | debug 3, "Unmarking record @{[ ref $self ]}, id @{[ $self->id ]} for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | $self->unmark_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | delete $params{_delete}; | |||||||||||||||||||||||||||||||||||||||||
| 535 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | die "didn't deal with restore on unsaved records"; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | } elsif($param eq '_action') { | ||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | my $action = $params{$param}; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | ▶ | $action = ref($action)||'' ? $action->[-1] : $action; # If action is a ref always use the last one | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $action or ""
MC/DC0%0/1
ref $action or ""
ref $action
Truth table0%0/2
A
A= ref $action
| |||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | debug 3, "Found _action param with value $action"; | |||||||||||||||||||||||||||||||||||||||||
| 542 | 0 | ▶ | if($action eq 'delete') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | debug 2, "Marking record @{[ ref $self ]}, id @{[ $self->id ]} for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | $self->mark_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | die "didn't deal with action 'delete' on unsaved records"; | |||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | } elsif($action eq 'nop') { | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # Just skip, this is just a no op to deal with checkboxes and radio controls in HTML | ||||||||||||||||||||||||||||||||||||||||||
| 551 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | die "Not sure what action '$action' is"; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | die "Not sure what to do with '$param'"; | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | 154 | ▶ | sub set_related_from_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_related_from_params: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 154 | my ($self, $related, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | 154 | my $rel_data = $self->relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 564 | 154 | ▶ | unless($rel_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | 10 | ▶ | if(my $rel_data = $self->_m2m_metadata->{$related}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | 10 | debug 2, "Setting params for $related on @{[ ref $self ]} using rel_type m2m"; | |||||||||||||||||||||||||||||||||||||||||
| 567 | 10 | return $self->set_m2m_related_from_params($related, $params, $rel_data); | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | 144 | my $rel_type = $rel_data->{attrs}{accessor}; | |||||||||||||||||||||||||||||||||||||||||
| 572 | 144 | debug 2, "Setting params for $related on @{[ ref $self ]} using rel_type $rel_type"; | |||||||||||||||||||||||||||||||||||||||||
| 573 | 144 | ▶ | return $self->set_single_related_from_params($related, $params) if $rel_type eq 'single'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | 53 | ▶ | return $self->set_multi_related_from_params($related, $params) if $rel_type eq 'multi'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | 0 | die "Unhandled relationship type: $rel_type"; | |||||||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | # m2m is tricky | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 10 | ▶ | sub set_m2m_related_from_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_m2m_related_from_params: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | 10 | my ($self, $related, $params, $rel_data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 10 | my $relation = $rel_data->{relation}; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 10 | my $foreign_relation = $rel_data->{foreign_relation}; | |||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | # We do this to allow for both multi create/update via an array (typical DBIC | ||||||||||||||||||||||||||||||||||||||||||
| 586 | # usage or via a hash of ordered keys (typical via CGI/Web). | ||||||||||||||||||||||||||||||||||||||||||
| 587 | 10 | my @param_rows = (); | |||||||||||||||||||||||||||||||||||||||||
| 588 | 10 | ▶ | if(ref($params) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | @param_rows = map { $params->{$_} } sort { $a <=> $b} keys %{$params || +{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params || {}
MC/DC0%0/1
$params || {}
$params
Truth table0%0/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | } elsif(ref($params) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 8 | ▶ | @param_rows = @{$params || []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params || []
MC/DC0%0/1
$params || []
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 592 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 593 | # An undefined nested set means there's nothing to do; anything else | ||||||||||||||||||||||||||||||||||||||||||
| 594 | # non reference is a caller mistake. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | 2 | ▶ | return unless defined $params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 596 | 1 | die "We expect '$params' to be some sort of reference but its not!"; | |||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 8 | debug 2, "Setting m2m relation '$related' for @{[ ref $self ]} via '$relation' => '$foreign_relation'"; | |||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | # TODO its possible we need to creeate the m2m cache here | ||||||||||||||||||||||||||||||||||||||||||
| 601 | # TODO We need to add PK columens to the @params_rows if we have them | ||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | #use Devel::Dwarn; | ||||||||||||||||||||||||||||||||||||||||||
| 604 | #Dwarn $self->result_source->_resolve_relationship_condition ( | ||||||||||||||||||||||||||||||||||||||||||
| 605 | # infer_values_based_on => +{}, | ||||||||||||||||||||||||||||||||||||||||||
| 606 | # rel_name => $relation, | ||||||||||||||||||||||||||||||||||||||||||
| 607 | # self_result_object => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 608 | # foreign_alias => $relation, | ||||||||||||||||||||||||||||||||||||||||||
| 609 | # self_alias => 'me', | ||||||||||||||||||||||||||||||||||||||||||
| 610 | #)->{inferred_values}; | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | 8 | return $self->set_multi_related_from_params($relation, [ map { +{ $foreign_relation => $_ } } @param_rows ]); | |||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | ## TODO | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 2 | ▶ | sub is_pruned { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_pruned: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 2 | ▶ | return shift->{__valiant_is_pruned} ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | 185 | ▶ | sub is_removed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_removed: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 185 | ▶ | return (($_[0]->{__valiant_is_pruned} || $_[0]->{__valiant_kiss_of_death}) ? 1:0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[0]{"__valiant_is_pruned"} or $_[0]{"__valiant_kiss_of_death"}
MC/DC100%2/2
$_[0]{"__valiant_is_pruned"} or $_[0]{"__valiant_kiss_of_death"}
$_[0]{"__valiant_is_pruned"}
$_[0]{"__valiant_kiss_of_death"}
Truth table100%3/3
A or B
A= $_[0]{"__valiant_is_pruned"}B= $_[0]{"__valiant_kiss_of_death"}
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | 61 | ▶ | sub set_multi_related_from_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_multi_related_from_params: called CC=55 SCAR=41.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | 61 | my ($self, $related, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | # We do this to allow for both multi create/update via an array (typical DBIC | ||||||||||||||||||||||||||||||||||||||||||
| 628 | # usage or via a hash of ordered keys (typical via CGI/Web). | ||||||||||||||||||||||||||||||||||||||||||
| 629 | 61 | my @param_rows = (); | |||||||||||||||||||||||||||||||||||||||||
| 630 | 61 | ▶ | if(ref($params) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | ▶ | @param_rows = map { $params->{$_} } sort { $a <=> $b} keys %{$params || +{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params || {}
MC/DC0%0/1
$params || {}
$params
Truth table0%0/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | } elsif(ref($params) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 633 | 59 | ▶ | @param_rows = @{$params || []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params || []
MC/DC0%0/1
$params || []
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 634 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 635 | # An undefined nested set means there's nothing to do; anything else | ||||||||||||||||||||||||||||||||||||||||||
| 636 | # non reference is a caller mistake. | ||||||||||||||||||||||||||||||||||||||||||
| 637 | 2 | ▶ | return unless defined $params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | 1 | die "We expect '$params' to be some sort of reference but its not!"; | |||||||||||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 59 | my $allow_destroy = $self->_related_allow_destroy($related); | |||||||||||||||||||||||||||||||||||||||||
| 642 | 59 | my $delete_omitted = $self->_related_delete_omitted($related); | |||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||
| 644 | # Queue up some meta data here just once. We get existing rows ans | ||||||||||||||||||||||||||||||||||||||||||
| 645 | # uniqiue key info (including PK info) | ||||||||||||||||||||||||||||||||||||||||||
| 646 | 59 | debug 2, "looking for $related cached or existing rows"; | |||||||||||||||||||||||||||||||||||||||||
| 647 | 59 | ▶ | my @existing_rows = @{ $self->$related->get_cache||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->$related->get_cache || []
MC/DC100%1/1
$self->$related->get_cache || []
$self->$related->get_cache
Truth table100%2/2
A
A= $self->$related->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 648 | 59 | ▶ | unless(@existing_rows) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 649 | 36 | debug 3, "cache was empty so going to check DB"; ## TODO this is to support ->discard_changes but maybe not need | |||||||||||||||||||||||||||||||||||||||||
| 650 | 36 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | 7 | @existing_rows = $self->$related->all; | |||||||||||||||||||||||||||||||||||||||||
| 652 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 653 | 29 | debug 3, "record not in storage, so no point in looking for related rows"; | |||||||||||||||||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||||||||||||||||
| 656 | 59 | debug 2, "Found @{[ scalar @existing_rows ]} existing rows for $related"; | |||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | 59 | debug 2, "looking for uniques for $related"; | |||||||||||||||||||||||||||||||||||||||||
| 659 | 59 | my @primary_columns = $self->$related->result_source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 59 | my %uniques = $self->$related->result_source->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||
| 661 | 59 | my @search_sets = ('primary'); | |||||||||||||||||||||||||||||||||||||||||
| 662 | 59 | my %nested = $self->result_class->accept_nested_for; | |||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | # Gather the list of fields which are single type rels | ||||||||||||||||||||||||||||||||||||||||||
| 665 | my @single_rels = grep { | ||||||||||||||||||||||||||||||||||||||||||
| 666 | 59 | my $rel_data = $self->$related->result_source->relationship_info($_); | |||||||||||||||||||||||||||||||||||||||||
| 667 | 79 | ▶ | ($rel_data && ($rel_data->{attrs}{accessor} eq 'single')) ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rel_data and $rel_data->{"attrs"}{"accessor"} eq "single"
MC/DC0%0/2
$rel_data and $rel_data->{"attrs"}{"accessor"} eq "single"
$rel_data
$rel_data->{"attrs"}{"accessor"} eq "single"
Truth table33%1/3
A and B
A= $rel_dataB= $rel_data->{"attrs"}{"accessor"} eq "single"
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | } $self->$related->result_source->relationships; | ||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||
| 670 | # Generally we alow finding the row via the PK only but the user can allow finding | ||||||||||||||||||||||||||||||||||||||||||
| 671 | # by any unique if that's what they really want. | ||||||||||||||||||||||||||||||||||||||||||
| 672 | 59 | ▶ | if($nested{$related}{find_with_uniques}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 20 | push @search_sets, grep { $_ ne 'primary' } keys %uniques; | |||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | # Ok now build up the new rows | ||||||||||||||||||||||||||||||||||||||||||
| 677 | 59 | my @related_models = (); | |||||||||||||||||||||||||||||||||||||||||
| 678 | 59 | debug 2, "starting loop to update/create related $related (total @{[ scalar @param_rows ]} rows in loop)"; | |||||||||||||||||||||||||||||||||||||||||
| 679 | 59 | foreach my $param_row (@param_rows) { | |||||||||||||||||||||||||||||||||||||||||
| 680 | 101 | debug 3, "top of new loop on param_rows"; | |||||||||||||||||||||||||||||||||||||||||
| 681 | 101 | ▶ | if(exists $param_row->{_nop}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | 0 | debug 3, "Is a NOP row, so skipping"; | |||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | # Ok so if $self is in storage we expect any $related FKs to match whatever is $self $PK. If that | ||||||||||||||||||||||||||||||||||||||||||
| 687 | # FK is empty we add it. If its there but has an unexpected value, that's an error (and probably | ||||||||||||||||||||||||||||||||||||||||||
| 688 | # a hacking attempt | ||||||||||||||||||||||||||||||||||||||||||
| 689 | 101 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 690 | 31 | my %cond = %{$self->result_source->relationship_info($related)->{cond}}; | |||||||||||||||||||||||||||||||||||||||||
| 691 | 31 | foreach my $fk_proto (keys %cond) { | |||||||||||||||||||||||||||||||||||||||||
| 692 | 31 | my $pk_proto = $cond{$fk_proto}; | |||||||||||||||||||||||||||||||||||||||||
| 693 | 31 | my ($pk) = ($pk_proto=~m/^self\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 694 | 31 | my ($fk) = ($fk_proto=~m/^foreign\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 31 | ▶ | if(exists $param_row->{$fk}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 697 | 1 | ▶ | unless($param_row->{$fk} eq $self->get_column($pk)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 698 | DBIx::Class::Valiant::Util::Exception::BadParameterFK->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 699 | 1 | fk_field=>$fk, fk_value=>$param_row->{$fk}, | |||||||||||||||||||||||||||||||||||||||||
| 700 | pk_field=>$pk, pk_value=>$self->get_column($pk), | ||||||||||||||||||||||||||||||||||||||||||
| 701 | related=>$related, me=>$self); | ||||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 703 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 30 | $param_row->{$fk} = $self->get_column($pk); # set it since that should help with the lookups | |||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||
| 707 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 708 | # Now if $self is not in storage we expect the related FK to be empty. If not throw error since | ||||||||||||||||||||||||||||||||||||||||||
| 709 | # its possible a hacking attempt | ||||||||||||||||||||||||||||||||||||||||||
| 710 | 70 | my %cond = %{$self->result_source->relationship_info($related)->{cond}}; | |||||||||||||||||||||||||||||||||||||||||
| 711 | 70 | foreach my $fk_proto (keys %cond) { | |||||||||||||||||||||||||||||||||||||||||
| 712 | 70 | my $pk_proto = $cond{$fk_proto}; | |||||||||||||||||||||||||||||||||||||||||
| 713 | 70 | my ($pk) = ($pk_proto=~m/^self\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 714 | 70 | my ($fk) = ($fk_proto=~m/^foreign\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | 70 | ▶ | if(exists $param_row->{$fk}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 717 | 1 | ▶ | unless(!defined($param_row->{$fk})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | DBIx::Class::Valiant::Util::Exception::BadParameterFK->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 719 | 1 | fk_field=>$fk, fk_value=>$param_row->{$fk}, | |||||||||||||||||||||||||||||||||||||||||
| 720 | pk_field=>$pk, pk_value=>'undef', | ||||||||||||||||||||||||||||||||||||||||||
| 721 | related=>$related, me=>$self); | ||||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||
| 727 | 99 | my $was_add = delete $param_row->{_add}; | |||||||||||||||||||||||||||||||||||||||||
| 728 | 99 | my $related_model; | |||||||||||||||||||||||||||||||||||||||||
| 729 | 99 | ▶ | if(blessed $param_row) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 730 | 4 | debug 1, "params are an object"; | |||||||||||||||||||||||||||||||||||||||||
| 731 | 4 | $related_model = $param_row; | |||||||||||||||||||||||||||||||||||||||||
| 732 | 4 | ▶ | $related_model->{__valiant_add} = 1 if $was_add; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 733 | ▶ | } elsif( (ref($param_row)||'') eq 'HASH') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
ref $param_row || ''
MC/DC0%0/1
ref $param_row || ''
ref $param_row
Truth table50%1/2
A
A= ref $param_row
| |||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | # The first thing to do is to see if we can find a row either in the existing cache | ||||||||||||||||||||||||||||||||||||||||||
| 736 | # or in the DB, matched on the Primary key and (if enabled) unique keys. | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 95 | my %keys_used_to_find = (); | |||||||||||||||||||||||||||||||||||||||||
| 738 | 95 | SEARCH_UNIQUE_KEYS: foreach my $key (@search_sets) { | |||||||||||||||||||||||||||||||||||||||||
| 739 | 95 | debug 2, "trying to find a row for $related using key $key"; | |||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||
| 741 | 95 | my @key_columns = @{$uniques{$key}}; | |||||||||||||||||||||||||||||||||||||||||
| 742 | 95 | my %matching = map { $_ => $param_row->{$_} } grep { exists $param_row->{$_} } @key_columns; | |||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | 95 | ▶ | next unless scalar(@key_columns) == scalar(keys(%matching)); # only match when its a full key match | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 746 | 14 | debug 2, "key $key exists in params"; | |||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||
| 748 | # Find matching if they exist in the cache | ||||||||||||||||||||||||||||||||||||||||||
| 749 | 14 | foreach my $row (@existing_rows) { | |||||||||||||||||||||||||||||||||||||||||
| 750 | 18 | my %unique_fields = map { $_ => $row->get_column($_) } @key_columns; | |||||||||||||||||||||||||||||||||||||||||
| 751 | 18 | my $a = join "", map { $_, $unique_fields{$_} } sort keys(%unique_fields); | |||||||||||||||||||||||||||||||||||||||||
| 752 | 18 | my $b = join "", map { $_, $matching{$_} } sort keys(%matching); | |||||||||||||||||||||||||||||||||||||||||
| 753 | 18 | ▶ | if($a eq $b) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 754 | 13 | $related_model = $row; | |||||||||||||||||||||||||||||||||||||||||
| 755 | 13 | ▶ | if($related_model) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 756 | 13 | ▶ | $related_model->{__valiant_add} = 1 if $was_add; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 757 | 13 | debug 2, "found related model for $related with $key in cache"; | |||||||||||||||||||||||||||||||||||||||||
| 758 | 13 | %keys_used_to_find = (%keys_used_to_find, %matching); | |||||||||||||||||||||||||||||||||||||||||
| 759 | last SEARCH_UNIQUE_KEYS | ||||||||||||||||||||||||||||||||||||||||||
| 760 | 13 | } | |||||||||||||||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | # If it doesn't match in the cache then we have to find it in the DB. TODO I'm skipping this | ||||||||||||||||||||||||||||||||||||||||||
| 765 | # lookup for now since I don't think we need it unless the user failed to properly prefetch. | ||||||||||||||||||||||||||||||||||||||||||
| 766 | # Its pointless to do this unless $self is in storage anyway | ||||||||||||||||||||||||||||||||||||||||||
| 767 | 1 | ▶ | unless($related_model) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 768 | 1 | debug 2, "Trying to find $related in DB with key $key (SKIPPING)"; | |||||||||||||||||||||||||||||||||||||||||
| 769 | #$related_model = $self->find_related($related, \%matching, +{key=>$key}) if $self->in_storage; | ||||||||||||||||||||||||||||||||||||||||||
| 770 | 1 | ▶ | if($related_model) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 771 | 0 | debug 2, "found related model for $related with $key in DB"; | |||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | %keys_used_to_find = (%keys_used_to_find, %matching); | |||||||||||||||||||||||||||||||||||||||||
| 773 | last SEARCH_UNIQUE_KEYS | ||||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||
| 778 | # If we get here its possible there's a single type relation we can use to complete | ||||||||||||||||||||||||||||||||||||||||||
| 779 | # the lookup. TODO: Not sure if this should be a param setting in the nested_for data | ||||||||||||||||||||||||||||||||||||||||||
| 780 | # (it probably should be). Also not sure if we should limit the ->find_related to | ||||||||||||||||||||||||||||||||||||||||||
| 781 | # just the FK rel. | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | 95 | ▶ | if(grep {$param_row->{$_}} @single_rels) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 784 | 18 | debug 2, "Falling back to default find_related for $related using full param_row"; | |||||||||||||||||||||||||||||||||||||||||
| 785 | 18 | ▶ | $related_model = $self->find_related($related, $param_row) unless $related_model || !%{$param_row}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$related_model or not %{$param_row;}
MC/DC0%0/2
$related_model or not %{$param_row;}
$related_model
not %{$param_row;}
Truth table33%1/3
A or not(B)
A= $related_modelB= not %{$param_row;}
| |||||||||||||||||||||||||||||||||||||||||||
| 786 | } | ||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | # OK so we didnt find it via keys either in the cache or in the DB so that means | ||||||||||||||||||||||||||||||||||||||||||
| 789 | # we need to just create it. | ||||||||||||||||||||||||||||||||||||||||||
| 790 | 95 | ▶ | unless($related_model) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | 82 | debug 2, "Didn't find related model '$related' so making it"; | |||||||||||||||||||||||||||||||||||||||||
| 792 | 82 | $related_model = $self->new_related($related, +{}); | |||||||||||||||||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||||||||||||||||
| 794 | 95 | debug 2, "About to set_from_params_recursively for @{[ ref $related_model ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||
| 796 | 95 | ▶ | if($was_add) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 797 | 0 | debug 3, "Since @{[ ref $related_model]} was an _add we won't validate since its empty"; | |||||||||||||||||||||||||||||||||||||||||
| 798 | #$related_model->errors->add(undef, 'newwww'); | ||||||||||||||||||||||||||||||||||||||||||
| 799 | #$related_model->skip_validate; Need to leave this off for now because doesn't play well with new models | ||||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | $related_model->{__valiant_donot_insert} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | ▶ | $related_model->{__valiant_add} = 1 if $was_add; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | 95 | ▶ | if($allow_destroy) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 805 | 54 | $related_model->{__valiant_allow_destroy} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | # Don't set params for the found keys (waste of time, no change) | ||||||||||||||||||||||||||||||||||||||||||
| 809 | 95 | my %params_for_recursive = %$param_row; | |||||||||||||||||||||||||||||||||||||||||
| 810 | 95 | ▶ | delete @params_for_recursive{ keys %keys_used_to_find} if %keys_used_to_find; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 811 | 95 | $related_model->set_from_params_recursively(%params_for_recursive); | |||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||
| 813 | # Ok, so after set_from_params_recursively if the $related_model is not in storage | ||||||||||||||||||||||||||||||||||||||||||
| 814 | # we 'might' have enough info to actually load it now. | ||||||||||||||||||||||||||||||||||||||||||
| 815 | 95 | ▶ | if(!$related_model->in_storage && !$related_model->is_marked_for_deletion) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$related_model->in_storage or $related_model->is_marked_for_deletion
MC/DC50%1/2
$related_model->in_storage or $related_model->is_marked_for_deletion
$related_model->in_storage
$related_model->is_marked_for_deletion
Truth table66%2/3
A or B
A= $related_model->in_storageB= $related_model->is_marked_for_deletion
| |||||||||||||||||||||||||||||||||||||||||||
| 816 | # TODO: We should skip this if we've already tried a full PK | ||||||||||||||||||||||||||||||||||||||||||
| 817 | 82 | ▶ | my %ident = %{ $related_model->_storage_ident_condition ||+{} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$related_model->_storage_ident_condition || {}
MC/DC0%0/1
$related_model->_storage_ident_condition || {}
$related_model->_storage_ident_condition
Truth table50%1/2
A
A= $related_model->_storage_ident_condition
| |||||||||||||||||||||||||||||||||||||||||||
| 818 | 82 | my $fully_id = 1; | |||||||||||||||||||||||||||||||||||||||||
| 819 | 82 | foreach my $ident(keys %ident) { | |||||||||||||||||||||||||||||||||||||||||
| 820 | 104 | ▶ | next if defined($related_model->get_column($ident)); # can't try to load from DB if the PK isn't fully found | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 821 | 75 | $fully_id = 0; | |||||||||||||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||||||||||||
| 823 | 82 | ▶ | if($fully_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 824 | 8 | my $copy = $related_model->get_from_storage(); # If we now have the full PK we can find it | |||||||||||||||||||||||||||||||||||||||||
| 825 | 8 | ▶ | $related_model = $copy if $copy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||||||||||||||||
| 828 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | die "Not sure what to do with $param_row"; | |||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||
| 831 | 99 | push @related_models, $related_model; | |||||||||||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||
| 834 | # This bit we diff between the rows loaded in the orignal cache and what it looks | ||||||||||||||||||||||||||||||||||||||||||
| 835 | # like after the multi update. Rows that are no longer present are marked for | ||||||||||||||||||||||||||||||||||||||||||
| 836 | # deletion if deletion is allowed | ||||||||||||||||||||||||||||||||||||||||||
| 837 | my @new_pks = map { | ||||||||||||||||||||||||||||||||||||||||||
| 838 | 15 | my $r = $_; | |||||||||||||||||||||||||||||||||||||||||
| 839 | +{ | ||||||||||||||||||||||||||||||||||||||||||
| 840 | 15 | map { $_ => $r->$_ } $r->result_source->primary_columns | |||||||||||||||||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||||||||||||||||
| 842 | 57 | } grep { $_->in_storage } @related_models; | |||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||
| 844 | 57 | debug 1, "Reviewing existing rows for relation $related (total @{[ scalar @existing_rows ]}) for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||
| 846 | 57 | foreach my $current (@existing_rows) { | |||||||||||||||||||||||||||||||||||||||||
| 847 | next if grep { | ||||||||||||||||||||||||||||||||||||||||||
| 848 | 57 | ▶ | my %fields = %$_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 849 | my @matches = grep { | ||||||||||||||||||||||||||||||||||||||||||
| 850 | 33 | $current->get_column($_) eq $fields{$_} | |||||||||||||||||||||||||||||||||||||||||
| 851 | } keys %fields; | ||||||||||||||||||||||||||||||||||||||||||
| 852 | 33 | ▶ | scalar(@matches) == keys %fields ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 853 | } @new_pks; | ||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||
| 855 | 43 | debug 2, "ok its not a new one"; | |||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||
| 857 | # Only mark for deletion if its actually in store and the relationship | ||||||||||||||||||||||||||||||||||||||||||
| 858 | # opted into replace-set semantics via delete_omitted. | ||||||||||||||||||||||||||||||||||||||||||
| 859 | 43 | ▶ | if($current->in_storage && $delete_omitted) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$current->in_storage and $delete_omitted
MC/DC100%2/2
$current->in_storage and $delete_omitted
$current->in_storage
$delete_omitted
Truth table100%3/3
A and B
A= $current->in_storageB= $delete_omitted
| |||||||||||||||||||||||||||||||||||||||||||
| 860 | 22 | debug 2, "Marking $current for deletion"; | |||||||||||||||||||||||||||||||||||||||||
| 861 | 22 | $current->{__valiant_allow_destroy} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 862 | 22 | $current->mark_for_deletion; # might be a good idea to find a way to expose this via an override-able method | |||||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||||||
| 864 | # Mark its children as pruned, recursively | ||||||||||||||||||||||||||||||||||||||||||
| 865 | 22 | ▶ | my $cb; $cb = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 866 | 32 | my $row = shift; | |||||||||||||||||||||||||||||||||||||||||
| 867 | 32 | ▶ | debug 3, "marking $row to be pruned" unless $row->is_marked_for_deletion; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 868 | 32 | $row->{__valiant_is_pruned} = 1; # unless $row->is_marked_for_deletion; | |||||||||||||||||||||||||||||||||||||||||
| 869 | 32 | ▶ | my @related = keys %{$row->{_relationship_data}||+{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$row->{'_relationship_data'} || {}
MC/DC100%1/1
$row->{'_relationship_data'} || {}
$row->{'_relationship_data'}
Truth table100%2/2
A
A= $row->{'_relationship_data'}
| |||||||||||||||||||||||||||||||||||||||||||
| 870 | # TODO only do this for has_one, might_have, has_many | ||||||||||||||||||||||||||||||||||||||||||
| 871 | 32 | debug 3, "$row has related data to prune @{[ join ',', @related ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 872 | 32 | foreach $related(@related) { | |||||||||||||||||||||||||||||||||||||||||
| 873 | 11 | debug 3, "checking $related for possible pruning"; | |||||||||||||||||||||||||||||||||||||||||
| 874 | 11 | ▶ | my @rows = @{$row->related_resultset($related)->get_cache||[]}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$row->related_resultset($related)->get_cache || []
MC/DC100%1/1
$row->related_resultset($related)->get_cache || []
$row->related_resultset($related)->get_cache
Truth table100%2/2
A
A= $row->related_resultset($related)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 875 | 11 | debug 3, "found @{[ scalar @rows ]} to be pruned"; | |||||||||||||||||||||||||||||||||||||||||
| 876 | 11 | foreach my $inner_row (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 877 | 10 | $cb->($inner_row); | |||||||||||||||||||||||||||||||||||||||||
| 878 | } | ||||||||||||||||||||||||||||||||||||||||||
| 879 | } | ||||||||||||||||||||||||||||||||||||||||||
| 880 | 22 | }; | |||||||||||||||||||||||||||||||||||||||||
| 881 | 22 | ▶ | $cb->($current) if $current->is_marked_for_deletion; # We check because 'mark_for_deletion' will skip unless 'allow_destroy' is set | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 882 | } | ||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | 43 | ▶ | if ($current->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | 28 | debug 3, "Current model is in storage so put it into cache"; | |||||||||||||||||||||||||||||||||||||||||
| 886 | 28 | push @related_models, $current; | |||||||||||||||||||||||||||||||||||||||||
| 887 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 888 | 15 | debug 3, "Current model is not in storage so don't bother to cache it since its marked for delete anyway"; | |||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||
| 892 | 57 | debug 3, "About to save cache for @{[ ref $self ]} related resultset $related; has @{[ scalar @related_models ]} models to cache"; | |||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | 57 | $self->related_resultset($related)->set_cache(\@related_models); | |||||||||||||||||||||||||||||||||||||||||
| 895 | 57 | $self->{_relationship_data}{$related} = \@related_models; | |||||||||||||||||||||||||||||||||||||||||
| 896 | 57 | $self->{__valiant_related_resultset}{$related} = \@related_models; # we have a private copy | |||||||||||||||||||||||||||||||||||||||||
| 897 | } | ||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||
| 899 | 91 | ▶ | sub set_single_related_from_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_single_related_from_params: called CC=39 SCAR=37.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 900 | 91 | my ($self, $related, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 901 | 91 | my %nested = $self->result_class->accept_nested_for; | |||||||||||||||||||||||||||||||||||||||||
| 902 | 91 | my $allow_destroy = $self->_related_allow_destroy($related); | |||||||||||||||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||||||||||||||||
| 904 | # Is there an existing related object in the cache? If so then we | ||||||||||||||||||||||||||||||||||||||||||
| 905 | # will merge params with existing rather than create a new one or | ||||||||||||||||||||||||||||||||||||||||||
| 906 | # run a new query to find one. I 'think' that's the most expected behavior... | ||||||||||||||||||||||||||||||||||||||||||
| 907 | 91 | ▶ | my ($related_result) = @{ $self->related_resultset($related)->get_cache ||[] }; # its single so only one | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->related_resultset($related)->get_cache || []
MC/DC100%1/1
$self->related_resultset($related)->get_cache || []
$self->related_resultset($related)->get_cache
Truth table100%2/2
A
A= $self->related_resultset($related)->get_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 908 | 91 | ▶ | $related_result = $self->{_relationship_data}{$related} unless $related_result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | 91 | ▶ | if($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 911 | ## TODO I might need to do something else if $params is blessed | ||||||||||||||||||||||||||||||||||||||||||
| 912 | ## TODO I think we need the below logic to see if $params has a PK and then use | ||||||||||||||||||||||||||||||||||||||||||
| 913 | ## that as an override. | ||||||||||||||||||||||||||||||||||||||||||
| 914 | # | ||||||||||||||||||||||||||||||||||||||||||
| 915 | ## TODO this is probably wrong if $params has different FKs or unique fields | ||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | 18 | debug 2, "Found cached related_result $related for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 918 | 18 | ▶ | $related_result->{__valiant_allow_destroy} = 1 if $allow_destroy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 919 | 18 | $related_result->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 920 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 921 | 73 | debug 2, "No cached related_result $related for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 922 | 73 | ▶ | if(blessed($params)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 923 | 2 | debug 2, "related_result $related for @{[ ref $self ]} is blessed object"; | |||||||||||||||||||||||||||||||||||||||||
| 924 | 2 | $related_result = $params; | |||||||||||||||||||||||||||||||||||||||||
| 925 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 926 | 71 | debug 2, "related_result $related for @{[ ref $self ]} is hashed params"; | |||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||
| 928 | # When doing an update on single rels (has_one/belongs_to) you need to figure out | ||||||||||||||||||||||||||||||||||||||||||
| 929 | # if you are recursing an update into that exist rel or if you are replacing that | ||||||||||||||||||||||||||||||||||||||||||
| 930 | # rel with a new one (find_or_new). | ||||||||||||||||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||||||||||||||||
| 932 | 71 | ▶ | if($self->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 933 | 13 | my %local_params = %$params; | |||||||||||||||||||||||||||||||||||||||||
| 934 | 13 | my %cond = %{$self->result_source->relationship_info($related)->{cond}}; | |||||||||||||||||||||||||||||||||||||||||
| 935 | 13 | my @pks = $self->result_source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||
| 936 | 13 | foreach my $fk_proto (keys %cond) { | |||||||||||||||||||||||||||||||||||||||||
| 937 | 13 | my $pk_proto = $cond{$fk_proto}; | |||||||||||||||||||||||||||||||||||||||||
| 938 | 13 | my ($pk) = ($pk_proto=~m/^self\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 939 | 13 | my ($fk) = ($fk_proto=~m/^foreign\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 940 | 13 | ▶ | next unless grep { $_ eq $pk } @pks;; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||||||||||||||||
| 942 | 10 | ▶ | if(exists $local_params{$fk}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 943 | 1 | ▶ | unless($local_params{$fk} eq $self->get_column($pk)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 944 | DBIx::Class::Valiant::Util::Exception::BadParameterFK->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 945 | 1 | fk_field=>$fk, fk_value=>$local_params{$fk}, | |||||||||||||||||||||||||||||||||||||||||
| 946 | pk_field=>$pk, pk_value=>$self->get_column($pk), | ||||||||||||||||||||||||||||||||||||||||||
| 947 | related=>$related, me=>$self); | ||||||||||||||||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||||||||||||||||
| 949 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 950 | #$local_params{$fk} = $self->get_column($pk); # set it since that should help with the lookups | ||||||||||||||||||||||||||||||||||||||||||
| 951 | } | ||||||||||||||||||||||||||||||||||||||||||
| 952 | } | ||||||||||||||||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||||||||||||||||
| 954 | 12 | debug 2, "Updating related result '$related' for @{[ ref $self ]} "; | |||||||||||||||||||||||||||||||||||||||||
| 955 | 1 | my %pk = map { $_ => delete $local_params{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 956 | 12 | grep { exists $local_params{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 957 | $self->related_resultset($related)->result_source->primary_columns; | ||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||
| 959 | # If the user supplied the PK that means use that exact record and replace the current one | ||||||||||||||||||||||||||||||||||||||||||
| 960 | # also apply any updates to that record as indicated. | ||||||||||||||||||||||||||||||||||||||||||
| 961 | 12 | ▶ | if(%pk) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 962 | 1 | debug 3, "Updating with exact record matching pk"; | |||||||||||||||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||||||||||||||||
| 964 | 1 | $related_result = $self->result_source->related_source($related)->resultset->find($params); ## TODO shouldnt this be \%pk?? | |||||||||||||||||||||||||||||||||||||||||
| 965 | 1 | $related_result = $self->result_source->related_source($related)->resultset->find(\%pk); ## TODO shouldnt this be \%pk?? | |||||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||||||
| 967 | 1 | $self->set_from_related($related, $related_result); | |||||||||||||||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||||||||||||||||
| 969 | #my $rev_data = $self->result_source->reverse_relationship_info($related); | ||||||||||||||||||||||||||||||||||||||||||
| 970 | #my ($reverse_related) = keys %$rev_data; | ||||||||||||||||||||||||||||||||||||||||||
| 971 | #$related_result->set_from_related($reverse_related, $self) if $reverse_related; # Don't have this for might_have | ||||||||||||||||||||||||||||||||||||||||||
| 972 | 1 | ▶ | $related_result->{__valiant_allow_destroy} = 1 if $allow_destroy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 973 | 1 | $related_result->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 974 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 975 | 11 | debug 3, "Updating with record from params (no matching PK)"; | |||||||||||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||||||||||||
| 977 | # If the user did not give us a PK either; | ||||||||||||||||||||||||||||||||||||||||||
| 978 | # 1) if undate_only=>1 then update the current record (if existing; create otherwise) | ||||||||||||||||||||||||||||||||||||||||||
| 979 | # 2) otherwise create a new record off the parent. | ||||||||||||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||||||||||||
| 981 | 11 | ▶ | if(+{$self->result_class->accept_nested_for}->{$related}{update_only}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 982 | 9 | debug 3, 'update_only true'; | |||||||||||||||||||||||||||||||||||||||||
| 983 | 9 | $related_result = $self->related_resultset($related)->single; | |||||||||||||||||||||||||||||||||||||||||
| 984 | 9 | ▶ | unless($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 985 | #$related_result = $self->find_or_new_related($related, $params); # TODO should find from any unique keys on | ||||||||||||||||||||||||||||||||||||||||||
| 986 | # If one doesn't already exist that means 'make a new one' | ||||||||||||||||||||||||||||||||||||||||||
| 987 | 2 | $related_result = $self->new_related($related, $params); | |||||||||||||||||||||||||||||||||||||||||
| 988 | } | ||||||||||||||||||||||||||||||||||||||||||
| 989 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 990 | 2 | debug 3, "update_only false for rel $related on @{[ $self]}"; | |||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||
| 992 | 2 | my %uniques = $self->related_resultset($related)->result_source->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||
| 993 | 2 | ▶ | if(%uniques) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 994 | 2 | debug 4, "Have unique constraints to try to find on"; | |||||||||||||||||||||||||||||||||||||||||
| 995 | 2 | foreach my $unique_key (keys %uniques) { | |||||||||||||||||||||||||||||||||||||||||
| 996 | 6 | ▶ | next if $unique_key eq 'primary'; # already done | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 997 | 4 | debug 4, "checking key $unique_key for related $related"; | |||||||||||||||||||||||||||||||||||||||||
| 998 | 4 | my %keys_found = map { $_=>$params->{$_} } grep { exists $params->{$_} } @{$uniques{$unique_key}}; | |||||||||||||||||||||||||||||||||||||||||
| 999 | #$related_result = $self->find_related($related, \%keys_found); | ||||||||||||||||||||||||||||||||||||||||||
| 1000 | 4 | ▶ | next unless %keys_found; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1001 | 2 | $related_result = $self->result_source->related_source($related)->resultset->find(\%keys_found); | |||||||||||||||||||||||||||||||||||||||||
| 1002 | 2 | ▶ | if($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1003 | 1 | debug 4, "found result with unique key $unique_key"; | |||||||||||||||||||||||||||||||||||||||||
| 1004 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 1005 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1007 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||
| 1009 | #$related_result = $self->result_source->related_source($related)->resultset->find($params); # TODO problably shoulld search on unique keys only | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | 2 | ▶ | unless($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1011 | 1 | debug 3, "Did not find result for $related so creating new result"; | |||||||||||||||||||||||||||||||||||||||||
| 1012 | # $self is in storage so it 'shou;d be safe to do this; | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | 1 | $related_result = $self->new_related($related, $params); | |||||||||||||||||||||||||||||||||||||||||
| 1014 | #$related_result = $self->result_source->related_source($related)->resultset->new_result($params); | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||||||||||||||||
| 1017 | 2 | $self->set_from_related($related, $related_result); | |||||||||||||||||||||||||||||||||||||||||
| 1018 | #my $rev_data = $self->result_source->reverse_relationship_info($related); | ||||||||||||||||||||||||||||||||||||||||||
| 1019 | # my ($key) = keys(%$rev_data); | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | #$related_result->set_from_related($key, $self) if $key; # Don't have this for might_hav | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | 11 | ▶ | $related_result->{__valiant_allow_destroy} = 1 if $allow_destroy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1023 | 11 | $related_result->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 1024 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1026 | 58 | debug 2, "Find or new related result '$related' for @{[ ref $self ]} which is not saved yet"; | |||||||||||||||||||||||||||||||||||||||||
| 1027 | # Ok so the object isn't in storage, which means you don't have an database supplied PKs. So | ||||||||||||||||||||||||||||||||||||||||||
| 1028 | # can't do related_resultset since that always returns nothing (if $self doesn't exist in the DB | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | # there's nothing in the DB to find. | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | #$related_result = $self->find_or_new_related($related, $params); | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | #$related_result->set_from_params_recursively(%$params); | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | 58 | my %local_params = %$params; | |||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||
| 1034 | 58 | my %cond = %{$self->result_source->relationship_info($related)->{cond}}; | |||||||||||||||||||||||||||||||||||||||||
| 1035 | 58 | my @pks = $self->result_source->primary_columns; | |||||||||||||||||||||||||||||||||||||||||
| 1036 | |||||||||||||||||||||||||||||||||||||||||||
| 1037 | 58 | my $flag = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1038 | 58 | foreach my $fk_proto (keys %cond) { | |||||||||||||||||||||||||||||||||||||||||
| 1039 | 58 | my $pk_proto = $cond{$fk_proto}; | |||||||||||||||||||||||||||||||||||||||||
| 1040 | 58 | my ($pk) = ($pk_proto=~m/^self\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 1041 | 58 | my ($fk) = ($fk_proto=~m/^foreign\.(.+)$/); | |||||||||||||||||||||||||||||||||||||||||
| 1042 | 58 | ▶ | next unless grep { $_ eq $pk } @pks; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1043 | 46 | ▶ | next unless exists $local_params{$fk}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1044 | 1 | ▶ | $flag++ if defined($local_params{$fk}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1045 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||||||||||||
| 1047 | 58 | ▶ | if($flag == scalar(@pks)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | 1 | DBIx::Class::Valiant::Util::Exception::BadParameters->throw(related=>$related, me=>$self); | |||||||||||||||||||||||||||||||||||||||||
| 1049 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||||||||||||||||
| 1051 | 0 | my %pk = map { $_ => delete $local_params{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 1052 | 57 | grep { exists $local_params{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 1053 | $self->related_resultset($related)->result_source->primary_columns; | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||||||||||||
| 1055 | # If the user supplied the PK that means use that exact record and replace the current one | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | # also apply any updates to that record as indicated. | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | 57 | ▶ | if(%pk) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1058 | 0 | debug 3, "setting with exact record matching pk %pk"; | |||||||||||||||||||||||||||||||||||||||||
| 1059 | 0 | $related_result = $self->result_source->related_source($related)->resultset->find(\%pk); | |||||||||||||||||||||||||||||||||||||||||
| 1060 | # $self->set_from_related($related, $related_result); | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | 0 | ▶ | unless($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1062 | 0 | debug 2, "did not find related result for $related so creating new one"; | |||||||||||||||||||||||||||||||||||||||||
| 1063 | 0 | $related_result = $self->new_related($related, +{}); | |||||||||||||||||||||||||||||||||||||||||
| 1064 | # If the user supplied PK and we didn't find them we defintely need to set an error | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | # since that's a hacking attempt possibly | ||||||||||||||||||||||||||||||||||||||||||
| 1066 | 0 | $self->errors->add($related, _t('related_not_found'), {related=>$related_result->model_name->human}); | |||||||||||||||||||||||||||||||||||||||||
| 1067 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1068 | 0 | ▶ | $related_result->{__valiant_allow_destroy} = 1 if $allow_destroy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1069 | 0 | $related_result->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 1070 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1071 | 57 | debug 3, "No PK for $related, lets see if there's other ways to find it"; | |||||||||||||||||||||||||||||||||||||||||
| 1072 | 57 | ▶ | if(my $flag = $nested{$related}{find_with_uniques}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1073 | 18 | debug 3, "finding $related for $self with find_with_uniques matching"; | |||||||||||||||||||||||||||||||||||||||||
| 1074 | 18 | my %uniques = $self->result_source->related_source($related)->unique_constraints; | |||||||||||||||||||||||||||||||||||||||||
| 1075 | 18 | foreach my $unique (keys %uniques) { | |||||||||||||||||||||||||||||||||||||||||
| 1076 | 26 | ▶ | next if $unique eq 'primary'; # already done | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1077 | 18 | my @key_columns = @{$uniques{$unique}}; | |||||||||||||||||||||||||||||||||||||||||
| 1078 | 18 | my %matching = map { $_ => $params->{$_} } grep { exists $params->{$_} } @key_columns; | |||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||
| 1080 | 18 | ▶ | next unless scalar(@key_columns) == scalar(keys(%matching)); # only match when its a full key match | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1081 | 18 | debug 2, "key $unique exists in params"; | |||||||||||||||||||||||||||||||||||||||||
| 1082 | 18 | $related_result = $self->result_source->related_source($related)->resultset->find(\%matching); | |||||||||||||||||||||||||||||||||||||||||
| 1083 | 18 | ▶ | if($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1084 | 13 | debug 3, "Found result via unique keys"; | |||||||||||||||||||||||||||||||||||||||||
| 1085 | 13 | last; | |||||||||||||||||||||||||||||||||||||||||
| 1086 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1087 | 5 | debug 3, "Did not find result via unique keys"; | |||||||||||||||||||||||||||||||||||||||||
| 1088 | 5 | $related_result = $self->new_related($related, +{}); | |||||||||||||||||||||||||||||||||||||||||
| 1089 | # So this is tricky. Generally if there's no PK and create is allowed | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | # we should just create a new record. But if we arw saying 'find_with_uniques' | ||||||||||||||||||||||||||||||||||||||||||
| 1091 | # that means the unique is pretty much a key and possible we don't want to let | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | # a possible hacker slip in a new record. So unless find_with_uniques is | ||||||||||||||||||||||||||||||||||||||||||
| 1093 | # 'allow_create' we should set an error. | ||||||||||||||||||||||||||||||||||||||||||
| 1094 | 5 | ▶ | unless($flag eq 'allow_create') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1095 | 0 | $self->errors->add($related, _t('related_not_found'), {related=>$related_result->model_name->human}); | |||||||||||||||||||||||||||||||||||||||||
| 1096 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1097 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1099 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||||||||||||||||
| 1101 | 57 | ▶ | unless($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1102 | 39 | debug 3, "Did not find result for $related so creating new result"; | |||||||||||||||||||||||||||||||||||||||||
| 1103 | 39 | $related_result = $self->new_related($related, $params); | |||||||||||||||||||||||||||||||||||||||||
| 1104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1105 | 57 | ▶ | $self->set_from_related($related, $related_result) unless $self->in_storage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1106 | 57 | ▶ | $related_result->{__valiant_allow_destroy} = 1 if $allow_destroy; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1107 | 57 | $related_result->set_from_params_recursively(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 1108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||||||||||||||||
| 1113 | 89 | $self->related_resultset($related)->set_cache([$related_result]); | |||||||||||||||||||||||||||||||||||||||||
| 1114 | 89 | $self->{_relationship_data}{$related} = $related_result; | |||||||||||||||||||||||||||||||||||||||||
| 1115 | 89 | $self->{__valiant_related_resultset}{$related} = [$related_result]; | |||||||||||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||||||||||||
| 1117 | # ok so... what about the reverse side of this rel? Seems this makes for infinite recursion | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | #my $rev_data = $self->result_source->reverse_relationship_info($related); | ||||||||||||||||||||||||||||||||||||||||||
| 1119 | #my ($key) = keys(%$rev_data); | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | #$related_result->{_relationship_data}{$key} = $self; | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||
| 1123 | 74 | ▶ | sub mutate_recursively { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mutate_recursively: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1124 | 74 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1125 | 74 | debug 2, "mutating relationships for @{[ $self ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||||||||||
| 1127 | 74 | ▶ | $self->_mutate if $self->is_changed || $self->is_marked_for_deletion; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_changed or $self->is_marked_for_deletion
MC/DC100%2/2
$self->is_changed or $self->is_marked_for_deletion
$self->is_changed
$self->is_marked_for_deletion
Truth table100%3/3
A or B
A= $self->is_changedB= $self->is_marked_for_deletion
| |||||||||||||||||||||||||||||||||||||||||||
| 1128 | 74 | foreach my $related (keys %{$self->{_relationship_data}}) { | |||||||||||||||||||||||||||||||||||||||||
| 1129 | #next unless $self->related_resultset($related)->first; # TODO don't think I need this | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | 17 | debug 2, "mutating relationship $related"; | |||||||||||||||||||||||||||||||||||||||||
| 1131 | 17 | $self->_mutate_related($related); | |||||||||||||||||||||||||||||||||||||||||
| 1132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||||||||||||
| 1135 | 42 | ▶ | sub _mutate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutate: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1136 | 42 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1137 | 42 | ▶ | if($self->is_marked_for_deletion) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1138 | 18 | debug 2, "deleting @{[ ref $self ]} if in storage"; | |||||||||||||||||||||||||||||||||||||||||
| 1139 | 18 | $self->delete_if_in_storage; | |||||||||||||||||||||||||||||||||||||||||
| 1140 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1141 | 24 | debug 2, "update_or_insert for @{[ ref $self ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||||||||||||
| 1143 | # Ok so when doing update we are losing the relationed info so brute forse | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | # cache and restore it (probably wrong but passing tests for now....) | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||||||||||||
| 1146 | 24 | ▶ | my %rels = %{$self->{_relationship_data}||+{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'_relationship_data'} || {}
MC/DC100%1/1
$self->{'_relationship_data'} || {}
$self->{'_relationship_data'}
Truth table100%2/2
A
A= $self->{'_relationship_data'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1147 | 24 | ▶ | my %rels_rs = %{$self->{related_resultsets}||+{}}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'related_resultsets'} || {}
MC/DC100%1/1
$self->{'related_resultsets'} || {}
$self->{'related_resultsets'}
Truth table100%2/2
A
A= $self->{'related_resultsets'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1148 | |||||||||||||||||||||||||||||||||||||||||||
| 1149 | 24 | $self->update_or_insert; | |||||||||||||||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||||||||||||||||
| 1151 | # copies but quite likely now incorrect even thos my tests pass..... | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | # maybe I need a better way to restore this stuff. | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | 24 | $self->{_relationship_data} = \%rels; | |||||||||||||||||||||||||||||||||||||||||
| 1154 | 24 | ▶ | $self->{related_resultsets} = \%rels_rs if %rels_rs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||||||||||
| 1158 | 92 | ▶ | sub _mutate_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutate_related: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||||||||||||
| 1159 | 92 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1160 | 92 | my $rel_data = $self->relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 1161 | 92 | ▶ | unless($rel_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1162 | 9 | ▶ | if(my $rel_data = $self->_m2m_metadata->{$related}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1163 | 9 | return $self->_mutate_m2m_related($related, $rel_data); | |||||||||||||||||||||||||||||||||||||||||
| 1164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||||||||||||||||
| 1167 | 83 | my $rel_type = $rel_data->{attrs}{accessor}; | |||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||
| 1169 | 83 | ▶ | return $self->_mutate_single_related($related) if $rel_type eq 'single'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1170 | 26 | ▶ | return $self->_mutate_multi_related($related) if $rel_type eq 'multi'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||||||||||
| 1172 | 0 | die "not sure how to mutate $related of type $rel_type"; | |||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||||||||||||||
| 1175 | 9 | ▶ | sub _mutate_m2m_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutate_m2m_related: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1176 | 9 | my ($self, $related, $rel_data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1177 | 9 | my $relation = $rel_data->{relation}; | |||||||||||||||||||||||||||||||||||||||||
| 1178 | 9 | my $foreign_relation = $rel_data->{foreign_relation}; | |||||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||||||
| 1180 | 9 | return $self->_mutate_multi_related($relation); | |||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||
| 1183 | 35 | ▶ | sub _mutate_multi_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutate_multi_related: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 1184 | 35 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1185 | 35 | ▶ | my @related_results = @{ $self->{__valiant_related_resultset}{$related} ||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'__valiant_related_resultset'}{$related} || []
MC/DC100%1/1
$self->{'__valiant_related_resultset'}{$related} || []
$self->{'__valiant_related_resultset'}{$related}
Truth table100%2/2
A
A= $self->{'__valiant_related_resultset'}{$related}
| |||||||||||||||||||||||||||||||||||||||||||
| 1186 | 35 | my $rev_data = $self->result_source->reverse_relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 1187 | 35 | my ($reverse_related) = keys %$rev_data; | |||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||
| 1189 | 35 | debug 2, "Trying to mutate multi relations '$related'"; | |||||||||||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||||||||||||
| 1191 | 35 | foreach my $related_result (@related_results) { | |||||||||||||||||||||||||||||||||||||||||
| 1192 | 46 | ▶ | next unless $related_result->is_changed || $related_result->is_marked_for_deletion || !$related_result->in_storage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$related_result->is_changed or $related_result->is_marked_for_deletion or not $related_result->in_storage
$related_result->is_changed or $related_result->is_marked_for_deletion
MC/DC66%2/3
$related_result->is_changed or $related_result->is_marked_for_deletion or not $related_result->in_storage
$related_result->is_changed
$related_result->is_marked_for_deletion
not $related_result->in_storage
Truth table75%3/4
A or B or not(C)
A= $related_result->is_changedB= $related_result->is_marked_for_deletionC= not $related_result->in_storage
| |||||||||||||||||||||||||||||||||||||||||||
| 1193 | 26 | debug 3, "@{[ ref $related_result ]} ready for mutating"; | |||||||||||||||||||||||||||||||||||||||||
| 1194 | 26 | ▶ | $related_result->set_from_related($reverse_related, $self) if $reverse_related; # Don't have this for might_have | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1195 | 26 | $related_result->mutate_recursively; | |||||||||||||||||||||||||||||||||||||||||
| 1196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||||||||||||
| 1198 | # If the mutation completed we need to remove all marked for delation | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | 35 | my @new_results = grep { !$_->is_marked_for_deletion } @related_results; | |||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||
| 1201 | 35 | $self->related_resultset($related)->set_cache(\@new_results); | |||||||||||||||||||||||||||||||||||||||||
| 1202 | 35 | $self->{_relationship_data}{$related} = \@new_results; | |||||||||||||||||||||||||||||||||||||||||
| 1203 | 35 | $self->{__valiant_related_resultset}{$related} = [@new_results]; | |||||||||||||||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||||||||||||||||
| 1205 | # TODO need to update rels | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||
| 1209 | 57 | ▶ | sub _mutate_single_related { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mutate_single_related: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1210 | 57 | my ($self, $related) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1211 | #my ($related_result) = @{ $self->{__valiant_related_resultset}{$related} ||[] }; | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | #my ($related_result) = @{ $self->related_resultset($related)->get_cache ||[] }; | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | 57 | my $related_result = $self->{_relationship_data}{$related}; | |||||||||||||||||||||||||||||||||||||||||
| 1214 | 57 | ▶ | unless($related_result) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1215 | 9 | debug 2, "Skipping _mutate_single_related because related $related is not cached"; | |||||||||||||||||||||||||||||||||||||||||
| 1216 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||||||||||||||||
| 1219 | 48 | my $rev_data = $self->result_source->reverse_relationship_info($related); | |||||||||||||||||||||||||||||||||||||||||
| 1220 | 48 | my ($reverse_related) = keys %$rev_data; | |||||||||||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||||||||||||
| 1222 | 48 | debug 2, "Trying to mutate @{[ ref $related_result ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1223 | 48 | debug 3, "@{[ ref $related_result ]} is_changed: @{[ $related_result->is_changed]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1224 | 48 | debug 3, "@{[ ref $related_result ]} is_marked_for_deletion: @{[ $related_result->is_marked_for_deletion]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1225 | 48 | debug 3, "@{[ ref $related_result ]} in_storage: @{[ $related_result->in_storage]}"; | |||||||||||||||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||||||||||||||||
| 1227 | 48 | ▶ | if($related_result->is_changed || $related_result->is_marked_for_deletion || !$related_result->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$related_result->is_changed or $related_result->is_marked_for_deletion or not $related_result->in_storage
$related_result->is_changed or $related_result->is_marked_for_deletion
MC/DC66%2/3
$related_result->is_changed or $related_result->is_marked_for_deletion or not $related_result->in_storage
$related_result->is_changed
$related_result->is_marked_for_deletion
not $related_result->in_storage
Truth table75%3/4
A or B or not(C)
A= $related_result->is_changedB= $related_result->is_marked_for_deletionC= not $related_result->in_storage
| |||||||||||||||||||||||||||||||||||||||||||
| 1228 | 16 | debug 3, "@{[ ref $related_result ]} ready for mutating"; | |||||||||||||||||||||||||||||||||||||||||
| 1229 | 16 | ▶ | $related_result->set_from_related($reverse_related, $self) if $reverse_related; # Don't have this for might_have | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1230 | 16 | $related_result->mutate_recursively; | |||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||
| 1232 | # I think if its in storage we need to set cache and all even if marked for deletation | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | #my @new_cache = $related_result->is_marked_for_deletion ? () : ($related_result); | ||||||||||||||||||||||||||||||||||||||||||
| 1234 | 16 | $self->related_resultset($related)->set_cache([$related_result]); | |||||||||||||||||||||||||||||||||||||||||
| 1235 | 16 | $self->{__valiant_related_resultset}{$related} = [$related_result]; | |||||||||||||||||||||||||||||||||||||||||
| 1236 | 16 | $self->{_relationship_data}{$related} = $related_result; | |||||||||||||||||||||||||||||||||||||||||
| 1237 | 16 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||
| 1240 | # Just continue, we need to do this since related objects might have changed... | ||||||||||||||||||||||||||||||||||||||||||
| 1241 | 32 | $related_result->mutate_recursively; | |||||||||||||||||||||||||||||||||||||||||
| 1242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||
| 1244 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||||||
| 1246 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||||||||||||
| 1248 | DBIx::Class::Valiant::Result - Base component to add Valiant functionality | ||||||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||||||
| 1250 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||||||||||||
| 1252 | package Example::Schema::Result::Person; | ||||||||||||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||||||||||||
| 1254 | use base 'DBIx::Class::Core'; | ||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||
| 1256 | __PACKAGE__->load_components('Valiant::Result'); | ||||||||||||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||||||||||||
| 1258 | Or just add to your base Result class | ||||||||||||||||||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||
| 1261 | package Example::Schema::Result; | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||||||||||||||||
| 1263 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | use base 'DBIx::Class::Core'; | ||||||||||||||||||||||||||||||||||||||||||
| 1266 | |||||||||||||||||||||||||||||||||||||||||||
| 1267 | __PACKAGE__->load_components('Valiant::Result'); | ||||||||||||||||||||||||||||||||||||||||||
| 1268 | |||||||||||||||||||||||||||||||||||||||||||
| 1269 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||||||||||||||||
| 1271 | Glue L<Valiant> validations into your DBIC result sources. This package just has some basic | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | API level docs, you should see L<DBIx::Class::Valiant> for a somewhat more detailed documentation | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | and examples. | ||||||||||||||||||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||||||||||||||||
| 1275 | =head1 CONTEXTS | ||||||||||||||||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||||||||||||||||
| 1277 | When doing an insert / create on a result, we automatically add a 'create' context which you | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | can use to limit validations to create events. Additionally for an update we add an 'update' | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | context. | ||||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||
| 1281 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 1282 | |||||||||||||||||||||||||||||||||||||||||||
| 1283 | This component adds the following class or package methods to your result classes. Please note | ||||||||||||||||||||||||||||||||||||||||||
| 1284 | this is only class methods added by this package, it does not cover those which are aggregated | ||||||||||||||||||||||||||||||||||||||||||
| 1285 | from the L<Valiant::Validates> role. | ||||||||||||||||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||||||||||||||||
| 1287 | =head2 auto_validation (Boolean) | ||||||||||||||||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||||||||||||||||
| 1289 | Defaults to true. When true Valiant will first perform a validation on the existing result | ||||||||||||||||||||||||||||||||||||||||||
| 1290 | object (and any related objects nested under it which have been loaded from the DB or created) | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | and if there are validation errors will skip persisting the data to the database. You can use | ||||||||||||||||||||||||||||||||||||||||||
| 1292 | this to disable this behavior globally. Please not there are features to enable skipping auto | ||||||||||||||||||||||||||||||||||||||||||
| 1293 | validation on a per result/set basis as well. | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||||||||||||||||
| 1295 | =head2 accept_nested_for (field => \%options) | ||||||||||||||||||||||||||||||||||||||||||
| 1296 | |||||||||||||||||||||||||||||||||||||||||||
| 1297 | Allows you to update / create related objected which are nested under the parent (via has_one, | ||||||||||||||||||||||||||||||||||||||||||
| 1298 | might_have or has_many defined relationships). Accepts the following hashref of options: | ||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||
| 1300 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||
| 1302 | =item allow_destroy | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||||||||||||||||
| 1304 | By default you cannot delete related (nested) results. Setting this to true allows that. | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||
| 1306 | =item delete_omitted | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||
| 1308 | By default (Rails parity, matching C<accepts_nested_attributes_for>) rows omitted from | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | a C<has_many> update are left alone; the only way to delete a related row is an explicit | ||||||||||||||||||||||||||||||||||||||||||
| 1310 | C<_delete> request under C<allow_destroy>. Setting this to true opts back into the old | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | replace-the-whole-set behavior: any existing related row not present in the update gets | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | deleted, and passing C<< [] >> deletes the entire collection. Accepts a coderef (called | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | with the parent result) for conditional behavior, exactly like C<allow_destroy>. This | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | option is independent of C<allow_destroy> -- C<allow_destroy> only governs the explicit | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | C<_delete> request; C<delete_omitted> is itself sufficient permission for omission deletes. | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||||||||||||||||
| 1317 | =item reject_if | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||||||
| 1319 | A coderef that will cause a nested result to skip if you return true. Arguments are the | ||||||||||||||||||||||||||||||||||||||||||
| 1320 | parent result and a hashref of the values to be used for the nested build: | ||||||||||||||||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||||||||||||||||
| 1322 | __PACKAGE__->accept_nested_for( | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | might => { | ||||||||||||||||||||||||||||||||||||||||||
| 1324 | reject_if => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | my ($self, $params) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1326 | return ($params->{value}||'') eq 'test14' ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||
| 1327 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 1328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1329 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1330 | |||||||||||||||||||||||||||||||||||||||||||
| 1331 | Please note that if you have this on a C<has_many> relationship the code ref will be invoked | ||||||||||||||||||||||||||||||||||||||||||
| 1332 | on each result in the collection of related results you are attempted to nest values into. This | ||||||||||||||||||||||||||||||||||||||||||
| 1333 | can impact performance. | ||||||||||||||||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||||||||||||||||
| 1335 | =item limit | ||||||||||||||||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||||||||||||||||
| 1337 | accepts a scalar which will cause the nested results to fail of the number of items is | ||||||||||||||||||||||||||||||||||||||||||
| 1338 | greater than the scalar. | ||||||||||||||||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||||||||||||||||
| 1340 | =item update_only | ||||||||||||||||||||||||||||||||||||||||||
| 1341 | |||||||||||||||||||||||||||||||||||||||||||
| 1342 | For C<has_one> or C<might_have> relationships will force update the existing nested result (if | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | any exists) even if you fail to set the primary key. Otherwise the current record will be | ||||||||||||||||||||||||||||||||||||||||||
| 1344 | deleted and a new one inserted. Default is false. | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||||||||||||||||
| 1346 | =item find_with_uniques | ||||||||||||||||||||||||||||||||||||||||||
| 1347 | |||||||||||||||||||||||||||||||||||||||||||
| 1348 | Generally we only try to find a matching row in the DB if a primary key is given. If you set | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | this to true then we also try to match using and unique keys establish for the related row. | ||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||
| 1351 | If you set this flag to 'allow_create' then we will allow the creation of a new related row if | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | we can't find one in the DB. Otherwise we will set an error if the related row can't be found. | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | You should be careful here because allowing the creation of a new related row could be a security | ||||||||||||||||||||||||||||||||||||||||||
| 1354 | risk (a hacker could try to insert a new related row by guessing the unique keys). That's | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | why the default behavior is to set an error if the related row can't be found. | ||||||||||||||||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||||||||||||||||
| 1357 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||||||||||||||||
| 1359 | B<NOTE> Recursion warning! You cannot currently C<accept_nested_for> for a relationship | ||||||||||||||||||||||||||||||||||||||||||
| 1360 | whose target result source is setting C<accept_nested_for> into yourself. This is probably | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | fixable, patches and use cases welcomed! | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | |||||||||||||||||||||||||||||||||||||||||||
| 1363 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||
| 1365 | This component adds the following object methods. Please note | ||||||||||||||||||||||||||||||||||||||||||
| 1366 | this is only class methods added by this package, it does not cover those which are aggregated | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | from the L<Valiant::Validates> role. | ||||||||||||||||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||||||||||||||||
| 1369 | =head2 is_marked_for_deletion | ||||||||||||||||||||||||||||||||||||||||||
| 1370 | |||||||||||||||||||||||||||||||||||||||||||
| 1371 | Will be true if the result has been marked for deletion. You might see this in a related result | ||||||||||||||||||||||||||||||||||||||||||
| 1372 | nested under a parent when an update calls for the record to be deleted but validation errors prevented | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | the deletion from occuring. | ||||||||||||||||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||||||||||||||||
| 1375 | =head2 is_pruned | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||||||||||||||||
| 1377 | Will be true if the result is nested under a result which has been C<marked_for_deletion>. The result | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | is not itself marked to be deleted (if validation passes) but it will no longer be attached to the | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | parent result under which it is nested. | ||||||||||||||||||||||||||||||||||||||||||
| 1380 | |||||||||||||||||||||||||||||||||||||||||||
| 1381 | =head2 is_removed | ||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||
| 1383 | Is true if C<is_pruned> or C<is_marked_for_deletion> is true. | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||
| 1385 | =head2 build_related | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||||||||||||||||
| 1387 | =head2 build_related_if_empty | ||||||||||||||||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||||||||||||||||
| 1389 | Builds a related result into the cache. The result is only in memory; it can be used to run validation | ||||||||||||||||||||||||||||||||||||||||||
| 1390 | but is not inserted unless specified later. | ||||||||||||||||||||||||||||||||||||||||||
| 1391 | |||||||||||||||||||||||||||||||||||||||||||
| 1392 | You might use these methods if you are validating a nested results but the results are not already in | ||||||||||||||||||||||||||||||||||||||||||
| 1393 | the database (see the C<t/dbic> tests for examples). | ||||||||||||||||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||||||||||||||||
| 1395 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | |||||||||||||||||||||||||||||||||||||||||||
| 1397 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||||||||||||||
| 1399 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||||||||||||||||
| 1401 | L<Valiant>, L<DBIx::Class> | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||
| 1403 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||||||||||||||||
| 1405 | Copyright 2026, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||||||||||||||||
| 1407 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | |||||||||||||||||||||||||||||||||||||||||||
| 1410 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||||||||||||||||