1 package DBIx::Class::SQLMaker::LimitDialects;
6 use List::Util 'first';
9 # constants are used not only here, but also in comparison tests
10 sub __rows_bindtype () {
11 +{ sqlt_datatype => 'integer' }
13 sub __offset_bindtype () {
14 +{ sqlt_datatype => 'integer' }
16 sub __total_bindtype () {
17 +{ sqlt_datatype => 'integer' }
22 DBIx::Class::SQLMaker::LimitDialects - SQL::Abstract::Limit-like functionality for DBIx::Class::SQLMaker
26 This module replicates a lot of the functionality originally found in
27 L<SQL::Abstract::Limit>. While simple limits would work as-is, the more
28 complex dialects that require e.g. subqueries could not be reliably
29 implemented without taking full advantage of the metadata locked within
30 L<DBIx::Class::ResultSource> classes. After reimplementation of close to
31 80% of the L<SQL::Abstract::Limit> functionality it was deemed more
32 practical to simply make an independent DBIx::Class-specific limit-dialect
35 =head1 SQL LIMIT DIALECTS
37 Note that the actual implementations listed below never use C<*> literally.
38 Instead proper re-aliasing of selectors and order criteria is done, so that
39 the limit dialect are safe to use on joined resultsets with clashing column
42 Currently the provided dialects are:
46 SELECT ... LIMIT $limit OFFSET $offset
48 Supported by B<PostgreSQL> and B<SQLite>
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 ];
57 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
64 SELECT ... LIMIT $offset $limit
66 Supported by B<MySQL> and any L<SQL::Statement> based DBD
70 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
71 $sql .= $self->_parse_rs_attrs( $rs_attrs ) . " LIMIT ";
74 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset ];
77 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
85 SELECT *, ROW_NUMBER() OVER( ORDER BY ... ) AS RNO__ROW__INDEX FROM (
88 ) WHERE RNO__ROW__INDEX BETWEEN ($offset+1) AND ($limit+$offset)
91 ANSI standard Limit/Offset implementation. Supported by B<DB2> and
96 my ($self, $sql, $rs_attrs, $rows, $offset ) = @_;
98 # get selectors, and scan the order_by (if any)
99 my ($stripped_sql, $in_sel, $out_sel, $alias_map, $extra_order_sel)
100 = $self->_subqueried_limit_attrs ( $sql, $rs_attrs );
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);
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
113 $in_sel .= sprintf (', %s AS %s',
115 $extra_order_sel->{$extra_col},
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}/;
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);
131 my $qalias = $self->_quote ($rs_attrs->{alias});
132 my $idx_name = $self->_quote ('rno__row__index');
134 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1], [ $self->__total_bindtype => $offset + $rows ];
138 SELECT $out_sel FROM (
139 SELECT $mid_sel, ROW_NUMBER() OVER( $rno_ord ) AS $idx_name FROM (
140 SELECT $in_sel ${stripped_sql}${group_having}
142 ) $qalias WHERE $idx_name >= ? AND $idx_name <= ?
148 # some databases are happy with OVER (), some need OVER (ORDER BY (SELECT (1)) )
149 sub _rno_default_order {
155 SELECT SKIP $offset FIRST $limit * FROM ...
157 Suported by B<Informix>, almost like LimitOffset. According to
158 L<SQL::Abstract::Limit> C<... SKIP $offset LIMIT $limit ...> is also supported.
162 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
164 $sql =~ s/^ \s* SELECT \s+ //ix
165 or $self->throw_exception("Unrecognizable SELECT: $sql");
167 return sprintf ('SELECT %s%s%s%s',
170 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
176 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
180 $self->_parse_rs_attrs ($rs_attrs),
186 SELECT FIRST $limit SKIP $offset * FROM ...
188 Supported by B<Firebird/Interbase>, reverse of SkipFirst. According to
189 L<SQL::Abstract::Limit> C<... ROWS $limit TO $offset ...> is also supported.
193 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
195 $sql =~ s/^ \s* SELECT \s+ //ix
196 or $self->throw_exception("Unrecognizable SELECT: $sql");
198 return sprintf ('SELECT %s%s%s%s',
200 push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
205 push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
211 $self->_parse_rs_attrs ($rs_attrs),
218 Depending on the resultset attributes one of:
221 SELECT *, ROWNUM rownum__index FROM (
223 ) WHERE ROWNUM <= ($limit+$offset)
224 ) WHERE rownum__index >= ($offset+1)
229 SELECT *, ROWNUM rownum__index FROM (
232 ) WHERE rownum__index BETWEEN ($offset+1) AND ($limit+$offset)
238 ) WHERE ROWNUM <= ($limit+1)
240 Supported by B<Oracle>.
244 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
246 my ($stripped_sql, $insel, $outsel) = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
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);
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
260 # FIXME - this is quite expensive, and does not perform caching of any sort
261 # as soon as some of the DQ work becomes viable consider switching this
264 $rs_attrs->{order_by}
266 $rs_attrs->{_rsroot_rsrc}->storage->_order_by_is_stable(
267 $rs_attrs->{from}, $rs_attrs->{order_by}
270 # if offset is 0 (first page) the we can skip a subquery
272 push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
275 SELECT $outsel FROM (
276 SELECT $insel ${stripped_sql}${order_group_having}
277 ) $qalias WHERE ROWNUM <= ?
281 push @{$self->{limit_bind}}, [ $self->__total_bindtype => $offset + $rows ], [ $self->__offset_bindtype => $offset + 1 ];
284 SELECT $outsel FROM (
285 SELECT $outsel, ROWNUM $idx_name FROM (
286 SELECT $insel ${stripped_sql}${order_group_having}
287 ) $qalias WHERE ROWNUM <= ?
288 ) $qalias WHERE $idx_name >= ?
293 push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset + 1 ], [ $self->__total_bindtype => $offset + $rows ];
296 SELECT $outsel FROM (
297 SELECT $outsel, ROWNUM $idx_name FROM (
298 SELECT $insel ${stripped_sql}${order_group_having}
300 ) $qalias WHERE $idx_name BETWEEN ? AND ?
305 # used by _Top and _FetchFirst below
306 sub _prep_for_skimming_limit {
307 my ( $self, $sql, $rs_attrs ) = @_;
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);
314 my $requested_order = delete $rs_attrs->{order_by};
315 $r{order_by_requested} = $self->_order_by ($requested_order);
317 # make up an order unless supplied or sanity check what we are given
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(
327 $inner_order = $requested_order;
331 { "$rs_attrs->{alias}.$_" }
333 $rs_attrs->{_rsroot_rsrc}->_identifying_column_set
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) );
342 # localise as we already have all the bind values we need
344 local $self->{order_bind};
345 $r{order_by_inner} = $self->_order_by ($inner_order);
348 for my $ch ($self->_order_by_chunks ($inner_order)) {
349 $ch = $ch->[0] if ref $ch eq 'ARRAY';
351 $ch =~ s/\s+ ( ASC|DESC ) \s* $//ix;
352 my $dir = uc ($1||'ASC');
354 push @out_chunks, \join (' ', $ch, $dir eq 'ASC' ? 'DESC' : 'ASC' );
357 $r{order_by_reversed} = $self->_order_by (\@out_chunks);
360 # this is the order supplement magic
361 $r{mid_sel} = $r{out_sel};
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
367 $r{in_sel} .= sprintf (', %s AS %s',
369 $extra_order_sel->{$extra_col},
372 $r{mid_sel} .= ', ' . $extra_order_sel->{$extra_col};
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}};
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 $_"
386 { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
387 grep { $_ !~ /[^\w\-]/ } # ignore functions
388 keys %$extra_order_sel
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}/
397 for ($r{order_by_reversed}, $r{order_by_requested});
401 # generate the rest of the sql
402 $r{grpby_having} = $self->_parse_rs_attrs ($rs_attrs);
404 $r{quoted_rs_alias} = $self->_quote ($rs_attrs->{alias});
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
419 Unreliable Top-based implementation, supported by B<< MSSQL < 2005 >>.
423 Due to its implementation, this limit dialect returns B<incorrect results>
424 when $limit+$offset > total amount of rows in the resultset.
429 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
431 my %l = %{ $self->_prep_for_skimming_limit($sql, $rs_attrs) };
433 $sql = sprintf ('SELECT TOP %u %s %s %s %s',
434 $rows + ($offset||0),
441 $sql = sprintf ('SELECT TOP %u %s FROM ( %s ) %s %s',
446 $l{order_by_reversed},
449 $sql = sprintf ('SELECT TOP %u %s FROM ( %s ) %s %s',
454 $l{order_by_requested},
455 ) if ( ($offset && $l{order_by_requested}) || ($l{mid_sel} ne $l{out_sel}) );
467 ) ORDER BY $reversed_original_order
468 FETCH FIRST $limit ROWS ONLY
469 ) ORDER BY $original_order
470 FETCH FIRST $limit ROWS ONLY
473 Unreliable FetchFirst-based implementation, supported by B<< IBM DB2 <= V5R3 >>.
477 Due to its implementation, this limit dialect returns B<incorrect results>
478 when $limit+$offset > total amount of rows in the resultset.
483 my ( $self, $sql, $rs_attrs, $rows, $offset ) = @_;
485 my %l = %{ $self->_prep_for_skimming_limit($sql, $rs_attrs) };
487 $sql = sprintf ('SELECT %s %s %s %s FETCH FIRST %u ROWS ONLY',
492 $rows + ($offset||0),
495 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s FETCH FIRST %u ROWS ONLY',
499 $l{order_by_reversed},
503 $sql = sprintf ('SELECT %s FROM ( %s ) %s %s FETCH FIRST %u ROWS ONLY',
507 $l{order_by_requested},
509 ) if ( ($offset && $l{order_by_requested}) || ($l{mid_sel} ne $l{out_sel}) );
514 =head2 RowCountOrGenericSubQ
516 This is not exactly a limit dialect, but more of a proxy for B<Sybase ASE>.
517 If no $offset is supplied the limit is simply performed as:
523 Otherwise we fall back to L</GenericSubQ>
527 sub _RowCountOrGenericSubQ {
529 my ($sql, $rs_attrs, $rows, $offset) = @_;
531 return $self->_GenericSubQ(@_) if $offset;
533 return sprintf <<"EOF", $rows, $sql;
546 SELECT COUNT(*) FROM $original_table cnt WHERE cnt.id < $original_table.id
547 ) BETWEEN $offset AND ($offset+$rows-1)
549 This is the most evil limit "dialect" (more of a hack) for I<really> stupid
550 databases. It works by ordering the set by some unique column, and calculating
551 the amount of rows that have a less-er value (thus emulating a L</RowNum>-like
552 index). Of course this implies the set can only be ordered by a single unique
553 column. Also note that this technique can be and often is B<excruciatingly
556 Currently used by B<Sybase ASE>, due to lack of any other option.
560 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
562 my $root_rsrc = $rs_attrs->{_rsroot_rsrc};
563 my $root_tbl_name = $root_rsrc->name;
565 my ($order_by, @rest) = do {
566 local $self->{quote_char};
567 $self->_order_by_chunks ($rs_attrs->{order_by})
577 ( ref $order_by eq 'ARRAY' and @$order_by == 1 )
580 $self->throw_exception (
581 'Generic Subquery Limit does not work on resultsets without an order, or resultsets '
582 . 'with complex order criteria (multicolumn and/or functions). Provide a single, '
583 . 'unique-column order criteria.'
587 ($order_by) = @$order_by if ref $order_by;
589 $order_by =~ s/\s+ ( ASC|DESC ) \s* $//ix;
590 my $direction = lc ($1 || 'asc');
592 my ($unq_sort_col) = $order_by =~ /(?:^|\.)([^\.]+)$/;
594 my $inf = $root_rsrc->storage->_resolve_column_info (
595 $rs_attrs->{from}, [$order_by, $unq_sort_col]
598 my $ord_colinfo = $inf->{$order_by} || $self->throw_exception("Unable to determine source of order-criteria '$order_by'");
600 if ($ord_colinfo->{-result_source}->name ne $root_tbl_name) {
601 $self->throw_exception(sprintf
602 "Generic Subquery Limit order criteria can be only based on the root-source '%s'"
603 . " (aliased as '%s')", $root_rsrc->source_name, $rs_attrs->{alias},
607 # make sure order column is qualified
608 $order_by = "$rs_attrs->{alias}.$order_by"
609 unless $order_by =~ /^$rs_attrs->{alias}\./;
612 my $ucs = { $root_rsrc->unique_constraints };
613 for (values %$ucs ) {
614 if (@$_ == 1 && "$rs_attrs->{alias}.$_->[0]" eq $order_by) {
619 $self->throw_exception(
620 "Generic Subquery Limit order criteria column '$order_by' must be unique (no unique constraint found)"
623 my ($stripped_sql, $in_sel, $out_sel, $alias_map, $extra_order_sel)
624 = $self->_subqueried_limit_attrs ($sql, $rs_attrs);
626 my $cmp_op = $direction eq 'desc' ? '>' : '<';
627 my $count_tbl_alias = 'rownum__emulation';
629 my $order_sql = $self->_order_by (delete $rs_attrs->{order_by});
630 my $group_having_sql = $self->_parse_rs_attrs($rs_attrs);
632 # add the order supplement (if any) as this is what will be used for the outer WHERE
633 $in_sel .= ", $_" for keys %{$extra_order_sel||{}};
637 $rownum_cond = 'BETWEEN ? AND ?';
639 push @{$self->{limit_bind}},
640 [ $self->__offset_bindtype => $offset ],
641 [ $self->__total_bindtype => $offset + $rows - 1]
645 $rownum_cond = '< ?';
647 push @{$self->{limit_bind}},
648 [ $self->__rows_bindtype => $rows ]
655 SELECT $in_sel ${stripped_sql}${group_having_sql}
657 WHERE ( SELECT COUNT(*) FROM %s %s WHERE %s $cmp_op %s ) $rownum_cond
659 ", map { $self->_quote ($_) } (
663 "$count_tbl_alias.$unq_sort_col",
669 # !!! THIS IS ALSO HORRIFIC !!! /me ashamed
671 # Generates inner/outer select lists for various limit dialects
672 # which result in one or more subqueries (e.g. RNO, Top, RowNum)
673 # Any non-root-table columns need to have their table qualifier
674 # turned into a column alias (otherwise names in subqueries clash
675 # and/or lose their source table)
677 # Returns mangled proto-sql, inner/outer strings of SQL QUOTED selectors
678 # with aliases (to be used in whatever select statement), and an alias
679 # index hashref of QUOTED SEL => QUOTED ALIAS pairs (to maybe be used
680 # for string-subst higher up).
681 # If an order_by is supplied, the inner select needs to bring out columns
682 # used in implicit (non-selected) orders, and the order condition itself
683 # needs to be realiased to the proper names in the outer query. Thus we
684 # also return a hashref (order doesn't matter) of QUOTED EXTRA-SEL =>
685 # QUOTED ALIAS pairs, which is a list of extra selectors that do *not*
686 # exist in the original select list
687 sub _subqueried_limit_attrs {
688 my ($self, $proto_sql, $rs_attrs) = @_;
690 $self->throw_exception(
691 'Limit dialect implementation usable only in the context of DBIC (missing $rs_attrs)'
692 ) unless ref ($rs_attrs) eq 'HASH';
694 # mangle the input sql as we will be replacing the selector entirely
696 $rs_attrs->{_selector_sql}
698 $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{_selector_sql}//ix
700 $self->throw_exception("Unrecognizable SELECT: $proto_sql");
703 my ($re_sep, $re_alias) = map { quotemeta $_ } ( $self->{name_sep}, $rs_attrs->{alias} );
705 # insulate from the multiple _recurse_fields calls below
706 local $self->{select_bind};
708 # correlate select and as, build selection index
709 my (@sel, $in_sel_index);
710 for my $i (0 .. $#{$rs_attrs->{select}}) {
712 my $s = $rs_attrs->{select}[$i];
713 my $sql_sel = $self->_recurse_fields ($s);
714 my $sql_alias = (ref $s) eq 'HASH' ? $s->{-as} : undef;
719 local $self->{quote_char};
720 $self->_recurse_fields ($s);
727 $self->throw_exception("Select argument $i ($s) without corresponding 'as'")
731 $in_sel_index->{$sql_sel}++;
732 $in_sel_index->{$self->_quote ($sql_alias)}++ if $sql_alias;
734 # record unqualified versions too, so we do not have
735 # to reselect the same column twice (in qualified and
737 if (! ref $s && $sql_sel =~ / $re_sep (.+) $/x) {
738 $in_sel_index->{$1}++;
743 # re-alias and remove any name separators from aliases,
744 # unless we are dealing with the current source alias
745 # (which will transcend the subqueries as it is necessary
746 # for possible further chaining)
747 my (@in_sel, @out_sel, %renamed);
748 for my $node (@sel) {
750 $node->{as} =~ / (?<! ^ $re_alias ) \. /x
752 $node->{unquoted_sql} =~ / (?<! ^ $re_alias ) $re_sep /x
754 $node->{as} = $self->_unqualify_colname($node->{as});
755 my $quoted_as = $self->_quote($node->{as});
756 push @in_sel, sprintf '%s AS %s', $node->{sql}, $quoted_as;
757 push @out_sel, $quoted_as;
758 $renamed{$node->{sql}} = $quoted_as;
761 push @in_sel, $node->{sql};
762 push @out_sel, $self->_quote ($node->{as});
765 # see if the order gives us anything
767 for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) {
769 $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY';
770 $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix;
772 next if $in_sel_index->{$chunk};
774 $extra_order_sel{$chunk} ||= $self->_quote (
775 'ORDER__BY__' . scalar keys %extra_order_sel
781 (map { join (', ', @$_ ) } (
786 keys %extra_order_sel ? \%extra_order_sel : (),
790 sub _unqualify_colname {
791 my ($self, $fqcn) = @_;
792 $fqcn =~ s/ \. /__/xg;
800 See L<DBIx::Class/CONTRIBUTORS>.
804 You may distribute this code under the same terms as Perl itself.