blib/lib/Class/Inflate.pm

filter: Statementstmt 87.6% 516 / 589 Branchbran 64.2% 149 / 232 Conditioncond 56.6% 47 / 83 MC/DCmcdc 34.6% 18 / 52 Subroutinesub 94.2% 33 / 35 Podpod 0.0% 0 / 16 totaltotal 75.7% 763 / 1007 CC 185 SCAR 36.4
CC
185
Cov
79%
inflation_sql
77.5
__ANON__
70.7
filter_values
45.2
CRAP 38.2
↑ Summary
1 package Class::Inflate;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 require Exporter;
8  
9 2 our @ISA = qw(Exporter);
10 2 our @EXPORT_OK = qw(inflate commit obliterate);
11 2 our @EXPORT = ('inflate'); # @EXPORT_OK;
12 2 our $VERSION = '0.07';
13 2 $::OBJECT = undef;
14  
15 2 ▶use Devel::Messenger qw(note);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Preloaded methods go here.
18  
19 2 ▶sub import {
Subroutine import: called CC=2 SCAR=7.2
20 # allows 'use' syntax
21 2 my ($class, @args) = @_;
22 2 ▶ @args and $class->make(@args);
Branch: 1 branch
BranchTrueFalseTotal
@args and $class->make(@args) 2 0 2
23 }
24  
25 2 ▶sub make {
Subroutine make: called CC=3 SCAR=11.0
Pod undocumented
26 # sets up glue to database based on 'use' statement
27 2 my ($generator_class, @args) = @_;
28 2 my $target_class = $generator_class->find_target_class;
29 2 my %persist = ();
30 2 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 6 2 8
31 6 my ($table, $config) = splice(@args, 0, 2);
32 6 $persist{$table} = $config;
33 }
34 # $generator_class->_install_method($target_class, $sub_name, $code);
35 2 foreach my $export (@EXPORT) {
36 2 my $generate_export = '_' . $export;
37 2 $generator_class->$generate_export($target_class, \%persist, $export);
38 }
39 }
40  
41 2 ▶sub find_target_class {
Subroutine find_target_class: called CC=3 SCAR=11.0
Pod undocumented
42 # determines where to export generated methods
43 2 my $class;
44 2 my $c = 0;
45 2 while (1) {
46 6 $class = (caller($c++))[0];
47 last unless ($class->isa('Class::Inflate') and
48 6 ▶ &{$class->can('ima_generator')});
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Class::Inflate") and &{$class->can("ima_generator");} 2 4 6
Condition66%2/3
$class->isa("Class::Inflate") and &{$class->can("ima_generator");}
!ll&&!rl&&r
204
MC/DC50%1/2
$class->isa("Class::Inflate") and &{$class->can("ima_generator");}
$class->isa("Class::Inflate") &{$class->can("ima_generator");}
Truth table66%2/3
A and B
A= $class->isa("Class::Inflate")B= &{$class->can("ima_generator");}
ABresult
0X0
100
111
49 }
50 2 return $class;
51 }
52  
53 4 ▶sub ima_generator {
Subroutine ima_generator: called CC=1 SCAR=0.0
Pod undocumented
54 # a subclass may redefine this to return 0, if it wishes
55 # to allow methods added to itself
56 4 1;
57 }
58  
59 2 ▶sub _install_method {
Subroutine _install_method: called CC=2 SCAR=7.0
60 # exports method to target class
61 2 my $generator_class = shift;
62 2 my $target_class = shift;
63 2 my $accessor = shift;
64 2 my $code = shift;
65 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶ unless (defined *{"$target_class\::$accessor"}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *{"${target_class}::$accessor";}{"CODE"}) 2 0 2
67 2 note \7, "building accessor '$target_class\::$accessor'\n";
68 2 return *{"$target_class\::$accessor"} = $code;
69 }
70 0 return;
71 }
72  
73 2 ▶sub _inflate {
Subroutine _inflate: called CC=1 SCAR=0.0
74 2 my $generator_class = shift;
75 2 my $target_class = shift;
76 2 my $persist = shift;
77 2 my $sub_name = shift;
78 3 ▶ $generator_class->_install_method($target_class, $sub_name, sub {
Subroutine __ANON__: called CC=15 SCAR=27.2
79 3 my $self = shift;
80 3 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 3 0 3
81 3 my $dbh = shift;
82 3 ▶ my $filter = shift || $self;
Condition33%1/3
shift() || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $self
shift() $self
Truth table33%1/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
83 3 my @sql = inflation_sql($class, $filter, $persist);
84 3 my @records = (); # in object format
85 3 my @data = (); # in table/field format
86 3 my %rows_fetched = ();
87 3 my %awaiting_join = ();
88 3 foreach my $sql (@sql) {
89 9 my @r = fetchrows($class, $dbh, $sql->{query}, $sql->{bind});
90 9 $rows_fetched{$sql->{table}} = \@r;
91 9 ▶ if (exists($persist->{$sql->{table}}{join}) and keys %{$persist->{$sql->{table}}{join}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$persist{$sql->{'table'}}{'join'} and keys %{$$persist{$sql->{'table'}}{'join'};}) { } 6 3 9
Condition100%3/3
exists $$persist{$sql->{"table"}}{"join"} and keys %{$$persist{$sql->{"table"}}{"join"};}
!ll&&!rl&&r
216
MC/DC100%2/2
exists $$persist{$sql->{"table"}}{"join"} and keys %{$$persist{$sql->{"table"}}{"join"};}
exists $$persist{$sql->{"table"}}{"join"} keys %{$$persist{$sql->{"table"}}{"join"};}
Truth table100%3/3
A and B
A= exists $$persist{$sql->{"table"}}{"join"}B= keys %{$$persist{$sql->{"table"}}{"join"};}
ABresult
0X0
100
111
92 6 foreach my $table (keys %{$persist->{$sql->{table}}{join}}) {
93 6 note \3, "want to join $sql->{table} to $table\n";
94 6 ▶ if (@data and $rows_fetched{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data and $rows_fetched{$table}) { } 3 3 6
Condition66%2/3
@data and $rows_fetched{$table}
!ll&&!rl&&r
303
MC/DC50%1/2
@data and $rows_fetched{$table}
@data $rows_fetched{$table}
Truth table66%2/3
A and B
A= @dataB= $rows_fetched{$table}
ABresult
0X0
100
111
95 3 join_records($class, $persist, $table, \@data, $sql->{table}, \@r);
96 } else {
97 3 ▶ $awaiting_join{$table} ||= [];
Condition100%2/2
$awaiting_join{$table} ||= []
l!l
12
MC/DC100%1/1
$awaiting_join{$table} ||= []
$awaiting_join{$table}
Truth table100%2/2
A
A= $awaiting_join{$table}
Aresult
11
00
98 3 push @{$awaiting_join{$table}}, $sql->{table};
99 3 note \3, " will wait till we have $table data\n";
100 }
101 }
102 } else {
103 # this is the master/parent table
104 3 note \3, "populating dataset from $sql->{table}\n";
105 3 $rows_fetched{$sql->{table}} = @r;
106 3 @data = map { { $sql->{table} => [$_] } } splice @r;
107 3 note \3, "placed " . scalar(@data) . ' of ' . scalar(@r) . " records in dataset\n";
108 }
109 9 ▶ if (@data and $awaiting_join{$sql->{table}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data and $awaiting_join{$sql->{"table"}}) 2 7 9
Condition100%3/3
@data and $awaiting_join{$sql->{"table"}}
!ll&&!rl&&r
342
MC/DC100%2/2
@data and $awaiting_join{$sql->{"table"}}
@data $awaiting_join{$sql->{"table"}}
Truth table100%3/3
A and B
A= @dataB= $awaiting_join{$sql->{"table"}}
ABresult
0X0
100
111
110 2 my $join_awaiting_records;
111 # need to recursively call this foreach loop
112 5 ▶ $join_awaiting_records = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
113 5 my $waiting = shift;
114 5 ▶ return unless exists($awaiting_join{$waiting});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $awaiting_join{$waiting} 3 2 5
115 2 note \3, "now we have $waiting data\n";
116 2 ▶ while (@{$awaiting_join{$waiting}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$awaiting_join{$waiting};}) 3 2 5
117 3 my $table = shift @{$awaiting_join{$waiting}};
118 3 join_records($class, $persist, $waiting, \@data, $table, $rows_fetched{$table});
119 3 $join_awaiting_records->($table);
120 }
121 2 };
122 2 $join_awaiting_records->($sql->{table});
123 }
124 }
125 # TODO remove Dumper
126 #use Data::Dumper;
127 #note "dataset:\n" . Dumper(\@data) . "\n";
128 3 ▶ my $inflated_object = inflated_object($class, $persist, \@data, [ref($self) ? $self : ()]);
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 3 0 3
129 3 ▶ while (my $record = $inflated_object->($dbh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = &$inflated_object($dbh)) 3 3 6
130 3 push @records, $record;
131 }
132 3 ▶ return @records if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 0 3
133 0 return \@records;
134 2 });
135 }
136  
137 3 ▶sub inflation_sql {
Subroutine inflation_sql: called CC=61 SCAR=43.5
Pod undocumented
138 3 my $class = shift;
139 3 my $filter = shift;
140 3 my $persist = shift;
141 3 ▶ die "inflate filter must be a HASH\n" unless (UNIVERSAL::isa($filter, 'HASH'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($filter, "HASH") 0 3 3
142 3 my @sql = ();
143 3 my $inflate = [];
144 3 foreach my $table (keys %$persist) {
145 9 push @$inflate, keys %{$persist->{$table}{methods}};
146 }
147 3 my $method_tables = method_tables($class, $persist);
148 3 my $inflation_fields = inflation_fields($class, $inflate, $persist, $method_tables);
149 3 my $filter_values = filter_values($class, $filter, $persist, $method_tables);
150 3 my %join_fields = (); # fields we must select because we will use them to match records
151 # newer code
152 3 foreach my $table (keys %$inflation_fields) {
153 9 note \5, "building query for table $table\n";
154 9 my $table_filter = add_filter_defaults($class, $filter_values, $persist, $table);
155 9 my @tables = ();
156 9 my @fields = ();
157 9 my @filter = ();
158 9 my @bind = ();
159 9 my $alias = '';
160 9 ▶ my $has_filter = exists($filter_values->{$table}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $filter_values->{$table} ? : 3 6 9
161 9 my $has_external_filter = scalar(keys %$filter_values) > $has_filter;
162 9 ▶ if ($has_external_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_external_filter) { } 6 3 9
163 # see if we can join to the external tables
164 6 my $matched = 0;
165 6 my $need_join = 0;
166 # add current table and fields
167 6 $alias = 'a';
168 6 my %table_alias = ( $table => $alias );
169 6 push @tables, $table . ' ' . $table_alias{$table};
170 6 push @fields, map { $table_alias{$table} . '.' . $_ } @{$inflation_fields->{$table}};
171 6 foreach my $external (keys %$filter_values) {
172 6 ▶ next if ($table eq $external);
Branch: 1 branch
BranchTrueFalseTotal
if $table eq $external 0 6 6
173 # add external table
174 6 $table_alias{$external} = ++$alias;
175 # see if all the fields in the filter join to fields in our table
176 #foreach my $field (@{$filter_values->{$external}{fields}}) {
177 # if (exists($persist->{$table}{join}{$external}) and exists($persist->{$table}{join}{$external}{$field})) {
178 # my $my_field_name = $persist->{$table}{join}{$external}{$field};
179 # # TODO get value right now?
180 # $matched++;
181 # } else {
182 # $need_join++;
183 # }
184 #}
185 }
186 # add join (field = field) to filter
187 6 my @aliases = keys %table_alias;
188 6 foreach my $external (@aliases) {
189 12 ▶ next if ($external eq $table);
Branch: 1 branch
BranchTrueFalseTotal
if $external eq $table 6 6 12
190 6 note \6, "will need to join from $table to $external\n";
191 6 ▶ my @pkey = exists($persist->{$table}{key}) ? @{$persist->{$table}{key}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $persist->{$table}{'key'} ? : 6 0 6
192 # iterate through tables we know how to join to
193 6 my @partial_forward_path = ();
194 6 ▶ my $next_path = join_path($persist, $table, $external, \@partial_forward_path, exists($persist->{$table}{join}) ? keys %{$persist->{$table}{join}} : ());
Branch: 1 branch
BranchTrueFalseTotal
exists $persist->{$table}{'join'} ? : 6 0 6
195 6 my @paths = ();
196 6 my ($path, $join) = run_path_iterator($next_path, $persist->{$table}, 0);
197 6 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 4 2 6
198 # if @path, but not all primary key fields are used in join, check for additional paths and possibly use multiple paths
199 6 ▶ while (@$path and grep { !$join->{$_} } @pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path and grep {not $join->{$_};} @pkey) 2 6 8
Condition100%3/3
@$path and grep {not $join->{$_};} @pkey
!ll&&!rl&&r
422
MC/DC100%2/2
@$path and grep {not $join->{$_};} @pkey
@$path grep {not $join->{$_};} @pkey
Truth table100%3/3
A and B
A= @$pathB= grep {not $join->{$_};} @pkey
ABresult
0X0
100
111
200 2 note \6, "checking for additional join paths\n";
201 2 ($path, $join) = run_path_iterator($next_path, $persist->{$table}, 0);
202 2 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 0 2 2
203 }
204 6 my $static_from = $table;
205 6 my $middle_join = 0;
206 6 ▶ if (!@paths and exists($persist->{$external}{join})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @paths and exists $persist->{$external}{"join"}) 2 4 6
Condition66%2/3
not @paths and exists $persist->{$external}{"join"}
!ll&&!rl&&r
402
MC/DC50%1/2
not @paths and exists $persist->{$external}{"join"}
not @paths exists $persist->{$external}{"join"}
Truth table66%2/3
not(A) and B
A= not @pathsB= exists $persist->{$external}{"join"}
ABresult
0X0
100
111
207 2 note \6, "checking for reverse join definition\n";
208 2 my @partial_reverse_path = ();
209 2 my $reverse_path = join_path($persist, $external, $table, \@partial_reverse_path, keys %{$persist->{$external}{join}});
210 2 ($path, $join) = run_path_iterator($reverse_path, $persist->{$table}, -1);
211 2 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 1 1 2
212 # if @path, but not all primary key fields are used in join, check for additional paths and possibly use multiple paths
213 2 ▶ while (@$path and grep { !$join->{$_} } @pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path and grep {not $join->{$_};} @pkey) 1 2 3
Condition66%2/3
@$path and grep {not $join->{$_};} @pkey
!ll&&!rl&&r
201
MC/DC50%1/2
@$path and grep {not $join->{$_};} @pkey
@$path grep {not $join->{$_};} @pkey
Truth table66%2/3
A and B
A= @$pathB= grep {not $join->{$_};} @pkey
ABresult
0X0
100
111
214 1 note \6, "checking for additional join paths\n";
215 1 ($path, $join) = run_path_iterator($reverse_path, $persist->{$table}, -1);
216 1 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 0 1 1
217 }
218 2 $static_from = $external;
219 2 ▶ if (!@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 1 1 2
220 # check for a common table both ends know how to join to, if there is no direct join path
221 1 my $next_path = meet_in_the_middle($table, $external, \@partial_forward_path, \@partial_reverse_path);
222 1 my ($path, $join) = run_path_iterator($next_path, $persist->{$table}, 0);
223 1 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 1 0 1
224 # if @path, but not all primary key fields are used in join, check for additional paths and possibly use multiple paths
225 1 ▶ while (@$path and grep { !$join->{$_} } @pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path and grep {not $join->{$_};} @pkey) 1 1 2
Condition66%2/3
@$path and grep {not $join->{$_};} @pkey
!ll&&!rl&&r
101
MC/DC50%1/2
@$path and grep {not $join->{$_};} @pkey
@$path grep {not $join->{$_};} @pkey
Truth table66%2/3
A and B
A= @$pathB= grep {not $join->{$_};} @pkey
ABresult
0X0
100
111
226 1 note \6, "checking for additional join paths\n";
227 1 ($path, $join) = run_path_iterator($next_path, $persist->{$table}, 0);
228 1 ▶ push @paths, $path if @$path;
Branch: 1 branch
BranchTrueFalseTotal
if @$path 0 1 1
229 }
230 1 ▶ if (@paths) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paths) 1 0 1
231 1 $static_from = $table;
232 1 $middle_join = 1;
233 }
234 }
235 }
236 6 foreach my $path (@paths) {
237 #my $from = $static_from;
238 6 my @from_table = $static_from;
239 6 ▶ if ($middle_join) {
Branch: 1 branch
BranchTrueFalseTotal
if ($middle_join) { } 1 5 6
240 1 @from_table = ($table, $external);
241 1 note \7, "will look for join columns between $table and $external, forwards and backwards\n";
242 } else {
243 5 note \7, "will look for join columns between $table and $external, forwards only\n";
244 }
245 6 foreach my $from (@from_table) {
246 7 my @path = @$path;
247 7 ▶ if ($middle_join and $from eq $external) {
Branch: 1 branch
BranchTrueFalseTotal
if ($middle_join and $from eq $external) { } 1 6 7
Condition100%3/3
$middle_join and $from eq $external
!ll&&!rl&&r
511
MC/DC100%2/2
$middle_join and $from eq $external
$middle_join $from eq $external
Truth table100%3/3
A and B
A= $middle_joinB= $from eq $external
ABresult
0X0
100
111
248 1 note \6, "looking for reverse join columns ($external to $table)\n";
249 1 @path = reverse @path;
250 1 shift @path; # remove $external from list
251 } else {
252 6 note \6, "looking for join columns ($table to $external)\n";
253 }
254 7 note \6, "path: " . join(' -> ', $from, @path) . "\n";
255 7 foreach my $step (@path) {
256 # add external table
257 8 ▶ my $t = ($step eq $table) ? $external : $step;
Branch: 1 branch
BranchTrueFalseTotal
$step eq $table ? : 1 7 8
258 8 ▶ $table_alias{$t} = ++$alias unless (exists($table_alias{$t}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $table_alias{$t} 1 7 8
259 8 ▶ push @tables, $t . ' ' . $table_alias{$t} unless (grep { $_ eq ($t . ' ' . $table_alias{$t}) } @tables);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $t . ' ' . $table_alias{$t};} @tables 7 1 8
260 8 ▶ $join_fields{$step} ||= [];
Condition100%2/2
$join_fields{$step} ||= []
l!l
44
MC/DC100%1/1
$join_fields{$step} ||= []
$join_fields{$step}
Truth table100%2/2
A
A= $join_fields{$step}
Aresult
11
00
261 8 ▶ foreach my $field (exists($persist->{$from}{join}{$step}) ? keys %{$persist->{$from}{join}{$step}} : ()) {
Branch: 1 branch
BranchTrueFalseTotal
exists $persist->{$from}{'join'}{$step} ? : 7 1 8
262 7 my $join_from = $table_alias{$from} . '.' . $persist->{$from}{join}{$step}{$field};
263 7 my $join_to = $table_alias{$step} . '.' . $field;
264 7 my $seen = 0;
265 7 foreach my $filter_item (@filter) {
266 1 ▶ if ($filter_item eq ($join_from . ' = ' . $join_to) or $filter_item eq ($join_to . ' = ' . $join_from)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_item eq $join_from . ' = ' . $join_to or $filter_item eq $join_to . ' = ' . $join_from) 0 1 1
Condition33%1/3
$filter_item eq $join_from . ' = ' . $join_to or $filter_item eq $join_to . ' = ' . $join_from
l!l&&r!l&&!r
001
MC/DC0%0/2
$filter_item eq $join_from . ' = ' . $join_to or $filter_item eq $join_to . ' = ' . $join_from
$filter_item eq $join_from . ' = ' . $join_to $filter_item eq $join_to . ' = ' . $join_from
Truth table33%1/3
A or B
A= $filter_item eq $join_from . ' = ' . $join_toB= $filter_item eq $join_to . ' = ' . $join_from
ABresult
1X1
011
000
267 0 $seen = 1;
268 0 last;
269 }
270 }
271 7 ▶ if ($seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen) 0 7 7
272 0 note \6, ' skipping ' . $from . '.' . $persist->{$from}{join}{$step}{$field} . ' = ' . $step . '.' . $field . "\n";
273 0 next;
274 }
275 7 note \6, ' joining ' . $from . '.' . $persist->{$from}{join}{$step}{$field} . ' = ' . $step . '.' . $field . "\n";
276 7 push @filter, $table_alias{$from} . '.' . $persist->{$from}{join}{$step}{$field} . ' = ' . $table_alias{$step} . '.' . $field;
277 7 push @{$join_fields{$from}}, $persist->{$from}{join}{$step}{$field};
278 7 push @{$join_fields{$step}}, $field;
279 }
280 8 $from = $step;
281 }
282 }
283 }
284 6 ▶ unless (@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 0 6 6
285 0 note \6, "no path from $table to $external found\n";
286 0 delete $table_alias{$external};
287 }
288 }
289 6 foreach my $external (keys %table_alias) {
290 # add external table conditions to filter
291 13 ▶ if (exists($filter_values->{$external})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter_values->{$external}) 6 7 13
292 6 ▶ my $next_filter = expand_bind(sub { $table_alias{$external} . '.' . shift }, $filter_values->{$external}{fields}, $filter_values->{$external}{values});
Subroutine __ANON__: called CC=1 SCAR=0.0
293 6 ▶ while (my ($f, $b) = $next_filter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$next_filter()) 6 6 12
294 6 push @filter, $f;
295 6 push @bind, @$b;
296 }
297 }
298 }
299 #if ($need_join) {
300 # # TODO join in SQL, select from both, remove other single table SQL
301 #} else {
302 # # TODO build SQL using our fieldnames for filter parameters
303 #}
304 } else {
305 # build SQL statement for this table - no joins necessary
306 3 push @tables, $table;
307 3 push @fields, @{$inflation_fields->{$table}};
308 3 ▶ if (exists($filter_values->{$table})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $filter_values->{$table}) 3 0 3
309 3 ▶ my $next_filter = expand_bind(sub { shift }, $filter_values->{$table}{fields}, $filter_values->{$table}{values});
Subroutine __ANON__: called CC=1 SCAR=0.0
310 3 ▶ while (my ($f, $b) = $next_filter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$next_filter()) 3 3 6
311 3 push @filter, $f;
312 3 push @bind, @$b;
313 }
314 }
315 }
316 9 ▶ if (keys %$table_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$table_filter) 0 9 9
317 0 ▶ my $prefix = $alias ? 'a.' : '';
Branch: 1 branch
BranchTrueFalseTotal
$alias ? : 0 0 0
318 0 ▶ my $next_filter = expand_bind(sub { $prefix . shift }, $table_filter->{fields}, $table_filter->{values});
Subroutine __ANON__: not called CC=1 SCAR=6.9
319 0 ▶ while (my ($f, $b) = $next_filter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$next_filter()) 0 0 0
320 0 push @filter, $f;
321 0 push @bind, @$b;
322 }
323 }
324 9 ▶ next unless @filter;
Branch: 1 branch
BranchTrueFalseTotal
unless @filter 0 9 9
325 9 push @sql, {
326 'bind' => \@bind,
327 'table' => $table,
328 'tables' => \@tables,
329 'fields' => \@fields,
330 'filter' => \@filter,
331 };
332 }
333 3 foreach my $sql (@sql) {
334 9 my ($table, $alias) = split(/\s+/, $sql->{tables}[0]);
335 9 ▶ $alias .= $alias ? '.' : '';
Branch: 1 branch
BranchTrueFalseTotal
$alias ? : 6 3 9
336 9 ▶ if (exists($join_fields{$table})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $join_fields{$table}) 9 0 9
337 9 my %current = map { $_ => 1 } @{$sql->{fields}};
338 9 foreach my $field (@{$join_fields{$table}}) {
339 14 ▶ unless (exists($current{$alias.$field})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $current{$alias . $field}) 0 14 14
340 0 push @{$sql->{fields}}, $alias.$field;
341 0 $current{$alias.$field}++;
342 0 note \6, "adding $table.$field to selection\n";
343 }
344 }
345 }
346 9 my $query = 'SELECT ' . join(', ', @{$sql->{fields}});
347 9 $query .= ' FROM ' . join(', ', @{$sql->{tables}});
348 9 $query .= ' WHERE ' . join(' AND ', @{$sql->{filter}});
349 9 $sql->{query} = $query;
350 9 ▶ note \5, 'built query: ' . $query . " -> " . join(', ', map { defined($_) ? $_ : '' } @{$sql->{bind}}) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 9 0 9
351 }
352 3 ▶ return @sql if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 0 3
353 0 return \@sql;
354 }
355  
356 9 ▶sub expand_bind {
Subroutine expand_bind: called CC=1 SCAR=0.6
Pod undocumented
357 # returns an iterator which returns a filter "$field = ?" and bind value
358 9 my $transform = shift;
359 9 my $fields = shift;
360 9 my $values = shift;
361 9 my $c = 0;
362 18 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=20.4
363 18 ▶ return if ($c >= @$fields);
Branch: 1 branch
BranchTrueFalseTotal
if $c >= @$fields 9 9 18
364 9 my $field = $transform->($fields->[$c]);
365 9 my $value = $values->[$c++];
366 9 my $operator = '=';
367 9 my $placeholder = '?';
368 9 ▶ if (UNIVERSAL::isa($value, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'ARRAY')) { } 0 9 9
369 0 ▶ if (@$value == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$value == 0) { } 0 0 0
elsif (@$value > 1) { } 0 0 0
370 0 push @$value, undef;
371 } elsif (@$value > 1) {
372 0 $operator = 'IN';
373 0 $placeholder = '(' . join(', ', map { '?' } @$value) . ')';
374 }
375 } else {
376 9 $value = [$value];
377 }
378 9 return ($field . ' ' . $operator . ' ' . $placeholder, $value);
379 }
380 9 }
381  
382 8 ▶sub join_path {
Subroutine join_path: called CC=2 SCAR=7.0
Pod undocumented
383 # determine possible paths to join two tables together
384 8 my $persist = shift;
385 8 my $launch = shift;
386 8 my $target = shift;
387 8 ▶ my $iterators = shift || [];
Condition50%1/2
shift() || []
l!l
80
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
388 8 my $spacing = '';
389 8 note \7, "creating iterator from $launch to $target\n";
390 8 push @$iterators, [member_of($spacing, $target, @_), []];
391 8 my $c = 0;
392 11 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
393 11 ▶ while ($c < @$iterators) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @$iterators) 15 6 21
394 15 my ($element, $match) = $iterators->[$c][0]->();
395 15 $spacing = ' ' x @{$iterators->[$c][1]};
396 15 ▶ unless (defined($element)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $element) 8 7 15
397 8 note \7, $spacing . "iterator $c is exhausted\n";
398 8 $c++;
399 8 next;
400 }
401 7 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 5 2 7
402 5 note \7, $spacing . "iterator $c found a join path: " . join(' -> ', @{$iterators->[$c][1]}, $element) . "\n";
403 5 return @{$iterators->[$c][1]}, $element;
404 } else {
405 0 ▶ if (grep { /^$element$/ } @{$iterators->[$c][1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^$element$/;} @{$$iterators[$c][1];}) { } 0 2 2
406 0 note \7, $spacing . "search loop detected: " . join(' -> ', @{$iterators->[$c][1]}, $element) . "\n";
407 } else {
408 2 $spacing .= ' ';
409 2 note \7, $spacing . "creating iterator from $element to $target\n";
410 2 push @$iterators, [member_of($spacing, $target, keys %{$persist->{$element}{join}}), [@{$iterators->[$c][1]}, $element]];
411 }
412 }
413 }
414 6 note \7, "all iterators exhausted\n";
415 6 return;
416 8 };
417 }
418  
419 10 ▶sub member_of {
Subroutine member_of: called CC=3 SCAR=11.0
Pod undocumented
420 # iterator to say if an element matches the target
421 10 ▶ my $spacing = shift || '';
Condition100%2/2
shift() || ''
l!l
28
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
422 10 my $target = shift;
423 10 my @possible = @_;
424 10 ▶ note \7, $spacing . " determining if " . join(' or ', map { "'$_'" } @possible) . " knows how to join to '$target'\n" if @possible;
Branch: 1 branch
BranchTrueFalseTotal
if @possible 7 3 10
425 15 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
426 15 ▶ while (my $element = shift(@possible)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @possible) 7 8 15
427 7 return ($element, ($element eq $target));
428 }
429 8 return;
430 }
431 10 }
432  
433 13 ▶sub run_path_iterator {
Subroutine run_path_iterator: called CC=5 SCAR=16.1
Pod undocumented
434 # kicks the iterator to find the next path. Returns the path, and fields it will use for the join
435 13 my $iterator = shift;
436 13 my $table_instructions = shift;
437 13 ▶ my $element = shift || 0; # expects 0 for forward, -1 for reverse path
Condition100%2/2
shift() || 0
l!l
310
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
438 13 my @path = $iterator->();
439 13 ▶ return (\@path, {}) unless @path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 7 6 13
440 6 ▶ my %join = map { $_ => 1 } (exists($table_instructions->{join}) and exists($table_instructions->{join}{$path[$element]})) ? values %{$table_instructions->{join}{$path[$element]}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $table_instructions->{'join'} && exists $table_instructions->{'join'}{$path[$element]} ? : 5 1 6
Condition66%2/3
exists $table_instructions->{"join"} and exists $table_instructions->{"join"}{$path[$element]}
!ll&&!rl&&r
015
MC/DC50%1/2
exists $table_instructions->{"join"} and exists $table_instructions->{"join"}{$path[$element]}
exists $table_instructions->{"join"} exists $table_instructions->{"join"}{$path[$element]}
Truth table66%2/3
A and B
A= exists $table_instructions->{"join"}B= exists $table_instructions->{"join"}{$path[$element]}
ABresult
0X0
100
111
441 6 return (\@path, \%join);
442 }
443  
444 1 ▶sub meet_in_the_middle {
Subroutine meet_in_the_middle: called CC=5 SCAR=16.2
Pod undocumented
445 1 my $launch = shift;
446 1 my $target = shift;
447 1 my $forward = shift;
448 1 my $reverse = shift;
449 1 my @queue = ();
450 1 foreach my $fpath (@$forward) {
451 2 ▶ next unless @{$fpath->[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$fpath->[1];} 1 1 2
452 1 foreach my $rpath (@$reverse) {
453 2 ▶ next unless @{$rpath->[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rpath->[1];} 1 1 2
454 1 push @queue, [[@{$fpath->[1]}], [reverse @{$rpath->[1]}]];
455 }
456 }
457 1 my %returned = ();
458 2 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.7
459 2 ▶ while (my $queue = shift(@queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $queue = shift @queue) 1 1 2
460 1 my ($fqueue, $rqueue) = @$queue;
461 1 my $c = 0;
462 1 foreach my $element (@$rqueue) {
463 1 $c++;
464 1 ▶ if ($element eq $fqueue->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq $fqueue->[-1]) 1 0 1
465 1 note \7, "found a meet-in-the-middle join at '$element': " . join(' -> ', $launch, @$fqueue) . ' | ' . join(' <- ', @$rqueue, $target) . "\n";
466 1 ▶ if ($returned{$element}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returned{$element}) 0 1 1
467 0 note \7, " (ignorning because we have already found a join through '$element')\n";
468 0 next;
469 }
470 1 my @path = (@$fqueue, splice(@$rqueue, $c), $target);
471 1 note \7, " which makes a join of: " . join(' -> ', $launch, @path) . "\n";
472 1 my %seen = ();
473 1 my @multiple = grep { $seen{$_}++ } @path;
474 1 ▶ if (@multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if (@multiple) 0 1 1
475 0 note \7, " (ignoring because join goes through " . join(' and ', @multiple) . " more than once)\n";
476 0 next;
477 }
478 1 $returned{$element}++;
479 1 return @path;
480 }
481 }
482 }
483 1 return;
484 }
485 1 }
486  
487 3 ▶sub inflation_fields {
Subroutine inflation_fields: called CC=15 SCAR=28.2
Pod undocumented
488 3 my $class = shift;
489 3 my $inflate = shift;
490 3 my $persist = shift;
491 3 ▶ my $method_tables = shift || method_tables($class, $persist);
Condition33%1/3
shift() || method_tables($class, $persist)
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || method_tables($class, $persist)
shift() method_tables($class, $persist)
Truth table33%1/3
A || B
A= shift()B= method_tables($class, $persist)
ABresult
1X1
011
000
492 3 my %fields = ();
493 3 foreach my $method (@$inflate) {
494 15 ▶ unless (exists($method_tables->{$method})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $method_tables->{$method}) 0 15 15
495 0 warn "ignoring unknown method '$method' for inflation\n";
496 0 next;
497 }
498 15 my $table = $method_tables->{$method};
499 15 ▶ next if (exists($fields{$table})); # already did this table
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields{$table} 6 9 15
500 9 my $methods = $persist->{$table}{methods};
501 9 my @fields = ();
502 # figure out which fields to select based on method names
503 9 foreach my $field (values %$methods) {
504 15 ▶ if (ref($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field) { } 3 12 15
505 3 ▶ if (ref($field) eq 'HASH' and exists($field->{fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field eq "HASH" and exists $field->{"fields"}) 3 0 3
Condition33%1/3
ref $field eq "HASH" and exists $field->{"fields"}
!ll&&!rl&&r
003
MC/DC0%0/2
ref $field eq "HASH" and exists $field->{"fields"}
ref $field eq "HASH" exists $field->{"fields"}
Truth table33%1/3
A and B
A= ref $field eq "HASH"B= exists $field->{"fields"}
ABresult
0X0
100
111
506 3 ▶ push @fields, ref($field->{fields}) eq 'ARRAY' ? @{$field->{fields}} : $field->{fields};
Branch: 1 branch
BranchTrueFalseTotal
ref $field->{'fields'} eq 'ARRAY' ? : 3 0 3
507 }
508 } else {
509 12 push @fields, $field;
510 }
511 }
512 9 ▶ next unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 9 9
513 # select any fields needed for joins
514 9 ▶ if (exists($persist->{$table}{join})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $persist->{$table}{"join"}) 6 3 9
515 6 my %selected = map { $_ => 1 } @fields;
516 6 foreach my $parent (keys %{$persist->{$table}{join}}) {
517 6 foreach my $field (values %{$persist->{$table}{join}{$parent}}) {
518 6 ▶ unless (exists($selected{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $selected{$field}) 6 0 6
519 6 push @fields, $field;
520 6 $selected{$field}++;
521 }
522 }
523 }
524 }
525 9 note \6, "will select from table $table\n";
526 9 note \6, "will select fields " . join(', ', @fields) . "\n";
527 9 $fields{$table} = \@fields;
528 }
529 3 return \%fields;
530 }
531  
532 3 ▶sub filter_values {
Subroutine filter_values: called CC=17 SCAR=38.1
Pod undocumented
533 # returns the table name, field names and bind values for any method name
534 3 my $class = shift;
535 3 my $filter = shift;
536 3 my $persist = shift;
537 3 ▶ my $method_tables = shift || method_tables($class, $persist);
Condition33%1/3
shift() || method_tables($class, $persist)
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || method_tables($class, $persist)
shift() method_tables($class, $persist)
Truth table33%1/3
A || B
A= shift()B= method_tables($class, $persist)
ABresult
1X1
011
000
538 3 my %values = ();
539 3 foreach my $method (keys %$filter) {
540 3 ▶ if (UNIVERSAL::can($filter, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($filter, $method)) 3 0 3
541 3 my $value = $filter->$method();
542 3 ▶ if (!defined($value) or !length($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 0 3 3
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
543 # undefined values of an object do not count as filter parameters
544 0 next;
545 }
546 }
547 # TODO skip warning if filter is an object, rather than a HASH
548 3 ▶ unless (exists($method_tables->{$method})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $method_tables->{$method}) 0 3 3
549 0 warn "ignoring unknown filter field '$method'\n";
550 0 next;
551 }
552 3 my $table = $method_tables->{$method};
553 3 my $methods = $persist->{$table}{methods};
554 3 note \6, "filtering on $method\n";
555 3 my $field = $methods->{$method};
556 3 my @field = ();
557 3 my @value = ();
558 3 local $::OBJECT = $filter;
559 3 ▶ if (ref($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field) { } 0 3 3
560 0 ▶ if (ref($field) eq 'HASH' and exists($field->{fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field eq "HASH" and exists $field->{"fields"}) 0 0 0
Condition0%0/3
ref $field eq "HASH" and exists $field->{"fields"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $field eq "HASH" and exists $field->{"fields"}
ref $field eq "HASH" exists $field->{"fields"}
Truth table0%0/3
A and B
A= ref $field eq "HASH"B= exists $field->{"fields"}
ABresult
0X0
100
111
561 0 ▶ push @field, ref($field->{fields}) eq 'ARRAY' ? @{$field->{fields}} : $field->{fields};
Branch: 1 branch
BranchTrueFalseTotal
ref $field->{'fields'} eq 'ARRAY' ? : 0 0 0
562 0 ▶ my $deflate = exists($field->{deflate}) ? $field->{deflate} : sub { @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $field->{'deflate'} ? : 0 0 0
563 0 ▶ if (UNIVERSAL::can($filter, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($filter, $method)) { } 0 0 0
564 0 push @value, $deflate->($filter->$method());
565 } else {
566 0 push @value, $deflate->($filter->{$method});
567 }
568 }
569 } else {
570 3 push @field, $field;
571 3 ▶ if (UNIVERSAL::can($filter, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($filter, $method)) { } 3 0 3
572 3 push @value, $filter->$method();
573 } else {
574 0 push @value, $filter->{$method};
575 }
576 }
577 3 ▶ unless (@field == @value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@field == @value) 0 3 3
578 0 warn "filter for $method specified " . scalar(@field) . " fields, but " . scalar(@value) . " values\n";
579 }
580 3 ▶ for (my $i = 0; $i < @field; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @field) 3 3 6
581 3 note \6, " ($table.$field[$i] = $value[$i])\n";
582 }
583 3 ▶ $values{$table} ||= { 'fields' => [], 'values' => [] };
Condition50%1/2
$values{$table} ||= {"fields", [], "values", []}
l!l
03
MC/DC0%0/1
$values{$table} ||= {"fields", [], "values", []}
$values{$table}
Truth table50%1/2
A
A= $values{$table}
Aresult
11
00
584 3 push @{$values{$table}{fields}}, @field;
585 3 push @{$values{$table}{values}}, @value;
586 }
587 3 return \%values;
588 }
589  
590 9 ▶sub add_filter_defaults {
Subroutine add_filter_defaults: called CC=6 SCAR=29.1
Pod undocumented
591 # add values from table filter hash, for fields which have not been set
592 9 my $class = shift;
593 9 my $filter_values = shift;
594 9 my $persist = shift;
595 9 my $table = shift;
596 9 my %new = ();
597 0 ▶ if (exists($persist->{$table}{filter}) and keys(%{$persist->{$table}{filter}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $persist->{$table}{"filter"} and keys %{$$persist{$table}{"filter"};}) 0 9 9
Condition33%1/3
exists $persist->{$table}{"filter"} and keys %{$$persist{$table}{"filter"};}
!ll&&!rl&&r
900
MC/DC0%0/2
exists $persist->{$table}{"filter"} and keys %{$$persist{$table}{"filter"};}
exists $persist->{$table}{"filter"} keys %{$$persist{$table}{"filter"};}
Truth table33%1/3
A and B
A= exists $persist->{$table}{"filter"}B= keys %{$$persist{$table}{"filter"};}
ABresult
0X0
100
111
598 0 note \6, "adding default filter values\n";
599 0 my %seen = ();
600 0 ▶ %seen = map { $_ => 1 } @{$filter_values->{$table}{fields}} if (exists($filter_values->{$table}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $filter_values->{$table} 0 0 0
601 0 my $default = $persist->{$table}{filter};
602 0 foreach my $field (keys %$default) {
603 0 ▶ unless (exists($seen{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen{$field}) 0 0 0
604 0 push @{$new{fields}}, $field;
605 0 push @{$new{values}}, $default->{$field};
606 0 note \6, " ($table.$field = $default->{$field})\n";
607 }
608 }
609 }
610 9 return \%new;
611 }
612  
613 9 ▶sub fetchrows {
Subroutine fetchrows: called CC=7 SCAR=20.6
Pod undocumented
614 9 my $class = shift;
615 9 my $dbh = shift;
616 9 my $query = shift;
617 9 my $bind = shift;
618 9 my @records = ();
619 9 ▶ if ($dbh and my $sth = $dbh->prepare($query)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and my $sth = $dbh->prepare($query)) 9 0 9
Condition33%1/3
$dbh and my $sth = $dbh->prepare($query)
!ll&&!rl&&r
009
MC/DC0%0/2
$dbh and my $sth = $dbh->prepare($query)
$dbh my $sth = $dbh->prepare($query)
Truth table33%1/3
A and B
A= $dbhB= my $sth = $dbh->prepare($query)
ABresult
0X0
100
111
620 9 ▶ note \2, $sth->{Statement} . ' -> ' . join(', ', map { defined($_) ? $_ : '' } @$bind) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 9 0 9
621 9 ▶ if ($sth->execute(@$bind)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->execute(@$bind)) 9 0 9
622 9 ▶ while (my $record = $sth->fetchrow_hashref('NAME_lc')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $sth->fetchrow_hashref("NAME_lc")) 12 9 21
623 12 push @records, $record;
624 }
625 }
626 }
627 9 note \2, "fetched " . scalar(@records) . " records\n";
628 9 ▶ return @records if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 9 0 9
629 0 return \@records;
630 }
631  
632 6 ▶sub join_records {
Subroutine join_records: called CC=9 SCAR=22.0
Pod undocumented
633 6 my $class = shift;
634 6 my $persist = shift;
635 6 my $parent = shift; # table name
636 6 my $data = shift; # master dataset
637 6 my $child = shift; # table name
638 6 my $records = shift; # records
639 6 ▶ my $join = $persist->{$child}{join}{$parent} || return; # can't join without instructions
Condition50%1/2
$persist->{$child}{'join'}{$parent} || (return)
l!l
60
MC/DC0%0/1
$persist->{$child}{'join'}{$parent} || (return)
$persist->{$child}{'join'}{$parent}
Truth table50%1/2
A
A= $persist->{$child}{'join'}{$parent}
Aresult
11
00
640 6 my %parent = (); # keyed off join identifier
641 6 my $children = 0; # count matches
642 6 my %joined = ();
643 6 note \5, "joining " . scalar(@$records) . " $child to matching $parent records\n";
644 6 foreach my $d (@$data) {
645 6 my @identifier = ();
646 6 foreach my $field (sort keys %$join) {
647 6 push @identifier, $field, $d->{$parent}[0]->{lc($field)};
648 }
649 6 my $identifier = join(':', @identifier);
650 #note \7, " building parent identifier $identifier\n";
651 6 push @{$parent{$identifier}}, $d;
652 }
653 6 foreach my $r (@$records) {
654 9 my @identifier = ();
655 9 foreach my $field (sort keys %$join) {
656 9 push @identifier, $field, $r->{lc($join->{$field})};
657 }
658 9 ▶ my $identifier = join(':', map { defined($_) ? $_ : '' } @identifier);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 18 0 18
659 #note \7, " building child identifier $identifier\n";
660 9 ▶ if (exists($parent{$identifier})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parent{$identifier}) 9 0 9
661 #note \7, " joining on $identifier\n";
662 9 foreach my $d (@{$parent{$identifier}}) {
663 # TODO do not push child record onto data record more than once (in multiple join scenario)
664 9 push @{$d->{$child}}, $r;
665 9 $children++;
666 9 $joined{$identifier}++;
667 }
668 }
669 }
670 6 note \5, "joined $children $child to " . scalar(keys %joined) . " $parent records\n";
671 6 return $children;
672 }
673  
674 3 ▶sub inflated_object {
Subroutine inflated_object: called CC=1 SCAR=0.5
Pod undocumented
675 # returns iterator to turn table/field data into an object
676 3 my $class = shift;
677 3 my $persist = shift;
678 3 my $data = shift; # ARRAY ref we shift from
679 3 my $objects = shift; # should only ever contain zero or one objects
680 3 my $c = 0;
681 6 ▶ return sub {
Subroutine __ANON__: called CC=27 SCAR=42.6
682 6 ▶ return unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 3 3 6
683 3 my ($dbh) = @_;
684 3 my $d = shift(@$data);
685 3 ▶ my $object = $objects->[$c++] ||= $class->new();
Condition33%1/3
$$objects[$c++] ||= $class->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$$objects[$c++] ||= $class->new
$$objects[$c++] $class->new
Truth table33%1/3
A ||= B
A= $$objects[$c++]B= $class->new
ABresult
1X1
011
000
686 3 local $::OBJECT = $object;
687 3 my @postinflate = ();
688 3 foreach my $table (keys %$d) {
689 9 my $records = $d->{$table};
690 9 note "[$c] inflating object $class with " . scalar(@$records) . " records from $table\n";
691 9 my $methods = $persist->{$table}{methods};
692 9 foreach my $method (keys %$methods) {
693 15 note \6, "inflating method $method\n";
694 15 my $field = $methods->{$method};
695 15 ▶ if (ref($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field) { } 3 12 15
696 3 ▶ if (ref($field) eq 'HASH' and exists($field->{fields})) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $field eq 'HASH' and exists $field->{'fields'}) { } 3 0 3
elsif (ref $field eq 'HASH') { } 0 0 0
Condition33%1/3
ref $field eq "HASH" and exists $field->{"fields"}
!ll&&!rl&&r
003
MC/DC0%0/2
ref $field eq "HASH" and exists $field->{"fields"}
ref $field eq "HASH" exists $field->{"fields"}
Truth table33%1/3
A and B
A= ref $field eq "HASH"B= exists $field->{"fields"}
ABresult
0X0
100
111
697 3 my @field = ();
698 3 ▶ push @field, ref($field->{fields}) eq 'ARRAY' ? @{$field->{fields}} : $field->{fields};
Branch: 1 branch
BranchTrueFalseTotal
ref $field->{'fields'} eq 'ARRAY' ? : 3 0 3
699 3 ▶ if (UNIVERSAL::can($object, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, $method)) { } 3 0 3
700 3 my @values = ();
701 3 foreach my $record (@$records) {
702 6 ▶ if (exists($field->{inflate})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{'inflate'}) { } 0 6 6
703 0 push @values, $field->{inflate}->(@{$record}{@field});
704 } else {
705 6 push @values, @{$record}{@field};
706 }
707 6 foreach my $field (@field) {
708 6 ▶ note \6, " ($field = " . (defined($record->{$field}) ? $record->{$field} : '') . ")\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $record->{$field} ? : 6 0 6
709 }
710 }
711 3 ▶ if ($field->{forceref} or ($field->{wantref} and @values > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{'forceref'} or $field->{'wantref'} and @values > 1) { } 3 0 3
Condition33%2/6
$field->{"forceref"} or $field->{"wantref"} and @values > 1
l!l&&r!l&&!r
030
$field->{"wantref"} and @values > 1
!ll&&!rl&&r
003
MC/DC0%0/3
$field->{"forceref"} or $field->{"wantref"} and @values > 1
$field->{"forceref"} $field->{"wantref"} @values > 1
Truth table25%1/4
A or B and C
A= $field->{"forceref"}B= $field->{"wantref"}C= @values > 1
ABCresult
1XX1
0111
00X0
0100
712 3 $object->$method(\@values);
713 } else {
714 0 $object->$method(@values);
715 }
716 3 ▶ if (exists($field->{postinflate})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"postinflate"}) 0 3 3
717 0 push @postinflate, $field->{postinflate};
718 }
719 } else {
720 # TODO some warning - can't run method on object
721 }
722 } elsif (ref($field) eq 'HASH') {
723 0 ▶ if (exists($field->{inflate})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"inflate"}) 0 0 0
724 0 push my @values, $field->{inflate}->();
725 0 foreach my $value (@values) {
726 0 note \6, " ( = $value)\n";
727 }
728 0 $object->$method(@values);
729 }
730 0 ▶ if (exists($field->{postinflate})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"postinflate"}) 0 0 0
731 0 push @postinflate, $field->{postinflate};
732 }
733 }
734 } else {
735 12 ▶ if (UNIVERSAL::can($object, $method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($object, $method)) { } 12 0 12
736 12 my @values = ();
737 12 foreach my $record (@$records) {
738 12 push @values, $record->{$field};
739 12 ▶ note \6, " ($field = " . (defined($record->{$field}) ? $record->{$field} : 'undef') . ")\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $record->{$field} ? : 12 0 12
740 }
741 12 $object->$method(@values);
742 } else {
743 # TODO some warning - can't run method on object
744 }
745 }
746 }
747 }
748 3 ▶ note \6, "running postinflate hooks\n" if @postinflate;
Branch: 1 branch
BranchTrueFalseTotal
if @postinflate 0 3 3
749 3 foreach my $code (@postinflate) {
750 0 $code->($dbh);
751 }
752 3 return $object;
753 3 };
754 }
755  
756 3 ▶sub method_tables {
Subroutine method_tables: called CC=3 SCAR=11.0
Pod undocumented
757 3 my $class = shift;
758 3 my $persist = shift;
759 3 my %table = ();
760 3 foreach my $table (keys %$persist) {
761 9 foreach my $method (keys %{$persist->{$table}{methods}}) {
762 15 $table{$method} = $table;
763 }
764 }
765 3 return \%table;
766 }
767  
768 2 1;
769 __END__
↑ Summary