From: Peter Rabbitson Date: Thu, 3 Dec 2009 09:03:18 +0000 (+0000) Subject: The correct top100 mssql solution and test X-Git-Tag: v0.08116~103^2~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f0bd60fc51329ed3c6cd16de4157477e075d4058;p=dbsrgits%2FDBIx-Class.git The correct top100 mssql solution and test --- diff --git a/lib/DBIx/Class/Storage/DBI/MSSQL.pm b/lib/DBIx/Class/Storage/DBI/MSSQL.pm index 94597ab..4add28c 100644 --- a/lib/DBIx/Class/Storage/DBI/MSSQL.pm +++ b/lib/DBIx/Class/Storage/DBI/MSSQL.pm @@ -178,6 +178,31 @@ sub _execute { sub last_insert_id { shift->_identity } +# +# MSSQL is retarded wrt ordered subselects. One needs to add a TOP 100% +# to *all* subqueries, do it here. +# +sub _select_args_to_query { + my $self = shift; + + my $sql_maker = $self->sql_maker; + my ($op, $bind, $ident, $bind_attrs, $select, $cond, $order) = $self->_select_args(@_); + + if (not scalar $sql_maker->_order_by_chunks ($order->{order_by}) ) { + # no ordering, just short circuit + return $self->next::method (@_); + } + + my ($sql, $bind, @rest) = $self->next::method (@_); + $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi; + + return wantarray + ? ($sql, $bind, @rest) + : \[ "($sql)", @$bind ] + ; +} + + # savepoint syntax is the same as in Sybase ASE sub _svp_begin { diff --git a/t/746mssql.t b/t/746mssql.t index f120c12..f474010 100644 --- a/t/746mssql.t +++ b/t/746mssql.t @@ -249,6 +249,20 @@ lives_ok ( sub { ]); }, 'populate without PKs supplied ok' ); +# make sure ordered subselects work +{ + my $book_owner_ids = $schema->resultset ('BooksInLibrary') + ->search ({}, { join => 'owner', distinct => 1, order_by => { -desc => 'owner'} }) + ->get_column ('owner'); + + my $owners = $schema->resultset ('Owners')->search ({ + id => { -in => $book_owner_ids->as_query } + }); + + is ($owners->count, 8, 'Correct amount of book owners'); + is ($owners->all, 8, 'Correct amount of book owner objects'); +} + # # try a prefetch on tables with identically named columns #