From: Arthur Axel "fREW" Schmidt Date: Thu, 3 Dec 2009 01:48:36 +0000 (+0000) Subject: Switch to RowNumberOver for MSSQL X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d2a1bb56d0b2ce3901280b031db971046958abc;p=dbsrgits%2FDBIx-Class-Historic.git Switch to RowNumberOver for MSSQL --- diff --git a/lib/DBIx/Class/SQLAHacks/MSSQL.pm b/lib/DBIx/Class/SQLAHacks/MSSQL.pm index 1b18b1e..6110c8d 100644 --- a/lib/DBIx/Class/SQLAHacks/MSSQL.pm +++ b/lib/DBIx/Class/SQLAHacks/MSSQL.pm @@ -4,30 +4,13 @@ package # Hide from PAUSE use base qw( DBIx::Class::SQLAHacks ); use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; -# -# MSSQL is retarded wrt TOP (crappy limit) and ordering. -# One needs to add a TOP to *all* ordered subqueries, if -# TOP has been used in the statement at least once. -# Do it here. -# -sub select { - my $self = shift; - - my ($sql, @bind) = $self->SUPER::select (@_); - - # ordering was requested and there are at least 2 SELECT/FROM pairs - # (thus subquery), and there is no TOP specified - if ( - $sql =~ /\bSELECT\b .+? \bFROM\b .+? \bSELECT\b .+? \bFROM\b/isx - && - $sql !~ /^ \s* SELECT \s+ TOP \s+ \d+ /xi - && - scalar $self->_order_by_chunks ($_[3]->{order_by}) - ) { - $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi; - } - - return wantarray ? ($sql, @bind) : $sql; +sub _RowNumberOver { + my $self = shift; + my $sql = $self->SUPER::_RowNumberOver(@_); + $sql =~ s/(\s*)SELECT\s Q1\.\*,\s ROW_NUMBER\(\)\s OVER\(\s \)\s AS\s ROW_NUM\s + FROM\s \(\n(\s*.*)\n\s*(.*)\n\s*\)\s Q1 + /$1SELECT Q1.*, ROW_NUMBER() OVER($3) AS ROW_NUM FROM (\n$2\n) Q1/ixm; + return $sql; } 1; diff --git a/lib/DBIx/Class/Storage/DBI/MSSQL.pm b/lib/DBIx/Class/Storage/DBI/MSSQL.pm index 2db2af7..94597ab 100644 --- a/lib/DBIx/Class/Storage/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Storage/DBI/MSSQL.pm @@ -212,7 +212,7 @@ sub _sql_maker_opts { $self->{_sql_maker_opts} = { %$opts }; } - return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} }; + return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} }; } 1;