8 # MASSIVE FIXME - there is a hole in ::RSC / as_subselect_rs
9 # losing the order. Needs a rework/extract of the realiaser,
10 # and that's a whole another bag of dicks
11 BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
13 use DBICTest::Schema::CD;
15 # the default scalarref table name will not work well for this test
16 DBICTest::Schema::CD->table('cd');
21 my $schema = DBICTest->init_schema;
23 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
25 my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
26 [qw/foo bar hello goodbye sensors read_count/],
32 # This is already provided by DBICTest
33 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
38 my ($ta, $tb) = $schema->resultset ('TwoKeys')
39 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ], { order_by => 'artist' })
42 my $tkfk_cnt = $tkfks->count;
44 my $non_void_ctx = $tkfks->populate ([
45 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
46 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
47 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
48 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
50 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
53 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
56 # create a resultset matching $fa and $fb only
57 my $fks = $schema->resultset ('FourKeys')->search (
59 sensors => { '!=', 'c' },
60 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
61 }, { join => { fourkeys_to_twokeys => 'twokeys' }}
64 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
65 $schema->is_executed_sql_bind( sub {
66 $fks->update ({ read_count => \ 'read_count + 1' })
69 SET read_count = read_count + 1
70 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
74 ]], 'Correct update-SQL with multijoin with pruning' );
76 is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
77 is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
78 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
80 # make the multi-join stick
81 my $fks_multi = $fks->search(
82 { 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } },
83 { order_by => [ $fks->result_source->primary_columns ] },
85 $schema->is_executed_sql_bind( sub {
86 $fks_multi->update ({ read_count => \ 'read_count + 1' })
90 'SELECT me.foo, me.bar, me.hello, me.goodbye
92 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
93 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
94 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
95 GROUP BY me.foo, me.bar, me.hello, me.goodbye
96 ORDER BY foo, bar, hello, goodbye
105 SET read_count = read_count + 1
106 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
108 ( (1) x 4, (2) x 4 ),
111 ], 'Correct update-SQL with multijoin without pruning' );
113 is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
114 is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
115 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
117 $schema->is_executed_sql_bind( sub {
118 my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
119 ok ($res, 'operation is true');
120 cmp_ok ($res, '==', 0, 'zero rows affected');
124 'SELECT me.foo, me.bar, me.hello, me.goodbye
126 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
127 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
128 WHERE "blah" = "bleh" AND ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
129 GROUP BY me.foo, me.bar, me.hello, me.goodbye
130 ORDER BY foo, bar, hello, goodbye
138 ], 'Correct null-delete-SQL with multijoin without pruning' );
141 # try the same sql with forced multicolumn in
142 $schema->is_executed_sql_bind( sub {
143 local $schema->storage->{_use_multicolumn_in} = 1;
145 # this can't actually execute on sqlite
146 eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
149 SET read_count = read_count + 1
151 (foo, bar, hello, goodbye) IN (
152 SELECT me.foo, me.bar, me.hello, me.goodbye
154 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
155 fourkeys_to_twokeys.f_bar = me.bar
156 AND fourkeys_to_twokeys.f_foo = me.foo
157 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
158 AND fourkeys_to_twokeys.f_hello = me.hello
159 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
160 ORDER BY foo, bar, hello, goodbye
168 ]], 'Correct update-SQL with multicolumn in support' );
170 $schema->is_executed_sql_bind( sub {
171 $fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
175 'SELECT me.foo, me.bar, me.hello, me.goodbye
177 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
178 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
179 LEFT JOIN twokeys twokeys
180 ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
181 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
182 GROUP BY me.foo, me.bar, me.hello, me.goodbye
190 SET read_count = read_count + 1
191 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
193 ( (1) x 4, (2) x 4 ),
196 ], 'Correct update-SQL with premultiplied restricting join without pruning' );
198 is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
199 is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
200 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
203 # Make sure multicolumn in or the equivalent functions correctly
206 my $sub_rs = $tkfks->search (
208 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
209 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
212 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
216 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
218 # attempts to delete a grouped rs should fail miserably
220 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
221 qr/attempted a delete operation on a resultset which does group_by/,
222 'Grouped rs update/delete not allowed',
225 # grouping on PKs only should pass
229 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
231 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
234 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
235 ->get_column ('pilot_sequence')->all
238 'Only two rows incremented',
241 # also make sure weird scalarref usage works (RT#51409)
243 \ 'pilot_sequence BETWEEN 11 AND 21',
244 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
247 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
248 ->get_column ('pilot_sequence')->all
251 'Only two rows incremented (where => scalarref works)',
255 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
258 { 'me.pilot_sequence' => 12 },
259 { 'me.autopilot' => 'b' },
263 lives_ok { $rs->update({ autopilot => 'z' }) }
264 'Update with table name qualifier in -or conditions lives';
266 [ $tkfks->search ({ pilot_sequence => [12, 22]})
267 ->get_column ('autopilot')->all
270 '... and yields the right data',
276 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
278 # make sure limit-only deletion works
279 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
280 $tkfks->search ({}, { rows => 1 })->delete;
281 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
284 # check with sql-equality, as sqlite will accept most bad sql just fine
286 my $rs = $schema->resultset('CD')->search(
287 { 'me.year' => { '!=' => 2010 } },
290 $schema->is_executed_sql_bind( sub {
291 $rs->search({}, { join => 'liner_notes' })->delete;
293 'DELETE FROM cd WHERE ( year != ? )',
295 ]], 'Non-restricting multijoins properly thrown out' );
297 $schema->is_executed_sql_bind( sub {
298 $rs->search({}, { prefetch => 'liner_notes' })->delete;
300 'DELETE FROM cd WHERE ( year != ? )',
302 ]], 'Non-restricting multiprefetch thrown out' );
304 $schema->is_executed_sql_bind( sub {
305 $rs->search({}, { prefetch => 'artist' })->delete;
307 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
309 ]], 'Restricting prefetch left in, selector thrown out');
311 ### switch artist and cd to fully qualified table names
312 ### make sure nothing is stripped out
313 my $cd_rsrc = $schema->source('CD');
314 $cd_rsrc->name('main.cd');
315 $cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
316 for $cd_rsrc->relationships;
318 my $art_rsrc = $schema->source('Artist');
319 $art_rsrc->name(\'main.artist');
320 $art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
321 for $art_rsrc->relationships;
323 $schema->is_executed_sql_bind( sub {
326 'DELETE FROM main.cd WHERE year != ?',
328 ]], 'delete with fully qualified table name' );
330 $rs->create({ title => 'foo', artist => 1, year => 2000 });
331 $schema->is_executed_sql_bind( sub {
336 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM main.cd me WHERE me.year != ?',
340 'DELETE FROM main.cd WHERE ( cdid = ? )',
344 ], 'delete_all with fully qualified table name' );
346 $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
347 my $cd42 = $rs->find(42);
349 $schema->is_executed_sql_bind( sub {
352 'DELETE FROM main.cd WHERE cdid = ?',
354 ]], 'delete of object from table with fully qualified name' );
356 $schema->is_executed_sql_bind( sub {
357 $cd42->related_resultset('artist')->delete
359 'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )',
361 ]], 'delete of related object from scalarref fully qualified named table' );
363 my $art3 = $schema->resultset('Artist')->find(3);
365 $schema->is_executed_sql_bind( sub {
366 $art3->related_resultset('cds')->delete;
368 'DELETE FROM main.cd WHERE ( artist = ? )',
370 ]], 'delete of related object from fully qualified named table' );
372 $schema->is_executed_sql_bind( sub {
373 $art3->cds_unordered->delete;
375 'DELETE FROM main.cd WHERE ( artist = ? )',
377 ]], 'delete of related object from fully qualified named table via relaccessor' );
379 $schema->is_executed_sql_bind( sub {
380 $rs->search({}, { prefetch => 'artist' })->delete;
382 'DELETE FROM main.cd WHERE ( cdid IN ( SELECT me.cdid FROM main.cd me JOIN main.artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
384 ]], 'delete with fully qualified table name and subquery correct' );
386 # check that as_subselect_rs works ok
387 # inner query is untouched, then a selector
388 # and an IN condition
389 $schema->is_executed_sql_bind( sub {
390 $schema->resultset('CD')->search({
392 'artist.name' => 'partytimecity',
395 })->as_subselect_rs->delete;
403 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
405 JOIN main.artist artist ON artist.artistid = me.artist
406 WHERE artist.name = ? AND me.cdid = ?
413 ]], 'Delete from as_subselect_rs works correctly' );