Add single() ro RSC
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / SQLAHacks / MSSQL.pm
CommitLineData
ac93965c 1package # Hide from PAUSE
2 DBIx::Class::SQLAHacks::MSSQL;
3
4use base qw( DBIx::Class::SQLAHacks );
5use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
6
7#
8# MSSQL is retarded wrt TOP (crappy limit) and ordering.
9# One needs to add a TOP to *all* ordered subqueries, if
10# TOP has been used in the statement at least once.
11# Do it here.
12#
13sub select {
14 my $self = shift;
15
16 my ($sql, @bind) = $self->SUPER::select (@_);
17
18 # ordering was requested and there are at least 2 SELECT/FROM pairs
19 # (thus subquery), and there is no TOP specified
20 if (
21 $sql =~ /\bSELECT\b .+? \bFROM\b .+? \bSELECT\b .+? \bFROM\b/isx
22 &&
23 $sql !~ /^ \s* SELECT \s+ TOP \s+ \d+ /xi
24 &&
25 scalar $self->_order_by_chunks ($_[3]->{order_by})
26 ) {
27 $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi;
28 }
29
30 return wantarray ? ($sql, @bind) : $sql;
31}
32
331;