blib/lib/DBIx/QuickORM/AggregateRow.pm

filter: Statementstmt 93.3% 70 / 75 Branchbran 83.3% 35 / 42 Conditioncond 80.6% 25 / 31 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 92.8% 13 / 14 Podpod 75.0% 6 / 8 totaltotal 85.6% 161 / 188 CC 63 SCAR 27.6
CC
63
Cov
88%
follow_fk
24.1
follow_fk
24.1
init
6.0
CRAP 15.7
« blib/lib/DBIx/QuickORM/Map.pm ↑ Summary blib/lib/DBIx/QuickORM/Connection.pm »
1 package DBIx::QuickORM::AggregateRow;
2 257 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 257 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 257 our $VERSION = '0.000031';
6  
7 257 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 257 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <orm
11 <source
12 <columns
13 <row_data
14 257 };
15  
16 257 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 257 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 It has no primary key, so it is never canonical: C<update>, C<delete>,
34 C<refresh>, and C<is_canon> croak. Field reads work as in
35 L<DBIx::QuickORM::Role::Row>, without any fetch: the row holds every column
36 it will ever have.
37  
38 =head1 SYNOPSIS
39  
40 use DBIx::QuickORM::Value qw/sql/;
41  
42 my $stat = $orm->handle('posts')
43 ->group_by('user_id')
44 ->fields({
45 user_id => 'user_id',
46 posts => [count => '*'],
47 top => [max => 'views'],
48 })
49 ->first;
50  
51 my $count = $stat->field('posts');
52 my $author = $stat->follow_fk('users')->only; # user_id keeps its foreign key
53  
54 my $signups = $orm->handle('users')
55 ->fields({
56 total => sql('COUNT(*)'),
57 newest => [max => 'created'],
58 })
59 ->only;
60  
61 my $year = $signups->field('newest')->datetime_ref->year; # MAX keeps the field's type: a DateTime
62  
63 =head1 ATTRIBUTES
64  
65 =over 4
66  
67 =item $orm = $row->orm
68  
69 =item $source = $row->source
70  
71 The L<DBIx::QuickORM> and the handle's source.
72  
73 =item \%columns = $row->columns
74  
75 Each column name mapped to its field record: C<orm_type>, C<db_type>,
76 C<type>, C<size>, C<affinity>, and C<fk> (C<< {$table => [{$column =>
77 $field}]} >>), each when known.
78  
79 =item \%data = $row->row_data
80  
81 The fetched values, keyed by column name.
82  
83 =back
84  
85 =cut
86  
87 26 ▶sub init {
Subroutine init: called CC=4 SCAR=17.9
Pod undocumented
88 0 ▶ my $self = shift;
Subroutine init: not called CC=4 SCAR=17.9
Pod undocumented
89 0 ▶ croak "An 'orm' is required" unless $self->{+ORM};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"orm"} 1 25 26
unless $self->{"orm"} 0 0 0
90 0 ▶ croak "'columns' must be a hashref" unless ref($self->{+COLUMNS}) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $self->{"columns"} eq "HASH" 1 24 25
unless ref $self->{"columns"} eq "HASH" 0 0 0
91 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 23 24
unless ref $self->{"row_data"} eq "HASH" 0 0 0
92 }
93  
94 =head1 PUBLIC METHODS
95  
96 =over 4
97  
98 =item $table = $row->table
99  
100 Undef: the row belongs to no table.
101  
102 =item $row->set_row_data(\%data)
103  
104 Croaks: an aggregate row's data is never replaced, so C<update_row> and
105 C<refresh_row> refuse one before anything is sent.
106  
107 =back
108  
109 =cut
110  
111 19 ▶sub table { undef }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
112 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
113  
114 =pod
115  
116 =over 4
117  
118 =item $value = $row->inflated_field($name)
119  
120 =item @values = $row->inflated_fields(\@names)
121  
122 =item @values = $row->inflated_fields(@names)
123  
124 Values inflated through each column's type, as L<DBIx::QuickORM::Role::Row>
125 hands them out. C<field> and C<fields> come from there. Croaks for a name
126 that is not a column.
127  
128 =back
129  
130 =cut
131  
132 20 ▶sub inflated_field { ($_[0]->inflated_fields($_[1]))[0] }
Subroutine inflated_field: called CC=1 SCAR=0.0
Pod documented
133  
134 22 ▶sub inflated_fields {
Subroutine inflated_fields: called CC=2 SCAR=7.4
Pod documented
135 22 my $self = shift;
136 0 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 22 22
137 22 return map { $self->_inflated_value($_, $self->_column($_)) } @names;
138 }
139  
140 =pod
141  
142 =over 4
143  
144 =item @raw = $row->raw_fields(\@names)
145  
146 =item @raw = $row->raw_fields(@names)
147  
148 Values as fetched. C<raw_field> comes from L<DBIx::QuickORM::Role::Row>.
149 Croaks for a name that is not a column.
150  
151 =back
152  
153 =cut
154  
155 5 ▶sub raw_fields {
Subroutine raw_fields: called CC=3 SCAR=11.0
Pod documented
156 5 my $self = shift;
157 5 ▶ my @names = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 4 5
158 5 $self->_column($_) for @names;
159 4 return @{$self->{+ROW_DATA}}{@names};
160 }
161  
162 =pod
163  
164 =over 4
165  
166 =item $h = $row->follow_fk($other_table)
167  
168 =item $h = $row->follow_fk($other_table, link => {$column => $target_field})
169  
170 =item $h = $row->follow_fk($other_table, via => {$column => $target_field})
171  
172 A handle on the rows of C<$other_table> that a column's foreign key reaches.
173 Without C<link>, exactly one foreign key of the columns must point at
174 C<$other_table>. C<link> names one column and the field its key targets.
175 C<via> does the same for a column with no key. The column and the target
176 field must exist.
177  
178 The handle is C<single> when the target is the other table's primary key or
179 a unique key, and C<multi> otherwise. A NULL value matches no rows.
180  
181 Croaks when no foreign key (or more than one, without C<link>) points at
182 C<$other_table>, when C<link> matches none, and when given both C<link> and
183 C<via>.
184  
185 =back
186  
187 =cut
188  
189 32 ▶sub follow_fk {
Subroutine follow_fk: called CC=24 SCAR=31.8
Pod documented
190 32 my $self = shift;
191 32 my ($other, %params) = @_;
192  
193 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
194 32 ▶ croak "Unknown argument(s) to follow_fk(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 31 32
195 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
196 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
197  
198 29 my $columns = $self->{+COLUMNS};
199 29 ▶ if (defined(my $via = $params{via})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $via = $params{"via"})) 5 24 29
200 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
201 4 my ($column, $field) = %$via;
202 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
203 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
204 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
205 1 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
206 }
207  
208 my @links = map {
209 24 my $column = $_;
210 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
211 } sort keys %$columns;
212  
213 24 my $link = $params{link};
214 24 ▶ if (defined $link) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 13 11 24
215 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
216 12 my ($column, $field) = %$link;
217 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
218 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
219 }
220  
221 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
222 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
223  
224 16 my ($column, $field) = @{$links[0]};
225 16 return $self->{+ORM}->_fk_handle($other, {$field => $self->{+ROW_DATA}->{$column}});
226 }
227  
228 =head1 PRIVATE METHODS
229  
230 =over 4
231  
232 =item \%column = $row->_column($name)
233  
234 The column's field record. Croaks when there is no such column.
235  
236 =back
237  
238 =cut
239  
240 30 ▶sub _column {
Subroutine _column: called CC=3 SCAR=11.1
241 30 my $self = shift;
242 30 my ($name) = @_;
243 30 ▶ 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
282
$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
244 }
245  
246 257 1;
247  
248 __END__
« blib/lib/DBIx/QuickORM/Map.pm ↑ Summary blib/lib/DBIx/QuickORM/Connection.pm »