| 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
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | if (my @keys = grep $sel->{$_}, @$before_setop, qw/group_by having/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 34 | 1 | for my $q (@{ $args->{queries} }) { | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | if ($q->{-literal}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || ()
MC/DC0%0/2
$args->{'type'} || ()
$args->{'type'}
()
Truth table33%1/3
A || B
A= $args->{'type'}B= ()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | ref($args) eq 'HASH' | ||||||||||||||||||||||||||||
| 52 | ? %$args | ||||||||||||||||||||||||||||
| 53 | : (to => @$args) | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if (my $as = delete $proto{as}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $proto{"using"} and ref(my $using = $proto{"using"}) ne "HASH"
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"
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $proto{using} = { -list => [ ref($using) eq 'ARRAY' ? @$using : $using ] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||