blib/lib/DBIx/QuickORM/AggregateRow.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 88.4% 46 / 52 Conditioncond 80.0% 44 / 55 MC/DCmcdc 55.1% 16 / 29 Subroutinesub 94.1% 16 / 17 Podpod 77.7% 7 / 9 totaltotal 85.4% 217 / 254 CC 93 SCAR 25.5
CC
93
Cov
89%
follow_fk
24.1
follow_fk
24.1
field
7.0
CRAP 12.8
« blib/lib/DBIx/QuickORM/Connection.pm ↑ Summary blib/lib/DBIx/QuickORM.pm »
1 package DBIx::QuickORM::AggregateRow;
2 303 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 303 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 303 our $VERSION = '0.000032';
6  
7 303 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 303 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <orm
11 <source
12 <columns
13 <row_data
14 303 };
15  
16 303 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 303 with 'DBIx::QuickORM::Role::Row';
18  
19 =pod
20  
21 =encoding UTF-8
22  
23 =head1 NAME
24  
25 DBIx::QuickORM::AggregateRow - A computed row that knows its columns' types and foreign keys.
26  
27 =head1 DESCRIPTION
28  
29 The row a grouped or computed handle hands out when the ORM uses row
30 objects (see L<DBIx::QuickORM::Handle/Computed columns>). Each column carries
31 what its spec gave it: a type to inflate through, and foreign keys to follow.
32  
33 A handle builds its aggregate rows with a plain bless, skipping C<new>'s
34 argument checks, while C<new> and C<init> are this class's own and no HashBase
35 init hook is registered. Otherwise it calls C<new>.
36  
37 It has no primary key, so it is never canonical: C<update>, C<delete>,
38 C<refresh>, and C<is_canon> croak. Field reads work as in
39 L<DBIx::QuickORM::Role::Row>, without any fetch: the row holds every column
40 it will ever have.
41  
42 =head1 SYNOPSIS
43  
44 use DBIx::QuickORM::Value qw/sql/;
45  
46 my $stat = $orm->handle('posts')
47 ->group_by('user_id')
48 ->fields({
49 user_id => 'user_id',
50 posts => [count => '*'],
51 top => [max => 'views'],
52 })
53 ->first;
54  
55 my $count = $stat->field('posts');
56 my $author = $stat->follow_fk('users')->only; # user_id keeps its foreign key
57  
58 my $signups = $orm->handle('users')
59 ->fields({
60 total => sql('COUNT(*)'),
61 newest => [max => 'created'],
62 })
63 ->only;
64  
65 my $year = $signups->field('newest')->datetime_ref->year; # MAX keeps the field's type: a DateTime
66  
67 =head1 ATTRIBUTES
68  
69 =over 4
70  
71 =item $orm = $row->orm
72  
73 =item $source = $row->source
74  
75 The L<DBIx::QuickORM> and the handle's source.
76  
77 =item \%columns = $row->columns
78  
79 Each column name mapped to its field record: C<orm_type>, C<db_type>,
80 C<type>, C<size>, C<affinity>, and C<fk> (C<< {$table => [{$column =>
81 $field}]} >>), each when known.
82  
83 =item \%data = $row->row_data
84  
85 The fetched values, keyed by column name.
86  
87 =back
88  
89 =cut
90  
91 22 ▶sub init {
Subroutine init: called CC=4 SCAR=17.9
Pod undocumented
92 0 ▶ my $self = shift;
Subroutine init: not called CC=4 SCAR=17.9
Pod undocumented
93 0 ▶ croak "An 'orm' is required" unless $self->{+ORM};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"orm"} 1 21 22
unless $self->{"orm"} 0 0 0
94 0 ▶ croak "'columns' must be a hashref" unless ref($self->{+COLUMNS}) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self->{"columns"} eq "HASH" 1 20 21
unless ref $self->{"columns"} eq "HASH" 0 0 0
95 0 ▶ croak "'row_data' must be a hashref" unless ref($self->{+ROW_DATA}) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self->{"row_data"} eq "HASH" 1 19 20
unless ref $self->{"row_data"} eq "HASH" 0 0 0
96 }
97  
98 =head1 PUBLIC METHODS
99  
100 =over 4
101  
102 =item $table = $row->table
103  
104 Undef: the row belongs to no table.
105  
106 =item $row->set_row_data(\%data)
107  
108 Croaks: an aggregate row's data is never replaced, so C<update_row> and
109 C<refresh_row> refuse one before anything is sent.
110  
111 =back
112  
113 =cut
114  
115 19 ▶sub table { undef }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
116 2 ▶sub set_row_data { croak "An aggregate row's data cannot be replaced" }
Subroutine set_row_data: called CC=1 SCAR=0.0
Pod documented
117  
118 =pod
119  
120 =over 4
121  
122 =item $value = $row->inflated_field($name)
123  
124 =item @values = $row->inflated_fields(\@names)
125  
126 =item @values = $row->inflated_fields(@names)
127  
128 Values inflated through each column's type, as L<DBIx::QuickORM::Role::Row>
129 hands them out. C<fields> comes from there. Croaks for a name that is not a
130 column.
131  
132 =back
133  
134 =cut
135  
136 7 ▶sub inflated_field { ($_[0]->inflated_fields($_[1]))[0] }
Subroutine inflated_field: called CC=1 SCAR=0.0
Pod documented
137  
138 =pod
139  
140 =over 4
141  
142 =item $value = $row->field($name)
143  
144 As C<inflated_field>, read straight from the column record and the row data
145 while the class keeps this class's read methods (see C<_direct_columns>). A
146 subclass that overrides one goes through C<inflated_field>, as does an
147 arrayref of names.
148  
149 =back
150  
151 =cut
152  
153 303 my (%DIRECT, %ORIG);
154  
155 36 ▶sub field {
Subroutine field: called CC=7 SCAR=19.5
Pod documented
156 36 my $self = shift;
157 36 ▶ return $self->inflated_field(@_) if ref($_[0]) || !($DIRECT{ref $self} // $self->_direct_columns);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] or not $DIRECT{ref $self} // $self->_direct_columns 6 30 36
Condition83%5/6
ref $_[0] or not $DIRECT{ref $self} // $self->_direct_columns
l!l&&r!l&&!r
3330
$DIRECT{ref $self} // $self->_direct_columns
l!l&&r!l&&!r
2760
MC/DC33%1/3
ref $_[0] or not $DIRECT{ref $self} // $self->_direct_columns
ref $_[0] $DIRECT{ref $self} $self->_direct_columns
Truth table75%3/4
A or not(B // C)
A= ref $_[0]B= $DIRECT{ref $self}C= $self->_direct_columns
ABCresult
1XX1
0001
01X0
0010
158  
159 30 my ($name) = @_;
160 30 local $_ = $name; # as the map in inflated_fields set it, so nothing this calls can change the caller's $_
161 30 ▶ my $column = $self->{+COLUMNS}->{$name} // croak "The aggregate row has no column '" . ($name // 'undef') . "'";
Condition100%4/4
$self->{"columns"}{$name} // croak(q[The aggregate row has no column '] . ($name // "undef") . q['])
l!l
264
$name // "undef"
l!l
22
MC/DC100%1/1
$self->{"columns"}{$name} // croak(q[The aggregate row has no column '] . ($name // "undef") . q['])
$self->{"columns"}{$name}
MC/DC100%1/1
$name // "undef"
$name
Truth table100%2/2
A
A= $self->{"columns"}{$name}
Aresult
11
00
Truth table100%2/2
A
A= $name
Aresult
11
00
162 26 ▶ return $self->{+ROW_DATA}->{$name} unless $column->{orm_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $column->{"orm_type"} 8 18 26
163 18 return ($self->_inflated_value($name, $column))[0];
164 }
165  
166 8 ▶sub inflated_fields {
Subroutine inflated_fields: called CC=2 SCAR=6.9
Pod documented
167 8 my $self = shift;
168 8 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 3 5 8
169 8 return map { $self->_inflated_value($_, $self->_column($_)) } @names;
170 }
171  
172 =pod
173  
174 =over 4
175  
176 =item @raw = $row->raw_fields(\@names)
177  
178 =item @raw = $row->raw_fields(@names)
179  
180 Values as fetched. C<raw_field> comes from L<DBIx::QuickORM::Role::Row>.
181 Croaks for a name that is not a column.
182  
183 =back
184  
185 =cut
186  
187 5 ▶sub raw_fields {
Subroutine raw_fields: called CC=3 SCAR=11.0
Pod documented
188 5 my $self = shift;
189 5 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 4 5
190 5 $self->_column($_) for @names;
191 4 return @{$self->{+ROW_DATA}}{@names};
192 }
193  
194 =pod
195  
196 =over 4
197  
198 =item $h = $row->follow_fk($other_table)
199  
200 =item $h = $row->follow_fk($other_table, link => {$column => $target_field})
201  
202 =item $h = $row->follow_fk($other_table, via => {$column => $target_field})
203  
204 A handle on the rows of C<$other_table> that a column's foreign key reaches.
205 Without C<link>, exactly one foreign key of the columns must point at
206 C<$other_table>. C<link> names one column and the field its key targets.
207 C<via> does the same for a column with no key. The column and the target
208 field must exist.
209  
210 The handle is C<single> when the target is the other table's primary key or
211 a unique key, and C<multi> otherwise. A NULL value matches no rows.
212  
213 Croaks when no foreign key (or more than one, without C<link>) points at
214 C<$other_table>, when C<link> matches none, and when given both C<link> and
215 C<via>.
216  
217 =back
218  
219 =cut
220  
221 32 ▶sub follow_fk {
Subroutine follow_fk: called CC=24 SCAR=31.8
Pod documented
222 32 my $self = shift;
223 32 my ($other, %params) = @_;
224  
225 32 ▶ my @unknown = sort grep { $_ ne 'link' && $_ ne 'via' } keys %params;
Condition100%3/3
$_ ne 'link' && $_ ne 'via'
!ll&&!rl&&r
1461
MC/DC100%2/2
$_ ne 'link' && $_ ne 'via'
$_ ne 'link' $_ ne 'via'
Truth table100%3/3
A && B
A= $_ ne 'link'B= $_ ne 'via'
ABresult
0X0
100
111
226 32 ▶ croak "Unknown argument(s) to follow_fk(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 31 32
227 31 ▶ croak "follow_fk() needs the table to follow to" unless defined($other) && length($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other and length $other 1 30 31
Condition66%2/3
defined $other and length $other
!ll&&!rl&&r
1030
MC/DC50%1/2
defined $other and length $other
defined $other length $other
Truth table66%2/3
A and B
A= defined $otherB= length $other
ABresult
0X0
100
111
228 30 ▶ croak "Give 'link' or 'via', not both" if defined($params{link}) && defined($params{via});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"link"} and defined $params{"via"} 1 29 30
Condition100%3/3
defined $params{"link"} and defined $params{"via"}
!ll&&!rl&&r
16131
MC/DC100%2/2
defined $params{"link"} and defined $params{"via"}
defined $params{"link"} defined $params{"via"}
Truth table100%3/3
A and B
A= defined $params{"link"}B= defined $params{"via"}
ABresult
0X0
100
111
229  
230 29 my $columns = $self->{+COLUMNS};
231 29 ▶ if (defined(my $via = $params{via})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $via = $params{"via"})) 5 24 29
232 5 ▶ croak "'via' must be one {column => target_field}" unless ref($via) eq 'HASH' && keys(%$via) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $via eq "HASH" and keys %$via == 1 1 4 5
Condition66%2/3
ref $via eq "HASH" and keys %$via == 1
!ll&&!rl&&r
014
MC/DC50%1/2
ref $via eq "HASH" and keys %$via == 1
ref $via eq "HASH" keys %$via == 1
Truth table66%2/3
A and B
A= ref $via eq "HASH"B= keys %$via == 1
ABresult
0X0
100
111
233 4 my ($column, $field) = %$via;
234 4 ▶ croak "'via' names '$column', which is not a column of the aggregate row" unless $columns->{$column};
Branch: 1 branch
BranchTrueFalseTotal
unless $columns->{$column} 1 3 4
235 3 ▶ croak "The table '$other' is not in the database map" unless $self->{+ORM}->db_map->table($other);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"orm"}->db_map->table($other) 1 2 3
236 2 ▶ croak "'via' names '" . ($field // 'undef') . "', which is not a field of '$other'" unless defined($field) && $self->{+ORM}->db_map->field($other, $field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and $self->{"orm"}->db_map->field($other, $field) 1 1 2
Condition60%3/5
defined $field and $self->{"orm"}->db_map->field($other, $field)
!ll&&!rl&&r
011
$field // "undef"
l!l
10
MC/DC50%1/2
defined $field and $self->{"orm"}->db_map->field($other, $field)
defined $field $self->{"orm"}->db_map->field($other, $field)
MC/DC0%0/1
$field // "undef"
$field
Truth table66%2/3
A and B
A= defined $fieldB= $self->{"orm"}->db_map->field($other, $field)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $field
Aresult
11
00
237 1 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
238 }
239  
240 my @links = map {
241 24 my $column = $_;
242 66 ▶ map { [$column, values %$_] } @{(($columns->{$column}->{fk} // {})->{$other}) // []};
Condition100%4/4
+($columns->{$column}{"fk"} // {})->{$other} // []
l!l
4521
$columns->{$column}{"fk"} // {}
l!l
588
MC/DC100%1/1
+($columns->{$column}{"fk"} // {})->{$other} // []
$columns->{$column}{"fk"}
Truth table100%2/2
A
A= $columns->{$column}{"fk"}
Aresult
11
00
243 } sort keys %$columns;
244  
245 24 my $link = $params{link};
246 24 ▶ if (defined $link) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 13 11 24
247 13 ▶ croak "'link' must be one {column => target_field}" unless ref($link) eq 'HASH' && keys(%$link) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $link eq "HASH" and keys %$link == 1 1 12 13
Condition66%2/3
ref $link eq "HASH" and keys %$link == 1
!ll&&!rl&&r
0112
MC/DC50%1/2
ref $link eq "HASH" and keys %$link == 1
ref $link eq "HASH" keys %$link == 1
Truth table66%2/3
A and B
A= ref $link eq "HASH"B= keys %$link == 1
ABresult
0X0
100
111
248 12 my ($column, $field) = %$link;
249 12 ▶ @links = grep { $_->[0] eq $column && $_->[1] eq $field } @links;
Condition100%3/3
$_->[0] eq $column && $_->[1] eq $field
!ll&&!rl&&r
19210
MC/DC100%2/2
$_->[0] eq $column && $_->[1] eq $field
$_->[0] eq $column $_->[1] eq $field
Truth table100%3/3
A && B
A= $_->[0] eq $columnB= $_->[1] eq $field
ABresult
0X0
100
111
250 12 ▶ croak "The link ($column => $field) matches no foreign key from a column to '$other'" unless @links;
Branch: 1 branch
BranchTrueFalseTotal
unless @links 2 10 12
251 }
252  
253 21 ▶ croak "No column of the aggregate row has a foreign key to '$other'" unless @links;
Branch: 1 branch
BranchTrueFalseTotal
unless @links 3 18 21
254 18 ▶ croak "More than one column of the aggregate row has a foreign key to '$other'; choose one with 'link'" if @links > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @links > 1 2 16 18
255  
256 16 my ($column, $field) = @{$links[0]};
257 16 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
258 }
259  
260 =head1 PRIVATE METHODS
261  
262 =over 4
263  
264 =item $bool = $class->_direct_columns
265  
266 True when C<field> may read columns directly: the class keeps this class's
267 C<inflated_field>, C<inflated_fields>, C<_column>, and C<row_data>, and the
268 role's C<_inflated_value>, as they were when the module loaded. Worked out
269 once per class, so a method replaced after that class's first read is not
270 seen, as with L<DBIx::QuickORM::Row/fast_bless>.
271  
272 =item \%column = $row->_column($name)
273  
274 The column's field record. Croaks when there is no such column.
275  
276 =back
277  
278 =cut
279  
280 9 ▶sub _direct_columns {
Subroutine _direct_columns: called CC=5 SCAR=16.3
281 9 ▶ my $class = ref($_[0]) || $_[0];
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
630
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
282 9 ▶ return $DIRECT{$class} //= (grep { ($class->can($_) // 0) != $ORIG{$_} } qw/inflated_field inflated_fields _column row_data _inflated_value/) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({($class->can($_) // 0) != $ORIG{$_};} 'inflated_field', 'inflated_fields', '_column', 'row_data', '_inflated_value') ? : 3 5 8
Condition75%3/4
$DIRECT{$class} //= grep({($class->can($_) // 0) != $ORIG{$_};} "inflated_field", "inflated_fields", "_column", "row_data", "_inflated_value") ? 0 : 1
l!l
18
$class->can($_) // 0
l!l
400
MC/DC100%1/1
$DIRECT{$class} //= grep({($class->can($_) // 0) != $ORIG{$_};} "inflated_field", "inflated_fields", "_column", "row_data", "_inflated_value") ? 0 : 1
$DIRECT{$class}
MC/DC0%0/1
$class->can($_) // 0
$class->can($_)
Truth table100%2/2
A
A= $DIRECT{$class}
Aresult
11
00
Truth table50%1/2
A
A= $class->can($_)
Aresult
11
00
283 }
284  
285 =pod
286  
287 =over 4
288  
289 =item $bool = $class->_bless_ok
290  
291 True when a handle may bless its aggregate rows directly: C<new> and
292 C<init> are still this class's own and no HashBase pre- or post-init hook is
293 registered. A handle asks when its query starts, so a hook registered while
294 one of its iterators is open applies to the next query.
295  
296 =back
297  
298 =cut
299  
300 20 ▶sub _bless_ok {
Subroutine _bless_ok: called CC=6 SCAR=18.1
301 20 my $class = shift;
302 20 ▶ return 0 if ($class->can('new') // 0) != $ORIG{new} || ($class->can('init') // 0) != $ORIG{init};
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new") // 0) != $ORIG{"new"} or ($class->can("init") // 0) != $ORIG{"init"} 2 18 20
Condition71%5/7
($class->can("new") // 0) != $ORIG{"new"} or ($class->can("init") // 0) != $ORIG{"init"}
l!l&&r!l&&!r
1118
$class->can("new") // 0
l!l
200
$class->can("init") // 0
l!l
190
MC/DC0%0/2
($class->can("new") // 0) != $ORIG{"new"} or ($class->can("init") // 0) != $ORIG{"init"}
$class->can("new") $class->can("init")
Truth table33%1/3
A or B
A= $class->can("new")B= $class->can("init")
ABresult
1X1
011
000
303 18 my @hooks = ($class->_pre_init, $class->_post_init);
304 18 ▶ return @hooks ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@hooks ? : 2 16 18
305 }
306  
307 16 ▶sub _column {
Subroutine _column: called CC=3 SCAR=11.1
308 16 my $self = shift;
309 16 my ($name) = @_;
310 16 ▶ return $self->{+COLUMNS}->{$name} // croak "The aggregate row has no column '" . ($name // 'undef') . "'";
Condition75%3/4
$self->{"columns"}{$name} // croak(q[The aggregate row has no column '] . ($name // "undef") . q['])
l!l
142
$name // "undef"
l!l
20
MC/DC100%1/1
$self->{"columns"}{$name} // croak(q[The aggregate row has no column '] . ($name // "undef") . q['])
$self->{"columns"}{$name}
MC/DC0%0/1
$name // "undef"
$name
Truth table100%2/2
A
A= $self->{"columns"}{$name}
Aresult
11
00
Truth table50%1/2
A
A= $name
Aresult
11
00
311 }
312  
313 # The read and construction methods as loaded, for _direct_columns and _bless_ok.
314 %ORIG = (
315 303 (map { $_ => __PACKAGE__->can($_) } qw/inflated_field inflated_fields _column row_data new init/),
316 _inflated_value => DBIx::QuickORM::Role::Row->can('_inflated_value'),
317 );
318  
319 303 1;
320  
321 __END__
« blib/lib/DBIx/QuickORM/Connection.pm ↑ Summary blib/lib/DBIx/QuickORM.pm »