Merge 'trunk' into 'count_distinct'
Peter Rabbitson [Thu, 7 May 2009 17:28:29 +0000 (17:28 +0000)]
r6164@Thesaurus (orig r6163):  ribasushi | 2009-05-07 19:09:01 +0200
 r6115@Thesaurus (orig r6114):  plu | 2009-05-03 10:39:16 +0200
 new branch to fix $rs->update and $rs->delete using the new as_query method

 r6116@Thesaurus (orig r6115):  plu | 2009-05-03 10:52:07 +0200
 Methods update/delete on resultset use now new as_query method to updated/delete properly on joined/prefetched resultset using a subquery. Therefore some tests have been added and some have been changed as well as the warnings around $rs->update/delete have been removed. Cheers!
 r6117@Thesaurus (orig r6116):  plu | 2009-05-03 11:13:48 +0200
 Using "is" instead of "cmp_ok"
 r6160@Thesaurus (orig r6159):  ribasushi | 2009-05-07 11:58:14 +0200
 Back out skip_parens support in as_query
 r6161@Thesaurus (orig r6160):  ribasushi | 2009-05-07 19:00:48 +0200
 This test is completely borked, needs a rewrite
 r6162@Thesaurus (orig r6161):  ribasushi | 2009-05-07 19:07:19 +0200
 Temporary fix or the IN ( ( ... ) ) problem until we get proper SQLA AST (needs SQLA released with commit 6158 to work)

r6165@Thesaurus (orig r6164):  ribasushi | 2009-05-07 19:11:46 +0200
Changes, remove merged branch
r6169@Thesaurus (orig r6168):  ribasushi | 2009-05-07 19:24:54 +0200
Bump SQLA dependency so -in/-between workarounds overload properly

Changes
Makefile.PL
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSetColumn.pm
lib/DBIx/Class/Storage/DBI.pm
t/53delete_chained.t [deleted file]
t/76joins.t
t/count/in_subquery.t [new file with mode: 0644]
t/resultset/as_query.t

diff --git a/Changes b/Changes
index 58c5737..0704243 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,7 +2,9 @@ Revision history for DBIx::Class
 
         - Refactor DBIx::Class::Storage::DBI::Sybase to automatically 
           load a subclass, namely Microsoft_SQL_Server.pm
-          (similar to DBIx::Class::Storage::DBI::ODBC) 
+          (similar to DBIx::Class::Storage::DBI::ODBC)
+        - Proper support for update/delete of joined resultsets
+          (using IN => $sub_rs->as_query)
 
 0.08102 2009-04-30 08:29:00 (UTC)
         - Fixed two subtle bugs when using columns or select/as
index bd6963a..2d8e90b 100644 (file)
@@ -16,7 +16,7 @@ configure_requires 'DBD::SQLite';
 requires 'DBD::SQLite'              => 1.23;
 requires 'Data::Page'               => 2.00;
 requires 'Scalar::Util'             => 0;
-requires 'SQL::Abstract'            => 1.53;
+requires 'SQL::Abstract'            => 1.54;
 requires 'SQL::Abstract::Limit'     => 0.13;
 requires 'Class::C3::Componentised' => 1.0005;
 requires 'Storable'                 => 0;
index aa6fa59..7611945 100644 (file)
@@ -1315,49 +1315,8 @@ sub _cond_for_update_delete {
   # No-op. No condition, we're updating/deleting everything
   return $cond unless ref $full_cond;
 
-  if (ref $full_cond eq 'ARRAY') {
-    $cond = [
-      map {
-        my %hash;
-        foreach my $key (keys %{$_}) {
-          $key =~ /([^.]+)$/;
-          $hash{$1} = $_->{$key};
-        }
-        \%hash;
-      } @{$full_cond}
-    ];
-  }
-  elsif (ref $full_cond eq 'HASH') {
-    if ((keys %{$full_cond})[0] eq '-and') {
-      $cond->{-and} = [];
-
-      my @cond = @{$full_cond->{-and}};
-      for (my $i = 0; $i < @cond; $i++) {
-        my $entry = $cond[$i];
-
-        my $hash;
-        if (ref $entry eq 'HASH') {
-          $hash = $self->_cond_for_update_delete($entry);
-        }
-        else {
-          $entry =~ /([^.]+)$/;
-          $hash->{$1} = $cond[++$i];
-        }
-
-        push @{$cond->{-and}}, $hash;
-      }
-    }
-    else {
-      foreach my $key (keys %{$full_cond}) {
-        $key =~ /([^.]+)$/;
-        $cond->{$1} = $full_cond->{$key};
-      }
-    }
-  }
-  else {
-    $self->throw_exception(
-      "Can't update/delete on resultset with condition unless hash or array"
-    );
+  foreach my $pk ($self->result_source->primary_columns) {
+      $cond->{$pk} = { -in => $self->get_column($pk)->as_query };
   }
 
   return $cond;
@@ -1385,13 +1344,8 @@ sub update {
   $self->throw_exception("Values for update must be a hash")
     unless ref $values eq 'HASH';
 
-  carp(   'WARNING! Currently $rs->update() does not generate proper SQL'
-        . ' on joined resultsets, and may affect rows well outside of the'
-        . ' contents of $rs. Use at your own risk' )
-    if ( $self->{attrs}{seen_join} );
-
   my $cond = $self->_cond_for_update_delete;
-   
+  
   return $self->result_source->storage->update(
     $self->result_source, $values, $cond
   );
@@ -1439,10 +1393,6 @@ to run. See also L<DBIx::Class::Row/delete>.
 delete may not generate correct SQL for a query with joins or a resultset
 chained from a related resultset.  In this case it will generate a warning:-
 
-  WARNING! Currently $rs->delete() does not generate proper SQL on
-  joined resultsets, and may delete rows well outside of the contents
-  of $rs. Use at your own risk
-
 In these cases you may find that delete_all is more appropriate, or you
 need to respecify your query in a way that can be expressed without a join.
 
@@ -1452,10 +1402,7 @@ sub delete {
   my ($self) = @_;
   $self->throw_exception("Delete should not be passed any arguments")
     if $_[1];
-  carp(   'WARNING! Currently $rs->delete() does not generate proper SQL'
-        . ' on joined resultsets, and may delete rows well outside of the'
-        . ' contents of $rs. Use at your own risk' )
-    if ( $self->{attrs}{seen_join} );
+
   my $cond = $self->_cond_for_update_delete;
 
   $self->result_source->storage->delete($self->result_source, $cond);
index 596df7c..2679803 100644 (file)
@@ -72,7 +72,7 @@ B<NOTE>: This feature is still experimental.
 
 =cut
 
-sub as_query { return shift->_resultset->as_query }
+sub as_query { return shift->_resultset->as_query(@_) }
 
 =head2 next
 
index f346524..a1bb464 100644 (file)
@@ -38,10 +38,10 @@ package # Hide from PAUSE
 
 use base qw/SQL::Abstract::Limit/;
 
-# This prevents the caching of $dbh in S::A::L, I believe
 sub new {
   my $self = shift->SUPER::new(@_);
 
+  # This prevents the caching of $dbh in S::A::L, I believe
   # If limit_dialect is a ref (like a $dbh), go ahead and replace
   #   it with what it resolves to:
   $self->{limit_dialect} = $self->_find_syntax($self->{limit_dialect})
@@ -50,6 +50,60 @@ sub new {
   $self;
 }
 
+
+
+# Some databases (sqlite) do not handle multiple parenthesis
+# around in/between arguments. A tentative x IN ( ( 1, 2 ,3) )
+# is interpreted as x IN 1 or something similar.
+#
+# Since we currently do not have access to the SQLA AST, resort
+# to barbaric mutilation of any SQL supplied in literal form
+
+sub _strip_outer_paren {
+  my ($self, $arg) = @_;
+
+use Data::Dumper;
+
+  return $self->_SWITCH_refkind ($arg, {
+    ARRAYREFREF => sub {
+      $$arg->[0] = __strip_outer_paren ($$arg->[0]);
+      return $arg;
+    },
+    SCALARREF => sub {
+      return \__strip_outer_paren( $$arg );
+    },
+    FALLBACK => sub {
+      return $arg
+    },
+  });
+}
+
+sub __strip_outer_paren {
+  my $sql = shift;
+
+  if ($sql and not ref $sql) {
+    while ($sql =~ /^ \s* \( (.*) \) \s* $/x ) {
+      $sql = $1;
+    }
+  }
+
+  return $sql;
+}
+
+sub _where_field_IN {
+  my ($self, $lhs, $op, $rhs) = @_;
+  $rhs = $self->_strip_outer_paren ($rhs);
+  return $self->SUPER::_where_field_IN ($lhs, $op, $rhs);
+}
+
+sub _where_field_BETWEEN {
+  my ($self, $lhs, $op, $rhs) = @_;
+  $rhs = $self->_strip_outer_paren ($rhs);
+  return $self->SUPER::_where_field_BETWEEN ($lhs, $op, $rhs);
+}
+
+
+
 # DB2 is the only remaining DB using this. Even though we are not sure if
 # RowNumberOver is still needed here (should be part of SQLA) leave the 
 # code in place
diff --git a/t/53delete_chained.t b/t/53delete_chained.t
deleted file mode 100644 (file)
index 4619548..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-use Test::More;
-use strict;
-use warnings;
-use lib qw(t/lib);
-use DBICTest;
-
-plan tests => 9;
-
-# This set of tests attempts to do a delete on a chained resultset, which
-# would lead to SQL DELETE with a JOIN, which is not supported by the 
-# SQL generator right now.
-# So it currently checks that these operations fail with a warning.
-# When the SQL generator is fixed this test will need fixing up appropriately.
-
-my $schema = DBICTest->init_schema();
-my $total_tracks = $schema->resultset('Track')->count;
-cmp_ok($total_tracks, '>', 0, 'need track records');
-
-# test that delete_related w/o conditions deletes all related records only
-{
-  my $w;
-  local $SIG{__WARN__} = sub { $w = shift };
-
-  my $artist = $schema->resultset("Artist")->find(3);
-  my $artist_tracks = $artist->cds->search_related('tracks')->count;
-  cmp_ok($artist_tracks, '<', $total_tracks, 'need more tracks than just related tracks');
-
-  ok(!eval{$artist->cds->search_related('tracks')->delete});
-  cmp_ok($schema->resultset('Track')->count, '==', $total_tracks, 'No tracks should be deleted');
-  like ($w, qr/Currently \$rs->delete\(\) does not generate proper SQL/, 'Delete join warning');
-}
-
-# test that delete_related w/conditions deletes just the matched related records only
-{
-  my $w;
-  local $SIG{__WARN__} = sub { $w = shift };
-
-  my $artist2 = $schema->resultset("Artist")->find(2);
-  my $artist2_tracks = $artist2->search_related('cds')->search_related('tracks')->count;
-  cmp_ok($artist2_tracks, '<', $total_tracks, 'need more tracks than related tracks');
-  
-  ok(!eval{$artist2->search_related('cds')->search_related('tracks')->delete});
-  cmp_ok($schema->resultset('Track')->count, '==', $total_tracks, 'No tracks should be deleted');
-  like ($w, qr/Currently \$rs->delete\(\) does not generate proper SQL/, 'Delete join warning');
-}
index 84d8ba5..39a51d3 100644 (file)
@@ -17,7 +17,7 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 18 );
+        : ( tests => 33 );
 }
 
 # figure out if we've got a version of sqlite that is older than 3.2.6, in
@@ -140,7 +140,7 @@ my $rs = $schema->resultset("CD")->search(
                          ] ] }
          );
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
 
@@ -148,7 +148,7 @@ $rs = $schema->resultset("CD")->search(
            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
            { join => 'artist' });
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
 
@@ -157,7 +157,7 @@ $rs = $schema->resultset("CD")->search(
              'liner_notes.notes' => 'Kill Yourself!' },
            { join => [ qw/artist liner_notes/ ] });
 
-cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
+is( $rs + 0, 1, "Single record in resultset");
 
 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
 
@@ -166,7 +166,7 @@ $rs = $schema->resultset("CD")->search(
     { 'artist' => 1 },
     { join => [qw/artist/], order_by => 'artist.name' }
 );
-cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
+is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
 
 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
   'Slicing beyond end of rs returns a zero count');
@@ -175,32 +175,83 @@ $rs = $schema->resultset("Artist")->search(
         { 'liner_notes.notes' => 'Kill Yourself!' },
         { join => { 'cds' => 'liner_notes' } });
 
-cmp_ok( $rs->count, '==', 1, "Single record in resultset");
+is( $rs->count, 1, "Single record in resultset");
 
 is($rs->first->name, 'We Are Goth', 'Correct record returned');
 
-# test for warnings on delete of joined resultset
-$rs = $schema->resultset("CD")->search(
-    { 'artist.name' => 'Caterwauler McCrae' },
-    { join => [qw/artist/]}
-);
-my $tst_delete_warning;
-eval {
-    local $SIG{__WARN__} = sub { $tst_delete_warning = shift };
-    $rs->delete();
-};
 
-ok( ($@ || $tst_delete_warning), 'fail/warning on attempt to delete a join-ed resultset');
-
-# test for warnings on update of joined resultset
-$rs = $schema->resultset("CD")->search(
-    { 'artist.name' => 'Random Boy Band' },
-    { join => [qw/artist/]}
-);
-my $tst_update_warning;
-eval {
-    local $SIG{__WARN__} = sub { $tst_update_warning = shift };
-    $rs->update({ 'artist' => 1 });
-};
-
-ok( ($@ || $tst_update_warning), 'fail/warning on attempt to update a join-ed resultset');
+{
+    $schema->populate('Artist', [
+        [ qw/artistid name/ ],
+        [ 4, 'Another Boy Band' ],
+    ]);
+    $schema->populate('CD', [
+        [ qw/cdid artist title year/ ],
+        [ 6, 2, "Greatest Hits", 2001 ],
+        [ 7, 4, "Greatest Hits", 2005 ],
+        [ 8, 4, "BoyBandBlues", 2008 ],
+    ]);
+    $schema->populate('TwoKeys', [
+        [ qw/artist cd/ ],
+        [ 2, 4 ],
+        [ 2, 6 ],
+        [ 4, 7 ],
+        [ 4, 8 ],
+    ]);
+    
+    sub cd_count {
+        return $schema->resultset("CD")->count;
+    }
+    sub tk_count {
+        return $schema->resultset("TwoKeys")->count;
+    }
+
+    is(cd_count(), 8, '8 rows in table cd');
+    is(tk_count(), 7, '7 rows in table twokeys');
+    sub artist1 {
+        return $schema->resultset("CD")->search(
+            { 'artist.name' => 'Caterwauler McCrae' },
+            { join => [qw/artist/]}
+        );
+    }
+    sub artist2 {
+        return $schema->resultset("CD")->search(
+            { 'artist.name' => 'Random Boy Band' },
+            { join => [qw/artist/]}
+        );
+    }
+
+    is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
+    ok( artist1()->delete, 'Successfully deleted 3 CDs' );
+    is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
+    is( artist2()->count, 2, '3 Random Boy Band CDs' );
+    ok( artist2()->update( { 'artist' => 1 } ) );
+    is( artist2()->count, 0, '0 Random Boy Band CDs' );
+    is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
+
+    # test update on multi-column-pk
+    sub tk1 {
+        return $schema->resultset("TwoKeys")->search(
+            {
+                'artist.name' => { like => '%Boy Band' },
+                'cd.title'    => 'Greatest Hits',
+            },
+            { join => [qw/artist cd/] }
+        );
+    }
+    sub tk2 {
+        return $schema->resultset("TwoKeys")->search(
+            { 'artist.name' => 'Caterwauler McCrae' },
+            { join => [qw/artist/]}
+        );
+    }
+    is( tk2()->count, 2, 'TwoKeys count == 2' );
+    is( tk1()->count, 2, 'TwoKeys count == 2' );
+    ok( tk1()->update( { artist => 1 } ) );
+    is( tk1()->count, 0, 'TwoKeys count == 0' );
+    is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
+    ok( tk2()->delete, 'Successfully deleted 4 CDs' );
+    is(cd_count(), 5, '5 rows in table cd');
+    is(tk_count(), 3, '3 rows in table twokeys');
+}
diff --git a/t/count/in_subquery.t b/t/count/in_subquery.t
new file mode 100644 (file)
index 0000000..1275c1e
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+
+use Test::More;
+
+plan ( tests => 1 );
+
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+
+my $schema = DBICTest->init_schema();
+
+{
+    my $rs = $schema->resultset("CD")->search(
+        { 'artist.name' => 'Caterwauler McCrae' },
+        { join => [qw/artist/]}
+    );
+    my $squery = $rs->get_column('cdid')->as_query;
+    my $subsel_rs = $schema->resultset("CD")->search( { cdid => { IN => $squery } } );
+    is($subsel_rs->count, $rs->count, 'Subselect on PK got the same row count');
+}
index 6865655..f849f7a 100644 (file)
@@ -75,5 +75,3 @@ TODO: {
     my $subsel_rs = $schema->resultset("CD")->search( { cdid => { IN => $rs->get_column('cdid')->as_query } } );
     cmp_ok($subsel_rs->count, '==', $rs->count, 'Subselect on PK got the same row count');
 }
-
-__END__