$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);
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) {
$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";
SELECT TOP $rows $outer_select FROM
(
$sql
- ) AS inner_sel
+ ) AS me
$order_by_outer
SQL
$sql = <<"SQL";
SELECT $outer_select FROM
- ( $sql ) AS outer_sel
+ ( $sql ) AS me
$order_by_requested;
SQL
]);
#
-# 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 ({
'(
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]
)',
[],
);
'(
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' ],