10 my $schema = DBICTest->init_schema();
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
19 order_by => 'me.trackid',
24 my $use_prefetch = $no_prefetch->search(
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
38 }, 'search_related prefetch with order_by works');
40 TODO: { local $TODO = 'Unqualified columns in where clauses can not be fixed without an SQLA rewrite' if SQL::Abstract->VERSION < 2;
42 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
53 my $use_prefetch = $no_prefetch->search(
61 scalar ($use_prefetch->all),
62 scalar ($no_prefetch->all),
63 "Amount of returned rows is right"
65 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
67 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
72 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
73 ->search_related('artwork_to_artist')->search_related('artist',
75 { prefetch => 'cds' },
77 is($rs->all, 0, 'prefetch without WHERE (objects)');
78 is($rs->count, 0, 'prefetch without WHERE (count)');
80 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
81 ->search_related('artwork_to_artist')->search_related('artist',
82 { 'cds.title' => 'foo' },
83 { prefetch => 'cds' },
85 is($rs->all, 0, 'prefetch with WHERE (objects)');
86 is($rs->count, 0, 'prefetch with WHERE (count)');
89 # test where conditions at the root of the related chain
90 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
91 my $artist = $artist_rs->next;
92 $artist->create_related ('cds', $_) for (
94 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
97 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
101 $rs = $artist_rs->search_related('cds')->search_related('genre',
102 { 'genre.name' => 'vague genre' },
103 { prefetch => 'cds' },
105 is($rs->all, 1, 'base without distinct (objects)');
106 is($rs->count, 1, 'base without distinct (count)');
107 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
108 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
109 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
112 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
113 { 'genre.name' => 'vague genre' },
115 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
116 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
118 $rs = $rs->search ({}, { distinct => 1} );
119 is($rs->all, 1, 'distinct without prefetch (objects)');
120 is($rs->count, 1, 'distinct without prefetch (count)');
123 $rs = $artist_rs->search_related('cds')->search_related('genre',
124 { 'genre.name' => 'vague genre' },
125 { prefetch => 'cds', distinct => 1 },
127 is($rs->all, 1, 'distinct with prefetch (objects)');
128 is($rs->count, 1, 'distinct with prefetch (count)');
131 local $TODO = "This makes another 2 trips to the database, it can't be right";
132 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
133 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
134 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
137 }, 'distinct generally works with prefetch on deep search_related chains');