Simple/Find.pm

filter: Statementstmt 75.4% 279 / 370 Branchbran 52.8% 75 / 142 Conditioncond 60.8% 45 / 74 MC/DCmcdc 40.4% 17 / 42 Subroutinesub 82.8% 29 / 35 Podpod 94.7% 18 / 19 totaltotal 67.8% 463 / 682 CC 112 SCAR 30.0
CC
112
Cov
68%
with
110.0
_get_slice
42.0
_parse_hash
32.1
CRAP 20.1
↑ Summary Simple.pm »
1 package ActiveRecord::Simple::Find;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use parent 'ActiveRecord::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use ActiveRecord::Simple::Utils qw/load_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 13 our $MAXIMUM_LIMIT = 100_000_000_000;
16  
17  
18 86 ▶sub new {
Subroutine new: called CC=18 SCAR=29.2
Pod documented
19 86 my ($self_class, $class, @param) = @_;
20  
21 #my $self = $class->new();
22 86 my $self = bless { class => $class } => $self_class;
23  
24 86 ▶ my $table_name = ($self->{class}->can('_get_table_name')) ? $self->{class}->_get_table_name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'class'}->can('_get_table_name') ? : 86 0 86
25 86 ▶ my $pkey = ($self->{class}->can('_get_primary_key')) ? $self->{class}->_get_primary_key : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'class'}->can('_get_primary_key') ? : 86 0 86
26  
27 86 ▶ croak 'can not get table_name for class ' . $self->{class} unless $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_name 0 86 86
28 #croak 'can not get primary_key for class ' . $self->{class} unless $pkey;
29  
30 86 ▶ $self->{prep_select_fields} //= [];
Condition50%1/2
$self->{"prep_select_fields"} //= []
l!l
086
MC/DC0%0/1
$self->{"prep_select_fields"} //= []
$self->{"prep_select_fields"}
Truth table50%1/2
A
A= $self->{"prep_select_fields"}
Aresult
11
00
31 86 ▶ $self->{prep_select_from} //= [];
Condition50%1/2
$self->{"prep_select_from"} //= []
l!l
086
MC/DC0%0/1
$self->{"prep_select_from"} //= []
$self->{"prep_select_from"}
Truth table50%1/2
A
A= $self->{"prep_select_from"}
Aresult
11
00
32 86 ▶ $self->{prep_select_where} //= [];
Condition50%1/2
$self->{"prep_select_where"} //= []
l!l
086
MC/DC0%0/1
$self->{"prep_select_where"} //= []
$self->{"prep_select_where"}
Truth table50%1/2
A
A= $self->{"prep_select_where"}
Aresult
11
00
33  
34 86 my ($fields, $from, $where);
35  
36 86 ▶ if (!ref $param[0] && scalar @param == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $param[0] and scalar @param == 1) { } 22 64 86
elsif (not ref $param[0] and scalar @param == 0) { } 33 31 64
elsif (ref $param[0] and ref $param[0] eq 'HASH') { } 23 8 31
elsif (ref $param[0] and ref $param[0] eq 'ARRAY') { } 2 6 8
Condition100%3/3
not ref $param[0] and scalar @param == 1
!ll&&!rl&&r
253922
MC/DC100%2/2
not ref $param[0] and scalar @param == 1
not ref $param[0] scalar @param == 1
Truth table100%3/3
not(A) and B
A= not ref $param[0]B= scalar @param == 1
ABresult
0X0
100
111
37 22 $fields = qq/"$table_name".*/;
38 22 $from = qq/"$table_name"/;
39 22 $where = qq/"$table_name"."$pkey" = ?/;
40  
41 $self->{BIND} = \@param
42 22 }
43 ▶ elsif (!ref $param[0] && scalar @param == 0) {
Condition100%3/3
not ref $param[0] and scalar @param == 0
!ll&&!rl&&r
25633
MC/DC100%2/2
not ref $param[0] and scalar @param == 0
not ref $param[0] scalar @param == 0
Truth table100%3/3
not(A) and B
A= not ref $param[0]B= scalar @param == 0
ABresult
0X0
100
111
44 33 $fields = qq/"$table_name".*/;
45 33 $from = qq/"$table_name"/;
46  
47 33 $self->{BIND} = undef;
48 }
49 ▶ elsif (ref $param[0] && ref $param[0] eq 'HASH') {
Condition100%3/3
ref $param[0] and ref $param[0] eq "HASH"
!ll&&!rl&&r
6223
MC/DC100%2/2
ref $param[0] and ref $param[0] eq "HASH"
ref $param[0] ref $param[0] eq "HASH"
Truth table100%3/3
A and B
A= ref $param[0]B= ref $param[0] eq "HASH"
ABresult
0X0
100
111
50 # find many by params
51 23 my ($bind, $condition_pairs) = $self->_parse_hash($param[0]);
52  
53 23 my $where_str = join q/ AND /, @$condition_pairs;
54  
55 23 $fields = qq/"$table_name".*/;
56 23 $from = qq/"$table_name"/;
57 23 $where = $where_str;
58  
59 23 $self->{BIND} = $bind;
60 }
61 ▶ elsif (ref $param[0] && ref $param[0] eq 'ARRAY') {
Condition66%2/3
ref $param[0] and ref $param[0] eq "ARRAY"
!ll&&!rl&&r
602
MC/DC50%1/2
ref $param[0] and ref $param[0] eq "ARRAY"
ref $param[0] ref $param[0] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $param[0]B= ref $param[0] eq "ARRAY"
ABresult
0X0
100
111
62 # find many by primary keys
63 2 my $whereinstr = join ', ', @{ $param[0] };
64  
65 2 $fields = qq/"$table_name".*/;
66 2 $from = qq/"$table_name"/;
67 2 $where = qq/"$table_name"."$pkey" IN ($whereinstr)/;
68  
69 2 $self->{BIND} = undef;
70 }
71 else {
72 # find many by condition
73 6 my $wherestr = shift @param;
74  
75 6 $fields = qq/"$table_name".*/;
76 6 $from = qq/"$table_name"/;
77 6 $where = $wherestr;
78  
79 6 $self->{BIND} = \@param;
80 }
81  
82 86 ▶ push @{ $self->{prep_select_fields} }, $fields if $fields;
Branch: 1 branch
BranchTrueFalseTotal
if $fields 86 0 86
83 86 ▶ push @{ $self->{prep_select_from} }, $from if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 86 0 86
84 86 ▶ push @{ $self->{prep_select_where} }, $where if $where;
Branch: 1 branch
BranchTrueFalseTotal
if $where 53 33 86
85  
86 86 return $self;
87 }
88  
89 8 ▶sub count {
Subroutine count: called CC=6 SCAR=17.9
Pod documented
90 8 my $inv = shift;
91 8 ▶ my $self = ref $inv ? $inv : $inv->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $inv ? : 8 0 8
92 8 $self->{prep_select_fields} = [ 'COUNT(*)' ];
93 8 ▶ if (@{ $self->{prep_group_by} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'prep_group_by'} or [];}) { } 2 6 8
Condition100%2/2
$self->{'prep_group_by'} || []
l!l
26
MC/DC100%1/1
$self->{'prep_group_by'} || []
$self->{'prep_group_by'}
Truth table100%2/2
A
A= $self->{'prep_group_by'}
Aresult
11
00
94 2 my $table_name = $self->{class}->_get_table_name;
95 2 push @{ $self->{prep_select_fields} }, map qq/"$table_name".$_/, @{ $self->{prep_group_by} };
96 2 my @group_by = @{ $self->{prep_group_by} };
97 2 s/"//g foreach @group_by;
98 2 my @results;
99 2 foreach my $item ($self->fetch) {
100 7 push my @line, (count => $item->{'COUNT(*)'}), map { $_ => $item->{$_} } @group_by;
101 7 push @results, { @line };
102 }
103 2 return @results;
104 }
105 else {
106 6 return $self->fetch->{'COUNT(*)'};
107 }
108 }
109  
110 6 ▶sub first {
Subroutine first: called CC=3 SCAR=11.0
Pod documented
111 6 my ($self, $limit) = @_;
112  
113 6 ▶ $limit //= 1;
Condition100%2/2
$limit //= 1
l!l
15
MC/DC100%1/1
$limit //= 1
$limit
Truth table100%2/2
A
A= $limit
Aresult
11
00
114  
115 6 ▶ $self->{class}->can('_get_primary_key') or croak 'Can\'t use "first" without primary key';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"class"}->can("_get_primary_key") or croak("Can't use \"first\" without primary key") 6 0 6
116 6 my $primary_key = $self->{class}->_get_primary_key;
117  
118 6 return $self->order_by($primary_key)->limit($limit)->fetch;
119 }
120  
121 3 ▶sub last {
Subroutine last: called CC=3 SCAR=11.3
Pod documented
122 3 my ($self, $limit) = @_;
123  
124 3 ▶ $self->{class}->can('_get_primary_key') or croak 'Can\'t use "first" without primary key';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"class"}->can("_get_primary_key") or croak("Can't use \"first\" without primary key") 3 0 3
125 3 my $primary_key = $self->{class}->_get_primary_key;
126 3 ▶ $limit //= 1;
Condition50%1/2
$limit //= 1
l!l
03
MC/DC0%0/1
$limit //= 1
$limit
Truth table50%1/2
A
A= $limit
Aresult
11
00
127  
128 3 return $self->order_by($primary_key)->desc->limit($limit)->fetch;
129 }
130  
131 7 ▶sub only {
Subroutine only: called CC=10 SCAR=23.1
Pod documented
132 7 my ($self, @fields) = @_;
133  
134 7 ▶ scalar @fields > 0 or croak 'Not defined fields for method "only"';
Branch: 1 branch
BranchTrueFalseTotal
scalar @fields > 0 or croak("Not defined fields for method \"only\"") 7 0 7
135 7 ▶ ref $self or croak 'Create an object abstraction before using the modifiers. Use methods like `find`, `first`, `last` at the beginning';
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Create an object abstraction before using the modifiers. Use methods like `find`, `first`, `last` at the beginning") 7 0 7
136  
137 7 ▶ if ($self->{class}->can('_get_primary_key')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"class"}->can("_get_primary_key")) 7 0 7
138 7 my $pk = $self->{class}->_get_primary_key;
139 7 ▶ push @fields, $pk if ! grep { $_ eq $pk } @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $pk;} @fields 1 6 7
140 }
141  
142 7 my $table_name = $self->{class}->_get_table_name;
143 7 ▶ my $mixins = $self->{class}->can('_get_mixins') ? $self->{class}->_get_mixins : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'class'}->can('_get_mixins') ? : 6 1 7
144  
145 my @filtered_prep_select_fields =
146 7 grep { $_ ne qq/"$table_name".*/ } @{ $self->{prep_select_fields} };
147 7 for my $fld (@fields) {
148 10 ▶ if ($mixins && grep { $_ eq $fld } keys %$mixins) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mixins and grep {$_ eq $fld;} keys %$mixins) { } 1 9 10
Condition100%3/3
$mixins and grep {$_ eq $fld;} keys %$mixins
!ll&&!rl&&r
271
MC/DC100%2/2
$mixins and grep {$_ eq $fld;} keys %$mixins
$mixins grep {$_ eq $fld;} keys %$mixins
Truth table100%3/3
A and B
A= $mixinsB= grep {$_ eq $fld;} keys %$mixins
ABresult
0X0
100
111
149 1 my $mixin = $mixins->{$fld}->($self->{class});
150 1 ▶ $mixin .= qq/ AS $fld/ unless $mixin =~ /as\s+\w+$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $mixin =~ /as\s+\w+$/i 1 0 1
151 1 push @filtered_prep_select_fields, $mixin;
152 }
153 else {
154 9 push @filtered_prep_select_fields, qq/"$table_name"."$fld"/;
155 }
156 }
157  
158 7 $self->{prep_select_fields} = \@filtered_prep_select_fields;
159  
160 7 return $self;
161 }
162  
163 # alias to only:
164 1 ▶sub fields { shift->only(@_) }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
165  
166 20 ▶sub order_by {
Subroutine order_by: called CC=2 SCAR=6.9
Pod documented
167 20 my ($self, @param) = @_;
168  
169 #return if not defined $self->{SQL}; ### TODO: die
170 20 ▶ $self->{prep_order_by} ||= [];
Condition100%2/2
$self->{"prep_order_by"} ||= []
l!l
119
MC/DC100%1/1
$self->{"prep_order_by"} ||= []
$self->{"prep_order_by"}
Truth table100%2/2
A
A= $self->{"prep_order_by"}
Aresult
11
00
171 20 push @{$self->{prep_order_by}}, map qq/"$_"/, @param;
172 20 delete $self->{prep_asc_desc};
173  
174 20 return $self;
175 }
176  
177 7 ▶sub desc {
Subroutine desc: called CC=1 SCAR=0.0
Pod documented
178 7 return shift->_order_by_direction('DESC');
179 }
180  
181 2 ▶sub asc {
Subroutine asc: called CC=1 SCAR=0.0
Pod documented
182 2 return shift->_order_by_direction('ASC');
183 }
184  
185 3 ▶sub group_by {
Subroutine group_by: called CC=2 SCAR=7.0
Pod documented
186 3 my ($self, @param) = @_;
187  
188 3 ▶ $self->{prep_group_by} ||= [];
Condition50%1/2
$self->{"prep_group_by"} ||= []
l!l
03
MC/DC0%0/1
$self->{"prep_group_by"} ||= []
$self->{"prep_group_by"}
Truth table50%1/2
A
A= $self->{"prep_group_by"}
Aresult
11
00
189 3 push @{$self->{prep_group_by}}, map qq/"$_"/, @param;
190 3 return $self;
191 }
192  
193 12 ▶sub limit {
Subroutine limit: called CC=2 SCAR=7.0
Pod documented
194 12 my ($self, $limit) = @_;
195  
196 #return if not defined $self->{SQL};
197 12 ▶ return $self if exists $self->{prep_limit};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prep_limit"} 0 12 12
198  
199 12 $self->{prep_limit} = $limit; ### TODO: move $limit to $self->{BIND}
200  
201 12 return $self;
202 }
203  
204 3 ▶sub offset {
Subroutine offset: called CC=2 SCAR=7.0
Pod documented
205 3 my ($self, $offset) = @_;
206  
207 #return if not defined $self->{SQL};
208 3 ▶ return $self if exists $self->{prep_offset};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prep_offset"} 0 3 3
209  
210 3 $self->{prep_offset} = $offset; ### TODO: move $offset to $self->{BIND}
211  
212 3 return $self;
213 }
214  
215 78 ▶sub fetch {
Subroutine fetch: called CC=10 SCAR=23.9
Pod documented
216 78 my ($self, $param) = @_;
217  
218 78 my ($read_only, $limit);
219 78 ▶ if (ref $param eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq 'HASH') { } 0 78 78
220 0 $limit = $param->{limit};
221 0 $read_only = $param->{read_only};
222 }
223 else {
224 78 $limit = $param;
225 }
226  
227 78 ▶ return $self->_get_slice($limit) if $self->{_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_objects"} 0 78 78
228  
229 78 $self->_finish_sql_stmt();
230 78 $self->_quote_sql_stmt();
231  
232 78 my $class = $self->{class};
233 78 ▶ my $sth = $self->dbh->prepare($self->{SQL}) or croak $self->dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare($self->{"SQL"}) or croak($self->dbh->errstr) 78 0 78
234  
235 78 ▶ $sth->execute(@{ $self->{BIND} }) or croak $self->dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@{$$self{"BIND"};}) or croak($self->dbh->errstr) 78 0 78
236 78 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 29 49 78
237 29 my @objects;
238 29 my $i = 0;
239 29 ▶ while (my $object_data = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $object_data = $sth->fetchrow_hashref) 80 29 109
240 80 $i++;
241 80 my $obj = $class->new($object_data);
242 80 $self->_finish_object_representation($obj, $object_data, $read_only);
243 80 push @objects, $obj;
244  
245 80 ▶ last if $limit && $i == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $i == $limit 0 80 80
Condition33%1/3
$limit and $i == $limit
!ll&&!rl&&r
8000
MC/DC0%0/2
$limit and $i == $limit
$limit $i == $limit
Truth table33%1/3
A and B
A= $limitB= $i == $limit
ABresult
0X0
100
111
246 }
247 29 delete $self->{has_joined_table};
248  
249 29 return @objects;
250 }
251 else {
252 49 ▶ my $object_data = $sth->fetchrow_hashref() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $object_data = $sth->fetchrow_hashref or return 49 0 49
253 49 my $obj = $class->new($object_data);
254 49 $self->_finish_object_representation($obj, $object_data, $read_only);
255 49 delete $self->{has_joined_table};
256  
257 49 return $obj;
258 }
259 }
260  
261 1 ▶sub upload {
Subroutine upload: called CC=1 SCAR=0.0
Pod undocumented
262 1 my ($self, $param) = @_;
263  
264 1 my $o = $self->fetch($param);
265 1 $_[0] = $o;
266  
267 1 return $_[0];
268 }
269  
270 7 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod documented
271 7 my ($self, $n) = @_;
272  
273 7 ▶ $n ||= 1;
Condition100%2/2
$n ||= 1
l!l
43
MC/DC100%1/1
$n ||= 1
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
274  
275 7 $self->{prep_limit} = $n;
276 7 ▶ $self->{prep_offset} = 0 unless defined $self->{prep_offset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"prep_offset"} 2 5 7
277 7 my @result = $self->fetch;
278  
279 7 $self->{prep_offset} += $n;
280  
281 7 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 3 7
282 }
283  
284 0 ▶sub with {
Subroutine with: not called CC=10 SCAR=47.0
Pod documented
285 0 my ($self, @rels) = @_;
286  
287 0 ▶ return $self if exists $self->{prep_left_joins};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prep_left_joins"} 0 0 0
288 0 ▶ return $self unless @rels;
Branch: 1 branch
BranchTrueFalseTotal
unless @rels 0 0 0
289  
290 0 ▶ $self->{class}->can('_get_relations')
Branch: 1 branch
BranchTrueFalseTotal
$self->{"class"}->can("_get_relations") or die "Class doesn't have any relations" 0 0 0
291 or die "Class doesn't have any relations";
292  
293 0 my $table_name = $self->{class}->_get_table_name;
294  
295 0 $self->{prep_left_joins} = [];
296 0 $self->{with} = \@rels;
297  
298 RELATION:
299 0 for my $rel_name (@rels) {
300 0 ▶ my $relation = $self->{class}->_get_relations->{$rel_name}
Branch: 1 branch
BranchTrueFalseTotal
my $relation = $self->{"class"}->_get_relations->{$rel_name} or next RELATION 0 0 0
301 or next RELATION;
302  
303 0 ▶ next RELATION unless grep { $_ eq $relation->{type} } qw/one only/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $relation->{"type"};} "one", "only" 0 0 0
304 0 my $rel_table_name = $relation->{class}->_get_table_name;
305 0 my $rel_columns = $relation->{class}->_get_columns;
306  
307 REL_COLUMN:
308 0 for (@$rel_columns) {
309 0 ▶ next REL_COLUMN if ref $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 0 0 0
310 0 push @{ $self->{prep_select_fields} }, qq/"$rel_table_name"."$_" AS "JOINED_$rel_name\_$_"/;
311 }
312  
313 0 ▶ if ($relation->{type} eq 'one') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relation->{"type"} eq "one") 0 0 0
314 0 my $join_sql = qq/LEFT JOIN "$rel_table_name" ON /;
315 0 $join_sql .= qq/"$rel_table_name"."$relation->{params}{pk}"/;
316 0 $join_sql .= qq/ = "$table_name"."$relation->{params}{fk}"/;
317  
318 0 push @{ $self->{prep_left_joins} }, $join_sql;
319 }
320 }
321  
322 0 return $self;
323 }
324  
325 0 ▶sub left_join { shift->with(@_) }
Subroutine left_join: not called CC=1 SCAR=6.9
Pod documented
326  
327 0 ▶sub to_sql {
Subroutine to_sql: not called CC=2 SCAR=17.9
Pod documented
328 0 my ($self) = @_;
329  
330 0 $self->_finish_sql_stmt();
331 0 $self->_quote_sql_stmt();
332  
333 0 ▶ return wantarray ? ($self->{SQL}, $self->{BIND}) : $self->{SQL};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
334 }
335  
336 7 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
337 7 my ($self) = @_;
338  
339 7 $self->{prep_select_fields} = ['1'];
340 7 $self->_finish_sql_stmt;
341 7 $self->_quote_sql_stmt;
342  
343 7 my $sth = $self->dbh->prepare($self->{SQL});
344 7 $sth->execute(@{ $self->{BIND} });
345  
346 7 return $sth->fetchrow_arrayref();
347 }
348  
349  
350 ### Private
351  
352 14 ▶sub _find_many_to_many {
Subroutine _find_many_to_many: called CC=8 SCAR=28.7
353 14 my ($self_class, $class, $param) = @_;
354  
355 14 ▶ return unless $self_class->dbh && $class && $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $self_class->dbh and $class and $param 0 14 14
Condition33%2/6
$self_class->dbh and $class and $param
!ll&&!rl&&r
0014
$self_class->dbh and $class
!ll&&!rl&&r
0014
MC/DC0%0/3
$self_class->dbh and $class and $param
$self_class->dbh $class $param
Truth table25%1/4
A and B and C
A= $self_class->dbhB= $classC= $param
ABCresult
0XX0
10X0
1100
1111
356  
357 14 my $mc_fkey;
358 14 my $class_opts = {};
359 14 my $root_class_opts = {};
360  
361 14 ▶ if ($param->{m_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'m_class'}) { } 0 14 14
362 #eval { load $param->{m_class} };
363 #if (!is_loaded $param->{m_class}) {
364 # load $param->{m_class};
365 # mark_as_loaded
366 #}
367 0 load_module $param->{m_class};
368  
369  
370 0 for my $opts ( values %{ $param->{m_class}->_get_relations } ) {
371 0 ▶ if ($opts->{class} eq $param->{root_class}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'class'} eq $param->{'root_class'}) { } 0 0 0
elsif ($opts->{'class'} eq $class) { } 0 0 0
372 0 $root_class_opts = $opts;
373 }
374 elsif ($opts->{class} eq $class) {
375 0 $class_opts = $opts;
376 }
377 }
378  
379 0 my $self = $self_class->new($class, @{ $param->{where_statement} });
380  
381 0 my $connected_table_name = $class->_get_table_name;
382 0 $self->{prep_select_from} = [ $param->{m_class}->_get_table_name ];
383  
384 0 push @{ $self->{prep_left_joins} },
385 'JOIN ' . $connected_table_name . ' ON ' . $connected_table_name . '.' . $class->_get_primary_key . ' = '
386 0 . $param->{m_class}->_get_table_name . '.' . $class_opts->{params}{fk};
387  
388 0 push @{ $self->{prep_select_where} },
389 0 $root_class_opts->{params}{fk} . ' = ' . $param->{self}->{ $param->{root_class}->_get_primary_key };
390  
391 0 return $self;
392 }
393 else {
394 14 my $self = $self_class->new($class, @{ $param->{where_statement} });
395  
396 14 my $connected_table_name = $class->_get_table_name;
397 14 $self->{prep_select_from} = [ $param->{via_table} ];
398 14 my $fk = ActiveRecord::Simple::Utils::class_to_table_name($class);
399 14 $fk .= '_id';
400  
401 14 push @{ $self->{prep_left_joins} },
402 'JOIN ' . $connected_table_name . ' ON ' . $connected_table_name . '.' . $class->_get_primary_key . ' = '
403 14 . $param->{via_table} . '.' . $fk;
404  
405 14 my $fk2 = ActiveRecord::Simple::Utils::class_to_table_name($param->{root_class}) . '_id';
406  
407 14 push @{ $self->{prep_select_where} },
408 14 $fk2 . ' = ' . $param->{self}->{ $param->{root_class}->_get_primary_key };
409  
410 14 return $self;
411 }
412  
413 }
414  
415 0 ▶sub _get_slice {
Subroutine _get_slice: not called CC=6 SCAR=37.4
416 0 my ($self, $time) = @_;
417  
418 return unless $self->{_objects}
419 && ref $self->{_objects} eq 'ARRAY'
420 0 ▶ && scalar @{ $self->{_objects} } > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_objects"} and ref $self->{"_objects"} eq "ARRAY" and scalar @{$self->{"_objects"};} > 0 0 0 0
Condition0%0/6
$self->{"_objects"} and ref $self->{"_objects"} eq "ARRAY" and scalar @{$self->{"_objects"};} > 0
!ll&&!rl&&r
000
$self->{"_objects"} and ref $self->{"_objects"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"_objects"} and ref $self->{"_objects"} eq "ARRAY" and scalar @{$self->{"_objects"};} > 0
$self->{"_objects"} ref $self->{"_objects"} eq "ARRAY" scalar @{$self->{"_objects"};} > 0
Truth table0%0/4
A and B and C
A= $self->{"_objects"}B= ref $self->{"_objects"} eq "ARRAY"C= scalar @{$self->{"_objects"};} > 0
ABCresult
0XX0
10X0
1100
1111
421  
422 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
423 0 ▶ $time ||= scalar @{ $self->{_objects} };
Condition0%0/2
$time ||= scalar @{$self->{"_objects"};}
l!l
00
MC/DC0%0/1
$time ||= scalar @{$self->{"_objects"};}
$time
Truth table0%0/2
A
A= $time
Aresult
11
00
424 0 return splice @{ $self->{_objects} }, 0, $time;
425 }
426 else {
427 0 return shift @{ $self->{_objects} };
428 }
429 }
430  
431 85 ▶sub _quote_sql_stmt {
Subroutine _quote_sql_stmt: called CC=4 SCAR=14.6
432 85 my ($self) = @_;
433  
434 85 ▶ return unless $self->{SQL} && $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SQL"} and $self->dbh 0 85 85
Condition33%1/3
$self->{"SQL"} and $self->dbh
!ll&&!rl&&r
0085
MC/DC0%0/2
$self->{"SQL"} and $self->dbh
$self->{"SQL"} $self->dbh
Truth table33%1/3
A and B
A= $self->{"SQL"}B= $self->dbh
ABresult
0X0
100
111
435  
436 85 my $driver_name = $self->dbh->{Driver}{Name};
437 85 ▶ $driver_name //= 'Pg';
Condition50%1/2
$driver_name //= "Pg"
l!l
850
MC/DC0%0/1
$driver_name //= "Pg"
$driver_name
Truth table50%1/2
A
A= $driver_name
Aresult
11
00
438 85 my $quotes_map = {
439 Pg => q/"/,
440 mysql => q/`/,
441 SQLite => q/`/,
442 };
443 85 my $quote = $quotes_map->{$driver_name};
444  
445 85 $self->{SQL} =~ s/"/$quote/g;
446  
447 85 return $self;
448 }
449  
450 129 ▶sub _finish_object_representation {
Subroutine _finish_object_representation: called CC=7 SCAR=29.1
451 129 my ($self, $obj, $object_data, $read_only) = @_;
452  
453 129 ▶ if ($self->{has_joined_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_joined_table"}) 27 102 129
454 RELATION:
455 27 for my $rel_name (@{ $self->{with} }) {
456 0 ▶ my $relation = $self->{class}->_get_relations->{$rel_name} or next RELATION;
Branch: 1 branch
BranchTrueFalseTotal
my $relation = $self->{"class"}->_get_relations->{$rel_name} or next RELATION 0 0 0
457 0 my %pairs = map { $_, $object_data->{$_} } grep { $_ =~ /^JOINED\_$rel_name\_/ } keys %$object_data;
458 0 ▶ next RELATION unless %pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless %pairs 0 0 0
459  
460 0 for my $key (keys %pairs) {
461 0 my $val = delete $pairs{$key};
462 0 $key =~ s/^JOINED\_$rel_name\_//;
463 0 $pairs{$key} = $val;
464 }
465 0 $obj->{"relation_instance_$rel_name"} = $relation->{class}->new(\%pairs);
466  
467 0 $obj->_delete_keys(qr/^JOINED\_$rel_name/);
468 }
469  
470 }
471  
472 129 ▶ $obj->{read_only} = 1 if defined $read_only;
Branch: 1 branch
BranchTrueFalseTotal
if defined $read_only 0 129 129
473 129 $obj->{isin_database} = 1;
474  
475 129 return $obj;
476 }
477  
478 85 ▶sub _finish_sql_stmt {
Subroutine _finish_sql_stmt: called CC=14 SCAR=26.4
479 85 my ($self) = @_;
480  
481 85 ▶ ref $self->{prep_select_fields} or croak 'Invalid prepare SQL statement';
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"prep_select_fields"} or croak("Invalid prepare SQL statement") 85 0 85
482 85 ▶ ref $self->{prep_select_from} or croak 'Invalid prepare SQL statement';
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"prep_select_from"} or croak("Invalid prepare SQL statement") 85 0 85
483  
484 85 my $table_name = $self->{class}->_get_table_name;
485 85 ▶ my @add = grep { $_ !~~ $self->{prep_select_fields} } map qq/"$table_name".$_/, @{ $self->{prep_group_by}||[] };
Condition100%2/2
$self->{'prep_group_by'} || []
l!l
382
MC/DC100%1/1
$self->{'prep_group_by'} || []
$self->{'prep_group_by'}
Truth table100%2/2
A
A= $self->{'prep_group_by'}
Aresult
11
00
486 85 push @{ $self->{prep_select_fields} }, @add;
487  
488 85 $self->{SQL} = "SELECT " . (join q/, /, @{ $self->{prep_select_fields} }) . "\n";
489 85 $self->{SQL} .= "FROM " . (join q/, /, @{ $self->{prep_select_from} }) . "\n";
490  
491 85 ▶ if (defined $self->{prep_left_joins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"prep_left_joins"}) 14 71 85
492 14 $self->{SQL} .= "$_\n" for @{ $self->{prep_left_joins} };
493 14 $self->{has_joined_table} = 1;
494 }
495  
496 85 ▶ if (@{ $self->{prep_select_where}||[] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"prep_select_where"} or [];}) 58 27 85
Condition50%1/2
$self->{'prep_select_where'} || []
l!l
850
MC/DC0%0/1
$self->{'prep_select_where'} || []
$self->{'prep_select_where'}
Truth table50%1/2
A
A= $self->{'prep_select_where'}
Aresult
11
00
497 58 $self->{SQL} .= "WHERE\n";
498 58 $self->{SQL} .= join " AND ", @{ $self->{prep_select_where} };
499 }
500  
501 85 ▶ if (@{ $self->{prep_group_by}||[] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"prep_group_by"} or [];}) 3 82 85
Condition100%2/2
$self->{'prep_group_by'} || []
l!l
382
MC/DC100%1/1
$self->{'prep_group_by'} || []
$self->{'prep_group_by'}
Truth table100%2/2
A
A= $self->{'prep_group_by'}
Aresult
11
00
502 3 $self->{SQL} .= ' GROUP BY ';
503 3 $self->{SQL} .= join q/, /, @{ $self->{prep_group_by} };
504 }
505  
506 85 ▶ if (@{ $self->{prep_order_by}||[] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"prep_order_by"} or [];}) 21 64 85
Condition100%2/2
$self->{'prep_order_by'} || []
l!l
2164
MC/DC100%1/1
$self->{'prep_order_by'} || []
$self->{'prep_order_by'}
Truth table100%2/2
A
A= $self->{'prep_order_by'}
Aresult
11
00
507 21 $self->{SQL} .= ' ORDER BY ';
508 21 $self->{SQL} .= join q/, /, @{ $self->{prep_order_by} };
509 }
510  
511 85 ▶ $self->{SQL} .= ' LIMIT ' . ($self->{prep_limit} // $MAXIMUM_LIMIT);
Condition66%2/3
$self->{"prep_limit"} // $ActiveRecord::Simple::Find::MAXIMUM_LIMIT
l!l&&r!l&&!r
19660
MC/DC0%0/2
$self->{"prep_limit"} // $ActiveRecord::Simple::Find::MAXIMUM_LIMIT
$self->{"prep_limit"} $ActiveRecord::Simple::Find::MAXIMUM_LIMIT
Truth table66%2/3
A // B
A= $self->{"prep_limit"}B= $ActiveRecord::Simple::Find::MAXIMUM_LIMIT
ABresult
1X1
011
000
512 85 ▶ $self->{SQL} .= ' OFFSET '. ($self->{prep_offset} // 0);
Condition100%2/2
$self->{"prep_offset"} // 0
l!l
1075
MC/DC100%1/1
$self->{"prep_offset"} // 0
$self->{"prep_offset"}
Truth table100%2/2
A
A= $self->{"prep_offset"}
Aresult
11
00
513  
514 85 return $self;
515 }
516  
517 23 ▶sub _parse_hash {
Subroutine _parse_hash: called CC=13 SCAR=34.7
518 23 my ($self, $param_hash) = @_;
519 23 my $class = $self->{class};
520 23 ▶ my $table_name = ($self->{class}->can('_get_table_name')) ? $self->{class}->_get_table_name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'class'}->can('_get_table_name') ? : 23 0 23
521 23 my ($bind, $condition_pairs) = ([],[]);
522 23 for my $param_name (keys %{ $param_hash }) {
523 23 ▶ if (ref $param_hash->{$param_name} eq 'ARRAY' and !ref $param_hash->{$param_name}[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $param_hash->{$param_name} eq 'ARRAY' and not ref $param_hash->{$param_name}[0]) { } 0 23 23
elsif (ref $param_hash->{$param_name}) { } 2 21 23
Condition33%1/3
ref $param_hash->{$param_name} eq "ARRAY" and not ref $param_hash->{$param_name}[0]
!ll&&!rl&&r
2300
MC/DC0%0/2
ref $param_hash->{$param_name} eq "ARRAY" and not ref $param_hash->{$param_name}[0]
ref $param_hash->{$param_name} eq "ARRAY" not ref $param_hash->{$param_name}[0]
Truth table33%1/3
A and not(B)
A= ref $param_hash->{$param_name} eq "ARRAY"B= not ref $param_hash->{$param_name}[0]
ABresult
0X0
100
111
524 0 my $instr = join q/, /, map { '?' } @{ $param_hash->{$param_name} };
525 0 push @$condition_pairs, qq/"$table_name"."$param_name" IN ($instr)/;
526 0 push @$bind, @{ $param_hash->{$param_name} };
527 }
528 elsif (ref $param_hash->{$param_name}) {
529 2 ▶ next if !$class->can('_get_relations');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("_get_relations") 0 2 2
530 2 ▶ my $relation = $class->_get_relations->{$param_name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $relation = $class->_get_relations->{$param_name} or next 2 0 2
531  
532 2 ▶ next if $relation->{type} ne 'one';
Branch: 1 branch
BranchTrueFalseTotal
if $relation->{"type"} ne "one" 0 2 2
533 2 my $fk = $relation->{params}{fk};
534 2 my $pk = $relation->{params}{pk};
535  
536 2 ▶ if (ref $param_hash->{$param_name} eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param_hash->{$param_name} eq __PACKAGE__) { } 0 2 2
537 0 my $object = $param_hash->{$param_name};
538  
539 0 my $tmp_table = qq/tmp_table_/ . sprintf("%x", $object);
540 0 my $request_table = $object->{class}->_get_table_name;
541  
542 0 $object->{prep_select_fields} = [qq/"$request_table"."$pk"/];
543 0 $object->_finish_sql_stmt;
544  
545 0 push @$condition_pairs, qq/"$table_name"."$fk" IN (SELECT "$tmp_table"."$pk" from ($object->{SQL}) as $tmp_table)/;
546 0 ▶ push @$bind, @{ $object->{BIND} } if ref $object->{BIND} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $object->{"BIND"} eq "ARRAY" 0 0 0
547 }
548 else {
549 2 my $object = $param_hash->{$param_name};
550  
551 2 ▶ if (ref $object eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object eq 'ARRAY') { } 0 2 2
552 0 push @$bind, map $_->$pk, @$object;
553 0 push @$condition_pairs, qq/"$table_name"."$fk" IN (@{[ join ', ', map "?", @$object ]})/;
554 }
555 else {
556 2 push @$condition_pairs, qq/"$table_name"."$fk" = ?/;
557 2 push @$bind, $object->$pk;
558 }
559 }
560 }
561 else {
562 21 ▶ if (defined $param_hash->{$param_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param_hash->{$param_name}) { } 21 0 21
563 21 push @$condition_pairs, qq/"$table_name"."$param_name" = ?/;
564 21 push @$bind, $param_hash->{$param_name};
565 }
566 else {
567 # is NULL
568 0 push @$condition_pairs, qq/"$table_name"."$param_name" IS NULL/;
569 }
570 }
571 }
572 23 return ($bind, $condition_pairs);
573 }
574  
575 9 ▶sub _order_by_direction {
Subroutine _order_by_direction: called CC=5 SCAR=17.1
576 9 my ($self, $direction) = @_;
577  
578 # There are no fields for order yet
579 9 ▶ return unless ref $self->{prep_order_by} eq 'ARRAY' and scalar @{ $self->{prep_order_by} } > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"prep_order_by"} eq "ARRAY" and scalar @{$self->{"prep_order_by"};} > 0 0 9 9
Condition33%1/3
ref $self->{"prep_order_by"} eq "ARRAY" and scalar @{$self->{"prep_order_by"};} > 0
!ll&&!rl&&r
009
MC/DC0%0/2
ref $self->{"prep_order_by"} eq "ARRAY" and scalar @{$self->{"prep_order_by"};} > 0
ref $self->{"prep_order_by"} eq "ARRAY" scalar @{$self->{"prep_order_by"};} > 0
Truth table33%1/3
A and B
A= ref $self->{"prep_order_by"} eq "ARRAY"B= scalar @{$self->{"prep_order_by"};} > 0
ABresult
0X0
100
111
580  
581 # asc/desc is called before: ->asc->desc
582 9 ▶ return if defined $self->{prep_asc_desc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prep_asc_desc"} 0 9 9
583  
584 # $direction should be ASC/DESC
585 9 ▶ return unless $direction =~ /^(ASC|DESC)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $direction =~ /^(ASC|DESC)$/i 0 9 9
586  
587 # Add $direction to the latest field
588 9 @{$self->{prep_order_by}}[-1] .= " $direction";
589 9 $self->{prep_asc_desc} = 1;
590  
591 9 return $self;
592 }
593  
594 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
595  
596 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
597 0 my $call = $AUTOLOAD;
598 0 my $self = shift;
599 0 my $class = ref $self;
600  
601 0 $call =~ s/.*:://;
602 0 my $error = "Can't call method `$call` on class $class.\nPerhaps you have forgotten to fetch your object?";
603  
604 0 croak $error;
605 }
606  
607 13 1;
608  
609 __END__;
↑ Summary Simple.pm »