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 DBIx::Class::_Util 'scope_guard';
15 use DBICTest::Schema::CD;
17 # the default scalarref table name will not work well for this test
18 DBICTest::Schema::CD->table('cd');
23 my $schema = DBICTest->init_schema;
25 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
27 my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
28 [qw/foo bar hello goodbye sensors read_count/],
34 # This is already provided by DBICTest
35 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
40 my ($ta, $tb) = $schema->resultset ('TwoKeys')
41 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ], { order_by => 'artist' })
44 my $tkfk_cnt = $tkfks->count;
46 my $non_void_ctx = $tkfks->populate ([
47 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
48 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
49 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
50 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
52 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
55 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
58 # create a resultset matching $fa and $fb only
59 my $fks = $schema->resultset ('FourKeys')->search (
61 sensors => { '!=', 'c' },
62 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
63 }, { join => { fourkeys_to_twokeys => 'twokeys' }}
66 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
67 $schema->is_executed_sql_bind( sub {
68 $fks->update ({ read_count => \ 'read_count + 1' })
71 SET read_count = read_count + 1
72 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
76 ]], 'Correct update-SQL with multijoin with pruning' );
78 is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
79 is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
80 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
82 # make the multi-join stick
83 my $fks_multi = $fks->search(
84 { 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } },
85 { order_by => [ $fks->result_source->primary_columns ] },
87 $schema->is_executed_sql_bind( sub {
88 $fks_multi->update ({ read_count => \ 'read_count + 1' })
92 'SELECT me.foo, me.bar, me.hello, me.goodbye
94 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
95 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
96 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
97 GROUP BY me.foo, me.bar, me.hello, me.goodbye
98 ORDER BY foo, bar, hello, goodbye
107 SET read_count = read_count + 1
108 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
110 ( (1) x 4, (2) x 4 ),
113 ], 'Correct update-SQL with multijoin without pruning' );
115 is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
116 is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
117 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
119 $schema->is_executed_sql_bind( sub {
120 my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
121 ok ($res, 'operation is true');
122 cmp_ok ($res, '==', 0, 'zero rows affected');
126 'SELECT me.foo, me.bar, me.hello, me.goodbye
128 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
129 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
130 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 != ?
131 GROUP BY me.foo, me.bar, me.hello, me.goodbye
132 ORDER BY foo, bar, hello, goodbye
140 ], 'Correct null-delete-SQL with multijoin without pruning' );
143 # try the same sql with forced multicolumn in
144 $schema->is_executed_sql_bind( sub {
146 my $orig_umi = $schema->storage->_use_multicolumn_in;
147 my $sg = scope_guard {
148 $schema->storage->_use_multicolumn_in($orig_umi);
151 $schema->storage->_use_multicolumn_in(1);
153 # this can't actually execute on sqlite
154 eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
157 SET read_count = read_count + 1
159 (foo, bar, hello, goodbye) IN (
160 SELECT me.foo, me.bar, me.hello, me.goodbye
162 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
163 fourkeys_to_twokeys.f_bar = me.bar
164 AND fourkeys_to_twokeys.f_foo = me.foo
165 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
166 AND fourkeys_to_twokeys.f_hello = me.hello
167 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
168 ORDER BY foo, bar, hello, goodbye
176 ]], 'Correct update-SQL with multicolumn in support' );
178 $schema->is_executed_sql_bind( sub {
179 $fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
183 'SELECT me.foo, me.bar, me.hello, me.goodbye
185 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
186 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
187 LEFT JOIN twokeys twokeys
188 ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
189 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
190 GROUP BY me.foo, me.bar, me.hello, me.goodbye
198 SET read_count = read_count + 1
199 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
201 ( (1) x 4, (2) x 4 ),
204 ], 'Correct update-SQL with premultiplied restricting join without pruning' );
206 is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
207 is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
208 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
211 # Make sure multicolumn in or the equivalent functions correctly
214 my $sub_rs = $tkfks->search (
216 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
217 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
220 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
224 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
226 # attempts to delete a grouped rs should fail miserably
228 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
229 qr/attempted a delete operation on a resultset which does group_by/,
230 'Grouped rs update/delete not allowed',
233 # grouping on PKs only should pass
237 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
239 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
242 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
243 ->get_column ('pilot_sequence')->all
246 'Only two rows incremented',
249 # also make sure weird scalarref usage works (RT#51409)
251 \ 'pilot_sequence BETWEEN 11 AND 21',
252 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
255 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
256 ->get_column ('pilot_sequence')->all
259 'Only two rows incremented (where => scalarref works)',
263 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
266 { 'me.pilot_sequence' => 12 },
267 { 'me.autopilot' => 'b' },
271 lives_ok { $rs->update({ autopilot => 'z' }) }
272 'Update with table name qualifier in -or conditions lives';
274 [ $tkfks->search ({ pilot_sequence => [12, 22]})
275 ->get_column ('autopilot')->all
278 '... and yields the right data',
284 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
286 # make sure limit-only deletion works
287 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
288 $tkfks->search ({}, { rows => 1 })->delete;
289 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
292 $tkfks->search ({}, { rows => 0 })->delete
293 } qr/rows attribute must be a positive integer/;
294 is ($tkfks->count, $tkfk_cnt, 'Nothing deleted');
296 # check with sql-equality, as sqlite will accept most bad sql just fine
298 my $rs = $schema->resultset('CD')->search(
299 { 'me.year' => { '!=' => 2010 } },
302 $schema->is_executed_sql_bind( sub {
303 $rs->search({}, { join => 'liner_notes' })->delete;
305 'DELETE FROM cd WHERE ( year != ? )',
307 ]], 'Non-restricting multijoins properly thrown out' );
309 $schema->is_executed_sql_bind( sub {
310 $rs->search({}, { prefetch => 'liner_notes' })->delete;
312 'DELETE FROM cd WHERE ( year != ? )',
314 ]], 'Non-restricting multiprefetch thrown out' );
316 $schema->is_executed_sql_bind( sub {
317 $rs->search({}, { prefetch => 'artist' })->delete;
319 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
321 ]], 'Restricting prefetch left in, selector thrown out');
323 ### switch artist and cd to fully qualified table names
324 ### make sure nothing is stripped out
325 my $cd_rsrc = $schema->source('CD');
326 $cd_rsrc->name('main.cd');
327 $cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
328 for $cd_rsrc->relationships;
330 my $art_rsrc = $schema->source('Artist');
331 $art_rsrc->name(\'main.artist');
332 $art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
333 for $art_rsrc->relationships;
335 $schema->is_executed_sql_bind( sub {
338 'DELETE FROM main.cd WHERE year != ?',
340 ]], 'delete with fully qualified table name' );
342 $rs->create({ title => 'foo', artist => 1, year => 2000 });
343 $schema->is_executed_sql_bind( sub {
348 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM main.cd me WHERE me.year != ?',
352 'DELETE FROM main.cd WHERE ( cdid = ? )',
356 ], 'delete_all with fully qualified table name' );
358 $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
359 my $cd42 = $rs->find(42);
361 $schema->is_executed_sql_bind( sub {
364 'DELETE FROM main.cd WHERE cdid = ?',
366 ]], 'delete of object from table with fully qualified name' );
368 $schema->is_executed_sql_bind( sub {
369 $cd42->related_resultset('artist')->delete
371 'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )',
373 ]], 'delete of related object from scalarref fully qualified named table' );
375 my $art3 = $schema->resultset('Artist')->find(3);
377 $schema->is_executed_sql_bind( sub {
378 $art3->related_resultset('cds')->delete;
380 'DELETE FROM main.cd WHERE ( artist = ? )',
382 ]], 'delete of related object from fully qualified named table' );
384 $schema->is_executed_sql_bind( sub {
385 $art3->cds_unordered->delete;
387 'DELETE FROM main.cd WHERE ( artist = ? )',
389 ]], 'delete of related object from fully qualified named table via relaccessor' );
391 $schema->is_executed_sql_bind( sub {
392 $rs->search({}, { prefetch => 'artist' })->delete;
394 '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 != ? ) ) )',
396 ]], 'delete with fully qualified table name and subquery correct' );
398 # check that as_subselect_rs works ok
399 # inner query is untouched, then a selector
400 # and an IN condition
401 $schema->is_executed_sql_bind( sub {
402 $schema->resultset('CD')->search({
404 'artist.name' => 'partytimecity',
407 })->as_subselect_rs->delete;
415 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
417 JOIN main.artist artist ON artist.artistid = me.artist
418 WHERE artist.name = ? AND me.cdid = ?
425 ]], 'Delete from as_subselect_rs works correctly' );