blib/lib/DBIx/QuickORM/Source/Join.pm

filter: Statementstmt 85.3% 151 / 177 Branchbran 72.4% 42 / 58 Conditioncond 72.2% 39 / 54 MC/DCmcdc 50.0% 16 / 32 Subroutinesub 91.3% 21 / 23 Podpod 83.3% 10 / 12 totaltotal 78.3% 279 / 356 CC 101 SCAR 25.0
CC
101
Cov
80%
init
32.4
init
32.4
results
15.0
CRAP 12.2
« blib/lib/DBIx/QuickORM/SchemaBuilder/Table.pm ↑ Summary blib/lib/DBIx/QuickORM.pm »
1 package DBIx::QuickORM::Source::Join;
2 259 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 259 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 259 our $VERSION = '0.000033';
6  
7 259 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 259 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 259 ▶use DBIx::QuickORM::JoinRow();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 259 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
13 <db_map
14 +components
15 +labels
16 +label_of
17 +records
18 +temp_records
19 259 };
20  
21 259 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 259 with 'DBIx::QuickORM::Role::Source';
23  
24 =pod
25  
26 =encoding UTF-8
27  
28 =head1 NAME
29  
30 DBIx::QuickORM::Source::Join - Tables joined together, as a source.
31  
32 =head1 DESCRIPTION
33  
34 A base table and the tables joined to it, as L<DBIx::QuickORM::Role::Source>
35 describes. Handles build joins with C<join>, C<inner_join>, and C<left_join>
36 (see L<DBIx::QuickORM::Handle>). Each result row is a
37 L<DBIx::QuickORM::JoinRow>.
38  
39 Each joined table is a component with a name (its alias in the SQL), joined
40 from an earlier component along a foreign key link, as an inner or a left
41 join. A join is an immutable value: adding a component returns a new one.
42  
43 Fields are named C<component.field> everywhere, so components may share
44 field names. The select list holds each component's default fields and
45 primary key.
46  
47 Every component with a primary key is ingested into the lookup table (as
48 for a table's rows, not under C<no_cache>), and within one query the same canon row data gives the same row object. The rows
49 belong to no single table, so a join cannot be written through.
50  
51 =head1 SYNOPSIS
52  
53 my $h = $orm->handle('orders')
54 ->join('customers')
55 ->left_join(
56 'employees',
57 as => 'rep',
58 link => {rep_id => 'id'},
59 );
60  
61 my $source = $h->source; # a DBIx::QuickORM::Source::Join
62  
63 =head1 ATTRIBUTES
64  
65 =over 4
66  
67 =item $map = $join->db_map
68  
69 Required: the L<DBIx::QuickORM::Map>.
70  
71 =item components => [{as => $name, table => $table}, {as => $name, table => $table, type => 'inner' or 'left', from => $name, link => \%link}, ...]
72  
73 Required at construction: the base table, then each joined component in
74 order. C<link> is C<< {from_field => field} >>, from the component named
75 C<from> to this one.
76  
77 Croaks without at least the base table, for a table not in the map, a
78 component name already taken, or a C<from> that names no earlier component.
79  
80 =back
81  
82 =cut
83  
84 99 ▶sub init {
Subroutine init: called CC=11 SCAR=34.8
Pod undocumented
85 0 ▶ my $self = shift;
Subroutine init: not called CC=11 SCAR=34.8
Pod undocumented
86  
87 0 ▶ my $map = $self->{+DB_MAP} or croak "A 'db_map' is required";
Branch: 2 branches
BranchTrueFalseTotal
my $map = $self->{"db_map"} or croak("A 'db_map' is required") 99 0 99
my $map = $self->{"db_map"} or croak("A 'db_map' is required") 0 0 0
88 0 my $components = $self->{+COMPONENTS};
89 0 ▶ croak "'components' must be an arrayref of at least the base table" unless ref($components) eq 'ARRAY' && @$components;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $components eq "ARRAY" and @$components 0 99 99
unless ref $components eq "ARRAY" and @$components 0 0 0
Condition16%1/6
ref $components eq "ARRAY" and @$components
!ll&&!rl&&r
0099
ref $components eq "ARRAY" and @$components
!ll&&!rl&&r
000
MC/DC0%0/2
ref $components eq "ARRAY" and @$components
ref $components eq "ARRAY" @$components
MC/DC0%0/2
ref $components eq "ARRAY" and @$components
ref $components eq "ARRAY" @$components
Truth table33%1/3
A and B
A= ref $components eq "ARRAY"B= @$components
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $components eq "ARRAY"B= @$components
ABresult
0X0
100
111
90  
91 0 my (%seen, %labels, %label_of, %records);
92 0 for my $i (0 .. $#$components) {
93 0 my ($as, $table, $from) = @{$components->[$i]}{qw/as table from/};
94 0 ▶ my $t = $map->table($table) or confess "The table '$table' is not in the database map";
Branch: 2 branches
BranchTrueFalseTotal
my $t = $map->table($table) or confess("The table '${table}' is not in the database map") 158 0 158
my $t = $map->table($table) or confess("The table '${table}' is not in the database map") 0 0 0
95 0 ▶ push @{$self->{+TEMP_RECORDS}} => [$table, $t] if $t->{temp};
Branch: 2 branches
BranchTrueFalseTotal
if $t->{"temp"} 3 155 158
if $t->{"temp"} 0 0 0
96 0 ▶ confess "The join has no component named '$from'" if $i && !$seen{$from};
Branch: 2 branches
BranchTrueFalseTotal
if $i and not $seen{$from} 2 156 158
if $i and not $seen{$from} 0 0 0
Condition50%3/6
$i and not $seen{$from}
!ll&&!rl&&r
99572
$i and not $seen{$from}
!ll&&!rl&&r
000
MC/DC100%2/2
$i and not $seen{$from}
$i not $seen{$from}
MC/DC0%0/2
$i and not $seen{$from}
$i not $seen{$from}
Truth table100%3/3
A and not(B)
A= $iB= not $seen{$from}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $iB= not $seen{$from}
ABresult
0X0
100
111
97 0 ▶ confess "The join already has a component named '$as'" if $seen{$as}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{$as}++ 2 154 156
if $seen{$as}++ 0 0 0
98  
99 0 my @fields = sort keys %{$t->{fields}};
100 0 for my $k (0 .. $#fields) {
101 0 my $label = "qorm_${i}_$k";
102 0 $labels{$label} = [$i, $fields[$k]];
103 0 $label_of{"$as.$fields[$k]"} = $label;
104 0 $records{$label} = $records{"$as.$fields[$k]"} = $t->{fields}->{$fields[$k]};
105 }
106 }
107  
108 0 $self->{+LABELS} = \%labels;
109 0 $self->{+LABEL_OF} = \%label_of;
110 0 $self->{+RECORDS} = \%records;
111 }
112  
113 =head1 PUBLIC METHODS
114  
115 =over 4
116  
117 =item undef = $join->table
118  
119 =item undef = $join->pk
120  
121 Undef: the rows belong to no single table, so a join has no table and no
122 primary key.
123  
124 =item () = $join->query_sql
125  
126 An empty list: a join is not a query.
127  
128 =item \%fields = $join->field_records
129  
130 The field records by internal label and by C<component.field>.
131  
132 =back
133  
134 =cut
135  
136 65 ▶sub table { undef }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
137 1 ▶sub pk { undef }
Subroutine pk: called CC=1 SCAR=0.0
Pod documented
138 1 ▶sub query_sql { () }
Subroutine query_sql: called CC=1 SCAR=0.0
Pod documented
139  
140 75 ▶sub field_records { $_[0]->{+RECORDS} }
Subroutine field_records: called CC=1 SCAR=0.0
Pod documented
141  
142 =pod
143  
144 =over 4
145  
146 =item $join2 = $join->extend($type, $table, as => $name, from => $name, link => \%link)
147  
148 A new join with one more component. C<$type> is C<inner> or C<left>. C<as>
149 defaults to the table name and C<from> to the base table's component.
150  
151 Croaks for any other C<$type>, and as construction does for a bad table,
152 C<as>, or C<from>.
153  
154 =back
155  
156 =cut
157  
158 53 ▶sub extend {
Subroutine extend: called CC=5 SCAR=16.2
Pod documented
159 53 my $self = shift;
160 53 my ($type, $table, %opts) = @_;
161  
162 53 ▶ croak "A join is 'inner' or 'left'" unless $type eq 'inner' || $type eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "inner" or $type eq "left" 1 52 53
Condition100%3/3
$type eq "inner" or $type eq "left"
l!l&&r!l&&!r
4481
MC/DC100%2/2
$type eq "inner" or $type eq "left"
$type eq "inner" $type eq "left"
Truth table100%3/3
A or B
A= $type eq "inner"B= $type eq "left"
ABresult
1X1
011
000
163 52 ▶ my $component = {type => $type, table => $table, as => $opts{as} // $table, from => $opts{from} // $self->{+COMPONENTS}->[0]->{as}, link => $opts{link}};
Condition66%4/6
$opts{"as"} // $table
l!l&&r!l&&!r
12400
$opts{"from"} // $self->{"components"}[0]{"as"}
l!l&&r!l&&!r
4480
MC/DC0%0/2
$opts{"as"} // $table
$opts{"as"} $table
MC/DC0%0/2
$opts{"from"} // $self->{"components"}[0]{"as"}
$opts{"from"} $self->{"components"}[0]{"as"}
Truth table66%2/3
A // B
A= $opts{"as"}B= $table
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $opts{"from"}B= $self->{"components"}[0]{"as"}
ABresult
1X1
011
000
164 52 return ref($self)->new(db_map => $self->{+DB_MAP}, components => [@{$self->{+COMPONENTS}}, $component]);
165 }
166  
167 =pod
168  
169 =over 4
170  
171 =item $table = $join->component_table($name)
172  
173 The table of a component, the base table's for an undef name. Croaks when
174 there is no such component.
175  
176 =item @tables = $join->tables
177  
178 Every component's table, in join order.
179  
180 =back
181  
182 =cut
183  
184 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod documented
185 0 map { $_->{table} } @{$_[0]->{+COMPONENTS}};
186 }
187  
188 65 ▶sub component_table {
Subroutine component_table: called CC=3 SCAR=11.0
Pod documented
189 65 my $self = shift;
190 65 my ($as) = @_;
191 65 my $components = $self->{+COMPONENTS};
192 65 ▶ return $components->[0]->{table} unless defined $as;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $as 49 16 65
193 16 my ($c) = grep { $_->{as} eq $as } @$components;
194 16 ▶ confess "The join has no component named '$as'" unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 1 15 16
195 15 return $c->{table};
196 }
197  
198 =pod
199  
200 =over 4
201  
202 =item $fields = $join->select_fields($given)
203  
204 The labeled select list, a hashref of label to C<component.field>.
205  
206 With no C<$given>, each component's default fields. For an arrayref of
207 C<component.field> names, those. Either way each component's primary key is
208 added. Computed fields (a hashref) come back as they are.
209  
210 Croaks for a plain field name (in either shape) that is not
211 C<component.field>, or that names no component or field.
212  
213 =back
214  
215 =cut
216  
217 39 ▶sub select_fields {
Subroutine select_fields: called CC=11 SCAR=24.0
Pod documented
218 39 my $self = shift;
219 39 my ($given) = @_;
220  
221 39 my $label_of = $self->{+LABEL_OF};
222 39 ▶ my @bad = grep { !ref($_) && !$label_of->{$_} } ref($given) eq 'HASH' ? values %$given : @{$given // []};
Branch: 1 branch
BranchTrueFalseTotal
ref $given eq 'HASH' ? : 11 28 39
Condition100%5/5
ref $_ || $label_of->{$_}
l!l&&r!l&&!r
7154
$given // []
l!l
622
MC/DC100%2/2
ref $_ || $label_of->{$_}
ref $_ $label_of->{$_}
MC/DC100%1/1
$given // []
$given
Truth table100%3/3
A || B
A= ref $_B= $label_of->{$_}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $given
Aresult
11
00
223 39 ▶ confess "A join names fields as component.field; '$bad[0]' is not a field of a component" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 4 35 39
224 35 ▶ return $given if ref($given) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "HASH" 10 25 35
225  
226 25 my $map = $self->{+DB_MAP};
227 25 my @names;
228 25 for my $c (@{$self->{+COMPONENTS}}) {
229 53 my $t = $map->table($c->{table});
230 53 ▶ push @names => map { "$c->{as}.$_" } @{$given ? [] : $t->{default_fields} // [sort keys %{$t->{fields}}]}, @{$t->{pk} // []};
Branch: 1 branch
BranchTrueFalseTotal
$given ? : 7 46 53
Condition100%4/4
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
l!l
343
$t->{"pk"} // []
l!l
494
MC/DC100%1/1
$t->{"default_fields"} // [sort(keys %{$$t{"fields"};})]
$t->{"default_fields"}
MC/DC100%1/1
$t->{"pk"} // []
$t->{"pk"}
Truth table100%2/2
A
A= $t->{"default_fields"}
Aresult
11
00
Truth table100%2/2
A
A= $t->{"pk"}
Aresult
11
00
231 }
232  
233 25 ▶ return {map { $label_of->{$_} => $_ } @{$given // []}, @names};
Condition100%2/2
$given // []
l!l
322
MC/DC100%1/1
$given // []
$given
Truth table100%2/2
A
A= $given
Aresult
11
00
234 }
235  
236 =pod
237  
238 =over 4
239  
240 =item ($sql, \@binds) = $join->from_sql($dialect)
241  
242 The base table and every C<JOIN ... ON ...>, each table with its component
243 name as its alias. No binds.
244  
245 =back
246  
247 =cut
248  
249 32 ▶sub from_sql {
Subroutine from_sql: called CC=3 SCAR=11.0
Pod documented
250 32 my $self = shift;
251 32 my ($dialect) = @_;
252 32 ▶ $self->_check_temps if $self->{+TEMP_RECORDS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"temp_records"} 2 30 32
253  
254 198 ▶ my $q = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
255 198 join '.' => map { $dialect->quote_identifier($_) } @_;
256 31 };
257 31 my ($base, @joined) = @{$self->{+COMPONENTS}};
258  
259 31 my $sql = $q->($base->{table}) . ' AS ' . $q->($base->{as});
260 31 for my $c (@joined) {
261 34 my $on = join ' AND ' => map { $q->($c->{from}, $_) . ' = ' . $q->($c->{as}, $c->{link}->{$_}) } sort keys %{$c->{link}};
262 34 $sql .= ' ' . uc($c->{type}) . ' JOIN ' . $q->($c->{table}) . ' AS ' . $q->($c->{as}) . " ON $on";
263 }
264  
265 31 return ($sql, []);
266 }
267  
268 =pod
269  
270 =over 4
271  
272 =item (\@rows, \&build) = $join->results($orm, \@raw, $ingest, %opts)
273  
274 Splits each fetched row into its components' row data and builds each into a
275 L<DBIx::QuickORM::JoinRow>.
276  
277 With C<$ingest>, the row data of components with a primary key is ingested. A
278 left-joined component that matched nothing (its primary key, or every field
279 without one, NULL) is undef.
280  
281 Component rows use their tables' row classes, the same canon row data giving
282 the same row object for the whole result. With C<row_class> undef, each row is
283 the join row's raw C<data> instead. Croaks for any other C<row_class>.
284  
285 =back
286  
287 =cut
288  
289 24 ▶sub results {
Subroutine results: called CC=15 SCAR=27.1
Pod documented
290 24 my $self = shift;
291 24 my ($orm, $raw, $ingest, %opts) = @_;
292 24 ▶ confess "A join handle's row_class can only be undef (for each row's raw data); its components use their tables' row classes" if defined $opts{row_class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"row_class"} 2 22 24
293  
294 22 my $components = $self->{+COMPONENTS};
295 22 my $labels = $self->{+LABELS};
296 22 my $map = $self->{+DB_MAP};
297 22 my $lookup = $orm->connection->lookup;
298  
299 22 my @rows;
300 22 for my $row (@$raw) {
301 43 my (%data, @parts);
302 43 for my $label (keys %$row) {
303 298 my ($i, $field) = @{$labels->{$label}};
304 298 $data{"$components->[$i]->{as}.$field"} = $parts[$i]->{$field} = $row->{$label};
305 }
306  
307 43 for my $i (0 .. $#$components) {
308 98 my $c = $components->[$i];
309 98 ▶ my $part = $parts[$i] // {};
Condition50%1/2
$parts[$i] // {}
l!l
980
MC/DC0%0/1
$parts[$i] // {}
$parts[$i]
Truth table50%1/2
A
A= $parts[$i]
Aresult
11
00
310 98 my $pk = $map->pk($c->{table});
311 98 ▶ my @key = $pk && @$pk ? @$pk : keys %$part;
Branch: 1 branch
BranchTrueFalseTotal
$pk && @$pk ? : 88 10 98
Condition66%2/3
$pk and @$pk
!ll&&!rl&&r
10088
MC/DC50%1/2
$pk and @$pk
$pk @$pk
Truth table66%2/3
A and B
A= $pkB= @$pk
ABresult
0X0
100
111
312 98 ▶ if (($c->{type} // '') eq 'left' && !grep { defined $part->{$_} } @key) {
Branch: 1 branch
BranchTrueFalseTotal
if (($c->{"type"} // "") eq "left" and not grep({defined $part->{$_};} @key)) 11 87 98
Condition100%5/5
($c->{"type"} // "") eq "left" and not grep({defined $part->{$_};} @key)
!ll&&!rl&&r
741311
$c->{"type"} // ""
l!l
5543
MC/DC100%2/2
($c->{"type"} // "") eq "left" and not grep({defined $part->{$_};} @key)
$c->{"type"} not grep({defined $part->{$_};} @key)
Truth table100%3/3
A and not(B)
A= $c->{"type"}B= not grep({defined $part->{$_};} @key)
ABresult
0X0
100
111
313 11 $parts[$i] = undef;
314 11 next;
315 }
316 87 ▶ $parts[$i] = $ingest && $pk && @$pk ? $lookup->ingest($c->{table}, $part) : $part;
Branch: 1 branch
BranchTrueFalseTotal
$ingest && $pk && @$pk ? : 77 10 87
Condition83%5/6
$ingest and $pk and @$pk
!ll&&!rl&&r
10077
$ingest and $pk
!ll&&!rl&&r
4677
MC/DC66%2/3
$ingest and $pk and @$pk
$ingest $pk @$pk
Truth table75%3/4
A and B and C
A= $ingestB= $pkC= @$pk
ABCresult
0XX0
10X0
1100
1111
317 }
318  
319 43 push @rows => [\%data, \@parts];
320 }
321  
322 22 ▶ return (\@rows, sub { $_[0]->[0] }) if exists $opts{row_class};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"row_class"} 2 20 22
323  
324 20 my @aliases = map { $_->{as} } @$components;
325 20 my @builders = map { $orm->_row_builder($_->{table}) } @$components;
326 20 my %built;
327 35 ▶ my $build = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
328 35 my ($data, $parts) = @{$_[0]};
329 35 my %rows;
330 35 for my $i (0 .. $#$components) {
331 79 ▶ my $part = $parts->[$i] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $part = $parts->[$i] or next 71 8 79
332 71 my $make = $builders[$i];
333 71 ▶ $rows{$aliases[$i]} = $built{refaddr($part)} //= $make ? $make->($part) : $part;
Branch: 1 branch
BranchTrueFalseTotal
$make ? : 54 7 61
Condition66%2/3
$built{builtin::refaddr($part)} //= $make ? &$make($part) : $part
l!l&&r!l&&!r
10610
MC/DC0%0/2
$built{builtin::refaddr($part)} //= $make ? &$make($part) : $part
$built{builtin::refaddr($part)} $make ? &$make($part) : $part
Truth table66%2/3
A //= B
A= $built{builtin::refaddr($part)}B= $make ? &$make($part) : $part
ABresult
1X1
011
000
334 }
335 35 return DBIx::QuickORM::JoinRow->new(aliases => \@aliases, rows => \%rows, data => $data);
336 20 };
337  
338 20 return (\@rows, $build);
339 }
340  
341 =pod
342  
343 =over 4
344  
345 =item $join->_check_temps
346  
347 Croaks unless every temporary table or view among the components is still
348 the one the join was made for.
349  
350 =back
351  
352 =cut
353  
354 2 ▶sub _check_temps {
Subroutine _check_temps: called CC=5 SCAR=16.2
355 2 my $self = shift;
356 2 my $map = $self->{+DB_MAP};
357 2 for my $temp (@{$self->{+TEMP_RECORDS}}) {
358 2 my ($table, $was) = @$temp;
359 2 my $now = $map->table($table);
360 2 ▶ next if $now && $now == $was;
Branch: 1 branch
BranchTrueFalseTotal
if $now and $now == $was 1 1 2
Condition66%2/3
$now and $now == $was
!ll&&!rl&&r
101
MC/DC50%1/2
$now and $now == $was
$now $now == $was
Truth table66%2/3
A and B
A= $nowB= $now == $was
ABresult
0X0
100
111
361 1 ▶ confess "The temporary table or view '$table' this join was made for " . ($now ? "was replaced by another of the same name" : "no longer exists (dropped, rolled back, or its connection replaced)");
Branch: 1 branch
BranchTrueFalseTotal
$now ? : 0 1 1
362 }
363 }
364  
365 259 1;
366  
367 __END__
« blib/lib/DBIx/QuickORM/SchemaBuilder/Table.pm ↑ Summary blib/lib/DBIx/QuickORM.pm »