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

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