DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 154 CC 14 SCAR 41.4
CC
14
Cov
0%
expand_join_condition
110.0
_collapsify
30.0
on
2.0
CRAP 63.0
1 package DBIx::Class::SQLMaker::Role::SQLA2Passthrough;
2  
3 our $VERSION = '2.000003';
4 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
5  
6 use strict;
7 use warnings;
8 use Exporter 'import';
9  
10 our @EXPORT = qw(on);
11  
12 sub on (&) {
13 my ($on) = @_;
14 sub {
15 my ($args) = @_;
16 $args->{self_resultsource}
17 ->schema->storage->sql_maker
18 ->expand_join_condition(
19 $on->($args),
20 $args
21 );
22 }
23 }
24  
25 use Role::Tiny;
26  
27 around select => sub {
28 my ($orig, $self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
29  
30 $fields = \[ $self->render_expr({ -list => [
31 grep defined,
32 map +(ref($_) eq 'HASH'
33 ? do {
34 my %f = %$_;
35 my $as = delete $f{-as};
36 my ($f, $rhs) = %f;
37 my $func = +{ ($f =~ /^-/ ? $f : "-${f}") => $rhs };
38 ($as
39 ? +{ -op => [ 'as', $func, { -ident => [ $as ] } ] }
40 : $func)
41 }
42 : $_), ref($fields) eq 'ARRAY' ? @$fields : $fields
43 ] }, -ident) ];
44  
45 if (my $gb = $rs_attrs->{group_by}) {
46 $rs_attrs = {
47 %$rs_attrs,
48 group_by => \[ $self->render_expr({ -list => $gb }, -ident) ]
49 };
50 }
51 $self->$orig($table, $fields, $where, $rs_attrs, $limit, $offset);
52 };
53  
54 sub expand_join_condition {
55 my ($self, $cond, $args) = @_;
56 my ($type, %known) = do {
57 if (my $obj = $args->{self_result_object}) {
58 (self => $obj->get_columns)
59 } elsif (my $val = $args->{foreign_values}) {
60 (foreign => %$val)
61 } else {
62 ('')
63 }
64 };
65 my $maybe = $type ? 1 : 0;
66 my $outside;
67 my $wrap = sub {
68 my ($orig) = @_;
69 $outside = $orig;
70 sub {
71 my $res = $orig->(@_);
72 my ($name, $col) = @{$res->{-ident}};
73 if ($name eq 'self' or $name eq 'foreign') {
74 if ($type eq $name) {
75 $maybe = 0 unless exists $known{$col};
76 }
77 return { -ident => [ $args->{"${name}_alias"}, $col ] };
78 }
79 return $res;
80 };
81 };
82 my $sqla = $self->clone->wrap_op_expander(ident => $wrap);
83 my $aqt = $sqla->expand_expr($cond, -ident);
84 return $aqt unless $maybe;
85 my $inner_wrap = sub {
86 my $res = $outside->(@_);
87 my ($name, $col) = @{$res->{-ident}};
88 if ($name eq 'self' or $name eq 'foreign') {
89 if ($type eq $name) {
90 return { -bind => [ $args->{"${name}_alias"}.'.'.$col, $known{$col} ] };
91 }
92 return { -ident => [ $args->{"${name}_alias"}, $col ] };
93 }
94 return $res;
95 };
96 $sqla->op_expander(ident => $inner_wrap);
97 my $inner_aqt = $self->_collapsify($sqla->expand_expr($cond, -ident));
98 return ($aqt, $inner_aqt);
99 }
100  
101 sub _collapsify {
102 my ($self, $aqt) = @_;
103 return $aqt unless my @opargs = @{$aqt->{-op}};
104 my ($logop, @args) = @opargs;
105 return $aqt unless $logop eq 'and';
106 my %collapsed = map {
107 my $q = $_;
108 return $aqt unless my @opargs = @{$q->{-op}};
109 my ($op, $lhs, @rest) = @opargs;
110 return $aqt unless my @ident = @{$lhs->{-ident}};
111 (join('.', @ident), { $op => \@rest });
112 } @args;
113 return \%collapsed;
114 }
115  
116 1;
117  
118 __END__