DBIx/Class/SQLMaker/ClassicExtensions.pm

filter: Statementstmt 94.6% 427 / 451 Branchbran 80.3% 159 / 198 Conditioncond 59.7% 80 / 134 MC/DCmcdc 38.9% 30 / 77 Subroutinesub 97.6% 42 / 43 Podpod 20.0% 1 / 5 totaltotal 81.3% 739 / 908 CC 168 SCAR 26.5
CC
168
Cov
85%
_where_op_multicolumn_in
59.3
_subqueried_limit_attrs
24.9
select
18.4
CRAP 14.1
« DBIx/Class/ResultSource/RowParser/Util.pm ↑ Summary DBIx/Class/Relationship/Base.pm »
1 package DBIx::Class::SQLMaker::ClassicExtensions;
2  
3 216 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 216 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 DBIx::Class::SQLMaker::ClassicExtensions - Class containing generic enhancements to SQL::Abstract::Classic
9  
10 =head1 DESCRIPTION
11  
12 This module is not intended to be used standalone. Instead it represents
13 a quasi-role, that one would "mix in" via classic C<@ISA> inheritance into
14 a DBIx::Class::SQLMaker-like provider. See
15 L<DBIx::Class::Storage::DBI/connect_call_rebase_sqlmaker> for more info.
16  
17 Currently the enhancements over L<SQL::Abstract::Classic> are:
18  
19 =over
20  
21 =item * Support for C<JOIN> statements (via extended C<table/from> support)
22  
23 =item * Support of functions in C<SELECT> lists
24  
25 =item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter)
26  
27 =item * A rudimentary multicolumn IN operator
28  
29 =item * Support of C<...FOR UPDATE> type of select statement modifiers
30  
31 =back
32  
33 =cut
34  
35 # to pull in CAG and the frame-boundary-markers
36 216 ▶use base 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 216 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 216 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 216 __PACKAGE__->mk_group_accessors (simple => qw/quote_char name_sep limit_dialect/);
41  
42 0 ▶sub _quoting_enabled {
Subroutine _quoting_enabled: not called CC=3 SCAR=24.8
43 0 ▶ ( defined $_[0]->{quote_char} and length $_[0]->{quote_char} ) ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'quote_char'} && length $_[0]{'quote_char'} ? : 0 0 0
Condition0%0/3
defined $_[0]{"quote_char"} and length $_[0]{"quote_char"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0]{"quote_char"} and length $_[0]{"quote_char"}
defined $_[0]{"quote_char"} length $_[0]{"quote_char"}
Truth table0%0/3
A and B
A= defined $_[0]{"quote_char"}B= length $_[0]{"quote_char"}
ABresult
0X0
100
111
44 }
45  
46 # for when I need a normalized l/r pair
47 3173 ▶sub _quote_chars {
Subroutine _quote_chars: called CC=6 SCAR=20.7
48  
49 # in case we are called in the old !!$sm->_quote_chars fashion
50 3173 ▶ return () if !wantarray and ( ! defined $_[0]->{quote_char} or ! length $_[0]->{quote_char} );
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray or defined $_[0]{"quote_char"} and length $_[0]{"quote_char"} 0 3173 3173
Condition16%1/6
wantarray or defined $_[0]{"quote_char"} and length $_[0]{"quote_char"}
l!l&&r!l&&!r
317300
defined $_[0]{"quote_char"} and length $_[0]{"quote_char"}
!ll&&!rl&&r
000
MC/DC0%0/3
wantarray or defined $_[0]{"quote_char"} and length $_[0]{"quote_char"}
wantarray defined $_[0]{"quote_char"} length $_[0]{"quote_char"}
Truth table25%1/4
A or B and C
A= wantarrayB= defined $_[0]{"quote_char"}C= length $_[0]{"quote_char"}
ABCresult
1XX1
0111
00X0
0100
51  
52 map
53 6346 ▶ { defined $_ ? $_ : '' }
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 4634 1712 6346
ref $_[0]{'quote_char'} ? : 2072 1101 3173
54 3173 ( ref $_[0]->{quote_char} ? (@{$_[0]->{quote_char}}) : ( ($_[0]->{quote_char}) x 2 ) )
55 ;
56 }
57  
58 # FIXME when we bring in the storage weaklink, check its schema
59 # weaklink and channel through $schema->throw_exception
60 150 ▶sub throw_exception { DBIx::Class::Exception->throw($_[1]) }
Subroutine throw_exception: called CC=1 SCAR=0.0
Pod undocumented
61  
62 60 ▶sub belch {
Subroutine belch: called CC=1 SCAR=0.0
Pod undocumented
63 60 shift; # throw away $self
64 60 carp( "Warning: ", @_ );
65 };
66  
67 149 ▶sub puke {
Subroutine puke: called CC=1 SCAR=0.0
Pod undocumented
68 149 shift->throw_exception("Fatal: " . join ('', @_));
69 };
70  
71 # constants-methods are used not only here, but also in comparison tests
72 1596 ▶sub __rows_bindtype () {
Subroutine __rows_bindtype: called CC=1 SCAR=0.0
73 1596 +{ sqlt_datatype => 'integer' }
74 }
75 110 ▶sub __offset_bindtype () {
Subroutine __offset_bindtype: called CC=1 SCAR=0.0
76 110 +{ sqlt_datatype => 'integer' }
77 }
78 43 ▶sub __total_bindtype () {
Subroutine __total_bindtype: called CC=1 SCAR=0.0
79 43 +{ sqlt_datatype => 'integer' }
80 }
81  
82 # the "oh noes offset/top without limit" constant
83 # limited to 31 bits for sanity (and consistency,
84 # since it may be handed to the like of sprintf %u)
85 #
86 # Also *some* builds of SQLite fail the test
87 # some_column BETWEEN ? AND ?: 1, 4294967295
88 # with the proper integer bind attrs
89 #
90 # Implemented as a method, since ::Storage::DBI also
91 # refers to it (i.e. for the case of software_limit or
92 # as the value to abuse with MSSQL ordered subqueries)
93 sub __max_int () { 0x7FFFFFFF };
94  
95 # we ne longer need to check this - DBIC has ways of dealing with it
96 # specifically ::Storage::DBI::_resolve_bindattrs()
97 sub _assert_bindval_matches_bindtype () { 1 };
98  
99 # poor man's de-qualifier
100 136560 ▶sub _quote {
Subroutine _quote: called CC=3 SCAR=11.1
101 136560 ▶ $_[0]->next::method( ( $_[0]{_dequalify_idents} and ! ref $_[1] )
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_dequalify_idents'} && !ref($_[1]) ? : 1202 135358 136560
Condition66%2/3
$_[0]{"_dequalify_idents"} and not ref $_[1]
!ll&&!rl&&r
13535801202
MC/DC50%1/2
$_[0]{"_dequalify_idents"} and not ref $_[1]
$_[0]{"_dequalify_idents"} not ref $_[1]
Truth table66%2/3
A and not(B)
A= $_[0]{"_dequalify_idents"}B= not ref $_[1]
ABresult
0X0
100
111
102 ? $_[1] =~ / ([^\.]+) $ /x
103 : $_[1]
104 );
105 }
106  
107 2 ▶sub _where_op_NEST {
Subroutine _where_op_NEST: called CC=1 SCAR=0.0
108 2 carp_unique ("-nest in search conditions is deprecated, you most probably wanted:\n"
109 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
110 );
111  
112 2 shift->next::method(@_);
113 }
114  
115 # Handle limit-dialect selection
116 7874 ▶sub select {
Subroutine select: called CC=16 SCAR=29.1
Pod documented
117 7874 my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
118  
119  
120 7874 ($fields, @{$self->{select_bind}}) = $self->_recurse_fields($fields);
121  
122 7873 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) 133 7740 7873
123 133 ▶ $self->throw_exception('A supplied offset must be a non-negative integer')
Branch: 1 branch
BranchTrueFalseTotal
if $offset =~ /\D/ or $offset < 0 0 133 133
Condition33%1/3
$offset =~ /\D/ or $offset < 0
l!l&&r!l&&!r
00133
MC/DC0%0/2
$offset =~ /\D/ or $offset < 0
$offset =~ /\D/ $offset < 0
Truth table33%1/3
A or B
A= $offset =~ /\D/B= $offset < 0
ABresult
1X1
011
000
124 if ( $offset =~ /\D/ or $offset < 0 );
125 }
126 7873 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
1337740
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
127  
128 7873 ▶ if (defined $limit) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $limit) { } 1673 6200 7873
elsif ($offset) { } 0 6200 6200
129 1673 ▶ $self->throw_exception('A supplied limit must be a positive integer')
Branch: 1 branch
BranchTrueFalseTotal
if $limit =~ /\D/ or $limit <= 0 0 1673 1673
Condition33%1/3
$limit =~ /\D/ or $limit <= 0
l!l&&r!l&&!r
001673
MC/DC0%0/2
$limit =~ /\D/ or $limit <= 0
$limit =~ /\D/ $limit <= 0
Truth table33%1/3
A or B
A= $limit =~ /\D/B= $limit <= 0
ABresult
1X1
011
000
130 if ( $limit =~ /\D/ or $limit <= 0 );
131 }
132 elsif ($offset) {
133 0 $limit = $self->__max_int;
134 }
135  
136  
137 7873 my ($sql, @bind);
138 7873 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) { } 1673 6200 7873
139 # this is legacy code-flow from SQLA::Limit, it is not set in stone
140  
141 1673 ($sql, @bind) = $self->next::method ($table, $fields, $where);
142  
143 1673 my $limiter;
144  
145 1673 ▶ if( $limiter = $self->can ('emulate_limit') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limiter = $self->can('emulate_limit')) { } 1 1672 1673
146 1 carp_unique(
147 'Support for the legacy emulate_limit() mechanism inherited from '
148 . 'SQL::Abstract::Limit has been deprecated, and will be removed at '
149 . 'some future point, as it gets in the way of architectural and/or '
150 . 'performance advances within DBIC. If your code uses this type of '
151 . 'limit specification please file an RT and provide the source of '
152 . 'your emulate_limit() implementation, so an acceptable upgrade-path '
153 . 'can be devised'
154 );
155 }
156 else {
157 1672 ▶ my $dialect = $self->limit_dialect
Branch: 1 branch
BranchTrueFalseTotal
my $dialect = $self->limit_dialect or $self->throw_exception("Unable to generate SQL-limit - no limit dialect specified on $self") 1672 0 1672
158 or $self->throw_exception( "Unable to generate SQL-limit - no limit dialect specified on $self" );
159  
160 1672 ▶ $limiter = $self->can ("_$dialect")
Branch: 1 branch
BranchTrueFalseTotal
$limiter = $self->can("_$dialect") or $self->throw_exception('DBIx::Class::SQLMaker::ClassicExtensions' . " does not implement the requested dialect '${dialect}'") 1672 0 1672
161 or $self->throw_exception(__PACKAGE__ . " does not implement the requested dialect '$dialect'");
162 }
163  
164 $sql = $self->$limiter (
165 $sql,
166 1673 ▶ { %{$rs_attrs||{}}, _selector_sql => $fields },
Condition50%1/2
$rs_attrs || {}
l!l
16730
MC/DC0%0/1
$rs_attrs || {}
$rs_attrs
Truth table50%1/2
A
A= $rs_attrs
Aresult
11
00
167 $limit,
168 $offset
169 );
170 }
171 else {
172 6200 ($sql, @bind) = $self->next::method ($table, $fields, $where, $rs_attrs);
173 }
174  
175 7870 push @{$self->{where_bind}}, @bind;
176  
177 # this *must* be called, otherwise extra binds will remain in the sql-maker
178 7870 my @all_bind = $self->_assemble_binds;
179  
180 $sql .= $self->_lock_select ($rs_attrs->{for})
181 7870 ▶ if $rs_attrs->{for};
Branch: 1 branch
BranchTrueFalseTotal
if $rs_attrs->{"for"} 3 7867 7870
182  
183 7870 ▶ return wantarray ? ($sql, @all_bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7870 0 7870
184 }
185  
186 7870 ▶sub _assemble_binds {
Subroutine _assemble_binds: called CC=2 SCAR=6.9
187 7870 my $self = shift;
188 7870 ▶ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where group having order limit/);
Condition100%2/2
delete $$self{"${_}_bind"} || []
l!l
2260140359
MC/DC100%1/1
delete $$self{"${_}_bind"} || []
delete $$self{"${_}_bind"}
Truth table100%2/2
A
A= delete $$self{"${_}_bind"}
Aresult
11
00
189 }
190  
191 216 my $for_syntax = {
192 update => 'FOR UPDATE',
193 shared => 'FOR SHARE',
194 };
195 2 ▶sub _lock_select {
Subroutine _lock_select: called CC=3 SCAR=11.2
196 2 my ($self, $type) = @_;
197  
198 2 my $sql;
199 2 ▶ if (ref($type) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'SCALAR') { } 1 1 2
200 1 $sql = "FOR $$type";
201 }
202 else {
203 1 ▶ $sql = $for_syntax->{$type} || $self->throw_exception( "Unknown SELECT .. FOR type '$type' requested" );
Condition33%1/3
$for_syntax->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
l!l&&r!l&&!r
100
MC/DC0%0/2
$for_syntax->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
$for_syntax->{$type} $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
Truth table33%1/3
A || B
A= $for_syntax->{$type}B= $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
ABresult
1X1
011
000
204 }
205  
206 2 return " $sql";
207 }
208  
209 # Handle default inserts
210 9122 ▶sub insert {
Subroutine insert: called CC=6 SCAR=19.7
Pod undocumented
211 # optimized due to hotttnesss
212 # my ($self, $table, $data, $options) = @_;
213  
214 # FIXME SQLMaker will emit INSERT INTO $table ( ) VALUES ( )
215 # which is sadly understood only by MySQL. Change default behavior here,
216 # until we fold the extra pieces into SQLMaker properly
217 9122 ▶ if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}) 2 9120 9122
Condition66%4/6
not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}
l!l&&r!l&&!r
029120
ref $_[2] eq "HASH" and not keys %{$_[2];}
!ll&&!rl&&r
091202
MC/DC33%1/3
not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}
not $_[2] ref $_[2] eq "HASH" not keys %{$_[2];}
Truth table50%2/4
not(A) or B and not(C)
A= not $_[2]B= ref $_[2] eq "HASH"C= not keys %{$_[2];}
ABCresult
1XX1
0111
00X0
0100
218 2 my @bind;
219 2 my $sql = sprintf(
220 'INSERT INTO %s DEFAULT VALUES', $_[0]->_quote($_[1])
221 );
222  
223 2 ▶ if ( ($_[3]||{})->{returning} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (+($_[3] || {})->{"returning"}) 0 2 2
Condition50%1/2
$_[3] || {}
l!l
02
MC/DC0%0/1
$_[3] || {}
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
224 0 my $s;
225 0 ($s, @bind) = $_[0]->_insert_returning ($_[3]);
226 0 $sql .= $s;
227 }
228  
229 2 return ($sql, @bind);
230 }
231  
232 9120 next::method(@_);
233 }
234  
235 52791 ▶sub _recurse_fields {
Subroutine _recurse_fields: called CC=13 SCAR=26.0
236 52791 my ($self, $fields) = @_;
237 52791 my $ref = ref $fields;
238 52791 ▶ return $self->_quote($fields) unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 43055 9736 52791
239 9736 ▶ return $$fields if $ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" 84 9652 9736
240  
241 9652 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 8354 1298 9652
elsif ($ref eq 'HASH') { } 877 421 1298
elsif ($ref eq 'REF' and ref $$fields eq 'ARRAY') { } 421 0 421
242 8354 my (@select, @bind);
243 8354 for my $field (@$fields) {
244 37384 my ($select, @new_bind) = $self->_recurse_fields($field);
245 37383 push @select, $select;
246 37383 push @bind, @new_bind;
247 }
248 8353 return (join(', ', @select), @bind);
249 }
250 elsif ($ref eq 'HASH') {
251 877 my %hash = %$fields; # shallow copy
252  
253 877 my $as = delete $hash{-as}; # if supplied
254  
255 877 my ($func, $rhs, @toomany) = %hash;
256  
257 # there should be only one pair
258 877 ▶ if (@toomany) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toomany) 0 877 877
259 0 $self->throw_exception( "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ) );
260 }
261  
262 877 ▶ if (lc ($func) eq 'distinct' && ref $rhs eq 'ARRAY' && @$rhs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $func eq "distinct" and ref $rhs eq "ARRAY" and @$rhs > 1) 1 876 877
Condition83%5/6
lc $func eq "distinct" and ref $rhs eq "ARRAY" and @$rhs > 1
!ll&&!rl&&r
87601
lc $func eq "distinct" and ref $rhs eq "ARRAY"
!ll&&!rl&&r
87421
MC/DC66%2/3
lc $func eq "distinct" and ref $rhs eq "ARRAY" and @$rhs > 1
lc $func eq "distinct" ref $rhs eq "ARRAY" @$rhs > 1
Truth table75%3/4
A and B and C
A= lc $func eq "distinct"B= ref $rhs eq "ARRAY"C= @$rhs > 1
ABCresult
0XX0
10X0
1100
1111
263 1 $self->throw_exception (
264 'The select => { distinct => ... } syntax is not supported for multiple columns.'
265 .' Instead please use { group_by => [ qw/' . (join ' ', @$rhs) . '/ ] }'
266 .' or { select => [ qw/' . (join ' ', @$rhs) . '/ ], distinct => 1 }'
267 );
268 }
269  
270 876 my ($rhs_sql, @rhs_bind) = $self->_recurse_fields($rhs);
271 876 ▶ my $select = sprintf ('%s( %s )%s',
Branch: 1 branch
BranchTrueFalseTotal
$as ? : 85 791 876
272 $self->_sqlcase($func),
273 $rhs_sql,
274 $as
275 ? sprintf (' %s %s', $self->_sqlcase('as'), $self->_quote ($as) )
276 : ''
277 );
278  
279 876 return ($select, @rhs_bind);
280 }
281 ▶ elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
Condition33%1/3
$ref eq "REF" and ref $$fields eq "ARRAY"
!ll&&!rl&&r
00421
MC/DC0%0/2
$ref eq "REF" and ref $$fields eq "ARRAY"
$ref eq "REF" ref $$fields eq "ARRAY"
Truth table33%1/3
A and B
A= $ref eq "REF"B= ref $$fields eq "ARRAY"
ABresult
0X0
100
111
282 421 return @{$$fields};
283 }
284 else {
285 0 $self->throw_exception( $ref . qq{ unexpected in _recurse_fields()} );
286 }
287 }
288  
289  
290 # this used to be a part of _order_by but is broken out for clarity.
291 # What we have been doing forever is hijacking the $order arg of
292 # SQLAC::select to pass in arbitrary pieces of data (first the group_by,
293 # then pretty much the entire resultset attr-hash, as more and more
294 # things in the SQLMaker space need to have more info about the $rs they
295 # create SQL for. The alternative would be to keep expanding the
296 # signature of _select with more and more positional parameters, which
297 # is just gross.
298 #
299 # FIXME - this will have to transition out to a subclass when the effort
300 # of folding the SQL generating machinery into SQLMaker takes place
301 10119 ▶sub _parse_rs_attrs {
Subroutine _parse_rs_attrs: called CC=5 SCAR=16.1
302 10119 my ($self, $arg) = @_;
303  
304 10119 my $sql = '';
305  
306 10119 ▶ if ($arg->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"group_by"}) 476 9643 10119
307 476 ▶ if ( my ($group_sql, @group_bind) = $self->_recurse_fields($arg->{group_by}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_recurse_fields($arg->{"group_by"})) 476 0 476
308 476 $sql .= $self->_sqlcase(' group by ') . $group_sql;
309 476 push @{$self->{group_bind}}, @group_bind;
310 }
311 }
312  
313 10119 ▶ if (defined $arg->{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"having"}) 99 10020 10119
314 99 my ($frag, @bind) = $self->_recurse_where($arg->{having});
315 99 push(@{$self->{having_bind}}, @bind);
316 99 $sql .= $self->_sqlcase(' having ') . $frag;
317 }
318  
319 10119 ▶ if (defined $arg->{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"order_by"}) 3846 6273 10119
320 3846 $sql .= $self->_order_by ($arg->{order_by});
321 }
322  
323 10116 return $sql;
324 }
325  
326 10207 ▶sub _order_by {
Subroutine _order_by: called CC=3 SCAR=11.0
327 10207 my ($self, $arg) = @_;
328  
329 # check that we are not called in legacy mode (order_by as 4th argument)
330 10207 ▶ if (ref $arg eq 'HASH' and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH' and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)) { } 6194 4013 10207
Condition100%3/3
ref $arg eq "HASH" and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
!ll&&!rl&&r
401216194
MC/DC100%2/2
ref $arg eq "HASH" and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
ref $arg eq "HASH" not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
Truth table100%3/3
A and not(B)
A= ref $arg eq "HASH"B= not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
ABresult
0X0
100
111
331 6194 return $self->_parse_rs_attrs ($arg);
332 }
333 else {
334 4013 my ($sql, @bind) = $self->next::method($arg);
335 4010 push @{$self->{order_bind}}, @bind;
336 4010 return $sql;
337 }
338 }
339  
340 1574 ▶sub _split_order_chunk {
Subroutine _split_order_chunk: called CC=3 SCAR=11.0
341 1574 my ($self, $chunk) = @_;
342  
343 # strip off sort modifiers, but always succeed, so $1 gets reset
344 1574 $chunk =~ s/ (?: \s+ (ASC|DESC) )? \s* $//ix;
345  
346 return (
347 1574 ▶ $chunk,
Branch: 1 branch
BranchTrueFalseTotal
$1 && uc $1 eq 'DESC' ? : 647 927 1574
Condition100%3/3
$1 and uc $1 eq "DESC"
!ll&&!rl&&r
703224647
MC/DC100%2/2
$1 and uc $1 eq "DESC"
$1 uc $1 eq "DESC"
Truth table100%3/3
A and B
A= $1B= uc $1 eq "DESC"
ABresult
0X0
100
111
348 ( $1 and uc($1) eq 'DESC' ) ? 1 : 0,
349 );
350 }
351  
352 18489 ▶sub _table {
Subroutine _table: called CC=6 SCAR=18.0
353 # optimized due to hotttnesss
354 # my ($self, $from) = @_;
355 18489 ▶ if (my $ref = ref $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $_[1]) 8935 9554 18489
356 8935 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 7866 1069 8935
elsif ($ref eq 'HASH') { } 0 1069 1069
elsif ($ref eq 'REF' and ref ${$_[1];} eq 'ARRAY') { } 1 2136 2137
357 7866 return $_[0]->_recurse_from(@{$_[1]});
358 }
359 elsif ($ref eq 'HASH') {
360 0 return $_[0]->_recurse_from($_[1]);
361 }
362 1 ▶ elsif ($ref eq 'REF' && ref ${$_[1]} eq 'ARRAY') {
Condition66%2/3
$ref eq "REF" and ref ${$_[1];} eq "ARRAY"
!ll&&!rl&&r
106801
MC/DC50%1/2
$ref eq "REF" and ref ${$_[1];} eq "ARRAY"
$ref eq "REF" ref ${$_[1];} eq "ARRAY"
Truth table66%2/3
A and B
A= $ref eq "REF"B= ref ${$_[1];} eq "ARRAY"
ABresult
0X0
100
111
363 1 my ($sql, @bind) = @{ ${$_[1]} };
364 1 push @{$_[0]->{from_bind}}, @bind;
365 1 return $sql
366 }
367 }
368 10622 return $_[0]->next::method ($_[1]);
369 }
370  
371 3292 ▶sub _generate_join_clause {
Subroutine _generate_join_clause: called CC=3 SCAR=11.0
372 3292 my ($self, $join_type) = @_;
373  
374 $join_type = $self->{_default_jointype}
375 3292 ▶ if ! defined $join_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $join_type 1542 1750 3292
376  
377 3292 ▶ return sprintf ('%s JOIN ',
Branch: 1 branch
BranchTrueFalseTotal
$join_type ? : 1755 1537 3292
378 $join_type ? $self->_sqlcase($join_type) : ''
379 );
380 }
381  
382 8992 ▶sub _recurse_from {
Subroutine _recurse_from: called CC=1 SCAR=0.0
383 8992 my $self = shift;
384 8992 return join (' ', $self->_gen_from_blocks(@_) );
385 }
386  
387 8998 ▶sub _gen_from_blocks {
Subroutine _gen_from_blocks: called CC=6 SCAR=17.9
388 8998 my ($self, $from, @joins) = @_;
389  
390 8998 my @fchunks = $self->_from_chunk_to_sql($from);
391  
392 8998 for (@joins) {
393 3294 my ($to, $on) = @$_;
394  
395 # check whether a join type exists
396 3294 ▶ my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
Branch: 1 branch
BranchTrueFalseTotal
ref $to eq 'ARRAY' ? : 2 3292 3294
397 3294 my $join_type;
398 3294 ▶ if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}) 1752 1542 3294
Condition66%2/3
ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}
!ll&&!rl&&r
015421752
MC/DC50%1/2
ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}
ref $to_jt eq "HASH" defined $to_jt->{"-join_type"}
Truth table66%2/3
A and B
A= ref $to_jt eq "HASH"B= defined $to_jt->{"-join_type"}
ABresult
0X0
100
111
399 1752 $join_type = $to_jt->{-join_type};
400 1752 $join_type =~ s/^\s+ | \s+$//xg;
401 }
402  
403 3294 my @j = $self->_generate_join_clause( $join_type );
404  
405 3294 ▶ if (ref $to eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to eq 'ARRAY') { } 2 3292 3294
406 2 push(@j, '(', $self->_recurse_from(@$to), ')');
407 }
408 else {
409 3292 push(@j, $self->_from_chunk_to_sql($to));
410 }
411  
412 3294 my ($sql, @bind) = $self->_join_condition($on);
413 3294 push(@j, ' ON ', $sql);
414 3294 push @{$self->{from_bind}}, @bind;
415  
416 3294 push @fchunks, join '', @j;
417 }
418  
419 8998 return @fchunks;
420 }
421  
422 24580 ▶sub _from_chunk_to_sql {
Subroutine _from_chunk_to_sql: called CC=7 SCAR=19.7
423 24580 my ($self, $fromspec) = @_;
424  
425 24580 return join (' ', do {
426 24580 ▶ if (! ref $fromspec) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $fromspec) { } 9036 15544 24580
elsif (ref $fromspec eq 'SCALAR') { } 3006 12538 15544
elsif (ref $fromspec eq 'REF' and ref $$fromspec eq 'ARRAY') { } 248 12290 12538
elsif (ref $fromspec eq 'HASH') { } 12290 0 12290
427 9036 $self->_quote($fromspec);
428 }
429 elsif (ref $fromspec eq 'SCALAR') {
430 3006 $$fromspec;
431 }
432 ▶ elsif (ref $fromspec eq 'REF' and ref $$fromspec eq 'ARRAY') {
Condition66%2/3
ref $fromspec eq "REF" and ref $$fromspec eq "ARRAY"
!ll&&!rl&&r
122900248
MC/DC50%1/2
ref $fromspec eq "REF" and ref $$fromspec eq "ARRAY"
ref $fromspec eq "REF" ref $$fromspec eq "ARRAY"
Truth table66%2/3
A and B
A= ref $fromspec eq "REF"B= ref $$fromspec eq "ARRAY"
ABresult
0X0
100
111
433 248 push @{$self->{from_bind}}, @{$$fromspec}[1..$#$$fromspec];
434 248 $$fromspec->[0];
435 }
436 elsif (ref $fromspec eq 'HASH') {
437 my ($as, $table, $toomuch) = ( map
438 12290 { $_ => $fromspec->{$_} }
439 12290 ( grep { $_ !~ /^\-/ } keys %$fromspec )
440 );
441  
442 12290 ▶ $self->throw_exception( "Only one table/as pair expected in from-spec but an exra '$toomuch' key present" )
Branch: 1 branch
BranchTrueFalseTotal
if defined $toomuch 0 12290 12290
443 if defined $toomuch;
444  
445 12290 ($self->_from_chunk_to_sql($table), $self->_quote($as) );
446 }
447 else {
448 0 $self->throw_exception('Unsupported from refkind: ' . ref $fromspec );
449 }
450 });
451 }
452  
453 3294 ▶sub _join_condition {
Subroutine _join_condition: called CC=7 SCAR=23.6
454 3294 my ($self, $cond) = @_;
455  
456 # Backcompat for the old days when a plain hashref
457 # { 't1.col1' => 't2.col2' } meant ON t1.col1 = t2.col2
458 3294 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'HASH' and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])) { } 13 3281 3294
elsif (ref $cond eq 'ARRAY') { } 0 3281 3281
Condition66%6/9
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])
!ll&&!rl&&r
32701113
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./
!ll&&!rl&&r
0327024
ref $cond eq "HASH" and keys %$cond == 1
!ll&&!rl&&r
003294
MC/DC50%2/4
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])
ref $cond eq "HASH" keys %$cond == 1 (keys %$cond)[0] =~ /\./ not ref((values %$cond)[0])
Truth table60%3/5
A and B and C and not(D)
A= ref $cond eq "HASH"B= keys %$cond == 1C= (keys %$cond)[0] =~ /\./D= not ref((values %$cond)[0])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
459 ref $cond eq 'HASH'
460 and
461 keys %$cond == 1
462 and
463 (keys %$cond)[0] =~ /\./
464 and
465 ! ref ( (values %$cond)[0] )
466 ) {
467 13 carp_unique(
468 "ResultSet {from} structures with conditions not conforming to the "
469 . "SQL::Abstract::Classic syntax are deprecated: you either need to stop "
470 . "abusing {from} altogether, or express the condition properly using the "
471 . "{ -ident => ... } operator"
472 );
473 13 $cond = { keys %$cond => { -ident => values %$cond } }
474 }
475 elsif ( ref $cond eq 'ARRAY' ) {
476 # do our own ORing so that the hashref-shim above is invoked
477 0 my @parts;
478 my @binds;
479 0 foreach my $c (@$cond) {
480 0 my ($sql, @bind) = $self->_join_condition($c);
481 0 push @binds, @bind;
482 0 push @parts, $sql;
483 }
484 0 return join(' OR ', @parts), @binds;
485 }
486  
487 3294 return $self->_recurse_where($cond);
488 }
489  
490 # !!! EXPERIMENTAL API !!! WILL CHANGE !!!
491 #
492 # This is rather odd, but vanilla SQLA* variants do not have support for
493 # multicolumn-IN expressions
494 # Currently has only one callsite in ResultSet, body moved into this subclass
495 # to raise API questions like:
496 # - how do we convey a list of idents...?
497 # - can binds reside on lhs?
498 #
499 # !!! EXPERIMENTAL API !!! WILL CHANGE !!!
500 9 ▶sub _where_op_multicolumn_in {
Subroutine _where_op_multicolumn_in: called CC=14 SCAR=40.8
501 9 my ($self, $lhs, $rhs) = @_;
502  
503 9 ▶ if (! ref $lhs or ref $lhs eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $lhs or ref $lhs eq 'ARRAY') { } 9 0 9
elsif (ref $lhs eq 'SCALAR') { } 0 0 0
elsif (ref $lhs eq 'REF' and ref $$lhs eq 'ARRAY') { } 0 0 0
Condition33%1/3
not ref $lhs or ref $lhs eq "ARRAY"
l!l&&r!l&&!r
090
MC/DC0%0/2
not ref $lhs or ref $lhs eq "ARRAY"
not ref $lhs ref $lhs eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $lhsB= ref $lhs eq "ARRAY"
ABresult
1X1
011
000
504 9 my (@sql, @bind);
505 9 ▶ for (ref $lhs ? @$lhs : $lhs) {
Branch: 1 branch
BranchTrueFalseTotal
ref $lhs ? : 9 0 9
506 36 ▶ if (! ref $_) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $_) { } 36 0 36
elsif (ref $_ eq 'SCALAR') { } 0 0 0
elsif (ref $_ eq 'REF' and ref $$_ eq 'ARRAY') { } 0 0 0
507 36 push @sql, $self->_quote($_);
508 }
509 elsif (ref $_ eq 'SCALAR') {
510 0 push @sql, $$_;
511 }
512 ▶ elsif (ref $_ eq 'REF' and ref $$_ eq 'ARRAY') {
Condition0%0/3
ref $_ eq "REF" and ref $$_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "REF" and ref $$_ eq "ARRAY"
ref $_ eq "REF" ref $$_ eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_ eq "REF"B= ref $$_ eq "ARRAY"
ABresult
0X0
100
111
513 0 my ($s, @b) = @$$_;
514 0 push @sql, $s;
515 0 push @bind, @b;
516 }
517 else {
518 0 $self->throw_exception("ARRAY of @{[ ref $_ ]}es unsupported for multicolumn IN lhs...");
519 }
520 }
521 9 $lhs = \[ join(', ', @sql), @bind];
522 }
523 elsif (ref $lhs eq 'SCALAR') {
524 0 $lhs = \[ $$lhs ];
525 }
526 ▶ elsif (ref $lhs eq 'REF' and ref $$lhs eq 'ARRAY' ) {
Condition0%0/3
ref $lhs eq "REF" and ref $$lhs eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $lhs eq "REF" and ref $$lhs eq "ARRAY"
ref $lhs eq "REF" ref $$lhs eq "ARRAY"
Truth table0%0/3
A and B
A= ref $lhs eq "REF"B= ref $$lhs eq "ARRAY"
ABresult
0X0
100
111
527 # noop
528 }
529 else {
530 0 $self->throw_exception( ref($lhs) . "es unsupported for multicolumn IN lhs...");
531 }
532  
533 # is this proper...?
534 9 $rhs = \[ $self->_recurse_where($rhs) ];
535  
536 9 for ($lhs, $rhs) {
537 18 ▶ $$_->[0] = "( $$_->[0] )"
Branch: 1 branch
BranchTrueFalseTotal
unless $$_->[0] =~ /^ \s* \( .* \) \s* $/sx 9 9 18
538 unless $$_->[0] =~ /^ \s* \( .* \) \s* $/xs;
539 }
540  
541 9 \[ join( ' IN ', shift @$$lhs, shift @$$rhs ), @$$lhs, @$$rhs ];
542 }
543  
544  
545 ###
546 ### Code that mostly used to be in DBIC::SQLMaker::LimitDialects
547 ###
548  
549 1548 ▶sub _LimitOffset {
Subroutine _LimitOffset: called CC=2 SCAR=6.9
550 1548 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
551 1548 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ?";
552 1548 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
553 1548 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 43 1505 1548
554 43 $sql .= " OFFSET ?";
555 43 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
556 }
557 1548 return $sql;
558 }
559  
560 3 ▶sub _LimitXY {
Subroutine _LimitXY: called CC=2 SCAR=6.9
561 3 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
562 3 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ";
563 3 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 2 1 3
564 2 $sql .= '?, ';
565 2 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
566 }
567 3 $sql .= '?';
568 3 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
569  
570 3 return $sql;
571 }
572  
573 14 ▶sub _RowNumberOver {
Subroutine _RowNumberOver: called CC=7 SCAR=19.5
574 14 my ($self, $sql, $rs_attrs, $rows, $offset ) = @_;
575  
576 # get selectors, and scan the order_by (if any)
577 14 my $sq_attrs = $self->_subqueried_limit_attrs ( $sql, $rs_attrs );
578  
579 # make up an order if none exists
580 14 ▶ my $requested_order = (delete $rs_attrs->{order_by}) || $self->_rno_default_order;
Condition66%2/3
delete $rs_attrs->{'order_by'} || $self->_rno_default_order
l!l&&r!l&&!r
608
MC/DC50%1/2
delete $rs_attrs->{'order_by'} || $self->_rno_default_order
delete $rs_attrs->{'order_by'} $self->_rno_default_order
Truth table66%2/3
A || B
A= delete $rs_attrs->{'order_by'}B= $self->_rno_default_order
ABresult
1X1
011
000
581  
582 # the order binds (if any) will need to go at the end of the entire inner select
583 14 local $self->{order_bind};
584 14 my $rno_ord = $self->_order_by ($requested_order);
585 14 push @{$self->{select_bind}}, @{$self->{order_bind}};
586  
587 # this is the order supplement magic
588 14 my $mid_sel = $sq_attrs->{selection_outer};
589 14 ▶ if (my $extra_order_sel = $sq_attrs->{order_supplement}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_order_sel = $sq_attrs->{"order_supplement"}) 4 10 14
590 4 for my $extra_col (sort
591 2 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
592 keys %$extra_order_sel
593 ) {
594 $sq_attrs->{selection_inner} .= sprintf (', %s AS %s',
595 $extra_col,
596 6 $extra_order_sel->{$extra_col},
597 );
598 }
599 }
600  
601 # and this is order re-alias magic
602 14 for my $map ($sq_attrs->{order_supplement}, $sq_attrs->{outer_renames}) {
603 28 ▶ for my $col (sort { (length $b) <=> (length $a) } keys %{$map||{}} ) {
Condition100%2/2
$map || {}
l!l
1216
MC/DC100%1/1
$map || {}
$map
Truth table100%2/2
A
A= $map
Aresult
11
00
604 28 my $re_col = quotemeta ($col);
605 28 $rno_ord =~ s/$re_col/$map->{$col}/;
606 }
607 }
608  
609 # whatever is left of the order_by (only where is processed at this point)
610 14 my $group_having = $self->_parse_rs_attrs($rs_attrs);
611  
612 14 my $qalias = $self->_quote ($rs_attrs->{alias});
613 14 my $idx_name = $self->_quote ('rno__row__index');
614  
615 14 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1], [ $self->__total_bindtype => $offset + $rows ];
616  
617 14 return <<EOS;
618  
619 SELECT $sq_attrs->{selection_outer} FROM (
620 SELECT $mid_sel, ROW_NUMBER() OVER( $rno_ord ) AS $idx_name FROM (
621 SELECT $sq_attrs->{selection_inner} $sq_attrs->{query_leftover}${group_having}
622 ) $qalias
623 ) $qalias WHERE $idx_name >= ? AND $idx_name <= ?
624  
625 EOS
626  
627 }
628  
629 # some databases are happy with OVER (), some need OVER (ORDER BY (SELECT (1)) )
630 8 ▶sub _rno_default_order {
Subroutine _rno_default_order: called CC=1 SCAR=0.0
631 8 return undef;
632 }
633  
634 10 ▶sub _SkipFirst {
Subroutine _SkipFirst: called CC=3 SCAR=11.0
635 10 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
636  
637 10 ▶ $sql =~ s/^ \s* SELECT \s+ //ix
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ s/^ \s* SELECT \s+ //xi or $self->throw_exception("Unrecognizable SELECT: $sql") 10 0 10
638 or $self->throw_exception("Unrecognizable SELECT: $sql");
639  
640 return sprintf ('SELECT %s%s%s%s',
641 $offset
642 ? do {
643 8 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
644 8 'SKIP ? '
645 }
646 : ''
647 ,
648 10 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 8 2 10
649 10 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
650 10 'FIRST ? '
651 },
652 $sql,
653 $self->_parse_rs_attrs ($rs_attrs),
654 );
655 }
656  
657 10 ▶sub _FirstSkip {
Subroutine _FirstSkip: called CC=3 SCAR=11.0
658 10 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
659  
660 10 ▶ $sql =~ s/^ \s* SELECT \s+ //ix
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ s/^ \s* SELECT \s+ //xi or $self->throw_exception("Unrecognizable SELECT: $sql") 10 0 10
661 or $self->throw_exception("Unrecognizable SELECT: $sql");
662  
663 return sprintf ('SELECT %s%s%s%s',
664 do {
665 10 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
666 10 ▶ 'FIRST ? '
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 8 2 10
667 },
668 $offset
669 10 ? do {
670 8 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
671 8 'SKIP ? '
672 }
673 : ''
674 ,
675 $sql,
676 $self->_parse_rs_attrs ($rs_attrs),
677 );
678 }
679  
680 14 ▶sub _RowNum {
Subroutine _RowNum: called CC=4 SCAR=13.9
681 14 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
682  
683 14 my $sq_attrs = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
684  
685 14 my $qalias = $self->_quote ($rs_attrs->{alias});
686 14 my $idx_name = $self->_quote ('rownum__index');
687 14 my $order_group_having = $self->_parse_rs_attrs($rs_attrs);
688  
689  
690 # if no offset (e.g. first page) - we can skip one of the subqueries
691 14 ▶ if (! $offset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($offset) 4 10 14
692 4 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
693  
694 4 return <<EOS;
695 SELECT $sq_attrs->{selection_outer} FROM (
696 SELECT $sq_attrs->{selection_inner} $sq_attrs->{query_leftover}${order_group_having}
697 ) $qalias WHERE ROWNUM <= ?
698 EOS
699 }
700  
701 #
702 # There are two ways to limit in Oracle, one vastly faster than the other
703 # on large resultsets: https://decipherinfosys.wordpress.com/2007/08/09/paging-and-countstopkey-optimization/
704 # However Oracle is retarded and does not preserve stable ROWNUM() values
705 # when called twice in the same scope. Therefore unless the resultset is
706 # ordered by a unique set of columns, it is not safe to use the faster
707 # method, and the slower BETWEEN query is used instead
708 #
709 # FIXME - this is quite expensive, and does not perform caching of any sort
710 # as soon as some of the SQLMaker-inlining work becomes viable consider adding
711 # some rudimentary caching support
712 10 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($rs_attrs->{'order_by'} and $rs_attrs->{'result_source'}->storage->_order_by_is_stable(@{$rs_attrs;}{'from', 'order_by', 'where'})) { } 3 7 10
Condition100%3/3
$rs_attrs->{"order_by"} and $rs_attrs->{"result_source"}->storage->_order_by_is_stable(@{$rs_attrs;}{"from", "order_by", "where"})
!ll&&!rl&&r
523
MC/DC100%2/2
$rs_attrs->{"order_by"} and $rs_attrs->{"result_source"}->storage->_order_by_is_stable(@{$rs_attrs;}{"from", "order_by", "where"})
$rs_attrs->{"order_by"} $rs_attrs->{"result_source"}->storage->_order_by_is_stable(@{$rs_attrs;}{"from", "order_by", "where"})
Truth table100%3/3
A and B
A= $rs_attrs->{"order_by"}B= $rs_attrs->{"result_source"}->storage->_order_by_is_stable(@{$rs_attrs;}{"from", "order_by", "where"})
ABresult
0X0
100
111
713 $rs_attrs->{order_by}
714 and
715 $rs_attrs->{result_source}->storage->_order_by_is_stable(
716 5 @{$rs_attrs}{qw/from order_by where/}
717 )
718 ) {
719 3 push @{$self->{limit_bind}}, [ $self->__total_bindtype => $offset + $rows ], [ $self->__offset_bindtype => $offset + 1 ];
720  
721 3 return <<EOS;
722 SELECT $sq_attrs->{selection_outer} FROM (
723 SELECT $sq_attrs->{selection_outer}, ROWNUM AS $idx_name FROM (
724 SELECT $sq_attrs->{selection_inner} $sq_attrs->{query_leftover}${order_group_having}
725 ) $qalias WHERE ROWNUM <= ?
726 ) $qalias WHERE $idx_name >= ?
727 EOS
728 }
729 else {
730 7 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1 ], [ $self->__total_bindtype => $offset + $rows ];
731  
732 7 return <<EOS;
733 SELECT $sq_attrs->{selection_outer} FROM (
734 SELECT $sq_attrs->{selection_outer}, ROWNUM AS $idx_name FROM (
735 SELECT $sq_attrs->{selection_inner} $sq_attrs->{query_leftover}${order_group_having}
736 ) $qalias
737 ) $qalias WHERE $idx_name BETWEEN ? AND ?
738 EOS
739 }
740 }
741  
742 # used by _Top and _FetchFirst below
743 49 ▶sub _prep_for_skimming_limit {
Subroutine _prep_for_skimming_limit: called CC=14 SCAR=26.4
744 49 my ( $self, $sql, $rs_attrs ) = @_;
745  
746 # get selectors
747 49 my $sq_attrs = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
748  
749 49 my $requested_order = delete $rs_attrs->{order_by};
750 49 $sq_attrs->{order_by_requested} = $self->_order_by ($requested_order);
751 49 $sq_attrs->{grpby_having} = $self->_parse_rs_attrs ($rs_attrs);
752  
753 # without an offset things are easy
754 49 ▶ if (! $rs_attrs->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rs_attrs->{'offset'}) { } 9 40 49
755 9 $sq_attrs->{order_by_inner} = $sq_attrs->{order_by_requested};
756 }
757 else {
758 40 $sq_attrs->{quoted_rs_alias} = $self->_quote ($rs_attrs->{alias});
759  
760 # localise as we already have all the bind values we need
761 40 local $self->{order_bind};
762  
763 # make up an order unless supplied or sanity check what we are given
764 40 my $inner_order;
765 40 ▶ if ($sq_attrs->{order_by_requested}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq_attrs->{'order_by_requested'}) { } 25 15 40
766 $self->throw_exception (
767 'Unable to safely perform "skimming type" limit with supplied unstable order criteria'
768 ) unless ($rs_attrs->{result_source}->schema->storage->_order_by_is_stable(
769 $rs_attrs->{from},
770 $requested_order,
771 $rs_attrs->{where},
772 25 ▶ ));
Branch: 1 branch
BranchTrueFalseTotal
unless $rs_attrs->{"result_source"}->schema->storage->_order_by_is_stable($rs_attrs->{"from"}, $requested_order, $rs_attrs->{"where"}) 0 25 25
773  
774 25 $inner_order = $requested_order;
775 }
776 else {
777 $inner_order = [ map
778 15 { "$rs_attrs->{alias}.$_" }
779 ( @{
780 15 $rs_attrs->{result_source}->_identifying_column_set
781 ||
782 $self->throw_exception(sprintf(
783 'Unable to auto-construct stable order criteria for "skimming type" limit '
784 15 ▶ . "dialect based on source '%s'", $rs_attrs->{result_source}->name) );
Condition33%1/3
$rs_attrs->{'result_source'}->_identifying_column_set || $self->throw_exception(sprintf(q[Unable to auto-construct stable order criteria for "skimming type" limit dialect based on source '%s'], $rs_attrs->{'result_source'}->name))
l!l&&r!l&&!r
1500
MC/DC0%0/2
$rs_attrs->{'result_source'}->_identifying_column_set || $self->throw_exception(sprintf(q[Unable to auto-construct stable order criteria for "skimming type" limit dialect based on source '%s'], $rs_attrs->{'result_source'}->name))
$rs_attrs->{'result_source'}->_identifying_column_set $self->throw_exception(sprintf(q[Unable to auto-construct stable order criteria for "skimming type" limit dialect based on source '%s'], $rs_attrs->{'result_source'}->name))
Truth table33%1/3
A || B
A= $rs_attrs->{'result_source'}->_identifying_column_setB= $self->throw_exception(sprintf(q[Unable to auto-construct stable order criteria for "skimming type" limit dialect based on source '%s'], $rs_attrs->{'result_source'}->name))
ABresult
1X1
011
000
785 } )
786 ];
787 }
788  
789 40 $sq_attrs->{order_by_inner} = $self->_order_by ($inner_order);
790  
791 40 my @out_chunks;
792 40 for my $ch ($self->_order_by_chunks ($inner_order)) {
793 56 ▶ $ch = $ch->[0] if ref $ch eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ch eq "ARRAY" 22 34 56
794  
795 56 ($ch, my $is_desc) = $self->_split_order_chunk($ch);
796  
797 # !NOTE! outside chunks come in reverse order ( !$is_desc )
798 56 ▶ push @out_chunks, { ($is_desc ? '-asc' : '-desc') => \$ch };
Branch: 1 branch
BranchTrueFalseTotal
$is_desc ? : 9 47 56
799 }
800  
801 40 $sq_attrs->{order_by_middle} = $self->_order_by (\@out_chunks);
802  
803 # this is the order supplement magic
804 40 $sq_attrs->{selection_middle} = $sq_attrs->{selection_outer};
805 40 ▶ if (my $extra_order_sel = $sq_attrs->{order_supplement}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_order_sel = $sq_attrs->{"order_supplement"}) 19 21 40
806 19 for my $extra_col (sort
807 11 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
808 keys %$extra_order_sel
809 ) {
810 $sq_attrs->{selection_inner} .= sprintf (', %s AS %s',
811 $extra_col,
812 29 $extra_order_sel->{$extra_col},
813 );
814  
815 29 $sq_attrs->{selection_middle} .= ', ' . $extra_order_sel->{$extra_col};
816 }
817  
818 # Whatever order bindvals there are, they will be realiased and
819 # reselected, and need to show up at end of the initial inner select
820 19 push @{$self->{select_bind}}, @{$self->{order_bind}};
821 }
822  
823 # and this is order re-alias magic
824 40 for my $map ($sq_attrs->{order_supplement}, $sq_attrs->{outer_renames}) {
825 80 ▶ for my $col (sort { (length $b) <=> (length $a) } keys %{$map||{}}) {
Condition100%2/2
$map || {}
l!l
5129
MC/DC100%1/1
$map || {}
$map
Truth table100%2/2
A
A= $map
Aresult
11
00
826 101 my $re_col = quotemeta ($col);
827 $_ =~ s/$re_col/$map->{$col}/
828 101 for ($sq_attrs->{order_by_middle}, $sq_attrs->{order_by_requested});
829 }
830 }
831 }
832  
833 49 $sq_attrs;
834 }
835  
836 26 ▶sub _Top {
Subroutine _Top: called CC=7 SCAR=19.5
837 26 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
838  
839 26 my $lim = $self->_prep_for_skimming_limit($sql, $rs_attrs);
840  
841 $sql = sprintf ('SELECT TOP %u %s %s %s %s',
842 $rows + ($offset||0),
843 $offset ? $lim->{selection_inner} : $lim->{selection_original},
844 $lim->{query_leftover},
845 $lim->{grpby_having},
846 $lim->{order_by_inner},
847 26 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 21 5 26
Condition100%2/2
$offset || 0
l!l
215
MC/DC100%1/1
$offset || 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
848  
849 $sql = sprintf ('SELECT TOP %u %s FROM ( %s ) %s %s',
850 $rows,
851 $lim->{selection_middle},
852 $sql,
853 $lim->{quoted_rs_alias},
854 $lim->{order_by_middle},
855 26 ▶ ) if $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset 21 5 26
856  
857 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s',
858 $lim->{selection_outer},
859 $sql,
860 $lim->{quoted_rs_alias},
861 $lim->{order_by_requested},
862 ) if $offset and (
863 $lim->{order_by_requested} or $lim->{selection_middle} ne $lim->{selection_outer}
864 26 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"} 13 13 26
Condition83%5/6
$offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"}
!ll&&!rl&&r
5813
$lim->{"order_by_requested"} or $lim->{"selection_middle"} ne $lim->{"selection_outer"}
l!l&&r!l&&!r
1308
MC/DC66%2/3
$offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"}
$offset $lim->{"order_by_requested"} $lim->{"selection_middle"} ne $lim->{"selection_outer"}
Truth table75%3/4
A and B or C
A= $offsetB= $lim->{"order_by_requested"}C= $lim->{"selection_middle"} ne $lim->{"selection_outer"}
ABCresult
0XX0
1000
11X1
1011
865  
866 26 return $sql;
867 }
868  
869 23 ▶sub _FetchFirst {
Subroutine _FetchFirst: called CC=7 SCAR=19.5
870 23 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
871  
872 23 my $lim = $self->_prep_for_skimming_limit($sql, $rs_attrs);
873  
874 $sql = sprintf ('SELECT %s %s %s %s FETCH FIRST %u ROWS ONLY',
875 $offset ? $lim->{selection_inner} : $lim->{selection_original},
876 $lim->{query_leftover},
877 $lim->{grpby_having},
878 $lim->{order_by_inner},
879 23 ▶ $rows + ($offset||0),
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 19 4 23
Condition100%2/2
$offset || 0
l!l
194
MC/DC100%1/1
$offset || 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
880 );
881  
882 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s FETCH FIRST %u ROWS ONLY',
883 $lim->{selection_middle},
884 $sql,
885 $lim->{quoted_rs_alias},
886 $lim->{order_by_middle},
887 23 ▶ $rows,
Branch: 1 branch
BranchTrueFalseTotal
if $offset 19 4 23
888 ) if $offset;
889  
890  
891 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s',
892 $lim->{selection_outer},
893 $sql,
894 $lim->{quoted_rs_alias},
895 $lim->{order_by_requested},
896 ) if $offset and (
897 $lim->{order_by_requested} or $lim->{selection_middle} ne $lim->{selection_outer}
898 23 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"} 12 11 23
Condition83%5/6
$offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"}
!ll&&!rl&&r
4712
$lim->{"order_by_requested"} or $lim->{"selection_middle"} ne $lim->{"selection_outer"}
l!l&&r!l&&!r
1207
MC/DC66%2/3
$offset and $lim->{"order_by_requested"} || $lim->{"selection_middle"} ne $lim->{"selection_outer"}
$offset $lim->{"order_by_requested"} $lim->{"selection_middle"} ne $lim->{"selection_outer"}
Truth table75%3/4
A and B or C
A= $offsetB= $lim->{"order_by_requested"}C= $lim->{"selection_middle"} ne $lim->{"selection_outer"}
ABCresult
0XX0
1000
11X1
1011
899  
900 23 return $sql;
901 }
902  
903 24 ▶sub _GenericSubQ {
Subroutine _GenericSubQ: called CC=18 SCAR=29.0
904 24 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
905  
906 24 my $main_rsrc = $rs_attrs->{result_source};
907  
908 # Explicitly require an order_by
909 # GenSubQ is slow enough as it is, just emulating things
910 # like in other cases is not wise - make the user work
911 # to shoot their DBA in the foot
912 $self->throw_exception (
913 'Generic Subquery Limit does not work on resultsets without an order. Provide a stable, '
914 . 'main-table-based order criteria.'
915 24 ▶ ) unless $rs_attrs->{order_by};
Branch: 1 branch
BranchTrueFalseTotal
unless $rs_attrs->{"order_by"} 0 24 24
916  
917 24 my $usable_order_colinfo = $main_rsrc->storage->_extract_colinfo_of_stable_main_source_order_by_portion(
918 $rs_attrs
919 );
920  
921 $self->throw_exception(
922 'Generic Subquery Limit can not work with order criteria based on sources other than the main one'
923 ) if (
924 24 ▶ ! keys %{$usable_order_colinfo||{}}
Condition50%1/2
$usable_order_colinfo || {}
l!l
240
MC/DC0%0/1
$usable_order_colinfo || {}
$usable_order_colinfo
Truth table50%1/2
A
A= $usable_order_colinfo
Aresult
11
00
925 or
926 grep
927 24 ▶ { $_->{-source_alias} ne $rs_attrs->{alias} }
Branch: 1 branch
BranchTrueFalseTotal
if not keys %{$usable_order_colinfo or {};} or grep {$_->{"-source_alias"} ne $rs_attrs->{"alias"};} values %$usable_order_colinfo 0 24 24
Condition33%1/3
not keys %{$usable_order_colinfo or {};} or grep {$_->{"-source_alias"} ne $rs_attrs->{"alias"};} values %$usable_order_colinfo
l!l&&r!l&&!r
0024
MC/DC0%0/2
not keys %{$usable_order_colinfo or {};} or grep {$_->{"-source_alias"} ne $rs_attrs->{"alias"};} values %$usable_order_colinfo
not keys %{$usable_order_colinfo or {};} grep {$_->{"-source_alias"} ne $rs_attrs->{"alias"};} values %$usable_order_colinfo
Truth table33%1/3
not(A) or B
A= not keys %{$usable_order_colinfo or {};}B= grep {$_->{"-source_alias"} ne $rs_attrs->{"alias"};} values %$usable_order_colinfo
ABresult
1X1
011
000
928 (values %$usable_order_colinfo)
929 );
930  
931 ###
932 ###
933 ### we need to know the directions after we figured out the above - reextract *again*
934 ### this is eyebleed - trying to get it to work at first
935 24 my $supplied_order = delete $rs_attrs->{order_by};
936  
937 24 my @order_bits = do {
938 24 local $self->{quote_char};
939 24 local $self->{order_bind};
940 24 ▶ map { ref $_ ? $_->[0] : $_ } $self->_order_by_chunks ($supplied_order)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 72 12 84
941 };
942  
943 # truncate to what we'll use
944 24 $#order_bits = ( (keys %$usable_order_colinfo) - 1 );
945  
946 # @order_bits likely will come back quoted (due to how the prefetch
947 # rewriter operates
948 # Hence supplement the column_info lookup table with quoted versions
949 24 ▶ if ($self->quote_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->quote_char) 15 9 24
950 $usable_order_colinfo->{$self->_quote($_)} = $usable_order_colinfo->{$_}
951 15 for keys %$usable_order_colinfo;
952 }
953  
954 # calculate the condition
955 24 my $count_tbl_alias = 'rownum__emulation';
956 24 my $main_alias = $rs_attrs->{alias};
957 24 my $main_tbl_name = $main_rsrc->name;
958  
959 24 my (@unqualified_names, @qualified_names, @is_desc, @new_order_by);
960  
961 24 for my $bit (@order_bits) {
962  
963 70 ($bit, my $is_desc) = $self->_split_order_chunk($bit);
964  
965 70 push @is_desc, $is_desc;
966 70 push @unqualified_names, $usable_order_colinfo->{$bit}{-colname};
967 70 push @qualified_names, $usable_order_colinfo->{$bit}{-fq_colname};
968  
969 70 ▶ push @new_order_by, { ($is_desc ? '-desc' : '-asc') => $usable_order_colinfo->{$bit}{-fq_colname} };
Branch: 1 branch
BranchTrueFalseTotal
$is_desc ? : 38 32 70
970 };
971  
972 24 my (@where_cond, @skip_colpair_stack);
973 24 for my $i (0 .. $#order_bits) {
974 70 my $ci = $usable_order_colinfo->{$order_bits[$i]};
975  
976 70 my ($subq_col, $main_col) = map { "$_.$ci->{-colname}" } ($count_tbl_alias, $main_alias);
977 70 ▶ my $cur_cond = { $subq_col => { ($is_desc[$i] ? '>' : '<') => { -ident => $main_col } } };
Branch: 1 branch
BranchTrueFalseTotal
$is_desc[$i] ? : 38 32 70
978  
979 70 push @skip_colpair_stack, [
980 { $main_col => { -ident => $subq_col } },
981 ];
982  
983 # we can trust the nullability flag because
984 # we already used it during _id_col_set resolution
985 #
986 70 ▶ if ($ci->{is_nullable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ci->{"is_nullable"}) 24 46 70
987 24 push @{$skip_colpair_stack[-1]}, { $main_col => undef, $subq_col=> undef };
988  
989 24 ▶ $cur_cond = [
Branch: 2 branches
BranchTrueFalseTotal
$is_desc[$i] ? : 14 10 24
$is_desc[$i] ? : 14 10 24
990 {
991 ($is_desc[$i] ? $subq_col : $main_col) => { '!=', undef },
992 ($is_desc[$i] ? $main_col : $subq_col) => undef,
993 },
994 {
995 $subq_col => { '!=', undef },
996 $main_col => { '!=', undef },
997 -and => $cur_cond,
998 },
999 ];
1000 }
1001  
1002 70 push @where_cond, { '-and', => [ @skip_colpair_stack[0..$i-1], $cur_cond ] };
1003 }
1004  
1005 # reuse the sqlmaker WHERE, this will not be returning binds
1006 24 my $counted_where = do {
1007 24 local $self->{where_bind};
1008 24 $self->where(\@where_cond);
1009 };
1010  
1011 # construct the rownum condition by hand
1012 24 my $rownum_cond;
1013 24 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) { } 15 9 24
1014 15 $rownum_cond = 'BETWEEN ? AND ?';
1015 15 push @{$self->{limit_bind}},
1016 [ $self->__offset_bindtype => $offset ],
1017 [ $self->__total_bindtype => $offset + $rows - 1]
1018 ;
1019 }
1020 else {
1021 9 $rownum_cond = '< ?';
1022 9 push @{$self->{limit_bind}},
1023 [ $self->__rows_bindtype => $rows ]
1024 ;
1025 }
1026  
1027 # and what we will order by inside
1028 24 my $inner_order_sql = do {
1029 24 local $self->{order_bind};
1030  
1031 24 my $s = $self->_order_by (\@new_order_by);
1032  
1033 $self->throw_exception('Inner gensubq order may not contain binds... something went wrong')
1034 24 ▶ if @{$self->{order_bind}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"order_bind"};} 0 24 24
1035  
1036 24 $s;
1037 };
1038  
1039 ### resume originally scheduled programming
1040 ###
1041 ###
1042  
1043 # we need to supply the order for the supplements to be properly calculated
1044 24 my $sq_attrs = $self->_subqueried_limit_attrs (
1045 $sql, { %$rs_attrs, order_by => \@new_order_by }
1046 );
1047  
1048 24 my $in_sel = $sq_attrs->{selection_inner};
1049  
1050 # add the order supplement (if any) as this is what will be used for the outer WHERE
1051 24 $in_sel .= ", $_" for sort keys %{$sq_attrs->{order_supplement}};
1052  
1053 24 my $group_having_sql = $self->_parse_rs_attrs($rs_attrs);
1054  
1055  
1056 return sprintf ("
1057 SELECT $sq_attrs->{selection_outer}
1058 FROM (
1059 SELECT $in_sel $sq_attrs->{query_leftover}${group_having_sql}
1060 ) %s
1061 WHERE ( SELECT COUNT(*) FROM %s %s $counted_where ) $rownum_cond
1062 $inner_order_sql
1063 72 ", map { $self->_quote ($_) } (
1064 $rs_attrs->{alias},
1065 24 $main_tbl_name,
1066 $count_tbl_alias,
1067 ));
1068 }
1069  
1070  
1071 # !!! THIS IS ALSO HORRIFIC !!! /me ashamed
1072 #
1073 # Generates inner/outer select lists for various limit dialects
1074 # which result in one or more subqueries (e.g. RNO, Top, RowNum)
1075 # Any non-main-table columns need to have their table qualifier
1076 # turned into a column alias (otherwise names in subqueries clash
1077 # and/or lose their source table)
1078 #
1079 # Returns mangled proto-sql, inner/outer strings of SQL QUOTED selectors
1080 # with aliases (to be used in whatever select statement), and an alias
1081 # index hashref of QUOTED SEL => QUOTED ALIAS pairs (to maybe be used
1082 # for string-subst higher up).
1083 # If an order_by is supplied, the inner select needs to bring out columns
1084 # used in implicit (non-selected) orders, and the order condition itself
1085 # needs to be realiased to the proper names in the outer query. Thus we
1086 # also return a hashref (order doesn't matter) of QUOTED EXTRA-SEL =>
1087 # QUOTED ALIAS pairs, which is a list of extra selectors that do *not*
1088 # exist in the original select list
1089 101 ▶sub _subqueried_limit_attrs {
Subroutine _subqueried_limit_attrs: called CC=22 SCAR=32.2
1090 101 my ($self, $proto_sql, $rs_attrs) = @_;
1091  
1092 101 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rs_attrs eq "HASH" 0 101 101
1093 'Limit dialect implementation usable only in the context of DBIC (missing $rs_attrs)'
1094 ) unless ref ($rs_attrs) eq 'HASH';
1095  
1096 # mangle the input sql as we will be replacing the selector entirely
1097 101 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($rs_attrs->{"_selector_sql"} and $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{"_selector_sql"}\E//xi) 0 101 101
Condition33%1/3
$rs_attrs->{"_selector_sql"} and $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{"_selector_sql"}\E//xi
!ll&&!rl&&r
00101
MC/DC0%0/2
$rs_attrs->{"_selector_sql"} and $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{"_selector_sql"}\E//xi
$rs_attrs->{"_selector_sql"} $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{"_selector_sql"}\E//xi
Truth table33%1/3
A and B
A= $rs_attrs->{"_selector_sql"}B= $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{"_selector_sql"}\E//xi
ABresult
0X0
100
111
1098 $rs_attrs->{_selector_sql}
1099 and
1100 $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{_selector_sql}//ix
1101 ) {
1102 0 $self->throw_exception("Unrecognizable SELECT: $proto_sql");
1103 }
1104  
1105 101 my ($re_sep, $re_alias) = map { quotemeta $_ } ( $self->{name_sep}, $rs_attrs->{alias} );
1106  
1107 # correlate select and as, build selection index
1108 101 my (@sel, $in_sel_index);
1109 101 for my $i (0 .. $#{$rs_attrs->{select}}) {
1110  
1111 425 my $s = $rs_attrs->{select}[$i];
1112 425 ▶ my $sql_alias = (ref $s) eq 'HASH' ? $s->{-as} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $s eq 'HASH' ? : 0 425 425
1113  
1114 # we throw away the @bind here deliberately
1115 425 my ($sql_sel) = $self->_recurse_fields ($s);
1116  
1117 push @sel, {
1118 arg => $s,
1119 sql => $sql_sel,
1120 unquoted_sql => do {
1121 425 local $self->{quote_char};
1122 425 ▶ ($self->_recurse_fields ($s))[0]; # ignore binds again
Condition33%2/6
$sql_alias || $rs_attrs->{'as'}[$i] || $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
l!l&&r!l&&!r
42500
$sql_alias or $rs_attrs->{"as"}[$i]
l!l&&r!l&&!r
04250
MC/DC0%0/3
$sql_alias || $rs_attrs->{'as'}[$i] || $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
$sql_alias $rs_attrs->{"as"}[$i] $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
Truth table25%1/4
A or B || C
A= $sql_aliasB= $rs_attrs->{"as"}[$i]C= $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
ABCresult
1XX1
01X1
0011
0000
1123 },
1124 as =>
1125 $sql_alias
1126 ||
1127 425 $rs_attrs->{as}[$i]
1128 ||
1129 $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
1130 ,
1131 };
1132  
1133 # anything with a placeholder in it needs re-selection
1134 425 ▶ $in_sel_index->{$sql_sel}++ unless $sql_sel =~ / (?: ^ | \W ) \? (?: \W | $ ) /x;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql_sel =~ / (?: ^ | \W ) \? (?: \W | $ ) /x 384 41 425
1135  
1136 425 ▶ $in_sel_index->{$self->_quote ($sql_alias)}++ if $sql_alias;
Branch: 1 branch
BranchTrueFalseTotal
if $sql_alias 0 425 425
1137  
1138 # record unqualified versions too, so we do not have
1139 # to reselect the same column twice (in qualified and
1140 # unqualified form)
1141 425 ▶ if (! ref $s && $sql_sel =~ / $re_sep (.+) $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $s and $sql_sel =~ / $re_sep (.+) $/x) 378 47 425
Condition66%2/3
not ref $s and $sql_sel =~ / $re_sep (.+) $/x
!ll&&!rl&&r
470378
MC/DC50%1/2
not ref $s and $sql_sel =~ / $re_sep (.+) $/x
not ref $s $sql_sel =~ / $re_sep (.+) $/x
Truth table66%2/3
not(A) and B
A= not ref $sB= $sql_sel =~ / $re_sep (.+) $/x
ABresult
0X0
100
111
1142 378 $in_sel_index->{$1}++;
1143 }
1144 }
1145  
1146  
1147 # re-alias and remove any name separators from aliases,
1148 # unless we are dealing with the current source alias
1149 # (which will transcend the subqueries as it is necessary
1150 # for possible further chaining)
1151 # same for anything we do not recognize
1152 101 my ($sel, $renamed);
1153 101 for my $node (@sel) {
1154 425 push @{$sel->{original}}, $node->{sql};
1155  
1156 425 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $$in_sel_index{$node->{'sql'}} or $node->{'as'} =~ / (?<! ^ $re_alias ) \. /x or $node->{'unquoted_sql'} =~ / (?<! ^ $re_alias ) $re_sep /x) { } 163 262 425
Condition100%6/6
not $$in_sel_index{$node->{"sql"}} or $node->{"as"} =~ / (?<! ^ $re_alias ) \. /x or $node->{"unquoted_sql"} =~ / (?<! ^ $re_alias ) $re_sep /x
l!l&&r!l&&!r
14221262
not $$in_sel_index{$node->{"sql"}} or $node->{"as"} =~ / (?<! ^ $re_alias ) \. /x
l!l&&r!l&&!r
41101283
MC/DC100%3/3
not $$in_sel_index{$node->{"sql"}} or $node->{"as"} =~ / (?<! ^ $re_alias ) \. /x or $node->{"unquoted_sql"} =~ / (?<! ^ $re_alias ) $re_sep /x
not $$in_sel_index{$node->{"sql"}} $node->{"as"} =~ / (?<! ^ $re_alias ) \. /x $node->{"unquoted_sql"} =~ / (?<! ^ $re_alias ) $re_sep /x
Truth table100%4/4
not(A) or B or C
A= not $$in_sel_index{$node->{"sql"}}B= $node->{"as"} =~ / (?<! ^ $re_alias ) \. /xC= $node->{"unquoted_sql"} =~ / (?<! ^ $re_alias ) $re_sep /x
ABCresult
1XX1
01X1
0011
0000
1157 ! $in_sel_index->{$node->{sql}}
1158 or
1159 $node->{as} =~ / (?<! ^ $re_alias ) \. /x
1160 or
1161 $node->{unquoted_sql} =~ / (?<! ^ $re_alias ) $re_sep /x
1162 ) {
1163 163 $node->{as} = $self->_unqualify_colname($node->{as});
1164 163 my $quoted_as = $self->_quote($node->{as});
1165 163 push @{$sel->{inner}}, sprintf '%s AS %s', $node->{sql}, $quoted_as;
1166 163 push @{$sel->{outer}}, $quoted_as;
1167 163 $renamed->{$node->{sql}} = $quoted_as;
1168 }
1169 else {
1170 262 push @{$sel->{inner}}, $node->{sql};
1171 262 ▶ push @{$sel->{outer}}, $self->_quote (ref $node->{arg} ? $node->{as} : $node->{arg});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'arg'} ? : 0 262 262
1172 }
1173 }
1174  
1175 # see if the order gives us anything
1176 101 my $extra_order_sel;
1177 101 for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) {
1178 # order with bind
1179 136 ▶ $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $chunk eq "ARRAY" 106 30 136
1180 136 ($chunk) = $self->_split_order_chunk($chunk);
1181  
1182 136 ▶ next if $in_sel_index->{$chunk};
Branch: 1 branch
BranchTrueFalseTotal
if $in_sel_index->{$chunk} 81 55 136
1183  
1184 $extra_order_sel->{$chunk} ||= $self->_quote (
1185 55 ▶ 'ORDER__BY__' . sprintf '%03d', scalar keys %{$extra_order_sel||{}}
Condition40%2/5
$extra_order_sel->{$chunk} ||= $self->_quote('ORDER__BY__' . sprintf("%03d", scalar keys %{$extra_order_sel or {};}))
l!l&&r!l&&!r
0550
$extra_order_sel || {}
l!l
550
MC/DC0%0/2
$extra_order_sel->{$chunk} ||= $self->_quote('ORDER__BY__' . sprintf("%03d", scalar keys %{$extra_order_sel or {};}))
$extra_order_sel->{$chunk} $self->_quote('ORDER__BY__' . sprintf("%03d", scalar keys %{$extra_order_sel or {};}))
MC/DC0%0/1
$extra_order_sel || {}
$extra_order_sel
Truth table33%1/3
A ||= B
A= $extra_order_sel->{$chunk}B= $self->_quote('ORDER__BY__' . sprintf("%03d", scalar keys %{$extra_order_sel or {};}))
ABresult
1X1
011
000
Truth table50%1/2
A
A= $extra_order_sel
Aresult
11
00
1186 );
1187 }
1188  
1189 return {
1190 query_leftover => $proto_sql,
1191 101 (map {( "selection_$_" => join (', ', @{$sel->{$_}} ) )} keys %$sel ),
1192 outer_renames => $renamed,
1193 order_supplement => $extra_order_sel,
1194 };
1195 }
1196  
1197 163 ▶sub _unqualify_colname {
Subroutine _unqualify_colname: called CC=1 SCAR=0.0
1198 163 my ($self, $fqcn) = @_;
1199 163 $fqcn =~ s/ \. /__/xg;
1200 163 return $fqcn;
1201 }
1202  
1203 =head1 FURTHER QUESTIONS?
1204  
1205 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
1206  
1207 =head1 COPYRIGHT AND LICENSE
1208  
1209 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
1210 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
1211 redistribute it and/or modify it under the same terms as the
1212 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
1213  
1214 =cut
1215  
1216 216 1;
« DBIx/Class/ResultSource/RowParser/Util.pm ↑ Summary DBIx/Class/Relationship/Base.pm »