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/SchemaBuilder/Table.pm »
1 package DBIx::QuickORM::AggregateRow;
2 258 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 258 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 258 our $VERSION = '0.000033';
6  
7 258 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 258 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <orm
11 <source
12 <columns
13 <row_data
14 258 };
15  
16 258 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 258 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.
130  
131 # $signups from the SYNOPSIS
132 my $newest = $signups->inflated_field('newest'); # through the DateTime type
133 my ($total, $latest) = $signups->inflated_fields('total', 'newest');
134 my @same = $signups->inflated_fields(['total', 'newest']);
135  
136 C<fields> comes from there. Croaks for a name that is not a
137 column.
138  
139 =back
140  
141 =cut
142  
143 7 ▶sub inflated_field { ($_[0]->inflated_fields($_[1]))[0] }
Subroutine inflated_field: called CC=1 SCAR=0.0
Pod documented
144  
145 =pod
146  
147 =over 4
148  
149 =item $value = $row->field($name)
150  
151 As C<inflated_field>, read straight from the column record and the row data
152 while the class keeps this class's read methods (see C<_direct_columns>). A
153 subclass that overrides one goes through C<inflated_field>, as does an
154 arrayref of names.
155  
156 =back
157  
158 =cut
159  
160 258 my (%DIRECT, %ORIG);
161  
162 35 ▶sub field {
Subroutine field: called CC=7 SCAR=19.5
Pod documented
163 35 my $self = shift;
164 35 ▶ 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 29 35
Condition83%5/6
ref $_[0] or not $DIRECT{ref $self} // $self->_direct_columns
l!l&&r!l&&!r
3329
$DIRECT{ref $self} // $self->_direct_columns
l!l&&r!l&&!r
2750
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
165  
166 29 my ($name) = @_;
167 29 local $_ = $name; # as the map in inflated_fields set it, so nothing this calls can change the caller's $_
168 29 ▶ 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
254
$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
169 25 ▶ return $self->{+ROW_DATA}->{$name} unless $column->{orm_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $column->{"orm_type"} 7 18 25
170 18 return ($self->_inflated_value($name, $column))[0];
171 }
172  
173 8 ▶sub inflated_fields {
Subroutine inflated_fields: called CC=2 SCAR=6.9
Pod documented
174 8 my $self = shift;
175 8 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 3 5 8
176 8 return map { $self->_inflated_value($_, $self->_column($_)) } @names;
177 }
178  
179 =pod
180  
181 =over 4
182  
183 =item @raw = $row->raw_fields(\@names)
184  
185 =item @raw = $row->raw_fields(@names)
186  
187 Values as fetched. C<raw_field> comes from L<DBIx::QuickORM::Role::Row>.
188  
189 my ($count, $when) = $signups->raw_fields('total', 'newest'); # $when is the timestamp text
190 my @raw = $signups->raw_fields(['total', 'newest']);
191  
192 Croaks for a name that is not a column.
193  
194 =back
195  
196 =cut
197  
198 5 ▶sub raw_fields {
Subroutine raw_fields: called CC=3 SCAR=11.0
Pod documented
199 5 my $self = shift;
200 5 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 4 5
201 5 $self->_column($_) for @names;
202 4 return @{$self->{+ROW_DATA}}{@names};
203 }
204  
205 =pod
206  
207 =over 4
208  
209 =item $h = $row->follow_fk($other_table)
210  
211 =item $h = $row->follow_fk($other_table, link => {$column => $target_field})
212  
213 =item $h = $row->follow_fk($other_table, via => {$column => $target_field})
214  
215 A handle on the rows of C<$other_table> that a column's foreign key reaches.
216  
217 use DBIx::QuickORM::Value qw/sql/;
218  
219 my $span = $orm->handle('posts')
220 ->group_by('title')
221 ->order_by('title')
222 ->fields({
223 title => 'title',
224 first => [min => 'user_id'], # MIN and MAX keep user_id's facts, its key included
225 last => [max => 'user_id'],
226 oldest => sql('MIN(user_id)'), # the ORM cannot read SQL, so this has no key
227 })
228 ->first;
229  
230 my $last = $span->follow_fk('users', link => {last => 'id'})->only; # first and last both reach users: name one
231 my $oldest = $span->follow_fk('users', via => {oldest => 'id'})->only; # oldest has no key: say where it points
232  
233 Both take C<< {this row's column => the other table's field} >>. Without
234 either, the columns' foreign keys must give exactly one such pair into
235 C<$other_table>. C<link> picks one of those pairs, by its column and target
236 field. C<via> follows a pair no key gives, such as from a column made by
237 C<sql()>. The column and the target field must exist.
238  
239 The handle is C<single> when the target is the other table's primary key or
240 a unique key, and C<multi> otherwise. A NULL value matches no rows.
241  
242 Croaks when no foreign key (or more than one, without C<link>) points at
243 C<$other_table>, when C<link> matches none, and when given both C<link> and
244 C<via>.
245  
246 =back
247  
248 =cut
249  
250 32 ▶sub follow_fk {
Subroutine follow_fk: called CC=24 SCAR=31.8
Pod documented
251 32 my $self = shift;
252 32 my ($other, %params) = @_;
253  
254 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
255 32 ▶ croak "Unknown argument(s) to follow_fk(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 31 32
256 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
257 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
258  
259 29 my $columns = $self->{+COLUMNS};
260 29 ▶ if (defined(my $via = $params{via})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $via = $params{"via"})) 5 24 29
261 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
262 4 my ($column, $field) = %$via;
263 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
264 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
265 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
266 1 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
267 }
268  
269 my @links = map {
270 24 my $column = $_;
271 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
272 } sort keys %$columns;
273  
274 24 my $link = $params{link};
275 24 ▶ if (defined $link) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 13 11 24
276 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
277 12 my ($column, $field) = %$link;
278 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
279 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
280 }
281  
282 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
283 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
284  
285 16 my ($column, $field) = @{$links[0]};
286 16 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
287 }
288  
289 =head1 PRIVATE METHODS
290  
291 =over 4
292  
293 =item $bool = $class->_direct_columns
294  
295 True when C<field> may read columns directly: the class keeps this class's
296 C<inflated_field>, C<inflated_fields>, C<_column>, and C<row_data>, and the
297 role's C<_inflated_value>, as they were when the module loaded. Worked out
298 once per class, so a method replaced after that class's first read is not
299 seen, as with L<DBIx::QuickORM::Row/fast_bless>.
300  
301 =item \%column = $row->_column($name)
302  
303 The column's field record. Croaks when there is no such column.
304  
305 =back
306  
307 =cut
308  
309 8 ▶sub _direct_columns {
Subroutine _direct_columns: called CC=5 SCAR=16.3
310 8 ▶ my $class = ref($_[0]) || $_[0];
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
530
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
311 8 ▶ 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 4 7
Condition75%3/4
$DIRECT{$class} //= grep({($class->can($_) // 0) != $ORIG{$_};} "inflated_field", "inflated_fields", "_column", "row_data", "_inflated_value") ? 0 : 1
l!l
17
$class->can($_) // 0
l!l
350
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
312 }
313  
314 =pod
315  
316 =over 4
317  
318 =item $bool = $class->_bless_ok
319  
320 True when a handle may bless its aggregate rows directly: C<new> and
321 C<init> are still this class's own and no HashBase pre- or post-init hook is
322 registered. A handle asks when its query starts, so a hook registered while
323 one of its iterators is open applies to the next query.
324  
325 =back
326  
327 =cut
328  
329 18 ▶sub _bless_ok {
Subroutine _bless_ok: called CC=6 SCAR=18.1
330 18 my $class = shift;
331 18 ▶ 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 16 18
Condition71%5/7
($class->can("new") // 0) != $ORIG{"new"} or ($class->can("init") // 0) != $ORIG{"init"}
l!l&&r!l&&!r
1116
$class->can("new") // 0
l!l
180
$class->can("init") // 0
l!l
170
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
332 16 my @hooks = ($class->_pre_init, $class->_post_init);
333 16 ▶ return @hooks ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@hooks ? : 2 14 16
334 }
335  
336 16 ▶sub _column {
Subroutine _column: called CC=3 SCAR=11.1
337 16 my $self = shift;
338 16 my ($name) = @_;
339 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
340 }
341  
342 # The read and construction methods as loaded, for _direct_columns and _bless_ok.
343 %ORIG = (
344 258 (map { $_ => __PACKAGE__->can($_) } qw/inflated_field inflated_fields _column row_data new init/),
345 _inflated_value => DBIx::QuickORM::Role::Row->can('_inflated_value'),
346 );
347  
348 258 1;
349  
350 __END__
« blib/lib/DBIx/QuickORM/Connection.pm ↑ Summary blib/lib/DBIx/QuickORM/SchemaBuilder/Table.pm »