8 use DBICTest::Schema::CD;
10 # the default scalarref table name will not work well for this test
11 DBICTest::Schema::CD->table('cd');
16 my $schema = DBICTest->init_schema;
18 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
20 my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
21 [qw/foo bar hello goodbye sensors read_count/],
27 # This is already provided by DBICTest
28 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
33 my ($ta, $tb) = $schema->resultset ('TwoKeys')
34 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
37 my $tkfk_cnt = $tkfks->count;
39 my $non_void_ctx = $tkfks->populate ([
40 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
41 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
42 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
43 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
45 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
48 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
51 # create a resultset matching $fa and $fb only
52 my $fks = $schema->resultset ('FourKeys')->search (
54 sensors => { '!=', 'c' },
55 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
56 }, { join => { fourkeys_to_twokeys => 'twokeys' }}
59 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
60 $schema->is_executed_sql_bind( sub {
61 $fks->update ({ read_count => \ 'read_count + 1' })
64 SET read_count = read_count + 1
65 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
69 ]], 'Correct update-SQL with multijoin with pruning' );
71 is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
72 is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
73 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
75 # make the multi-join stick
76 my $fks_multi = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
77 $schema->is_executed_sql_bind( sub {
78 $fks_multi->update ({ read_count => \ 'read_count + 1' })
82 'SELECT me.foo, me.bar, me.hello, me.goodbye
84 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
85 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
86 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
87 GROUP BY me.foo, me.bar, me.hello, me.goodbye
96 SET read_count = read_count + 1
97 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
102 ], 'Correct update-SQL with multijoin without pruning' );
104 is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
105 is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
106 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
108 $schema->is_executed_sql_bind( sub {
109 my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
110 ok ($res, 'operation is true');
111 cmp_ok ($res, '==', 0, 'zero rows affected');
115 'SELECT me.foo, me.bar, me.hello, me.goodbye
117 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
118 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
119 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 != ?
120 GROUP BY me.foo, me.bar, me.hello, me.goodbye
128 ], 'Correct null-delete-SQL with multijoin without pruning' );
131 # try the same sql with forced multicolumn in
132 $schema->is_executed_sql_bind( sub {
133 local $schema->storage->{_use_multicolumn_in} = 1;
135 # this can't actually execute on sqlite
136 eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
139 SET read_count = read_count + 1
141 (foo, bar, hello, goodbye) IN (
142 SELECT me.foo, me.bar, me.hello, me.goodbye
144 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
145 fourkeys_to_twokeys.f_bar = me.bar
146 AND fourkeys_to_twokeys.f_foo = me.foo
147 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
148 AND fourkeys_to_twokeys.f_hello = me.hello
149 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
157 ]], 'Correct update-SQL with multicolumn in support' );
159 $schema->is_executed_sql_bind( sub {
160 $fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
164 'SELECT me.foo, me.bar, me.hello, me.goodbye
166 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
167 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
168 LEFT JOIN twokeys twokeys
169 ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
170 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
171 GROUP BY me.foo, me.bar, me.hello, me.goodbye
179 SET read_count = read_count + 1
180 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
182 ( (1) x 4, (2) x 4 ),
185 ], 'Correct update-SQL with premultiplied restricting join without pruning' );
187 is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
188 is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
189 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
192 # Make sure multicolumn in or the equivalent functions correctly
195 my $sub_rs = $tkfks->search (
197 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
198 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
201 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
205 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
207 # attempts to delete a grouped rs should fail miserably
209 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
210 qr/attempted a delete operation on a resultset which does group_by/,
211 'Grouped rs update/delete not allowed',
214 # grouping on PKs only should pass
218 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
220 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
223 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
224 ->get_column ('pilot_sequence')->all
227 'Only two rows incremented',
230 # also make sure weird scalarref usage works (RT#51409)
232 \ 'pilot_sequence BETWEEN 11 AND 21',
233 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
236 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
237 ->get_column ('pilot_sequence')->all
240 'Only two rows incremented (where => scalarref works)',
244 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
247 { 'me.pilot_sequence' => 12 },
248 { 'me.autopilot' => 'b' },
252 lives_ok { $rs->update({ autopilot => 'z' }) }
253 'Update with table name qualifier in -or conditions lives';
255 [ $tkfks->search ({ pilot_sequence => [12, 22]})
256 ->get_column ('autopilot')->all
259 '... and yields the right data',
265 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
267 # make sure limit-only deletion works
268 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
269 $tkfks->search ({}, { rows => 1 })->delete;
270 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
273 # check with sql-equality, as sqlite will accept most bad sql just fine
275 my $rs = $schema->resultset('CD')->search(
276 { 'me.year' => { '!=' => 2010 } },
279 $schema->is_executed_sql_bind( sub {
280 $rs->search({}, { join => 'liner_notes' })->delete;
282 'DELETE FROM cd WHERE ( year != ? )',
284 ]], 'Non-restricting multijoins properly thrown out' );
286 $schema->is_executed_sql_bind( sub {
287 $rs->search({}, { prefetch => 'liner_notes' })->delete;
289 'DELETE FROM cd WHERE ( year != ? )',
291 ]], 'Non-restricting multiprefetch thrown out' );
293 $schema->is_executed_sql_bind( sub {
294 $rs->search({}, { prefetch => 'artist' })->delete;
296 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
298 ]], 'Restricting prefetch left in, selector thrown out');
300 ### switch artist and cd to fully qualified table names
301 ### make sure nothing is stripped out
302 my $cd_rsrc = $schema->source('CD');
303 $cd_rsrc->name('main.cd');
304 $cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
305 for $cd_rsrc->relationships;
307 my $art_rsrc = $schema->source('Artist');
308 $art_rsrc->name(\'main.artist');
309 $art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
310 for $art_rsrc->relationships;
312 $schema->is_executed_sql_bind( sub {
315 'DELETE FROM main.cd WHERE year != ?',
317 ]], 'delete with fully qualified table name' );
319 $rs->create({ title => 'foo', artist => 1, year => 2000 });
320 $schema->is_executed_sql_bind( sub {
325 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM main.cd me WHERE me.year != ?',
329 'DELETE FROM main.cd WHERE ( cdid = ? )',
333 ], 'delete_all with fully qualified table name' );
335 $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
336 my $cd42 = $rs->find(42);
338 $schema->is_executed_sql_bind( sub {
341 'DELETE FROM main.cd WHERE cdid = ?',
343 ]], 'delete of object from table with fully qualified name' );
345 $schema->is_executed_sql_bind( sub {
346 $cd42->related_resultset('artist')->delete
348 'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )',
350 ]], 'delete of related object from scalarref fully qualified named table' );
352 my $art3 = $schema->resultset('Artist')->find(3);
354 $schema->is_executed_sql_bind( sub {
355 $art3->related_resultset('cds')->delete;
357 'DELETE FROM main.cd WHERE ( artist = ? )',
359 ]], 'delete of related object from fully qualified named table' );
361 $schema->is_executed_sql_bind( sub {
362 $art3->cds_unordered->delete;
364 'DELETE FROM main.cd WHERE ( artist = ? )',
366 ]], 'delete of related object from fully qualified named table via relaccessor' );
368 $schema->is_executed_sql_bind( sub {
369 $rs->search({}, { prefetch => 'artist' })->delete;
371 '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 != ? ) ) )',
373 ]], 'delete with fully qualified table name and subquery correct' );
375 # check that as_subselect_rs works ok
376 # inner query is untouched, then a selector
377 # and an IN condition
378 $schema->is_executed_sql_bind( sub {
379 $schema->resultset('CD')->search({
381 'artist.name' => 'partytimecity',
384 })->as_subselect_rs->delete;
392 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
394 JOIN main.artist artist ON artist.artistid = me.artist
395 WHERE artist.name = ? AND me.cdid = ?
402 ]], 'Delete from as_subselect_rs works correctly' );