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