Applied mst fixes for delete on resultsetin [839] to update. Factored out common...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
index bd6ede7..08e81b5 100644 (file)
@@ -51,7 +51,7 @@ In the examples below, the following table classes are used:
 
 =head1 METHODS
 
-=head2 new 
+=head2 new
 
 =over 4
 
@@ -326,8 +326,8 @@ sub find {
       return keys %{$rs->{collapse}} ? $rs->next : $rs->single;
   } else {
       return keys %{$self->{collapse}} ?
-       $self->search($query)->next :
-       $self->single($query);
+        $self->search($query)->next :
+        $self->single($query);
   }
 }
 
@@ -400,7 +400,7 @@ sub single {
   if ($where) {
     if (defined $attrs->{where}) {
       $attrs->{where} = {
-        '-and' => 
+        '-and' =>
             [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
                $where, delete $attrs->{where} ]
       };
@@ -464,12 +464,13 @@ three records, call:
 
 sub slice {
   my ($self, $min, $max) = @_;
-  my $attrs = { %{ $self->{attrs} || {} } };
-  $attrs->{offset} ||= 0;
+  my $attrs = {}; # = { %{ $self->{attrs} || {} } };
+  $attrs->{offset} = $self->{attrs}{offset} || 0;
   $attrs->{offset} += $min;
   $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
-  my $slice = (ref $self)->new($self->result_source, $attrs);
-  return (wantarray ? $slice->all : $slice);
+  return $self->search(undef(), $attrs);
+  #my $slice = (ref $self)->new($self->result_source, $attrs);
+  #return (wantarray ? $slice->all : $slice);
 }
 
 =head2 next
@@ -491,6 +492,10 @@ Can be used to efficiently iterate over records in the resultset:
     print $cd->title;
   }
 
+Note that you need to store the resultset object, and call C<next> on it. 
+Calling C<< resultset('Table')->next >> repeatedly will always return the
+first record from the resultset.
+
 =cut
 
 sub next {
@@ -504,8 +509,8 @@ sub next {
     return ($self->all)[0];
   }
   my @row = (exists $self->{stashed_row} ?
-              @{delete $self->{stashed_row}} :
-              $self->cursor->next
+               @{delete $self->{stashed_row}} :
+               $self->cursor->next
   );
 #  warn Dumper(\@row); use Data::Dumper;
   return unless (@row);
@@ -562,7 +567,7 @@ sub _collapse_result {
   my @collapse;
   if (defined $prefix) {
     @collapse = map {
-       m/^\Q${prefix}.\E(.+)$/ ? ($1) : ()
+        m/^\Q${prefix}.\E(.+)$/ ? ($1) : ()
     } keys %{$self->{collapse}}
   } else {
     @collapse = keys %{$self->{collapse}};
@@ -581,7 +586,7 @@ sub _collapse_result {
     my (@final, @raw);
     while ( !(grep {
                 !defined($tree->[0]->{$_}) ||
-               $co_check{$_} ne $tree->[0]->{$_}
+                $co_check{$_} ne $tree->[0]->{$_}
               } @co_key) ) {
       push(@final, $tree);
       last unless (@raw = $self->cursor->next);
@@ -662,7 +667,7 @@ sub _count { # Separated out so pager can get the full count
           @distinct = ($column);
           last;
         }
-      } 
+      }
     }
 
     $select = { count => { distinct => \@distinct } };
@@ -778,6 +783,59 @@ sub first {
   return $_[0]->reset->next;
 }
 
+# _cond_for_update_delete
+#
+# update/delete require the condition to be modified to handle
+# the differing SQL syntax available.  This transforms the $self->{cond}
+# appropriately, returning the new condition
+
+sub _cond_for_update_delete {
+  my ($self) = @_;
+  my $cond = {};
+
+  if (!ref($self->{cond})) {
+    # No-op. No condition, we're update/deleting everything
+  }
+  elsif (ref $self->{cond} eq 'ARRAY') {
+    $cond = [
+      map {
+        my %hash;
+        foreach my $key (keys %{$_}) {
+          $key =~ /([^.]+)$/;
+          $hash{$1} = $_->{$key};
+        }
+        \%hash;
+        } @{$self->{cond}}
+    ];
+  }
+  elsif (ref $self->{cond} eq 'HASH') {
+    if ((keys %{$self->{cond}})[0] eq '-and') {
+      $cond->{-and} = [
+        map {
+          my %hash;
+          foreach my $key (keys %{$_}) {
+            $key =~ /([^.]+)$/;
+            $hash{$1} = $_->{$key};
+          }
+          \%hash;
+          } @{$self->{cond}{-and}}
+      ];
+    }
+    else {
+      foreach my $key (keys %{$self->{cond}}) {
+        $key =~ /([^.]+)$/;
+        $cond->{$1} = $self->{cond}{$key};
+      }
+    }
+  }
+  else {
+    $self->throw_exception(
+               "Can't update/delete on resultset with condition unless hash or array");
+  }
+  return $cond;
+}
+
+
 =head2 update
 
 =over 4
@@ -798,8 +856,11 @@ sub update {
   my ($self, $values) = @_;
   $self->throw_exception("Values for update must be a hash")
     unless ref $values eq 'HASH';
+
+  my $cond = $self->_cond_for_update_delete;
+
   return $self->result_source->storage->update(
-    $self->result_source->from, $values, $self->{cond}
+    $self->result_source->from, $values, $cond
   );
 }
 
@@ -848,43 +909,9 @@ sub delete {
   my ($self) = @_;
   my $del = {};
 
-  if (!ref($self->{cond})) {
-
-    # No-op. No condition, we're deleting everything
-
-  } elsif (ref $self->{cond} eq 'ARRAY') {
+  my $cond = $self->_cond_for_update_delete;
 
-    $del = [ map { my %hash;
-      foreach my $key (keys %{$_}) {
-        $key =~ /([^.]+)$/;
-        $hash{$1} = $_->{$key};
-      }; \%hash; } @{$self->{cond}} ];
-
-  } 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 =~ /([^.]+)$/;
-        $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);
+  $self->result_source->storage->delete($self->result_source->from, $cond);
   return 1;
 }
 
@@ -1420,7 +1447,7 @@ below.
 
 =back
 
-Contains one or more relationships that should be fetched along with the main 
+Contains one or more relationships that should be fetched along with the main
 query (when they are accessed afterwards they will have already been
 "prefetched").  This is useful for when you know you will need the related
 objects, because it saves at least one query:
@@ -1472,7 +1499,7 @@ avoid using C<from> unless you cannot achieve the desired result using C<join>.
 In simple terms, C<from> works as follows:
 
     [
-        { <alias> => <table>, -join-type => 'inner|left|right' }
+        { <alias> => <table>, -join_type => 'inner|left|right' }
         [] # nested JOIN (optional)
         { <table.column> => <foreign_table.foreign_key> }
     ]
@@ -1535,7 +1562,7 @@ with a father in the person table, we could explicitly use C<INNER JOIN>:
             from => [
                 { child => 'person' },
                 [
-                    { father => 'person', -join-type => 'inner' },
+                    { father => 'person', -join_type => 'inner' },
                     { 'father.id' => 'child.father_id' }
                 ],
             ]
@@ -1581,6 +1608,20 @@ A arrayref of columns to group by. Can include columns of joined tables.
 
   group_by => [qw/ column1 column2 ... /]
 
+=head2 having
+
+=over 4
+
+=item Value: $condition
+
+=back
+
+HAVING is a select statement attribute that is applied between GROUP BY and
+ORDER BY. It is applied to the after the grouping calculations have been
+done. 
+
+  having => { 'count(employee)' => { '>=', 100 } }
+
 =head2 distinct
 
 =over 4
@@ -1602,7 +1643,7 @@ revisit rows in your ResultSet:
     ... do stuff ...
   }
 
-  $rs->first; # without cache, this would issue a query 
+  $rs->first; # without cache, this would issue a query
 
 By default, searches are not cached.