-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix self-referential resultset update/delete on MySQL (aggravated by 3…
…1073ac) MySQL is unable to figure out it needs a temp-table when it is trying to update a table with a condition it derived from that table. So what we do here is give it a helpful nudge by rewriting any "offending" subquery to a double subquery post-sql-generation. Performance seems to be about the same for moderately large sets. If it becomes a problem later we can always revisit and add the ability to induce "row-by-row" update/deletion instead. The implementation sucks, but is rather concise and most importantly contained to the MySQL codepath only - it does not affect the rest of the code flow in any way.
- Loading branch information
Showing
6 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
|
||
use lib qw(t/lib); | ||
use DBICTest; | ||
use DBICTest::Schema; | ||
use DBIC::SqlMakerTest; | ||
use DBIC::DebugObj; | ||
|
||
my $schema = DBICTest::Schema->connect (DBICTest->_database, { quote_char => '`' }); | ||
# cheat | ||
require DBIx::Class::Storage::DBI::mysql; | ||
bless ( $schema->storage, 'DBIx::Class::Storage::DBI::mysql' ); | ||
|
||
# check that double-subqueries are properly wrapped | ||
{ | ||
my ($sql, @bind); | ||
my $debugobj = DBIC::DebugObj->new (\$sql, \@bind); | ||
my $orig_debugobj = $schema->storage->debugobj; | ||
my $orig_debug = $schema->storage->debug; | ||
|
||
$schema->storage->debugobj ($debugobj); | ||
$schema->storage->debug (1); | ||
|
||
# the expected SQL may seem wastefully nonsensical - this is due to | ||
# CD's tablename being \'cd', which triggers the "this can be anything" | ||
# mode, and forces a subquery. This in turn forces *another* subquery | ||
# because mysql is being mysql | ||
# Also we know it will fail - never deployed. All we care about is the | ||
# SQL to compare | ||
eval { $schema->resultset ('CD')->update({ genreid => undef }) }; | ||
is_same_sql_bind ( | ||
$sql, | ||
\@bind, | ||
'UPDATE cd SET `genreid` = ? WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )', | ||
[ 'NULL' ], | ||
'Correct update-SQL with double-wrapped subquery', | ||
); | ||
|
||
# same comment as above | ||
eval { $schema->resultset ('CD')->delete }; | ||
is_same_sql_bind ( | ||
$sql, | ||
\@bind, | ||
'DELETE FROM cd WHERE `cdid` IN ( SELECT * FROM ( SELECT `me`.`cdid` FROM cd `me` ) `_forced_double_subquery` )', | ||
[], | ||
'Correct delete-SQL with double-wrapped subquery', | ||
); | ||
|
||
# and a really contrived example (we test it live in t/71mysql.t) | ||
my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } }); | ||
my ($count_sql, @count_bind) = @${$rs->count_rs->as_query}; | ||
eval { | ||
$schema->resultset('Artist')->search( | ||
{ artistid => { | ||
-in => $rs->get_column('artistid') | ||
->as_query | ||
} }, | ||
)->update({ name => \[ "CONCAT( `name`, '_bell_out_of_', $count_sql )", @count_bind ] }); | ||
}; | ||
|
||
is_same_sql_bind ( | ||
$sql, | ||
\@bind, | ||
q( | ||
UPDATE `artist` | ||
SET `name` = CONCAT(`name`, '_bell_out_of_', ( | ||
SELECT * | ||
FROM ( | ||
SELECT COUNT( * ) | ||
FROM `artist` `me` | ||
WHERE `name` LIKE ? | ||
) `_forced_double_subquery` | ||
)) | ||
WHERE | ||
`artistid` IN ( | ||
SELECT * | ||
FROM ( | ||
SELECT `me`.`artistid` | ||
FROM `artist` `me` | ||
WHERE `name` LIKE ? | ||
) `_forced_double_subquery` ) | ||
), | ||
[ ("'baby_%'") x 2 ], | ||
); | ||
|
||
$schema->storage->debugobj ($orig_debugobj); | ||
$schema->storage->debug ($orig_debug); | ||
} | ||
|
||
done_testing; |