Subject: | Question about collapse attribute |
Hello.
I asked about the behavior of collapse attribute.
I shows the test code first.
# full version is here https://github.com/bokutin/p5-ex-schema
use Modern::Perl;
use Test::More;
use Ex1::Schema;
# CREATE TABLE room (
# id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
# name
# );
#
# CREATE TABLE desk (
# id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
# room_id INTEGER,
# name
# );
# CREATE INDEX idx_desk_room_id ON desk(room_id);
my $schema = do {
my $DB_FILE = "ex.sqlite";
system( "rm $DB_FILE && sqlite3 $DB_FILE < schema.sql" ) == 0 or die;
Ex1::Schema->connect("dbi:SQLite:dbname=$DB_FILE");
};
{
my $rs = $schema->resultset('Room');
$rs->create(
{
name => 'room1',
desks => [
{ name => 'desk1' },
{ name => 'desk2' },
{ name => 'desk3' },
],
},
);
is $rs->count, 1;
}
{
my $rs = $schema->resultset('Room')->search(
{
'desks.name' => [ qw(desk1 desk2) ],
},
{
join => 'desks',
}
);
is $rs->count, 2, "not collapsed if attrs has join";
is 0+($rs->all), 2, "fetch == count";
}
{
my $rs = $schema->resultset('Room')->search(
{
'desks.name' => [ qw(desk1 desk2) ],
},
{
prefetch => 'desks',
}
);
is $rs->count, 1, "auto collapsed if attrs has prefetch";
is 0+($rs->all), 1, "fetch == count";
}
{
my $rs = $schema->resultset('Room')->search(
{
'desks.name' => [ qw(desk1 desk2) ],
},
{
join => 'desks',
distinct => 1,
}
);
is $rs->count, 1, "collapsed if attrs has join & distinct";
is 0+($rs->all), 1, "fetch == count";
}
{
my $rs = $schema->resultset('Room')->search(
{
'desks.name' => [ qw(desk1 desk2) ],
},
{
join => 'desks',
collapse => 1,
}
);
is $rs->count, 1, "collapsed if attrs has join & collapse";
is 0+($rs->all), 2, "fetch != count! why??";
}
done_testing;
The last test is part of the question.
The case of a combination of collapse and join, collapse will be considered to count(), but collapse is not considered to all().
Looking at the code, I found the following.
% git checkout v0.08270
% vim
% git diff
diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm
index ffade21..9d14685 100644
--- a/lib/DBIx/Class/ResultSet.pm
+++ b/lib/DBIx/Class/ResultSet.pm
@@ -1407,7 +1407,7 @@ sub _construct_results {
) ? 1 : 0 ) unless defined $self->{_result_inflator}{is_hri};
- if (! $attrs->{_related_results_construction}) {
+ if (! $attrs->{_related_results_construction} and ! $attrs->{collapse}) {
# construct a much simpler array->hash folder for the one-table cases right here
if ($self->{_result_inflator}{is_hri}) {
for my $r (@$rows) {
With the above changes, collapse will be considered to all().
prove test also passes.
Behavior of the collapse attribute is a bug? Or it is a specification?
Cheers,
Tomohiro Hosaka