10 my $schema = DBICTest->init_schema();
13 my $debugcb = sub { $queries++; };
14 my $orig_debug = $schema->storage->debug;
17 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
23 order_by => 'me.trackid',
28 my $use_prefetch = $no_prefetch->search(
35 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
37 scalar ($use_prefetch->all),
38 scalar ($no_prefetch->all),
39 "Amount of returned rows is right"
42 }, 'search_related prefetch with order_by works');
45 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
56 my $use_prefetch = $no_prefetch->search(
64 scalar ($use_prefetch->all),
65 scalar ($no_prefetch->all),
66 "Amount of returned rows is right"
68 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
70 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
73 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
74 ->search_related('artwork_to_artist')->search_related('artist',
76 { prefetch => 'cds' },
78 is($rs->all, 0, 'prefetch without WHERE (objects)');
79 is($rs->count, 0, 'prefetch without WHERE (count)');
81 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
82 ->search_related('artwork_to_artist')->search_related('artist',
83 { 'cds.title' => 'foo' },
84 { prefetch => 'cds' },
86 is($rs->all, 0, 'prefetch with WHERE (objects)');
87 is($rs->count, 0, 'prefetch with WHERE (count)');
90 # test where conditions at the root of the related chain
91 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
92 my $artist = $artist_rs->next;
93 $artist->create_related ('cds', $_) for (
95 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
98 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
102 $rs = $artist_rs->search_related('cds')->search_related('genre',
103 { 'genre.name' => 'vague genre' },
104 { prefetch => 'cds' },
106 is($rs->all, 1, 'base without distinct (objects)');
107 is($rs->count, 1, 'base without distinct (count)');
108 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
109 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
110 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
113 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
114 { 'genre.name' => 'vague genre' },
116 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
117 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
119 $rs = $rs->search ({}, { distinct => 1} );
120 is($rs->all, 1, 'distinct without prefetch (objects)');
121 is($rs->count, 1, 'distinct without prefetch (count)');
124 $rs = $artist_rs->search_related('cds')->search_related('genre',
125 { 'genre.name' => 'vague genre' },
126 { prefetch => 'cds', distinct => 1 },
128 is($rs->all, 1, 'distinct with prefetch (objects)');
129 is($rs->count, 1, 'distinct with prefetch (count)');
132 local $TODO = "This makes another 2 trips to the database, it can't be right";
135 $schema->storage->debugcb ($debugcb);
136 $schema->storage->debug (1);
138 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
139 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
140 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
142 is ($queries, 0, 'No extra queries fired (prefetch survives search_related)');
144 $schema->storage->debugcb (undef);
145 $schema->storage->debug ($orig_debug);
148 }, 'distinct generally works with prefetch on deep search_related chains');