Require bugfixed Module::Install
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / LimitDialects.pm
CommitLineData
d5dedbd6 1package DBIx::Class::SQLMaker::LimitDialects;
7fca91be 2
3use warnings;
4use strict;
5
7fca91be 6use List::Util 'first';
7use namespace::clean;
8
fcb7fcbb 9# constants are used not only here, but also in comparison tests
10sub __rows_bindtype () {
11 +{ sqlt_datatype => 'integer' }
12}
13sub __offset_bindtype () {
14 +{ sqlt_datatype => 'integer' }
15}
16sub __total_bindtype () {
17 +{ sqlt_datatype => 'integer' }
18}
19
d5dedbd6 20=head1 NAME
21
22DBIx::Class::SQLMaker::LimitDialects - SQL::Abstract::Limit-like functionality for DBIx::Class::SQLMaker
23
24=head1 DESCRIPTION
25
26This module replicates a lot of the functionality originally found in
27L<SQL::Abstract::Limit>. While simple limits would work as-is, the more
28complex dialects that require e.g. subqueries could not be reliably
29implemented without taking full advantage of the metadata locked within
30L<DBIx::Class::ResultSource> classes. After reimplementation of close to
3180% of the L<SQL::Abstract::Limit> functionality it was deemed more
32practical to simply make an independent DBIx::Class-specific limit-dialect
33provider.
34
35=head1 SQL LIMIT DIALECTS
36
37Note that the actual implementations listed below never use C<*> literally.
38Instead proper re-aliasing of selectors and order criteria is done, so that
39the limit dialect are safe to use on joined resultsets with clashing column
40names.
41
42Currently the provided dialects are:
43
d5dedbd6 44=head2 LimitOffset
45
46 SELECT ... LIMIT $limit OFFSET $offset
47
48Supported by B<PostgreSQL> and B<SQLite>
49
50=cut
7fca91be 51sub _LimitOffset {
fcb7fcbb 52 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
53 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ?";
54 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
55 if ($offset) {
56 $sql .= " OFFSET ?";
57 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
58 }
7fca91be 59 return $sql;
60}
61
d5dedbd6 62=head2 LimitXY
63
64 SELECT ... LIMIT $offset $limit
65
66Supported by B<MySQL> and any L<SQL::Statement> based DBD
67
68=cut
7fca91be 69sub _LimitXY {
fcb7fcbb 70 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
71 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ";
72 if ($offset) {
73 $sql .= '?, ';
74 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
75 }
76 $sql .= '?';
77 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
78
7fca91be 79 return $sql;
80}
d5dedbd6 81
82=head2 RowNumberOver
83
84 SELECT * FROM (
85 SELECT *, ROW_NUMBER() OVER( ORDER BY ... ) AS RNO__ROW__INDEX FROM (
86 SELECT ...
87 )
88 ) WHERE RNO__ROW__INDEX BETWEEN ($offset+1) AND ($limit+$offset)
89
90
91ANSI standard Limit/Offset implementation. Supported by B<DB2> and
92B<< MSSQL >= 2005 >>.
93
94=cut
7fca91be 95sub _RowNumberOver {
96 my ($self, $sql, $rs_attrs, $rows, $offset ) = @_;
97
7fca91be 98 # get selectors, and scan the order_by (if any)
69d3c270 99 my ($stripped_sql, $in_sel, $out_sel, $alias_map, $extra_order_sel)
100 = $self->_subqueried_limit_attrs ( $sql, $rs_attrs );
7fca91be 101
102 # make up an order if none exists
103 my $requested_order = (delete $rs_attrs->{order_by}) || $self->_rno_default_order;
104 my $rno_ord = $self->_order_by ($requested_order);
105
106 # this is the order supplement magic
107 my $mid_sel = $out_sel;
108 if ($extra_order_sel) {
109 for my $extra_col (sort
110 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
111 keys %$extra_order_sel
112 ) {
113 $in_sel .= sprintf (', %s AS %s',
114 $extra_col,
115 $extra_order_sel->{$extra_col},
116 );
7fca91be 117 }
118 }
119
120 # and this is order re-alias magic
121 for ($extra_order_sel, $alias_map) {
122 for my $col (keys %$_) {
123 my $re_col = quotemeta ($col);
124 $rno_ord =~ s/$re_col/$_->{$col}/;
125 }
126 }
127
128 # whatever is left of the order_by (only where is processed at this point)
129 my $group_having = $self->_parse_rs_attrs($rs_attrs);
130
131 my $qalias = $self->_quote ($rs_attrs->{alias});
132 my $idx_name = $self->_quote ('rno__row__index');
133
69d3c270 134 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1], [ $self->__total_bindtype => $offset + $rows ];
135
136 return <<EOS;
7fca91be 137
138SELECT $out_sel FROM (
139 SELECT $mid_sel, ROW_NUMBER() OVER( $rno_ord ) AS $idx_name FROM (
69d3c270 140 SELECT $in_sel ${stripped_sql}${group_having}
7fca91be 141 ) $qalias
fcb7fcbb 142) $qalias WHERE $idx_name >= ? AND $idx_name <= ?
7fca91be 143
144EOS
145
7fca91be 146}
147
148# some databases are happy with OVER (), some need OVER (ORDER BY (SELECT (1)) )
149sub _rno_default_order {
150 return undef;
151}
152
d5dedbd6 153=head2 SkipFirst
154
155 SELECT SKIP $offset FIRST $limit * FROM ...
156
157Suported by B<Informix>, almost like LimitOffset. According to
158L<SQL::Abstract::Limit> C<... SKIP $offset LIMIT $limit ...> is also supported.
159
160=cut
7fca91be 161sub _SkipFirst {
162 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
163
164 $sql =~ s/^ \s* SELECT \s+ //ix
70c28808 165 or $self->throw_exception("Unrecognizable SELECT: $sql");
7fca91be 166
167 return sprintf ('SELECT %s%s%s%s',
168 $offset
fcb7fcbb 169 ? do {
8b31f62e 170 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
fcb7fcbb 171 'SKIP ? '
172 }
7fca91be 173 : ''
174 ,
fcb7fcbb 175 do {
8b31f62e 176 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
fcb7fcbb 177 'FIRST ? '
178 },
7fca91be 179 $sql,
180 $self->_parse_rs_attrs ($rs_attrs),
181 );
182}
183
d5dedbd6 184=head2 FirstSkip
185
186 SELECT FIRST $limit SKIP $offset * FROM ...
187
188Supported by B<Firebird/Interbase>, reverse of SkipFirst. According to
189L<SQL::Abstract::Limit> C<... ROWS $limit TO $offset ...> is also supported.
190
191=cut
7fca91be 192sub _FirstSkip {
193 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
194
195 $sql =~ s/^ \s* SELECT \s+ //ix
70c28808 196 or $self->throw_exception("Unrecognizable SELECT: $sql");
7fca91be 197
198 return sprintf ('SELECT %s%s%s%s',
fcb7fcbb 199 do {
8b31f62e 200 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
fcb7fcbb 201 'FIRST ? '
202 },
7fca91be 203 $offset
fcb7fcbb 204 ? do {
8b31f62e 205 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
fcb7fcbb 206 'SKIP ? '
207 }
7fca91be 208 : ''
209 ,
210 $sql,
211 $self->_parse_rs_attrs ($rs_attrs),
212 );
213}
214
6a6394f1 215
d5dedbd6 216=head2 RowNum
217
6a6394f1 218Depending on the resultset attributes one of:
219
d5dedbd6 220 SELECT * FROM (
221 SELECT *, ROWNUM rownum__index FROM (
222 SELECT ...
d9672fb9 223 ) WHERE ROWNUM <= ($limit+$offset)
224 ) WHERE rownum__index >= ($offset+1)
d5dedbd6 225
6a6394f1 226or
227
228 SELECT * FROM (
229 SELECT *, ROWNUM rownum__index FROM (
230 SELECT ...
231 )
232 ) WHERE rownum__index BETWEEN ($offset+1) AND ($limit+$offset)
233
234or
235
236 SELECT * FROM (
237 SELECT ...
238 ) WHERE ROWNUM <= ($limit+1)
239
d5dedbd6 240Supported by B<Oracle>.
241
242=cut
7fca91be 243sub _RowNum {
244 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
245
69d3c270 246 my ($stripped_sql, $insel, $outsel) = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
7fca91be 247
248 my $qalias = $self->_quote ($rs_attrs->{alias});
249 my $idx_name = $self->_quote ('rownum__index');
250 my $order_group_having = $self->_parse_rs_attrs($rs_attrs);
251
6a6394f1 252 #
253 # There are two ways to limit in Oracle, one vastly faster than the other
254 # on large resultsets: https://decipherinfosys.wordpress.com/2007/08/09/paging-and-countstopkey-optimization/
255 # However Oracle is retarded and does not preserve stable ROWNUM() values
256 # when called twice in the same scope. Therefore unless the resultset is
257 # ordered by a unique set of columns, it is not safe to use the faster
258 # method, and the slower BETWEEN query is used instead
259 #
7cec4356 260 # FIXME - this is quite expensive, and does not perform caching of any sort
6a6394f1 261 # as soon as some of the DQ work becomes viable consider switching this
262 # over
7cec4356 263 if (
264 $rs_attrs->{order_by}
265 and
266 $rs_attrs->{_rsroot_rsrc}->storage->_order_by_is_stable(
267 $rs_attrs->{from}, $rs_attrs->{order_by}
268 )
269 ) {
6a6394f1 270 # if offset is 0 (first page) the we can skip a subquery
271 if (! $offset) {
272 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
273
274 return <<EOS;
275SELECT $outsel FROM (
276 SELECT $insel ${stripped_sql}${order_group_having}
277) $qalias WHERE ROWNUM <= ?
278EOS
279 }
280 else {
281 push @{$self->{limit_bind}}, [ $self->__total_bindtype => $offset + $rows ], [ $self->__offset_bindtype => $offset + 1 ];
69d3c270 282
6a6394f1 283 return <<EOS;
7fca91be 284SELECT $outsel FROM (
285 SELECT $outsel, ROWNUM $idx_name FROM (
69d3c270 286 SELECT $insel ${stripped_sql}${order_group_having}
fcb7fcbb 287 ) $qalias WHERE ROWNUM <= ?
288) $qalias WHERE $idx_name >= ?
7fca91be 289EOS
6a6394f1 290 }
d9672fb9 291 }
292 else {
6a6394f1 293 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1 ], [ $self->__total_bindtype => $offset + $rows ];
69d3c270 294
295 return <<EOS;
6a6394f1 296SELECT $outsel FROM (
297 SELECT $outsel, ROWNUM $idx_name FROM (
69d3c270 298 SELECT $insel ${stripped_sql}${order_group_having}
6a6394f1 299 ) $qalias
300) $qalias WHERE $idx_name BETWEEN ? AND ?
d9672fb9 301EOS
6a6394f1 302 }
303}
7fca91be 304
6a6394f1 305# used by _Top and _FetchFirst below
96eacdb7 306sub _prep_for_skimming_limit {
307 my ( $self, $sql, $rs_attrs ) = @_;
7fca91be 308
7fca91be 309 # get selectors
69d3c270 310 my (%r, $alias_map, $extra_order_sel);
311 ($r{inner_sql}, $r{in_sel}, $r{out_sel}, $alias_map, $extra_order_sel)
312 = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
7fca91be 313
314 my $requested_order = delete $rs_attrs->{order_by};
96eacdb7 315 $r{order_by_requested} = $self->_order_by ($requested_order);
7fca91be 316
86bb5a27 317 # make up an order unless supplied or sanity check what we are given
318 my $inner_order;
319 if ($r{order_by_requested}) {
320 $self->throw_exception (
321 'Unable to safely perform "skimming type" limit with supplied unstable order criteria'
322 ) unless $rs_attrs->{_rsroot_rsrc}->schema->storage->_order_by_is_stable(
323 $rs_attrs->{from},
324 $requested_order
325 );
326
327 $inner_order = $requested_order;
328 }
329 else {
330 $inner_order = [ map
3f5b99fe 331 { "$rs_attrs->{alias}.$_" }
86bb5a27 332 ( @{
333 $rs_attrs->{_rsroot_rsrc}->_identifying_column_set
334 ||
335 $self->throw_exception(sprintf(
336 'Unable to auto-construct stable order criteria for "skimming type" limit '
337 . "dialect based on source '%s'", $rs_attrs->{_rsroot_rsrc}->name) );
338 } )
339 ];
340 }
7fca91be 341
7fca91be 342 # localise as we already have all the bind values we need
343 {
344 local $self->{order_bind};
96eacdb7 345 $r{order_by_inner} = $self->_order_by ($inner_order);
7fca91be 346
347 my @out_chunks;
348 for my $ch ($self->_order_by_chunks ($inner_order)) {
349 $ch = $ch->[0] if ref $ch eq 'ARRAY';
350
351 $ch =~ s/\s+ ( ASC|DESC ) \s* $//ix;
352 my $dir = uc ($1||'ASC');
353
354 push @out_chunks, \join (' ', $ch, $dir eq 'ASC' ? 'DESC' : 'ASC' );
355 }
356
96eacdb7 357 $r{order_by_reversed} = $self->_order_by (\@out_chunks);
7fca91be 358 }
359
360 # this is the order supplement magic
96eacdb7 361 $r{mid_sel} = $r{out_sel};
7fca91be 362 if ($extra_order_sel) {
363 for my $extra_col (sort
364 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
365 keys %$extra_order_sel
366 ) {
96eacdb7 367 $r{in_sel} .= sprintf (', %s AS %s',
7fca91be 368 $extra_col,
369 $extra_order_sel->{$extra_col},
370 );
371
96eacdb7 372 $r{mid_sel} .= ', ' . $extra_order_sel->{$extra_col};
7fca91be 373 }
374
8b31f62e 375 # Whatever order bindvals there are, they will be realiased and
376 # need to show up in front of the entire initial inner subquery
377 push @{$self->{pre_select_bind}}, @{$self->{order_bind}};
7fca91be 378 }
379
86bb5a27 380 # if this is a part of something bigger, we need to add back all
381 # the extra order_by's, as they may be relied upon by the outside
382 # of a prefetch or something
383 if ($rs_attrs->{_is_internal_subuery} and keys %$extra_order_sel) {
384 $r{out_sel} .= sprintf ", $extra_order_sel->{$_} AS $_"
385 for sort
386 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
387 grep { $_ !~ /[^\w\-]/ } # ignore functions
388 keys %$extra_order_sel
389 ;
390 }
391
7fca91be 392 # and this is order re-alias magic
393 for my $map ($extra_order_sel, $alias_map) {
394 for my $col (keys %$map) {
395 my $re_col = quotemeta ($col);
396 $_ =~ s/$re_col/$map->{$col}/
96eacdb7 397 for ($r{order_by_reversed}, $r{order_by_requested});
7fca91be 398 }
399 }
400
401 # generate the rest of the sql
96eacdb7 402 $r{grpby_having} = $self->_parse_rs_attrs ($rs_attrs);
7fca91be 403
96eacdb7 404 $r{quoted_rs_alias} = $self->_quote ($rs_attrs->{alias});
405
406 \%r;
407}
408
409=head2 Top
410
411 SELECT * FROM
412
413 SELECT TOP $limit FROM (
414 SELECT TOP $limit FROM (
415 SELECT TOP ($limit+$offset) ...
416 ) ORDER BY $reversed_original_order
417 ) ORDER BY $original_order
418
419Unreliable Top-based implementation, supported by B<< MSSQL < 2005 >>.
420
421=head3 CAVEAT
422
423Due to its implementation, this limit dialect returns B<incorrect results>
424when $limit+$offset > total amount of rows in the resultset.
425
426=cut
427
428sub _Top {
429 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
430
431 my %l = %{ $self->_prep_for_skimming_limit($sql, $rs_attrs) };
7fca91be 432
433 $sql = sprintf ('SELECT TOP %u %s %s %s %s',
434 $rows + ($offset||0),
96eacdb7 435 $l{in_sel},
436 $l{inner_sql},
437 $l{grpby_having},
438 $l{order_by_inner},
7fca91be 439 );
440
441 $sql = sprintf ('SELECT TOP %u %s FROM ( %s ) %s %s',
442 $rows,
96eacdb7 443 $l{mid_sel},
7fca91be 444 $sql,
96eacdb7 445 $l{quoted_rs_alias},
446 $l{order_by_reversed},
7fca91be 447 ) if $offset;
448
449 $sql = sprintf ('SELECT TOP %u %s FROM ( %s ) %s %s',
450 $rows,
96eacdb7 451 $l{out_sel},
452 $sql,
453 $l{quoted_rs_alias},
454 $l{order_by_requested},
455 ) if ( ($offset && $l{order_by_requested}) || ($l{mid_sel} ne $l{out_sel}) );
456
457 return $sql;
458}
459
460=head2 FetchFirst
461
462 SELECT * FROM
463 (
464 SELECT * FROM (
465 SELECT * FROM (
466 SELECT * FROM ...
467 ) ORDER BY $reversed_original_order
468 FETCH FIRST $limit ROWS ONLY
469 ) ORDER BY $original_order
470 FETCH FIRST $limit ROWS ONLY
471 )
472
473Unreliable FetchFirst-based implementation, supported by B<< IBM DB2 <= V5R3 >>.
474
475=head3 CAVEAT
476
477Due to its implementation, this limit dialect returns B<incorrect results>
478when $limit+$offset > total amount of rows in the resultset.
479
480=cut
481
482sub _FetchFirst {
483 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
484
485 my %l = %{ $self->_prep_for_skimming_limit($sql, $rs_attrs) };
486
487 $sql = sprintf ('SELECT %s %s %s %s FETCH FIRST %u ROWS ONLY',
488 $l{in_sel},
489 $l{inner_sql},
490 $l{grpby_having},
491 $l{order_by_inner},
492 $rows + ($offset||0),
493 );
494
495 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s FETCH FIRST %u ROWS ONLY',
496 $l{mid_sel},
497 $sql,
498 $l{quoted_rs_alias},
499 $l{order_by_reversed},
500 $rows,
501 ) if $offset;
502
503 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s FETCH FIRST %u ROWS ONLY',
504 $l{out_sel},
7fca91be 505 $sql,
96eacdb7 506 $l{quoted_rs_alias},
507 $l{order_by_requested},
508 $rows,
509 ) if ( ($offset && $l{order_by_requested}) || ($l{mid_sel} ne $l{out_sel}) );
7fca91be 510
7fca91be 511 return $sql;
512}
513
d5dedbd6 514=head2 RowCountOrGenericSubQ
515
516This is not exactly a limit dialect, but more of a proxy for B<Sybase ASE>.
517If no $offset is supplied the limit is simply performed as:
518
519 SET ROWCOUNT $limit
520 SELECT ...
521 SET ROWCOUNT 0
522
523Otherwise we fall back to L</GenericSubQ>
524
525=cut
96eacdb7 526
7fca91be 527sub _RowCountOrGenericSubQ {
528 my $self = shift;
529 my ($sql, $rs_attrs, $rows, $offset) = @_;
530
531 return $self->_GenericSubQ(@_) if $offset;
532
533 return sprintf <<"EOF", $rows, $sql;
534SET ROWCOUNT %d
535%s
536SET ROWCOUNT 0
537EOF
538}
539
d5dedbd6 540=head2 GenericSubQ
541
542 SELECT * FROM (
543 SELECT ...
544 )
545 WHERE (
546 SELECT COUNT(*) FROM $original_table cnt WHERE cnt.id < $original_table.id
547 ) BETWEEN $offset AND ($offset+$rows-1)
548
549This is the most evil limit "dialect" (more of a hack) for I<really> stupid
550databases. It works by ordering the set by some unique column, and calculating
551the amount of rows that have a less-er value (thus emulating a L</RowNum>-like
552index). Of course this implies the set can only be ordered by a single unique
553column. Also note that this technique can be and often is B<excruciatingly
554slow>.
555
556Currently used by B<Sybase ASE>, due to lack of any other option.
557
558=cut
7fca91be 559sub _GenericSubQ {
560 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
561
4376a157 562 my $root_rsrc = $rs_attrs->{_rsroot_rsrc};
7fca91be 563 my $root_tbl_name = $root_rsrc->name;
564
528e717e 565 my ($first_order_by) = do {
7fca91be 566 local $self->{quote_char};
528e717e 567 map { ref $_ ? $_->[0] : $_ } $self->_order_by_chunks ($rs_attrs->{order_by})
568 } or $self->throw_exception (
569 'Generic Subquery Limit does not work on resultsets without an order. Provide a single, '
570 . 'unique-column order criteria.'
571 );
7fca91be 572
528e717e 573 $first_order_by =~ s/\s+ ( ASC|DESC ) \s* $//ix;
7fca91be 574 my $direction = lc ($1 || 'asc');
575
528e717e 576 my ($first_ord_alias, $first_ord_col) = $first_order_by =~ /^ (?: ([^\.]+) \. )? ([^\.]+) $/x;
7fca91be 577
528e717e 578 $self->throw_exception(sprintf
579 "Generic Subquery Limit order criteria can be only based on the root-source '%s'"
580 . " (aliased as '%s')", $root_rsrc->source_name, $rs_attrs->{alias},
581 ) if ($first_ord_alias and $first_ord_alias ne $rs_attrs->{alias});
7fca91be 582
528e717e 583 $first_ord_alias ||= $rs_attrs->{alias};
7fca91be 584
70c28808 585 $self->throw_exception(
528e717e 586 "Generic Subquery Limit first order criteria '$first_ord_col' must be unique"
587 ) unless $root_rsrc->_identifying_column_set([$first_ord_col]);
7fca91be 588
69d3c270 589 my ($stripped_sql, $in_sel, $out_sel, $alias_map, $extra_order_sel)
590 = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
7fca91be 591
592 my $cmp_op = $direction eq 'desc' ? '>' : '<';
593 my $count_tbl_alias = 'rownum__emulation';
594
595 my $order_sql = $self->_order_by (delete $rs_attrs->{order_by});
596 my $group_having_sql = $self->_parse_rs_attrs($rs_attrs);
597
598 # add the order supplement (if any) as this is what will be used for the outer WHERE
599 $in_sel .= ", $_" for keys %{$extra_order_sel||{}};
600
69d3c270 601 my $rownum_cond;
602 if ($offset) {
603 $rownum_cond = 'BETWEEN ? AND ?';
604
605 push @{$self->{limit_bind}},
606 [ $self->__offset_bindtype => $offset ],
607 [ $self->__total_bindtype => $offset + $rows - 1]
608 ;
609 }
610 else {
611 $rownum_cond = '< ?';
612
613 push @{$self->{limit_bind}},
614 [ $self->__rows_bindtype => $rows ]
615 ;
616 }
617
618 return sprintf ("
7fca91be 619SELECT $out_sel
620 FROM (
69d3c270 621 SELECT $in_sel ${stripped_sql}${group_having_sql}
7fca91be 622 ) %s
69d3c270 623WHERE ( SELECT COUNT(*) FROM %s %s WHERE %s $cmp_op %s ) $rownum_cond
7fca91be 624$order_sql
69d3c270 625 ", map { $self->_quote ($_) } (
626 $rs_attrs->{alias},
627 $root_tbl_name,
628 $count_tbl_alias,
528e717e 629 "$count_tbl_alias.$first_ord_col",
630 "$first_ord_alias.$first_ord_col",
69d3c270 631 ));
7fca91be 632}
633
634
635# !!! THIS IS ALSO HORRIFIC !!! /me ashamed
636#
637# Generates inner/outer select lists for various limit dialects
638# which result in one or more subqueries (e.g. RNO, Top, RowNum)
639# Any non-root-table columns need to have their table qualifier
640# turned into a column alias (otherwise names in subqueries clash
641# and/or lose their source table)
642#
69d3c270 643# Returns mangled proto-sql, inner/outer strings of SQL QUOTED selectors
644# with aliases (to be used in whatever select statement), and an alias
8273e845 645# index hashref of QUOTED SEL => QUOTED ALIAS pairs (to maybe be used
69d3c270 646# for string-subst higher up).
7fca91be 647# If an order_by is supplied, the inner select needs to bring out columns
648# used in implicit (non-selected) orders, and the order condition itself
649# needs to be realiased to the proper names in the outer query. Thus we
650# also return a hashref (order doesn't matter) of QUOTED EXTRA-SEL =>
651# QUOTED ALIAS pairs, which is a list of extra selectors that do *not*
652# exist in the original select list
7fca91be 653sub _subqueried_limit_attrs {
69d3c270 654 my ($self, $proto_sql, $rs_attrs) = @_;
7fca91be 655
70c28808 656 $self->throw_exception(
657 'Limit dialect implementation usable only in the context of DBIC (missing $rs_attrs)'
658 ) unless ref ($rs_attrs) eq 'HASH';
7fca91be 659
f74d22e2 660 # mangle the input sql as we will be replacing the selector entirely
661 unless (
662 $rs_attrs->{_selector_sql}
663 and
664 $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{_selector_sql}//ix
665 ) {
666 $self->throw_exception("Unrecognizable SELECT: $proto_sql");
667 }
69d3c270 668
3f5b99fe 669 my ($re_sep, $re_alias) = map { quotemeta $_ } ( $self->{name_sep}, $rs_attrs->{alias} );
7fca91be 670
69d3c270 671 # insulate from the multiple _recurse_fields calls below
672 local $self->{select_bind};
673
7fca91be 674 # correlate select and as, build selection index
675 my (@sel, $in_sel_index);
676 for my $i (0 .. $#{$rs_attrs->{select}}) {
677
678 my $s = $rs_attrs->{select}[$i];
679 my $sql_sel = $self->_recurse_fields ($s);
680 my $sql_alias = (ref $s) eq 'HASH' ? $s->{-as} : undef;
681
7fca91be 682 push @sel, {
683 sql => $sql_sel,
69d3c270 684 unquoted_sql => do {
685 local $self->{quote_char};
686 $self->_recurse_fields ($s);
687 },
7fca91be 688 as =>
689 $sql_alias
690 ||
691 $rs_attrs->{as}[$i]
692 ||
70c28808 693 $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
7fca91be 694 ,
695 };
696
697 $in_sel_index->{$sql_sel}++;
698 $in_sel_index->{$self->_quote ($sql_alias)}++ if $sql_alias;
699
700 # record unqualified versions too, so we do not have
701 # to reselect the same column twice (in qualified and
702 # unqualified form)
703 if (! ref $s && $sql_sel =~ / $re_sep (.+) $/x) {
704 $in_sel_index->{$1}++;
705 }
706 }
707
708
709 # re-alias and remove any name separators from aliases,
710 # unless we are dealing with the current source alias
711 # (which will transcend the subqueries as it is necessary
712 # for possible further chaining)
713 my (@in_sel, @out_sel, %renamed);
714 for my $node (@sel) {
3f5b99fe 715 if (
716 $node->{as} =~ / (?<! ^ $re_alias ) \. /x
717 or
718 $node->{unquoted_sql} =~ / (?<! ^ $re_alias ) $re_sep /x
719 ) {
7fca91be 720 $node->{as} = $self->_unqualify_colname($node->{as});
721 my $quoted_as = $self->_quote($node->{as});
722 push @in_sel, sprintf '%s AS %s', $node->{sql}, $quoted_as;
723 push @out_sel, $quoted_as;
724 $renamed{$node->{sql}} = $quoted_as;
725 }
726 else {
727 push @in_sel, $node->{sql};
728 push @out_sel, $self->_quote ($node->{as});
729 }
730 }
7fca91be 731 # see if the order gives us anything
732 my %extra_order_sel;
733 for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) {
734 # order with bind
735 $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY';
736 $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix;
737
738 next if $in_sel_index->{$chunk};
739
740 $extra_order_sel{$chunk} ||= $self->_quote (
741 'ORDER__BY__' . scalar keys %extra_order_sel
742 );
743 }
744
745 return (
69d3c270 746 $proto_sql,
7fca91be 747 (map { join (', ', @$_ ) } (
748 \@in_sel,
749 \@out_sel)
750 ),
751 \%renamed,
752 keys %extra_order_sel ? \%extra_order_sel : (),
753 );
754}
755
756sub _unqualify_colname {
757 my ($self, $fqcn) = @_;
3f5b99fe 758 $fqcn =~ s/ \. /__/xg;
7fca91be 759 return $fqcn;
760}
761
7621;
d5dedbd6 763
764=head1 AUTHORS
765
766See L<DBIx::Class/CONTRIBUTORS>.
767
768=head1 LICENSE
769
770You may distribute this code under the same terms as Perl itself.
771
772=cut