my ( $order_by_inner, $order_by_outer ) = $self->_order_directions($limit_order);
my $order_by_requested = $self->_order_by ($req_order);
+ # generate the rest
+ delete $order->{$_} for qw/order_by _virtual_order_by/;
+ my $grpby_having = $self->_order_by ($order);
+
+ # short circuit for counts - the ordering complexity is needless
+ if ($self->{_dbic_rs_attrs}{-for_count_only}) {
+ 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;
}
-
- # generate the rest
- delete $order->{$_} for qw/order_by _virtual_order_by/;
- my $grpby_having = $self->_order_by ($order);
-
-
my $inner_lim = $rows + $offset;
$sql = "SELECT TOP $inner_lim $inner_select $sql $grpby_having $order_by_inner";
plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
unless ($dsn && $user);
-plan tests => 23;
+plan tests => 25;
my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
local $TODO = 'limit past end of resultset problem';
is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
+ is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
+
+ # make sure count does not become overly complex FIXME
+ is_same_sql_bind (
+ $owners->page(3)->count_rs->as_query,
+ '(
+ 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
+ )',
+ [],
+ );
}
- # make sure count does not become overly complex FIXME
- is_same_sql_bind (
- $owners->page(2)->count_rs->as_query,
- '(
- SELECT COUNT( * )
- FROM (
- SELECT TOP 3 id
- FROM (
- SELECT TOP 6 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 ASC
- ) AS inner_sel
- ORDER BY id DESC
- ) count_subq
- )',
- [],
- );
-
# try a ->belongs_to direction (no select collapse, group_by should work)
my $books = $schema->resultset ('BooksInLibrary')->search ({
'owner.name' => [qw/wiggle woggle/],
local $TODO = 'limit past end of resultset problem';
is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
+ is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
+
+ # make sure count does not become overly complex FIXME
+ is_same_sql_bind (
+ $books->page(2)->count_rs->as_query,
+ '(
+ 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
+ )',
+ [
+ [ 'owner.name' => 'wiggle' ],
+ [ 'owner.name' => 'woggle' ],
+ [ 'source' => 'Library' ],
+ ],
+ );
}
- # make sure count does not become overly complex FIXME
- is_same_sql_bind (
- $books->page(2)->count_rs->as_query,
- '(
- SELECT COUNT( * )
- FROM (
- SELECT TOP 2 id
- FROM (
- SELECT TOP 4 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 ASC
- ) AS inner_sel
- ORDER BY id DESC
- ) count_subq
- )',
- [
- [ 'owner.name' => 'wiggle' ],
- [ 'owner.name' => 'woggle' ],
- [ 'source' => 'Library' ],
- ],
- );
-
}
# clean up our mess