blib/lib/DBIO/FilterColumn.pm

filter: Statementstmt 100% 85 / 85 Branchbran 65.2% 30 / 46 Conditioncond 65.2% 15 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 10 / 10 totaltotal 82.5% 161 / 195 CC 37 SCAR 15.8
CC
37
Cov
84%
filter_column
9.9
get_dirty_columns
5.5
_column_from_storage
5.2
CRAP 4.8
« blib/lib/DBIO/Storage/DBI/UniqueIdentifier.pm ↑ Summary blib/lib/DBIO/ChangeLog/Schema.pm »
1 package DBIO::FilterColumn;
2 # ABSTRACT: Automatically convert column data
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.900007';
7  
8 6 ▶use base 'DBIO::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use DBIO::Util qw(is_literal_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 8 ▶sub filter_column {
Subroutine filter_column: called CC=8 SCAR=22.9
Pod documented
14 8 my ($self, $col, $attrs) = @_;
15  
16 8 my $colinfo = $self->column_info($col);
17  
18 $self->throw_exception("FilterColumn can not be used on a column with a declared InflateColumn inflator")
19 8 ▶ if defined $colinfo->{_inflate_info} and $self->isa('DBIO::InflateColumn');
Branch: 1 branch
BranchTrueFalseTotal
if defined $colinfo->{"_inflate_info"} and $self->isa("DBIO::InflateColumn") 0 8 8
Condition33%1/3
defined $colinfo->{"_inflate_info"} and $self->isa("DBIO::InflateColumn")
!ll&&!rl&&r
800
MC/DC0%0/2
defined $colinfo->{"_inflate_info"} and $self->isa("DBIO::InflateColumn")
defined $colinfo->{"_inflate_info"} $self->isa("DBIO::InflateColumn")
Truth table33%1/3
A and B
A= defined $colinfo->{"_inflate_info"}B= $self->isa("DBIO::InflateColumn")
ABresult
0X0
100
111
20  
21 8 ▶ $self->throw_exception("No such column $col to filter")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($col) 0 8 8
22 unless $self->has_column($col);
23  
24 8 ▶ $self->throw_exception('filter_column expects a hashref of filter specifications')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "HASH" 0 8 8
25 unless ref $attrs eq 'HASH';
26  
27 $self->throw_exception('An invocation of filter_column() must specify either a filter_from_storage or filter_to_storage')
28 8 ▶ unless $attrs->{filter_from_storage} || $attrs->{filter_to_storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"} 0 8 8
Condition66%2/3
$attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"}
l!l&&r!l&&!r
710
MC/DC0%0/2
$attrs->{"filter_from_storage"} or $attrs->{"filter_to_storage"}
$attrs->{"filter_from_storage"} $attrs->{"filter_to_storage"}
Truth table66%2/3
A or B
A= $attrs->{"filter_from_storage"}B= $attrs->{"filter_to_storage"}
ABresult
1X1
011
000
29  
30 8 $colinfo->{_filter_info} = $attrs;
31 8 my $acc = $colinfo->{accessor};
32 8 ▶ $self->mk_group_accessors(filtered_column => [ (defined $acc ? $acc : $col), $col]);
Branch: 1 branch
BranchTrueFalseTotal
defined $acc ? : 0 8 8
33 8 return 1;
34 }
35  
36 2 ▶sub _column_from_storage {
Subroutine _column_from_storage: called CC=5 SCAR=16.6
37 2 my ($self, $col, $value) = @_;
38  
39 2 ▶ return $value if is_literal_value($value);
Branch: 1 branch
BranchTrueFalseTotal
if &is_literal_value($value) 0 2 2
40  
41 2 ▶ my $info = $self->result_source->column_info($col)
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->result_source->column_info($col) or $self->throw_exception("No column info for $col") 2 0 2
42 or $self->throw_exception("No column info for $col");
43  
44 2 ▶ return $value unless exists $info->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"_filter_info"} 0 2 2
45  
46 2 my $filter = $info->{_filter_info}{filter_from_storage};
47  
48 2 ▶ return defined $filter ? $self->$filter($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $filter ? : 1 1 2
49 }
50  
51 31 ▶sub _column_to_storage {
Subroutine _column_to_storage: called CC=5 SCAR=16.6
52 31 my ($self, $col, $value) = @_;
53  
54 31 ▶ return $value if is_literal_value($value);
Branch: 1 branch
BranchTrueFalseTotal
if &is_literal_value($value) 2 29 31
55  
56 29 ▶ my $info = $self->result_source->column_info($col) or
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->result_source->column_info($col) or $self->throw_exception("No column info for $col") 29 0 29
57 $self->throw_exception("No column info for $col");
58  
59 29 ▶ return $value unless exists $info->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"_filter_info"} 0 29 29
60  
61 29 my $unfilter = $info->{_filter_info}{filter_to_storage};
62  
63 29 ▶ return defined $unfilter ? $self->$unfilter($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $unfilter ? : 29 0 29
64 }
65  
66  
67 3 ▶sub get_filtered_column {
Subroutine get_filtered_column: called CC=3 SCAR=11.0
Pod documented
68 3 my ($self, $col) = @_;
69  
70 $self->throw_exception("$col is not a filtered column")
71 3 ▶ unless exists $self->result_source->column_info($col)->{_filter_info};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->result_source->column_info($col)->{"_filter_info"} 0 3 3
72  
73 return $self->{_filtered_column}{$col}
74 3 ▶ if exists $self->{_filtered_column}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_filtered_column"}{$col} 1 2 3
75  
76 2 my $val = $self->get_column($col);
77  
78 2 return $self->{_filtered_column}{$col} = $self->_column_from_storage(
79 $col, $val
80 );
81 }
82  
83 108 ▶sub get_column {
Subroutine get_column: called CC=3 SCAR=11.0
Pod documented
84 108 my ($self, $col) = @_;
85  
86 ! exists $self->{_column_data}{$col}
87 and
88 exists $self->{_filtered_column}{$col}
89 and
90 $self->{_column_data}{$col} = $self->_column_to_storage (
91 108 ▶ $col, $self->{_filtered_column}{$col}
Branch: 1 branch
BranchTrueFalseTotal
not exists $self->{"_column_data"}{$col} and exists $self->{"_filtered_column"}{$col} and $self->{"_column_data"}{$col} = $self->_column_to_storage($col, $self->{"_filtered_column"}{$col}) 15 93 108
Condition100%3/3
not exists $self->{"_column_data"}{$col} and exists $self->{"_filtered_column"}{$col}
!ll&&!rl&&r
91215
MC/DC100%2/2
not exists $self->{"_column_data"}{$col} and exists $self->{"_filtered_column"}{$col}
not exists $self->{"_column_data"}{$col} exists $self->{"_filtered_column"}{$col}
Truth table100%3/3
not(A) and B
A= not exists $self->{"_column_data"}{$col}B= exists $self->{"_filtered_column"}{$col}
ABresult
0X0
100
111
92 );
93  
94 108 return $self->next::method ($col);
95 }
96  
97 # sadly a separate codepath in Row.pm ( used by insert() )
98 13 ▶sub get_columns {
Subroutine get_columns: called CC=3 SCAR=11.0
Pod documented
99 13 my $self = shift;
100  
101 $self->{_column_data}{$_} = $self->_column_to_storage (
102 $_, $self->{_filtered_column}{$_}
103 13 ) for grep
104 11 { ! exists $self->{_column_data}{$_} }
105 13 ▶ keys %{$self->{_filtered_column}||{}}
Condition100%2/2
$self->{'_filtered_column'} || {}
l!l
94
MC/DC100%1/1
$self->{'_filtered_column'} || {}
$self->{'_filtered_column'}
Truth table100%2/2
A
A= $self->{'_filtered_column'}
Aresult
11
00
106 ;
107  
108 13 $self->next::method (@_);
109 }
110  
111 # and *another* separate codepath, argh!
112 8 ▶sub get_dirty_columns {
Subroutine get_dirty_columns: called CC=5 SCAR=17.1
Pod documented
113 8 my $self = shift;
114  
115 $self->{_dirty_columns}{$_}
116 and
117 ! exists $self->{_column_data}{$_}
118 and
119 $self->{_column_data}{$_} = $self->_column_to_storage (
120 $_, $self->{_filtered_column}{$_}
121 )
122 8 ▶ for keys %{$self->{_filtered_column}||{}};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_dirty_columns"}{$_} and not exists $self->{"_column_data"}{$_} and $self->{"_column_data"}{$_} = $self->_column_to_storage($_, $self->{"_filtered_column"}{$_}) 0 8 8
Condition60%3/5
$self->{'_filtered_column'} || {}
l!l
80
$self->{"_dirty_columns"}{$_} and not exists $self->{"_column_data"}{$_}
!ll&&!rl&&r
440
MC/DC0%0/1
$self->{'_filtered_column'} || {}
$self->{'_filtered_column'}
MC/DC0%0/2
$self->{"_dirty_columns"}{$_} and not exists $self->{"_column_data"}{$_}
$self->{"_dirty_columns"}{$_} not exists $self->{"_column_data"}{$_}
Truth table50%1/2
A
A= $self->{'_filtered_column'}
Aresult
11
00
Truth table66%2/3
A and not(B)
A= $self->{"_dirty_columns"}{$_}B= not exists $self->{"_column_data"}{$_}
ABresult
0X0
100
111
123  
124 8 $self->next::method(@_);
125 }
126  
127 48 ▶sub store_column {
Subroutine store_column: called CC=1 SCAR=0.0
Pod documented
128 48 my ($self, $col) = (shift, @_);
129  
130 # blow cache
131 48 delete $self->{_filtered_column}{$col};
132  
133 48 $self->next::method(@_);
134 }
135  
136 18 ▶sub has_column_loaded {
Subroutine has_column_loaded: called CC=2 SCAR=6.9
Pod documented
137 18 my ($self, $col) = @_;
138 18 ▶ return 1 if exists $self->{_filtered_column}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_filtered_column"}{$col} 2 16 18
139 16 return $self->next::method($col);
140 }
141  
142  
143 32 ▶sub set_filtered_column {
Subroutine set_filtered_column: called CC=3 SCAR=11.0
Pod documented
144 32 my ($self, $col, $filtered) = @_;
145  
146 # unlike IC, FC does not need to deal with the 'filter' abomination
147 # thus we can short-curcuit filtering entirely and never call set_column
148 # in case this is already a dirty change OR the row never touched storage
149 32 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->in_storage or $self->is_column_changed($col)) { } 23 9 32
Condition66%2/3
not $self->in_storage or $self->is_column_changed($col)
l!l&&r!l&&!r
2309
MC/DC50%1/2
not $self->in_storage or $self->is_column_changed($col)
not $self->in_storage $self->is_column_changed($col)
Truth table66%2/3
not(A) or B
A= not $self->in_storageB= $self->is_column_changed($col)
ABresult
1X1
011
000
150 ! $self->in_storage
151 or
152 $self->is_column_changed($col)
153 ) {
154 23 $self->make_column_dirty($col);
155 23 delete $self->{_column_data}{$col};
156 }
157 else {
158 9 $self->set_column($col, $self->_column_to_storage($col, $filtered));
159 };
160  
161 32 return $self->{_filtered_column}{$col} = $filtered;
162 }
163  
164 4 ▶sub prepare_update {
Subroutine prepare_update: called CC=5 SCAR=16.2
Pod documented
165 4 my ($self, $data, @rest) = @_;
166  
167 4 my $colinfos = $self->result_source->columns_info;
168  
169 4 ▶ foreach my $col (keys %{$data||{}}) {
Condition50%1/2
$data || {}
l!l
40
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
170 8 ▶ if ( exists $colinfos->{$col}{_filter_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $colinfos->{$col}{"_filter_info"}) 4 4 8
171 4 $self->set_filtered_column($col, delete $data->{$col});
172  
173 # FIXME update() reaches directly into the object-hash
174 # and we may *not* have a filtered value there - thus
175 # the void-ctx filter-trigger
176 4 ▶ $self->get_column($col) unless exists $self->{_column_data}{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_column_data"}{$col} 0 4 4
177 }
178 }
179  
180 4 return $self->next::method($data, @rest);
181 }
182  
183 21 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
184 21 my ($class, $data, @rest) = @_;
185  
186 my $rsrc = $data->{-result_source}
187 21 ▶ or $class->throw_exception('Sourceless rows are not supported with DBIO::FilterColumn');
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = $data->{"-result_source"} or $class->throw_exception("Sourceless rows are not supported with DBIO::FilterColumn") 21 0 21
188  
189 21 my $obj = $class->next::method($data, @rest);
190  
191 21 my $colinfos = $rsrc->columns_info;
192  
193 21 ▶ foreach my $col (keys %{$data||{}}) {
Condition50%1/2
$data || {}
l!l
210
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
194 21 ▶ if (exists $colinfos->{$col}{_filter_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $colinfos->{$col}{"_filter_info"}) 21 0 21
195 21 $obj->set_filtered_column($col, $data->{$col});
196 }
197 }
198  
199 21 return $obj;
200 }
201  
202 6 1;
203  
204 __END__
« blib/lib/DBIO/Storage/DBI/UniqueIdentifier.pm ↑ Summary blib/lib/DBIO/ChangeLog/Schema.pm »