[Bast-commits] r8033 - in
DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class:
SQLAHacks Storage/DBI
ribasushi at dev.catalyst.perl.org
ribasushi at dev.catalyst.perl.org
Fri Dec 4 11:07:05 GMT 2009
Author: ribasushi
Date: 2009-12-04 11:07:05 +0000 (Fri, 04 Dec 2009)
New Revision: 8033
Modified:
DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/SQLAHacks/MSSQL.pm
DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm
Log:
Add support for unordered limited resultsets
Rename the limit helper to signify it is MS specific
Make sure we don't lose group_by/having clauses
Modified: DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/SQLAHacks/MSSQL.pm
===================================================================
--- DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/SQLAHacks/MSSQL.pm 2009-12-04 10:47:22 UTC (rev 8032)
+++ DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/SQLAHacks/MSSQL.pm 2009-12-04 11:07:05 UTC (rev 8033)
@@ -1,27 +1,45 @@
package # Hide from PAUSE
DBIx::Class::SQLAHacks::MSSQL;
+use warnings;
+use strict;
+
use base qw( DBIx::Class::SQLAHacks );
use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
-sub _RowNumberOver {
+# an MSSQL-specific implementation of the Row-Number-Over limiting
+# technique
+
+sub _MSRowNumberOver {
my ($self, $sql, $order, $rows, $offset ) = @_;
- $offset += 1;
- my $last = $rows + $offset - 1;
- my ( $order_by ) = $self->_order_by( $order );
+ # get the order_by only
+ my $order_by = $self->_order_by(
+ (delete $order->{order_by}) || do {
- $sql = <<"SQL";
-SELECT * FROM
-(
- SELECT Q1.*, ROW_NUMBER() OVER( $order_by ) AS ROW_NUM FROM (
- $sql
- ) Q1
-) Q2
-WHERE ROW_NUM BETWEEN $offset AND $last
+ # no order was supplied - make something up:
+ my $rsrc = $self->{_dbic_rs_attrs}{_source_handle}->resolve;
+ if (my @pk = $rsrc->primary_columns) {
+ \@pk;
+ }
+ else {
+ [($rsrc->columns)[0]];
+ }
+ }
+ );
-SQL
+ # whatever is left
+ my $group_having = $self->_order_by($order);
+ $sql = sprintf (<<'EOS', $order_by, $sql, $group_having, $offset + 1, $offset + $rows, );
+
+SELECT * FROM (
+ SELECT orig_query.*, ROW_NUMBER() OVER(%s ) AS rno__row__index FROM (%s%s) orig_query
+) rno_subq WHERE rno__row__index BETWEEN %d AND %d
+
+EOS
+
+ $sql =~ s/\s*\n\s*/ /g; # easier to read in the debugger
return $sql;
}
Modified: DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm
===================================================================
--- DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm 2009-12-04 10:47:22 UTC (rev 8032)
+++ DBIx-Class/0.08/branches/mssql_rno_pagination/lib/DBIx/Class/Storage/DBI/MSSQL.pm 2009-12-04 11:07:05 UTC (rev 8033)
@@ -247,7 +247,7 @@
$self->{_sql_maker_opts} = { %$opts };
}
- return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} };
+ return { limit_dialect => 'MSRowNumberOver', %{$self->{_sql_maker_opts}||{}} };
}
1;
More information about the Bast-commits
mailing list