blib/lib/SQL/Abstract/Plugin/TableAlias.pm

filter: Statementstmt 96.0% 172 / 179 Branchbran 80.8% 55 / 68 Conditioncond 60.3% 38 / 63 MC/DCmcdc 16.6% 6 / 36 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 1 totaltotal 79.0% 287 / 363 CC 73 SCAR 32.1
CC
73
Cov
85%
_alias_from_array
49.4
_alias_select_array
16.4
_alias_where_array
6.0
CRAP 24.7
↑ Summary
1 package SQL::Abstract::Plugin::TableAlias;
2 3 ▶use strict; use warnings; our $VERSION = '0.05';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 with 'SQL::Abstract::Role::Plugin';
6  
7 sub register_extensions {
8 2 ▶ my ($self, $sqla) = @_;
Subroutine register_extensions: called CC=1 SCAR=0.0
Pod undocumented
9 2 $sqla->plugin('+ExtraClauses');
10 $sqla->wrap_expander(select => sub {
11 2 ▶ $self->cb('_alias_select', $_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 });
13 }
14  
15 sub _alias_select {
16 31 ▶ my ($self, $orig, $select, $args) = @_;
Subroutine _alias_select: called CC=3 SCAR=12.5
17 31 ▶ $args = $self->_alias_from_array($args) if (ref $args eq 'HASH' && ref $args->{from} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $args eq "HASH" and ref $args->{"from"} eq "ARRAY" 31 0 31
Condition33%1/3
ref $args eq "HASH" and ref $args->{"from"} eq "ARRAY"
!ll&&!rl&&r
0031
MC/DC0%0/2
ref $args eq "HASH" and ref $args->{"from"} eq "ARRAY"
ref $args eq "HASH" ref $args->{"from"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $args eq "HASH"B= ref $args->{"from"} eq "ARRAY"
ABresult
0X0
100
111
18 31 return $self->sqla->$orig($select, $args);
19 }
20  
21 sub _alias_from_array {
22 31 ▶ my ($self, $args) = @_;
Subroutine _alias_from_array: called CC=36 SCAR=39.0
23  
24 31 ▶ $args->{talias} ||= [];
Condition100%2/2
$args->{"talias"} ||= []
l!l
1219
MC/DC100%1/1
$args->{"talias"} ||= []
$args->{"talias"}
Truth table100%2/2
A
A= $args->{"talias"}
Aresult
11
00
25  
26 31 my ($i, @map, %columns) = 0;
27 31 for my $from (@{ $args->{from} }) {
28 96 ▶ my $ref = ref $from || "";
Condition100%2/2
ref $from || ''
l!l
2967
MC/DC100%1/1
ref $from || ''
ref $from
Truth table100%2/2
A
A= ref $from
Aresult
11
00
29 96 ▶ if ( $ref eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 26 70 96
elsif ($ref eq 'ARRAY') { } 3 67 70
elsif (not $ref || $from =~ /^-/) { } 38 29 67
30 26 ▶ if ($from->{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from->{'to'}) { } 26 0 26
31 26 ▶ $from->{as} ||= scalar @{$args->{talias}} ? shift(@{$args->{talias}}) : ref $from->{to} ? $from->{to}->{-ident}->[0] : $from->{to};
Branch: 2 branches
BranchTrueFalseTotal
scalar @{$args->{'talias'};} ? : 13 13 26
ref $from->{'to'} ? : 1 12 13
Condition33%1/3
$from->{"as"} ||= scalar @{$args->{"talias"};} ? shift @{$$args{"talias"};} : (ref $from->{"to"} ? $from->{"to"}{"-ident"}[0] : $from->{"to"})
l!l&&r!l&&!r
0260
MC/DC0%0/2
$from->{"as"} ||= scalar @{$args->{"talias"};} ? shift @{$$args{"talias"};} : (ref $from->{"to"} ? $from->{"to"}{"-ident"}[0] : $from->{"to"})
$from->{"as"} scalar @{$args->{"talias"};} ? shift @{$$args{"talias"};} : (ref $from->{"to"} ? $from->{"to"}{"-ident"}[0] : $from->{"to"})
Truth table33%1/3
A ||= B
A= $from->{"as"}B= scalar @{$args->{"talias"};} ? shift @{$$args{"talias"};} : (ref $from->{"to"} ? $from->{"to"}{"-ident"}[0] : $from->{"to"})
ABresult
1X1
011
000
32 26 push @map, $from->{as};
33 26 ▶ if ($from->{on}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from->{"on"}) 26 0 26
34 26 ▶ if ($from->{on}->{-op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from->{'on'}{'-op'}) { } 2 24 26
35 2 ▶ if (scalar @{$from->{on}->{-op}->[1]->{-ident}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$from->{"on"}{"-op"}[1]{"-ident"};} == 1) 2 0 2
36 2 unshift @{$from->{on}{-op}[1]{-ident}}, $map[-2];
37 }
38 2 ▶ if (scalar @{$from->{on}->{-op}->[2]->{-ident}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$from->{"on"}{"-op"}[2]{"-ident"};} == 1) 2 0 2
39 2 unshift @{$from->{on}{-op}[2]{-ident}}, $map[-1];
40 }
41 } else {
42 24 for my $on (keys %{$from->{on}}) {
43 24 ▶ if (ref $from->{on}->{$on}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $from->{"on"}{$on}) 24 0 24
44 24 for my $oo (keys %{$from->{on}->{$on}}) {
45 24 $from->{on}->{$on}->{$oo} = sprintf("%s.%s", $map[-1], $from->{on}->{$on}->{$oo});
46 }
47 }
48 24 $from->{on}->{sprintf("%s.%s", $map[-2], $on)} = delete $from->{on}->{$on};
49 }
50 }
51 }
52 } else {
53 0 push @map, $from->{$_}->{-as} ||= shift(@{$args->{talias}}) || $_
54 0 ▶ for ( sort keys %{ $from } );
Condition0%0/6
$from->{$_}{"-as"} ||= shift @{$$args{"talias"};} || $_
l!l&&r!l&&!r
000
shift @{$$args{'talias'};} || $_
l!l&&r!l&&!r
000
MC/DC0%0/3
$from->{$_}{"-as"} ||= shift @{$$args{"talias"};} || $_
$from->{$_}{"-as"} shift @{$$args{'talias'};} $_
Truth table0%0/4
A ||= B || C
A= $from->{$_}{"-as"}B= shift @{$$args{'talias'};}C= $_
ABCresult
1XX1
01X1
0011
0000
55 }
56 } elsif ($ref eq 'ARRAY') {
57 3 ▶ if ($from->[1] eq 'as') {
Branch: 1 branch
BranchTrueFalseTotal
if ($from->[1] eq 'as') { } 0 3 3
58 0 push @map, $from->[2];
59 } else {
60 3 ▶ push @map, shift @{$args->{talias}} || $from->[0];
Condition66%2/3
shift @{$$args{'talias'};} || $from->[0]
l!l&&r!l&&!r
120
MC/DC0%0/2
shift @{$$args{'talias'};} || $from->[0]
shift @{$$args{'talias'};} $from->[0]
Truth table66%2/3
A || B
A= shift @{$$args{'talias'};}B= $from->[0]
ABresult
1X1
011
000
61 3 splice @{$from}, 1, 0, 'as', $map[-1];
62 }
63 3 for my $key ( keys %{ $from->[-1] } ) {
64 3 $from->[-1]->{_valid_column($key, $map[-2], {})} = _valid_column(delete $from->[-1]->{$key}, $map[-1], {});
65 }
66 ▶ } elsif (!$ref && $from !~ m/^-/) {
Condition66%2/3
$ref || $from =~ /^-/
l!l&&r!l&&!r
02938
MC/DC50%1/2
$ref || $from =~ /^-/
$ref $from =~ /^-/
Truth table66%2/3
A || B
A= $refB= $from =~ /^-/
ABresult
1X1
011
000
67 38 ▶ push @map, shift @{$args->{talias}} || $from;
Condition66%2/3
shift @{$$args{'talias'};} || $from
l!l&&r!l&&!r
12260
MC/DC0%0/2
shift @{$$args{'talias'};} || $from
shift @{$$args{'talias'};} $from
Truth table66%2/3
A || B
A= shift @{$$args{'talias'};}B= $from
ABresult
1X1
011
000
68 38 splice @{$args->{from}}, $i, 1, { $from => { 'as' => $map[-1] } };
69 }
70 96 $i++;
71 }
72  
73 ($args, %columns) = $self->_alias_select_array($args, \@map)
74 31 ▶ if ((ref($args->{select}) || "") eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"select"} || "") eq "ARRAY" 31 0 31
Condition50%1/2
ref $args->{'select'} || ''
l!l
310
MC/DC0%0/1
ref $args->{'select'} || ''
ref $args->{'select'}
Truth table50%1/2
A
A= ref $args->{'select'}
Aresult
11
00
75
76 31 my $where = ref $args->{where};
77 31 ▶ $where eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
$where eq 'HASH' ? : 5 26 31
Condition66%2/3
$where eq "ARRAY" and $self->_alias_where_array($args, $map[0], \%columns)
!ll&&!rl&&r
2060
MC/DC0%0/2
$where eq "ARRAY" and $self->_alias_where_array($args, $map[0], \%columns)
$where eq "ARRAY" $self->_alias_where_array($args, $map[0], \%columns)
Truth table66%2/3
A and B
A= $where eq "ARRAY"B= $self->_alias_where_array($args, $map[0], \%columns)
ABresult
0X0
100
111
78 ? $self->_alias_where_hash($args, $map[0], \%columns)
79 : $where eq 'ARRAY' && $self->_alias_where_array($args, $map[0], \%columns);
80  
81 31 my $order_by = ref $args->{order_by};
82 $order_by eq 'HASH'
83 ? $self->_alias_order_by_hash($args, $map[0], \%columns)
84 : $order_by eq 'ARRAY'
85 ? $self->_alias_order_by_array($args, $map[0], \%columns)
86 31 ▶ : defined $args->{order_by} && ! ref $order_by && $self->_alias_order_by_string($args, $map[0], \%columns);
Branch: 2 branches
BranchTrueFalseTotal
$order_by eq 'HASH' ? : 1 30 31
$order_by eq 'ARRAY' ? : 6 24 30
Condition66%4/6
defined $args->{"order_by"} and not ref $order_by and $self->_alias_order_by_string($args, $map[0], \%columns)
!ll&&!rl&&r
12120
defined $args->{"order_by"} and not ref $order_by
!ll&&!rl&&r
12012
MC/DC0%0/3
defined $args->{"order_by"} and not ref $order_by and $self->_alias_order_by_string($args, $map[0], \%columns)
defined $args->{"order_by"} not ref $order_by $self->_alias_order_by_string($args, $map[0], \%columns)
Truth table0%0/4
A and not(B) and C
A= defined $args->{"order_by"}B= not ref $order_byC= $self->_alias_order_by_string($args, $map[0], \%columns)
ABCresult
0XX0
10X0
1100
1111
87  
88 31 my $group_by = ref $args->{group_by};
89 31 ▶ $group_by eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
$group_by eq 'ARRAY' ? : 5 26 31
Condition66%2/3
$group_by eq "HASH" and $self->_alias_group_by_hash($args, $map[0], \%columns)
!ll&&!rl&&r
2420
MC/DC0%0/2
$group_by eq "HASH" and $self->_alias_group_by_hash($args, $map[0], \%columns)
$group_by eq "HASH" $self->_alias_group_by_hash($args, $map[0], \%columns)
Truth table66%2/3
A and B
A= $group_by eq "HASH"B= $self->_alias_group_by_hash($args, $map[0], \%columns)
ABresult
0X0
100
111
90 ? $self->_alias_group_by_array($args, $map[0], \%columns)
91 : $group_by eq 'HASH' && $self->_alias_group_by_hash($args, $map[0], \%columns);
92  
93 31 return $args;
94 }
95  
96 sub _alias_select_array {
97 31 ▶ my ($self, $args, $map) = @_;
Subroutine _alias_select_array: called CC=16 SCAR=28.0
98 31 my ($i, $last_array, @select, %columns) = -1;
99 31 for my $sel ( @{ $args->{select} } ) {
100 122 my $ref = ref $sel;
101 122 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 33 89 122
elsif ($ref eq 'HASH') { } 2 87 89
elsif (not $ref) { } 87 0 87
102 33 $i++;
103 33 ▶ for (my $l = 0; $l < scalar @{$sel}; $l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l < scalar @{$sel;}) 44 33 77
104 44 ▶ if (ref($sel->[$l]) || "" eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sel->[$l] or !1) { } 1 43 44
Condition100%2/2
ref $sel->[$l] or
l!l
143
MC/DC100%1/1
ref $sel->[$l] or
ref $sel->[$l]
Truth table100%2/2
A
A= ref $sel->[$l]
Aresult
11
00
105 1 for my $key ( keys %{ $sel->[$l] } ) {
106 1 $sel->[$l]{_valid_column($key, $map->[$i], {})} = delete $sel->[$l]{$key};
107 1 $columns{$key} = $map->[$i];
108 }
109 1 push @select, $sel->[$l];
110 } else {
111 43 $columns{$sel->[$l]} = $map->[$i];
112 43 push @select, sprintf("%s.%s", $map->[$i], $sel->[$l]);
113 }
114 }
115 33 $last_array = 1;
116 } elsif ( $ref eq 'HASH' ) {
117 2 ▶ $i++ if $i < 0 || $last_array && do { $last_array = 0; 1; };
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 or $last_array and do { $last_array = 0; 1 } 1 1 2
Condition50%3/6
$i < 0 or $last_array and do { $last_array = 0; 1 }
l!l&&r!l&&!r
101
$last_array and do { $last_array = 0; 1 }
!ll&&!rl&&r
100
MC/DC0%0/3
$i < 0 or $last_array and do { $last_array = 0; 1 }
$i < 0 $last_array do { $last_array = 0; 1 }
Truth table25%1/4
A or B and C
A= $i < 0B= $last_arrayC= do { $last_array = 0; 1 }
ABCresult
1XX1
0111
00X0
0100
118 2 for my $key ( keys %{ $sel } ) {
119 2 $sel->{_valid_column($key, $map->[$i], {})} = delete $sel->{$key};
120 2 $columns{$key} = $map->[$i];
121 }
122 2 push @select, $sel;
123 } elsif (! $ref) {
124 87 ▶ $i++ if $i < 0 || $last_array && do { $last_array = 0; 1; };
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 or $last_array and do { $last_array = 0; 1 } 31 56 87
Condition83%5/6
$i < 0 or $last_array and do { $last_array = 0; 1 }
l!l&&r!l&&!r
273030
$last_array and do { $last_array = 0; 1 }
!ll&&!rl&&r
5604
MC/DC0%0/3
$i < 0 or $last_array and do { $last_array = 0; 1 }
$i < 0 $last_array do { $last_array = 0; 1 }
Truth table50%2/4
A or B and C
A= $i < 0B= $last_arrayC= do { $last_array = 0; 1 }
ABCresult
1XX1
0111
00X0
0100
125 87 $columns{$sel} = $map->[$i];
126 87 push @select, sprintf("%s.%s", $map->[$i], $sel);
127 }
128 }
129 31 $args->{select} = \@select;
130 31 return ($args, %columns);
131 }
132  
133 sub _alias_where_hash {
134 17 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_where_hash: called CC=4 SCAR=14.8
135 17 my %where;
136 17 for my $w (keys %{ $args->{where} }) {
137 39 ▶ if (ref $args->{$w} eq 'HASH' && $args->{$w}->{-alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{$w} eq 'HASH' and $args->{$w}{'-alias'}) { } 0 39 39
Condition33%1/3
ref $args->{$w} eq "HASH" and $args->{$w}{"-alias"}
!ll&&!rl&&r
3900
MC/DC0%0/2
ref $args->{$w} eq "HASH" and $args->{$w}{"-alias"}
ref $args->{$w} eq "HASH" $args->{$w}{"-alias"}
Truth table33%1/3
A and B
A= ref $args->{$w} eq "HASH"B= $args->{$w}{"-alias"}
ABresult
0X0
100
111
138 0 $where{sprintf("%s.%s", delete $args->{where}->{$w}->{-alias}, $w)} = $args->{where}->{$w};
139 } else {
140 39 $where{_valid_column($w, $default, $columns)} = $args->{where}->{$w};
141 }
142 }
143 17 $args->{where} = \%where;
144 17 return $args;
145 }
146  
147 sub _alias_where_array {
148 6 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_where_array: called CC=6 SCAR=17.9
149 6 ▶ my $list = grep { $_ % 2 > 0 && $args->{where}->[$_] =~ m/-(and|or)/ } 0 .. scalar @{ $args->{where} };
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 > 0 and $args->{"where"}[$_] =~ /-(and|or)/ 8 14 22
150 6 ▶ return $self->_alias_where_list($args, $default, $columns) if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 6 6
151 6 for my $where (@{ $args->{where} }) {
152 16 ▶ my $ref = ref $where || "";
Condition100%2/2
ref $where || ''
l!l
124
MC/DC100%1/1
ref $where || ''
ref $where
Truth table100%2/2
A
A= ref $where
Aresult
11
00
153 16 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 12 4 16
154 12 my $update = $self->_alias_where_hash({ where => $where }, $default, $columns);
155 12 %{$where} = %{$update->{where}};
156 }
157 }
158 6 return $args;
159 }
160  
161 sub _alias_order_by_hash {
162 9 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_order_by_hash: called CC=5 SCAR=16.1
163 9 my %order_by;
164 9 for my $w (keys %{ $args->{order_by} }) {
165 9 my $ref = ref $args->{order_by}->{$w};
166 9 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 5 4 9
elsif (not $ref) { } 4 0 4
167 5 my @order;
168 5 for my $o ( @{ $args->{order_by}->{$w} } ) {
169 10 push @order, _valid_column($o, $default, $columns);
170 }
171 5 $order_by{$w} = \@order;
172 } elsif ( ! $ref ) {
173 4 $order_by{$w} = _valid_column($args->{order_by}{$w}, $default, $columns);
174 }
175 }
176 9 $args->{order_by} = \%order_by;
177 9 return $args;
178 }
179  
180 sub _alias_order_by_array {
181 6 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_order_by_array: called CC=4 SCAR=13.9
182 6 my $i = 0;
183 6 for my $w ( @{ $args->{order_by} }) {
184 16 ▶ my $ref = ref $w || "";
Condition100%2/2
ref $w || ''
l!l
88
MC/DC100%1/1
ref $w || ''
ref $w
Truth table100%2/2
A
A= ref $w
Aresult
11
00
185 16 ▶ if ($ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 8 8 16
186 8 my $new = $self->_alias_order_by_hash({ order_by => $w }, $default, $columns);
187 8 %{$w} = %{$new->{order_by}};
188 } else {
189 8 $args->{order_by}->[$i] = _valid_column($w, $default, $columns);
190 }
191 16 $i++;
192 }
193 6 return $args;
194 }
195  
196 sub _alias_order_by_string {
197 12 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_order_by_string: called CC=1 SCAR=0.0
198 12 $args->{order_by} = _valid_column($args->{order_by}, $default, $columns);
199 }
200  
201 sub _alias_group_by_array {
202 5 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_group_by_array: called CC=2 SCAR=6.9
203 5 my @group_by;
204 5 for my $group (@{ $args->{group_by} }) {
205 7 push @group_by, _valid_column($group, $default, $columns);
206 }
207 5 $args->{group_by} = \@group_by;
208 5 return $args;
209 }
210  
211 sub _alias_group_by_hash {
212 2 ▶ my ($self, $args, $default, $columns) = @_;
Subroutine _alias_group_by_hash: called CC=3 SCAR=11.0
213 2 my @group_by = shift @{ $args->{group_by}->{-op} };
214 2 for my $group (@{ $args->{group_by}->{-op} }) {
215 $group->{-ident} = ref $group->{-ident} ? [map {
216 4 _valid_column($_, $default, $columns);
217 4 ▶ } @{$group->{-ident}}] : _valid_column($group->{-ident}, $default, $columns);
Branch: 1 branch
BranchTrueFalseTotal
ref $group->{'-ident'} ? : 4 0 4
218 4 push @group_by, $group;
219 }
220 2 $args->{group_by}->{-op} = \@group_by;
221 2 return $args;
222 }
223  
224 sub _valid_column {
225 93 ▶ my ($column, $default, $columns) = @_;
Subroutine _valid_column: called CC=3 SCAR=11.0
226 93 ▶ return $column if ($column =~ m/[^.]+\W+/);
Branch: 1 branch
BranchTrueFalseTotal
if $column =~ /[^.]+\W+/ 1 92 93
227 92 ▶ return sprintf( "%s.%s", ($columns->{$column} || $default), $column);
Condition66%2/3
$columns->{$column} || $default
l!l&&r!l&&!r
57350
MC/DC0%0/2
$columns->{$column} || $default
$columns->{$column} $default
Truth table66%2/3
A || B
A= $columns->{$column}B= $default
ABresult
1X1
011
000
228 }
229  
230 3 1;
231  
232 __END__
↑ Summary