From: Peter Rabbitson Date: Sun, 21 Jun 2009 06:54:00 +0000 (+0000) Subject: Make sure quoting works X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=02d133f0f4c212e7842b869c26265f36cca18b77;p=dbsrgits%2FDBIx-Class-Historic.git Make sure quoting works --- diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index ba89264..7ff05d6 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -210,8 +210,12 @@ sub _Top { $order = { %$order }; #copy - my $req_order = [ $self->_order_by_chunks ($order->{order_by}) ]; - my $limit_order = [ @$req_order ? @$req_order : $self->_order_by_chunks ($order->{_virtual_order_by}) ]; + my $req_order = $order->{order_by}; + my $limit_order = + scalar $self->_order_by_chunks ($req_order) # exaime normalized version, collapses nesting + ? $req_order + : $order->{_virtual_order_by} + ; my ( $order_by_inner, $order_by_outer ) = $self->_order_directions($limit_order); my $order_by_requested = $self->_order_by ($req_order); @@ -225,7 +229,6 @@ sub _Top { return "SELECT TOP $rows $inner_select $sql $grpby_having $order_by_outer"; } - # we can't really adjust the order_by columns, as introspection is lacking # resort to simple substitution for my $col (keys %outer_col_aliases) { @@ -237,6 +240,7 @@ sub _Top { $order_by_inner =~ s/\s+$col\s+/$col_aliases{$col}/g; } + my $inner_lim = $rows + $offset; $sql = "SELECT TOP $inner_lim $inner_select $sql $grpby_having $order_by_inner"; @@ -247,7 +251,7 @@ sub _Top { SELECT TOP $rows $outer_select FROM ( $sql - ) AS inner_sel + ) AS me $order_by_outer SQL @@ -257,7 +261,7 @@ SQL $sql = <<"SQL"; SELECT $outer_select FROM - ( $sql ) AS outer_sel + ( $sql ) AS me $order_by_requested; SQL diff --git a/t/746mssql.t b/t/746mssql.t index a958986..bae2e7c 100644 --- a/t/746mssql.t +++ b/t/746mssql.t @@ -134,9 +134,13 @@ $schema->populate ('BooksInLibrary', [ ]); # -# try a distinct + prefetch on tables with identically named columns +# try a prefetch on tables with identically named columns # +# set quote char - make sure things work while quoted +$schema->storage->_sql_maker->{quote_char} = [qw/[ ]/]; +$schema->storage->_sql_maker->{name_sep} = '.'; + { # try a ->has_many direction (group_by is not possible on has_many with limit) my $owners = $schema->resultset ('Owners')->search ({ @@ -162,13 +166,13 @@ $schema->populate ('BooksInLibrary', [ '( SELECT COUNT( * ) FROM ( - SELECT TOP 3 me.id - FROM owners me - LEFT JOIN books books ON books.owner = me.id - WHERE ( books.id IS NOT NULL ) - GROUP BY me.id - ORDER BY me.id DESC - ) count_subq + SELECT TOP 3 [me].[id] + FROM [owners] [me] + LEFT JOIN [books] [books] ON [books].[owner] = [me].[id] + WHERE ( [books].[id] IS NOT NULL ) + GROUP BY [me].[id] + ORDER BY [me].[id] DESC + ) [count_subq] )', [], ); @@ -200,13 +204,13 @@ $schema->populate ('BooksInLibrary', [ '( SELECT COUNT( * ) FROM ( - SELECT TOP 2 me.id - FROM books me - JOIN owners owner ON owner.id = me.owner - WHERE ( ( ( owner.name = ? OR owner.name = ? ) AND source = ? ) ) - GROUP BY me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name - ORDER BY me.id DESC - ) count_subq + SELECT TOP 2 [me].[id] + FROM [books] [me] + JOIN [owners] [owner] ON [owner].[id] = [me].[owner] + WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) ) + GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[id], [owner].[name] + ORDER BY [me].[id] DESC + ) [count_subq] )', [ [ 'owner.name' => 'wiggle' ],