Revision history for DBIx::Class
+ - Fix issue where the query was becoming overly mangled when trying
+ to use pagination with a query that has a sub-select in the WHERE
+ clause.
+
0.08192 2011-05-10 04:20 (UTC)
* Fixes
- Fix serious regression on SQLite, corrupting data when an alphanum
}
;
- $sql = $self->$limiter ($sql, $rs_attrs, $limit, $offset);
+ $sql = $self->$limiter (
+ $sql,
+ { %{$rs_attrs||{}}, _selector_sql => $fields },
+ $limit,
+ $offset
+ );
}
else {
($sql, @bind) = $self->next::method ($table, $fields, $where, $rs_attrs);
'Limit dialect implementation usable only in the context of DBIC (missing $rs_attrs)'
) unless ref ($rs_attrs) eq 'HASH';
- # mangle the input sql as we will be replacing the selector
- $proto_sql =~ s/^ \s* SELECT \s+ .+ \s+ (?= \b FROM \b )//ix
- or $self->throw_exception("Unrecognizable SELECT: $proto_sql");
+ # mangle the input sql as we will be replacing the selector entirely
+ unless (
+ $rs_attrs->{_selector_sql}
+ and
+ $proto_sql =~ s/^ \s* SELECT \s* \Q$rs_attrs->{_selector_sql}//ix
+ ) {
+ $self->throw_exception("Unrecognizable SELECT: $proto_sql");
+ }
my ($re_sep, $re_alias) = map { quotemeta $_ } ( $self->{name_sep}, $rs_attrs->{alias} );
);
}
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'books.owner' => { -ident => 'owner.id' },
+}, { alias => 'owner', select => ['id'] } )->count_rs;
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search( { -exists => $subq->as_query }, { select => ['id','owner'], rows => 1 } );
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT [id], [owner] FROM (
+ SELECT [id], [owner], ROW_NUMBER() OVER( ) AS [rno__row__index] FROM (
+ SELECT [me].[id], [me].[owner]
+ FROM [books] [me]
+ WHERE ( ( (EXISTS (
+ SELECT COUNT( * ) FROM [owners] [owner] WHERE ( [books].[owner] = [owner].[id] )
+ )) AND [source] = ? ) )
+ ) [me]
+ ) [me] WHERE [rno__row__index] >= ? AND [rno__row__index] <= ?
+ )',
+ [
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ [ $OFFSET => 1 ],
+ [ $TOTAL => 1 ],
+ ],
+ 'Pagination with sub-query in WHERE works'
+);
+
+}
+
done_testing;
);
}
+{
+my $subq = $s->resultset('Owners')->search({
+ 'books.owner' => { -ident => 'owner.id' },
+}, { alias => 'owner', select => ['id'] } )->count_rs;
+
+my $rs_selectas_rel = $s->resultset('BooksInLibrary')->search( { -exists => $subq->as_query }, { select => ['id','owner'], rows => 1 } );
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '( SELECT id, owner FROM (
+ SELECT me.id, me.owner
+ FROM books me
+ WHERE ( ( (EXISTS (SELECT COUNT( * ) FROM owners owner WHERE ( books.owner = owner.id ))) AND source = ? ) )
+ ) me WHERE ROWNUM <= ?
+ )',
+ [
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ [ $TOTAL => 1 ],
+ ],
+ 'Pagination with sub-query in WHERE works'
+);
+
+}
+
done_testing;
);
}
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'books.owner' => { -ident => 'owner.id' },
+}, { alias => 'owner', select => ['id'] } )->count_rs;
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search( { -exists => $subq->as_query }, { select => ['id','owner'], rows => 1 } );
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(SELECT TOP 1 me.id, me.owner FROM books me WHERE ( ( (EXISTS (SELECT COUNT( * ) FROM owners owner WHERE ( books.owner = owner.id ))) AND source = ? ) ) ORDER BY me.id)',
+ [
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+ 'Pagination with sub-query in WHERE works'
+);
+
+}
+
done_testing;