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