[Bast-commits] r7898 - in DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond: lib/DBIx/Class/Storage t/prefetch

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Sun Nov 15 11:39:30 GMT 2009


Author: ribasushi
Date: 2009-11-15 11:39:29 +0000 (Sun, 15 Nov 2009)
New Revision: 7898

Modified:
   DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBI.pm
   DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBIHacks.pm
   DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/t/prefetch/via_search_related.t
Log:
Move more code to DBIHacks, put back the update/delete rs check, just in case

Modified: DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBI.pm	2009-11-15 11:24:43 UTC (rev 7897)
+++ DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBI.pm	2009-11-15 11:39:29 UTC (rev 7898)
@@ -1565,62 +1565,6 @@
   return $self->_execute('delete' => [], $source, $bind_attrs, @args);
 }
 
-# Most databases do not allow aliasing of tables in UPDATE/DELETE. Thus
-# a condition containing 'me' or other table prefixes will not work
-# at all. What this code tries to do (badly) is introspect the condition
-# and remove all column qualifiers. If it bails out early (returns undef)
-# the calling code should try another approach (e.g. a subquery)
-sub _strip_cond_qualifiers {
-  my ($self, $where) = @_;
-
-  my $cond = {};
-
-  # No-op. No condition, we're updating/deleting everything
-  return $cond unless $where;
-
-  if (ref $where eq 'ARRAY') {
-    $cond = [
-      map {
-        my %hash;
-        foreach my $key (keys %{$_}) {
-          $key =~ /([^.]+)$/;
-          $hash{$1} = $_->{$key};
-        }
-        \%hash;
-      } @$where
-    ];
-  }
-  elsif (ref $where eq 'HASH') {
-    if ( (keys %$where) == 1 && ( (keys %{$where})[0] eq '-and' )) {
-      $cond->{-and} = [];
-      my @cond = @{$where->{-and}};
-       for (my $i = 0; $i < @cond; $i++) {
-        my $entry = $cond[$i];
-        my $hash;
-        if (ref $entry eq 'HASH') {
-          $hash = $self->_strip_cond_qualifiers($entry);
-        }
-        else {
-          $entry =~ /([^.]+)$/;
-          $hash->{$1} = $cond[++$i];
-        }
-        push @{$cond->{-and}}, $hash;
-      }
-    }
-    else {
-      foreach my $key (keys %$where) {
-        $key =~ /([^.]+)$/;
-        $cond->{$1} = $where->{$key};
-      }
-    }
-  }
-  else {
-    return undef;
-  }
-
-  return $cond;
-}
-
 # We were sent here because the $rs contains a complex search
 # which will require a subquery to select the correct rows
 # (i.e. joined or limited resultsets, or non-introspectable conditions)
@@ -1634,8 +1578,22 @@
 
   my $rsrc = $rs->result_source;
 
+  # quick check if we got a sane rs on our hands
   my @pcols = $rsrc->primary_columns;
 
+  my $sel = $rs->_resolved_attrs->{select};
+  $sel = [ $sel ] unless ref $sel eq 'ARRAY';
+
+  if (
+      join ("\x00", map { join '.', $rs->{attrs}{alias}, $_ } sort @pcols)
+        ne
+      join ("\x00", sort @$sel )
+  ) {
+    $self->throw_exception (
+      '_subq_update_delete can not be called on resultsets selecting columns other than the primary keys'
+    );
+  }
+
   if (@pcols == 1) {
     return $self->$op (
       $rsrc,

Modified: DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBIHacks.pm
===================================================================
--- DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBIHacks.pm	2009-11-15 11:24:43 UTC (rev 7897)
+++ DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/lib/DBIx/Class/Storage/DBIHacks.pm	2009-11-15 11:39:29 UTC (rev 7898)
@@ -402,4 +402,61 @@
   return \@new_from;
 }
 
+# Most databases do not allow aliasing of tables in UPDATE/DELETE. Thus
+# a condition containing 'me' or other table prefixes will not work
+# at all. What this code tries to do (badly) is introspect the condition
+# and remove all column qualifiers. If it bails out early (returns undef)
+# the calling code should try another approach (e.g. a subquery)
+sub _strip_cond_qualifiers {
+  my ($self, $where) = @_;
+
+  my $cond = {};
+
+  # No-op. No condition, we're updating/deleting everything
+  return $cond unless $where;
+
+  if (ref $where eq 'ARRAY') {
+    $cond = [
+      map {
+        my %hash;
+        foreach my $key (keys %{$_}) {
+          $key =~ /([^.]+)$/;
+          $hash{$1} = $_->{$key};
+        }
+        \%hash;
+      } @$where
+    ];
+  }
+  elsif (ref $where eq 'HASH') {
+    if ( (keys %$where) == 1 && ( (keys %{$where})[0] eq '-and' )) {
+      $cond->{-and} = [];
+      my @cond = @{$where->{-and}};
+       for (my $i = 0; $i < @cond; $i++) {
+        my $entry = $cond[$i];
+        my $hash;
+        if (ref $entry eq 'HASH') {
+          $hash = $self->_strip_cond_qualifiers($entry);
+        }
+        else {
+          $entry =~ /([^.]+)$/;
+          $hash->{$1} = $cond[++$i];
+        }
+        push @{$cond->{-and}}, $hash;
+      }
+    }
+    else {
+      foreach my $key (keys %$where) {
+        $key =~ /([^.]+)$/;
+        $cond->{$1} = $where->{$key};
+      }
+    }
+  }
+  else {
+    return undef;
+  }
+
+  return $cond;
+}
+
+
 1;

Modified: DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/t/prefetch/via_search_related.t
===================================================================
--- DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/t/prefetch/via_search_related.t	2009-11-15 11:24:43 UTC (rev 7897)
+++ DBIx-Class/0.08/branches/prefetch_bug-unqualified_column_in_search_related_cond/t/prefetch/via_search_related.t	2009-11-15 11:39:29 UTC (rev 7898)
@@ -37,7 +37,6 @@
 
 }, 'search_related prefetch with order_by works');
 
-
 lives_ok ( sub {
   my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
     {




More information about the Bast-commits mailing list