1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
use strict;
use warnings;
use lib qw(t/lib);
use Test::More;
use Test::Exception;
# MASSIVE FIXME - there is a hole in ::RSC / as_subselect_rs
# losing the order. Needs a rework/extract of the realiaser,
# and that's a whole another bag of dicks
BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
use DBICTest::Schema::CD;
BEGIN {
# the default scalarref table name will not work well for this test
DBICTest::Schema::CD->table('cd');
}
use DBIx::Class::_Util 'scope_guard';
use DBICTest;
my $schema = DBICTest->init_schema;
my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
[qw/foo bar hello goodbye sensors read_count/],
[qw/1 1 1 1 a 10 /],
[qw/2 2 2 2 b 20 /],
[qw/1 1 1 2 c 30 /],
]);
# This is already provided by DBICTest
#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
# [qw/artist cd /],
# [qw/1 1 /],
# [qw/2 2 /],
#]);
my ($ta, $tb) = $schema->resultset ('TwoKeys')
->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ], { order_by => 'artist' })
->all;
my $tkfk_cnt = $tkfks->count;
my $non_void_ctx = $tkfks->populate ([
{ autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
{ autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
{ autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
{ autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
]);
is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
#
# Make sure the forced group by works (i.e. the joining does not cause double-updates)
#
# create a resultset matching $fa and $fb only
my $fks = $schema->resultset ('FourKeys')->search (
{
sensors => { '!=', 'c' },
( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
}, { join => { fourkeys_to_twokeys => 'twokeys' }}
);
my $read_count_inc = 0;
is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
$schema->is_executed_sql_bind( sub {
$fks->update ({ read_count => \ 'read_count + 1' });
$read_count_inc++;
}, [[
'UPDATE fourkeys
SET read_count = read_count + 1
WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
',
(1, 2) x 4,
'c',
]], 'Correct update-SQL with multijoin with pruning' );
is ($fa->discard_changes->read_count, 10 + $read_count_inc, 'Update ran only once on discard-join resultset');
is ($fb->discard_changes->read_count, 20 + $read_count_inc, 'Update ran only once on discard-join resultset');
is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
# make the multi-join stick
my $fks_multi = $fks->search(
{ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } },
{ order_by => [ $fks->result_source->primary_columns ] },
);
# Versions of libsqlite before 3.14 do not support multicolumn-in
# namely WHERE ( foo, bar ) IN ( SELECT foo, bar FROM ... )
#
# Run both variants to ensure the SQL is correct, and also observe whether
# the autodetection worked correctly for the current SQLite version
{
my $detected_can_mci = $schema->storage->_use_multicolumn_in ? 1 : 0;
for my $force_use_mci (0, 1) {
my $orig_use_mci = $schema->storage->_use_multicolumn_in;
my $sg = scope_guard {
$schema->storage->_use_multicolumn_in($orig_use_mci);
};
$schema->storage->_use_multicolumn_in( $force_use_mci);
$schema->is_executed_sql_bind( sub {
my $executed = 0;
eval {
$fks_multi->update ({ read_count => \ 'read_count + 1' });
$executed = 1;
$read_count_inc++;
};
is(
$executed,
( ( ! $detected_can_mci and $force_use_mci) ? 0 : 1 ),
"Executed status as expected with multicolumn-in capability ($detected_can_mci) combined with forced-mci-use ($force_use_mci)"
);
}, [
$force_use_mci
?(
[
'UPDATE fourkeys
SET read_count = read_count + 1
WHERE
(foo, bar, hello, goodbye) IN (
SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys 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
WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
ORDER BY foo, bar, hello, goodbye
)
',
( 1, 2) x 2,
666,
( 1, 2) x 2,
'c',
]
)
:(
[ 'BEGIN' ],
[
'SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
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
WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
GROUP BY me.foo, me.bar, me.hello, me.goodbye
ORDER BY foo, bar, hello, goodbye
',
(1, 2) x 2,
666,
(1, 2) x 2,
'c',
],
[
'UPDATE fourkeys
SET read_count = read_count + 1
WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
',
( (1) x 4, (2) x 4 ),
],
[ 'COMMIT' ],
)
], "Correct update-SQL with multijoin without pruning ( use_multicolumn_in forced to: $force_use_mci )" );
is ($fa->discard_changes->read_count, 10 + $read_count_inc, 'Update ran expected amount of times on joined resultset');
is ($fb->discard_changes->read_count, 20 + $read_count_inc, 'Update ran expected amount of times on joined resultset');
is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
$schema->is_executed_sql_bind( sub {
my $executed = 0;
eval {
my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
$executed = 1;
ok ($res, 'operation is true');
cmp_ok ($res, '==', 0, 'zero rows affected');
};
is(
$executed,
( ( ! $detected_can_mci and $force_use_mci) ? 0 : 1 ),
"Executed status as expected with multicolumn-in capability ($detected_can_mci) combined with forced-mci-use ($force_use_mci)"
);
}, [
$force_use_mci
? (
[
'DELETE FROM fourkeys
WHERE ( foo, bar, hello, goodbye ) IN (
SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
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
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 != ?
ORDER BY foo, bar, hello, goodbye
)',
(1, 2) x 2,
666,
(1, 2) x 2,
'c',
]
)
: (
[ 'BEGIN' ],
[
'SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
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
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 != ?
GROUP BY me.foo, me.bar, me.hello, me.goodbye
ORDER BY foo, bar, hello, goodbye
',
(1, 2) x 2,
666,
(1, 2) x 2,
'c',
],
[ 'COMMIT' ],
)
], 'Correct null-delete-SQL with multijoin without pruning' );
is ($fa->discard_changes->read_count, 10 + $read_count_inc, 'Noop update did not touch anything');
is ($fb->discard_changes->read_count, 20 + $read_count_inc, 'Noop update did not touch anything');
is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
$schema->is_executed_sql_bind( sub {
my $executed = 0;
eval {
$fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
$executed = 1;
$read_count_inc++;
};
is(
$executed,
( ( ! $detected_can_mci and $force_use_mci) ? 0 : 1 ),
"Executed status as expected with multicolumn-in capability ($detected_can_mci) combined with forced-mci-use ($force_use_mci)"
);
}, [
$force_use_mci
? (
[
'UPDATE fourkeys SET read_count = read_count + 1
WHERE ( foo, bar, hello, goodbye ) IN (
SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys 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
LEFT JOIN twokeys twokeys
ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
)',
(1, 2) x 4,
'c',
666,
]
)
: (
[ 'BEGIN' ],
[
'SELECT me.foo, me.bar, me.hello, me.goodbye
FROM fourkeys me
LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
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
LEFT JOIN twokeys twokeys
ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
GROUP BY me.foo, me.bar, me.hello, me.goodbye
',
(1, 2) x 4,
'c',
666,
],
[
'UPDATE fourkeys
SET read_count = read_count + 1
WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
',
( (1) x 4, (2) x 4 ),
],
[ 'COMMIT' ],
)
], 'Correct update-SQL with premultiplied restricting join without pruning' );
is ($fa->discard_changes->read_count, 10 + $read_count_inc, 'Update ran expected amount of times on joined resultset');
is ($fb->discard_changes->read_count, 20 + $read_count_inc, 'Update ran expected amount of times on joined resultset');
is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
}
}
#
# Make sure multicolumn in or the equivalent functions correctly
#
my $sub_rs = $tkfks->search (
[
{ map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
{ map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
],
{
join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
},
);
is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
# ensure we do not do something dumb on MCI-not-supporting engines
{
my $orig_use_mci = $schema->storage->_use_multicolumn_in;
my $sg = scope_guard {
$schema->storage->_use_multicolumn_in($orig_use_mci);
};
$schema->storage->_use_multicolumn_in(0);
# attempts to delete a global-grouped rs should fail miserably
throws_ok (
sub { $sub_rs->search ({}, { distinct => 1 })->delete },
qr/attempted a delete operation on a resultset which does group_by on columns other than the primary keys/,
'Grouped rs update/delete not allowed',
);
}
# grouping on PKs only should pass
$sub_rs->search (
{},
{
group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
},
)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
is_deeply (
[ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
->get_column ('pilot_sequence')->all
],
[qw/11 21 30 40/],
'Only two rows incremented',
);
# also make sure weird scalarref usage works (RT#51409)
$tkfks->search (
\ 'pilot_sequence BETWEEN 11 AND 21',
)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
is_deeply (
[ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
->get_column ('pilot_sequence')->all
],
[qw/12 22 30 40/],
'Only two rows incremented (where => scalarref works)',
);
{
my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
{
-or => [
{ 'me.pilot_sequence' => 12 },
{ 'me.autopilot' => 'b' },
],
}
);
lives_ok { $rs->update({ autopilot => 'z' }) }
'Update with table name qualifier in -or conditions lives';
is_deeply (
[ $tkfks->search ({ pilot_sequence => [12, 22]})
->get_column ('autopilot')->all
],
[qw/z z/],
'... and yields the right data',
);
}
$sub_rs->delete;
is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
# make sure limit-only deletion works
cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
$tkfks->search ({}, { rows => 1 })->delete;
is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
# check with sql-equality, as sqlite will accept most bad sql just fine
{
my $rs = $schema->resultset('CD')->search(
{ 'me.year' => { '!=' => 2010 } },
);
$schema->is_executed_sql_bind( sub {
$rs->search({}, { join => 'liner_notes' })->delete;
}, [[
'DELETE FROM cd WHERE ( year != ? )',
2010,
]], 'Non-restricting multijoins properly thrown out' );
$schema->is_executed_sql_bind( sub {
$rs->search({}, { prefetch => 'liner_notes' })->delete;
}, [[
'DELETE FROM cd WHERE ( year != ? )',
2010,
]], 'Non-restricting multiprefetch thrown out' );
$schema->is_executed_sql_bind( sub {
$rs->search({}, { prefetch => 'artist' })->delete;
}, [[
'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
2010,
]], 'Restricting prefetch left in, selector thrown out');
### switch artist and cd to fully qualified table names
### make sure nothing is stripped out
my $cd_rsrc = $schema->source('CD');
$cd_rsrc->name('main.cd');
$cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
for $cd_rsrc->relationships;
my $art_rsrc = $schema->source('Artist');
$art_rsrc->name(\'main.artist');
$art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
for $art_rsrc->relationships;
$schema->is_executed_sql_bind( sub {
$rs->delete
}, [[
'DELETE FROM main.cd WHERE year != ?',
2010,
]], 'delete with fully qualified table name' );
$rs->create({ title => 'foo', artist => 1, year => 2000 });
$schema->is_executed_sql_bind( sub {
$rs->delete_all
}, [
[ 'BEGIN' ],
[
'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM main.cd me WHERE me.year != ?',
2010,
],
[
'DELETE FROM main.cd WHERE ( cdid = ? )',
1,
],
[ 'COMMIT' ],
], 'delete_all with fully qualified table name' );
$rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
my $cd42 = $rs->find(42);
$schema->is_executed_sql_bind( sub {
$cd42->delete
}, [[
'DELETE FROM main.cd WHERE cdid = ?',
42,
]], 'delete of object from table with fully qualified name' );
$schema->is_executed_sql_bind( sub {
$cd42->related_resultset('artist')->delete
}, [[
'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )',
2,
]], 'delete of related object from scalarref fully qualified named table' );
my $art3 = $schema->resultset('Artist')->find(3);
$schema->is_executed_sql_bind( sub {
$art3->related_resultset('cds')->delete;
}, [[
'DELETE FROM main.cd WHERE ( artist = ? )',
3,
]], 'delete of related object from fully qualified named table' );
$schema->is_executed_sql_bind( sub {
$art3->cds_unordered->delete;
}, [[
'DELETE FROM main.cd WHERE ( artist = ? )',
3,
]], 'delete of related object from fully qualified named table via relaccessor' );
$schema->is_executed_sql_bind( sub {
$rs->search({}, { prefetch => 'artist' })->delete;
}, [[
'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 != ? ) ) )',
2010,
]], 'delete with fully qualified table name and subquery correct' );
# check that as_subselect_rs works ok
# inner query is untouched, then a selector
# and an IN condition
$schema->is_executed_sql_bind( sub {
$schema->resultset('CD')->search({
'me.cdid' => 1,
'artist.name' => 'partytimecity',
}, {
join => 'artist',
})->as_subselect_rs->delete;
}, [[
'
DELETE FROM main.cd
WHERE (
cdid IN (
SELECT me.cdid
FROM (
SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
FROM main.cd me
JOIN main.artist artist ON artist.artistid = me.artist
WHERE artist.name = ? AND me.cdid = ?
) me
)
)
',
'partytimecity',
1,
]], 'Delete from as_subselect_rs works correctly' );
}
done_testing;
|