| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | croak "'columns' must be a hashref" unless ref($self->{+COLUMNS}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | croak "'row_data' must be a hashref" unless ref($self->{+ROW_DATA}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
ref $_[0] or not $DIRECT{ref $self} // $self->_direct_columns
$DIRECT{ref $self} // $self->_direct_columns
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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['])
$name // "undef"
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}
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 25 | ▶ | return $self->{+ROW_DATA}->{$name} unless $column->{orm_type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC100%2/2
$_ ne 'link' && $_ ne 'via'
$_ ne 'link'
$_ ne 'via'
Truth table100%3/3
A && B
A= $_ ne 'link'B= $_ ne 'via'
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 32 | ▶ | croak "Unknown argument(s) to follow_fk(): " . join(', ' => @unknown) if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 31 | ▶ | croak "follow_fk() needs the table to follow to" unless defined($other) && length($other); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $other and length $other
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
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 30 | ▶ | croak "Give 'link' or 'via', not both" if defined($params{link}) && defined($params{via}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params{"link"} and defined $params{"via"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 29 | my $columns = $self->{+COLUMNS}; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 29 | ▶ | if (defined(my $via = $params{via})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 5 | ▶ | croak "'via' must be one {column => target_field}" unless ref($via) eq 'HASH' && keys(%$via) == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $via eq "HASH" and keys %$via == 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | ▶ | croak "The table '$other' is not in the database map" unless $self->{+ORM}->db_map->table($other); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition60%3/5
defined $field and $self->{"orm"}->db_map->field($other, $field)
$field // "undef"
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)
Truth table50%1/2
A
A= $field
| |||||||||||||||||||||||||||||||||||||||||||
| 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} // []
$columns->{$column}{"fk"} // {}
MC/DC100%1/1
+($columns->{$column}{"fk"} // {})->{$other} // []
$columns->{$column}{"fk"}
Truth table100%2/2
A
A= $columns->{$column}{"fk"}
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | } sort keys %$columns; | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 24 | my $link = $params{link}; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 24 | ▶ | if (defined $link) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 13 | ▶ | croak "'link' must be one {column => target_field}" unless ref($link) eq 'HASH' && keys(%$link) == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $link eq "HASH" and keys %$link == 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 12 | ▶ | croak "The link ($column => $field) matches no foreign key from a column to '$other'" unless @links; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 21 | ▶ | croak "No column of the aggregate row has a foreign key to '$other'" unless @links; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition75%3/4
$DIRECT{$class} //= grep({($class->can($_) // 0) != $ORIG{$_};} "inflated_field", "inflated_fields", "_column", "row_data", "_inflated_value") ? 0 : 1
$class->can($_) // 0
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}
Truth table50%1/2
A
A= $class->can($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition71%5/7
($class->can("new") // 0) != $ORIG{"new"} or ($class->can("init") // 0) != $ORIG{"init"}
$class->can("new") // 0
$class->can("init") // 0
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 16 | my @hooks = ($class->_pre_init, $class->_post_init); | |||||||||||||||||||||||||||||||||||||||||
| 333 | 16 | ▶ | return @hooks ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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['])
$name // "undef"
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}
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||