Fix for delete on full-table resultsets
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
index 4e5d1ee..da955a4 100644 (file)
@@ -655,26 +655,41 @@ Deletes the contents of the resultset from its result source.
 sub delete {
   my ($self) = @_;
   my $del = {};
-  $self->throw_exception("Can't delete on resultset with condition unless hash or array")
-    unless (ref($self->{cond}) eq 'HASH' || ref($self->{cond}) eq 'ARRAY');
-  if (ref $self->{cond} eq 'ARRAY') {
+
+  if (!ref($self->{cond})) {
+
+    # No-op. No condition, we're deleting everything
+
+  } elsif (ref $self->{cond} eq 'ARRAY') {
+
     $del = [ map { my %hash;
       foreach my $key (keys %{$_}) {
         $key =~ /([^.]+)$/;
         $hash{$1} = $_->{$key};
       }; \%hash; } @{$self->{cond}} ];
-  } elsif ((keys %{$self->{cond}})[0] eq '-and') {
-    $del->{-and} = [ map { my %hash;
-      foreach my $key (keys %{$_}) {
+
+  } elsif (ref $self->{cond} eq 'HASH') {
+
+    if ((keys %{$self->{cond}})[0] eq '-and') {
+
+      $del->{-and} = [ map { my %hash;
+        foreach my $key (keys %{$_}) {
+          $key =~ /([^.]+)$/;
+          $hash{$1} = $_->{$key};
+        }; \%hash; } @{$self->{cond}{-and}} ];
+
+    } else {
+
+      foreach my $key (keys %{$self->{cond}}) {
         $key =~ /([^.]+)$/;
-        $hash{$1} = $_->{$key};
-      }; \%hash; } @{$self->{cond}{-and}} ];
-  } else {
-    foreach my $key (keys %{$self->{cond}}) {
-      $key =~ /([^.]+)$/;
-      $del->{$1} = $self->{cond}{$key};
+        $del->{$1} = $self->{cond}{$key};
+      }
     }
+  } else {
+    $self->throw_exception(
+      "Can't delete on resultset with condition unless hash or array");
   }
+
   $self->result_source->storage->delete($self->result_source->from, $del);
   return 1;
 }