Centralize and sanify generation of synthetic group_by criteria
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
index a5217ae..3314b88 100644 (file)
@@ -60,7 +60,7 @@ my $fks = $schema->resultset ('FourKeys')->search (
   {
     sensors => { '!=', 'c' },
     ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
-  }, { join => 'fourkeys_to_twokeys'}
+  }, { join => { fourkeys_to_twokeys => 'twokeys' }}
 );
 
 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
@@ -87,11 +87,11 @@ is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join
 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
 
 # make the multi-join stick
-$fks = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
+my $fks_multi = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
 
 $schema->storage->debugobj ($debugobj);
 $schema->storage->debug (1);
-$fks->update ({ read_count => \ 'read_count + 1' });
+$fks_multi->update ({ read_count => \ 'read_count + 1' });
 $schema->storage->debugobj ($orig_debugobj);
 $schema->storage->debug ($orig_debug);
 
@@ -113,8 +113,8 @@ is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
 $schema->storage->_use_multicolumn_in (1);
 $schema->storage->debugobj ($debugobj);
 $schema->storage->debug (1);
-throws_ok { $fks->update ({ read_count => \ 'read_count + 1' }) } # this can't actually execute, we just need the "as_query"
-  qr/\Q DBI Exception:/ or do { $sql = ''; @bind = () };
+throws_ok { $fks_multi->update ({ read_count => \ 'read_count + 1' }) } # this can't actually execute, we just need the "as_query"
+  qr/\QDBI Exception:/ or do { $sql = ''; @bind = () };
 $schema->storage->_use_multicolumn_in (undef);
 $schema->storage->debugobj ($orig_debugobj);
 $schema->storage->debug ($orig_debug);
@@ -145,6 +145,30 @@ is_same_sql_bind (
   'Correct update-SQL with multicolumn in support',
 );
 
+# make a *premultiplied* join stick
+my $fks_premulti = $fks->search({ 'twokeys.artist' => { '!=' => 666 } });
+
+$schema->storage->debugobj ($debugobj);
+$schema->storage->debug (1);
+$fks_premulti->update ({ read_count => \ 'read_count + 1' });
+$schema->storage->debugobj ($orig_debugobj);
+$schema->storage->debug ($orig_debug);
+
+is_same_sql_bind (
+  $sql,
+  \@bind,
+  'UPDATE fourkeys
+   SET read_count = read_count + 1
+   WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )',
+  [ map { "'$_'" } ( (1) x 4, (2) x 4 ) ],
+  'Correct update-SQL with premultiplied restricting join without pruning',
+);
+
+is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
+is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
+is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
+
+
 #
 # Make sure multicolumn in or the equivalent functions correctly
 #
@@ -263,29 +287,84 @@ $schema->storage->debug (1);
     'Restricting prefetch left in, selector thrown out'
   );
 
-  $rs->result_source->name('schema_qualified.cd');
-  # this is expected to fail - we only want to collect the generated SQL
-  eval { $rs->delete };
+  # 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;
+
+  $rs->delete;
   is_same_sql_bind (
     $sql,
     \@bind,
-    'DELETE FROM schema_qualified.cd WHERE ( year != ? )',
+    'DELETE FROM main.cd WHERE ( year != ? )',
     ["'2010'"],
-    'delete with fully qualified table name and subquery correct'
+    'delete with fully qualified table name'
   );
 
-  # this is expected to fail - we only want to collect the generated SQL
-  eval { $rs->search({}, { prefetch => 'artist' })->delete };
+  $rs->create({ title => 'foo', artist => 1, year => 2000 });
+  $rs->delete_all;
   is_same_sql_bind (
     $sql,
     \@bind,
-    'DELETE FROM schema_qualified.cd WHERE ( cdid IN ( SELECT me.cdid FROM schema_qualified.cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
+    'DELETE FROM main.cd WHERE ( cdid = ? )',
+    ["'1'"],
+    'delete_all with fully qualified table name'
+  );
+
+  $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
+  $rs->find(42)->delete;
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'DELETE FROM main.cd WHERE ( cdid = ? )',
+    ["'42'"],
+    'delete of object from table with fully qualified name'
+  );
+
+  $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
+  $rs->find(42)->related_resultset('artist')->delete;
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    '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',
+  );
+
+  $schema->resultset('Artist')->find(3)->related_resultset('cds')->delete;
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'DELETE FROM main.cd WHERE ( artist = ? )',
+    ["'3'"],
+    'delete of related object from fully qualified named table',
+  );
+
+  $schema->resultset('Artist')->find(3)->cds_unordered->delete;
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'DELETE FROM main.cd WHERE ( artist = ? )',
+    ["'3'"],
+    'delete of related object from fully qualified named table via relaccessor',
+  );
+
+  $rs->search({}, { prefetch => 'artist' })->delete;
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    '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'
   );
 
-  $rs->result_source->name('cd');
-
   # check that as_subselect_rs works ok
   # inner query is untouched, then a selector
   # and an IN condition
@@ -300,14 +379,14 @@ $schema->storage->debug (1);
     $sql,
     \@bind,
     '
-      DELETE FROM cd
+      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 cd me
-                JOIN artist artist ON artist.artistid = me.artist
+                FROM main.cd me
+                JOIN main.artist artist ON artist.artistid = me.artist
               WHERE artist.name = ? AND me.cdid = ?
             ) me
         )