ee2e4cfb5db61df137e0f651308484d3260bca13
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks / MSSQL.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLAHacks::MSSQL;
3
4 use warnings;
5 use strict;
6
7 use base qw( DBIx::Class::SQLAHacks );
8 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
9
10 # an MSSQL-specific implementation of the Row-Number-Over limiting
11 # technique
12
13 sub _MSRowNumberOver {
14   my ($self, $sql, $order, $rows, $offset ) = @_;
15
16   # get the order_by only (or make up an order if none exists)
17   my $order_by = $self->_order_by(
18     (delete $order->{order_by}) || \ '(SELECT (1))'
19   );
20
21   # whatever is left
22   my $group_having = $self->_order_by($order);
23
24   $sql = sprintf (<<'EOS', $order_by, $sql, $group_having, $offset + 1, $offset + $rows, );
25
26 SELECT * FROM (
27   SELECT orig_query.*, ROW_NUMBER() OVER(%s ) AS rno__row__index FROM (%s%s) orig_query
28 ) rno_subq WHERE rno__row__index BETWEEN %d AND %d
29
30 EOS
31
32   $sql =~ s/\s*\n\s*/ /g;   # easier to read in the debugger
33   return $sql;
34 }
35
36 1;