SQL/Abstract/Plugin/ExtraClausesFixed.pm

filter: Statementstmt 92.6% 38 / 41 Branchbran 68.7% 11 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 79.7% 59 / 74 CC 12 SCAR 14.7
CC
12
Cov
83%
_expand_join
6.6
_render_setop
5.2
_expand_select
3.0
CRAP 4.4
« SQL/Abstract/Plugin/WindowFunctions.pm ↑ Summary SQL/Abstract/Plugin/Upsert.pm »
1 package SQL::Abstract::Plugin::ExtraClausesFixed;
2 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use experimental qw/signatures postderef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # TODO - get this upstreamed - `using` is borken RN b/c
5  
6 5 our $VERSION = '0.02_1';
7 5 extends 'SQL::Abstract::Plugin::ExtraClauses';
8  
9 has no_setop_parens => (
10 is => 'lazy',
11 1 ▶ builder => sub ($self) {
Subroutine _build_no_setop_parens: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 0 my $details = $self->sqla->_connection_info;
13 0 return $details->{SQL_DBMS_NAME} eq 'SQLite';
14 }
15 5 );
16  
17 # NOTE - upstream impl fails to put `group_by` and `having` before the setops; that's
18 # fixed here
19 44 ▶sub _expand_select {
Subroutine _expand_select: called CC=3 SCAR=11.0
20 44 my ($self, $orig, $before_setop, @args) = @_;
21 44 my $exp = $self->sqla->$orig(@args);
22 44 ▶ return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
Branch: 1 branch
BranchTrueFalseTotal
unless my $setop = +(my $sel = $exp->{"-select"})->{"setop"} 43 1 44
23 1 ▶ if (my @keys = grep $sel->{$_}, @$before_setop, qw/group_by having/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = grep($sel->{$_}, @$before_setop, ("group_by", "having"))) 1 0 1
24 1 my %inner;
25 1 @inner{@keys} = delete @{$sel}{@keys};
26 1 unshift @{ (values(%$setop))[0]{queries} }, { -select => \%inner };
27 }
28 1 return $exp;
29 }
30  
31 1 ▶sub _render_setop {
Subroutine _render_setop: called CC=5 SCAR=16.5
32 1 my ($self, $setop, $args) = @_;
33 1 ▶ if ($self->no_setop_parens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_setop_parens) 1 0 1
34 1 for my $q (@{ $args->{queries} }) {
35 2 ▶ if ($q->{-literal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q->{"-literal"}) 1 1 2
36 1 $q->{-literal}[0] =~ s/^\(|\)$//g;
37 }
38 }
39 }
40 $self->join_query_parts(
41 { -keyword => ' ' . join('_', $setop, ($args->{type} || ())) . ' ' },
42 map $self->render_aqt($_, $self->no_setop_parens),
43 1 ▶ @{ $args->{queries} }
Condition33%1/3
$args->{'type'} || ()
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'type'} || ()
$args->{'type'} ()
Truth table33%1/3
A || B
A= $args->{'type'}B= ()
ABresult
1X1
011
000
44 );
45 }
46  
47 # NOTE - upstream accidentally double expands `using`, so we need to replace that here
48 4 ▶sub _expand_join {
Subroutine _expand_join: called CC=6 SCAR=18.8
49 4 my ($self, undef, $args) = @_;
50 4 ▶ my %proto = (
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 0 4 4
51 ref($args) eq 'HASH'
52 ? %$args
53 : (to => @$args)
54 );
55 4 ▶ if (my $as = delete $proto{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $as = delete $proto{"as"}) 0 4 4
56 0 $proto{to} = $self->expand_expr({ -as => [ { -from_list => $proto{to} }, $as ] });
57 }
58 4 ▶ if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH") 2 2 4
Condition66%2/3
defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH"
!ll&&!rl&&r
202
MC/DC50%1/2
defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH"
defined $proto{"using"} ref(my $using = $proto{"using"}) ne "HASH"
Truth table66%2/3
A and B
A= defined $proto{"using"}B= ref(my $using = $proto{"using"}) ne "HASH"
ABresult
0X0
100
111
59 2 ▶ $proto{using} = { -list => [ ref($using) eq 'ARRAY' ? @$using : $using ] };
Branch: 1 branch
BranchTrueFalseTotal
ref $using eq 'ARRAY' ? : 2 0 2
60 }
61 my %ret = (
62 type => delete $proto{type},
63 4 to => $self->expand_expr({ -from_list => delete $proto{to} }, -ident),
64 );
65 4 %ret = (%ret, map +($_ => $self->expand_expr($proto{$_}, -ident)), sort keys %proto);
66 4 return +{ -join => \%ret };
67 }
68  
69 5 9092;
70  
71 =encoding utf8
72  
73 =head1 NAME
74  
75 SQL::Abstract::Plugin::ExtraClausesFixed - Fixes for ExtraClauses
76  
77 =head1 DESCRIPTION
78  
79 This is a subclass of SQL::Abstract::Plugin::ExtraClauses that fixes a few annoying bugs . Details below !
80  
81 B <EXPERIMENTAL>
82  
83 =head2 Using with DBIx::Class
84  
85 In order to use this with DBIx::Class, you simply need to apply the DBIC-SQLA2 plugin,
86 and then your SQLMaker will support these fixes!
87  
88 =head2 Bugs fixed
89  
90 =head3 using in -join
91  
92 The ExtraClauses plugin has a bug that it double expands the using option for joins, making them unusable unless you explicitly pass in a hashref. This fixes, that allowing you to do natural joins using multiple columns.
93  
94 =head3 setop inner select marshalling
95  
96 ExtraClauses has a bug where it puts the group_by and having clauses after setops (like UNION and friends). This is incorrect, the inner subquery is meant to have those clauses. This is fixed here.
97  
98 In order to use this feature, you must pass group_by or having with the bang override syntax ('!group_by' => ['things', 'and', 'stuff']). I may get an idea one day and figure out how to support it even with a bare group_by.
99  
100 =head3 SQLite setop parens
101  
102 Syntax across different DBMSs is annoying. DBIC likes assuming that you can arbitrarily
103 wrap subqueries in as many parens as you'd like. SQLite considers it a syntax error if the
104 queries that you join with a UNION are parenthesized. This class handles the selective
105 parenthesizing. It will also strip parens if necessary from the output of a $rs->as_query,
106 allowing you to use the setops in the most natural way.
107  
108 =cut
« SQL/Abstract/Plugin/WindowFunctions.pm ↑ Summary SQL/Abstract/Plugin/Upsert.pm »