if (
( $attrs->{rows} && keys %{$attrs->{collapse}} )
||
- ( $attrs->{group_by} && @{$attrs->{group_by}} &&
- $attrs->{_prefetch_select} && @{$attrs->{_prefetch_select}} )
+ ( $attrs->{group_by}
+ &&
+ @{$attrs->{group_by}}
+ &&
+ $attrs->{_prefetch_select}
+ &&
+ @{$attrs->{_prefetch_select}}
+ )
) {
+
($ident, $select, $where, $attrs)
= $self->_adjust_select_args_for_complex_prefetch ($ident, $select, $where, $attrs);
}
+
+ elsif (
+ ($attrs->{rows} || $attrs->{offset})
+ &&
+ ($sql_maker->limit_dialect eq 'RowNumberOver' || $sql_maker->limit_dialect eq 'Top' )
+ &&
+ (ref $ident eq 'ARRAY' && @$ident > 1) # indicates a join
+ &&
+ scalar $sql_maker->_order_by_chunks ($attrs->{order_by})
+ ) {
+ # the two limit dialects above mangle the SQL such that the join gets lost
+ # wrap a subquery here
+
+ push @limit, delete @{$attrs}{qw/rows offset/};
+
+ my $subq = $self->_select_args_to_query (
+ $ident,
+ $select,
+ $where,
+ $attrs,
+ );
+
+ $ident = {
+ -alias => $attrs->{alias},
+ -source_handle => $ident->[0]{-source_handle},
+ $attrs->{alias} => $subq,
+ };
+
+ # all part of the subquery now
+ delete @{$attrs}{qw/order_by group_by having/};
+ $where = undef;
+ }
+
elsif (! $attrs->{software_limit} ) {
push @limit, $attrs->{rows}, $attrs->{offset};
}
sub _select_args_to_query {
my $self = shift;
- # _select_args does some shady action at a distance
- # see DBI.pm for more info
- my $sql_maker = $self->sql_maker;
- my ($op, $bind, $ident, $bind_attrs, $select, $cond, $order, $rows, $offset);
- {
- local $sql_maker->{_dbic_rs_attrs};
- ($op, $bind, $ident, $bind_attrs, $select, $cond, $order, $rows, $offset) = $self->_select_args(@_);
- }
+ my ($sql, $prep_bind, @rest) = $self->next::method (@_);
- if (
- ($rows || $offset)
- ||
- not scalar $sql_maker->_order_by_chunks ($order->{order_by})
- ) {
- # either limited RS or no ordering, just short circuit
- return $self->next::method (@_);
+ # see if this is an ordered subquery
+ my $attrs = $_[3];
+ if ( scalar $self->sql_maker->_order_by_chunks ($attrs->{order_by}) ) {
+ $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi;
}
- my ($sql, $prep_bind, @rest) = $self->next::method (@_);
- $sql =~ s/^ \s* SELECT \s/SELECT TOP 100 PERCENT /xi;
-
return wantarray
? ($sql, $prep_bind, @rest)
: \[ "($sql)", @$prep_bind ]
prefetch => 'owner',
rows => 2, # 3 results total
order_by => { -desc => 'owner' },
- # there is no sane way to order by the right side of a grouped prefetch currently :(
- #order_by => { -desc => 'owner.name' },
},
);
is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
}
+# make sure right-join-side ordering limit works
+{
+ my $rs = $schema->resultset ('BooksInLibrary')->search (
+ {
+ 'owner.name' => [qw/wiggle woggle/],
+ },
+ {
+ join => 'owner',
+ order_by => { -desc => 'owner.name' },
+ }
+ );
+
+ is ($rs->all, 3, 'Correct amount of objects from right-sorted joined resultset');
+ my $limited_rs = $rs->search ({}, {rows => 2, offset => 2});
+ is ($limited_rs->count, 1, 'Correct count of limited right-sorted joined resultset');
+ is ($limited_rs->count_rs->next, 1, 'Correct count_rs of limited right-sorted joined resultset');
+ is ($limited_rs->all, 1, 'Correct amount of objects from limited right-sorted joined resultset');
+}
+
done_testing;
# clean up our mess