introduce row caching using related_resultset; has_many prefetch (single-level only...
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Row.pm
index 0d4a8a4..85a70a8 100644 (file)
@@ -66,6 +66,7 @@ sub insert {
   $source->storage->insert($source->from, { $self->get_columns });
   $self->in_storage(1);
   $self->{_dirty_columns} = {};
+  $self->{related_resultsets} = {};
   return $self;
 }
 
@@ -109,6 +110,7 @@ sub update {
     $self->throw_exception("Can't update ${self}: updated more than one row");
   }
   $self->{_dirty_columns} = {};
+  $self->{related_resultsets} = {};
   return $self;
 }
 
@@ -292,25 +294,35 @@ sub inflate_result {
                   },
                   ref $class || $class);
   my $schema;
-  PRE: foreach my $pre (keys %{$prefetch||{}}) {
-    my $pre_source = $source->related_source($pre);
-    $class->throw_exception("Can't prefetch non-existant relationship ${pre}") unless $pre_source;
-    my $fetched;
-    unless ($pre_source->primary_columns == grep { exists $prefetch->{$pre}[0]{$_} 
-       and !defined $prefetch->{$pre}[0]{$_} } $pre_source->primary_columns)
-    {
-      $fetched = $pre_source->result_class->inflate_result(
-                      $pre_source, @{$prefetch->{$pre}});      
+  foreach my $pre (keys %{$prefetch||{}}) {
+    my $pre_val = $prefetch->{$pre};
+    # if first prefetch item is arrayref, assume this is a has_many prefetch
+    # and that objects are pre inflated (TODO: check arrayref contents using "ref" to make sure)
+    if( ref $pre_val->[0] eq 'ARRAY' ) {
+      $new->related_resultset($pre)->set_cache( $pre_val->[0] );
     }
-    my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
-    $class->throw_exception("No accessor for prefetched $pre")
-      unless defined $accessor;
-    if ($accessor eq 'single') {
-      $new->{_relationship_data}{$pre} = $fetched;
-    } elsif ($accessor eq 'filter') {
-      $new->{_inflated_column}{$pre} = $fetched;
-    } else {
-      $class->throw_exception("Don't know how to store prefetched $pre");
+    else {
+      my $pre_source = $source->related_source($pre);
+      $class->throw_exception("Can't prefetch non-existent relationship ${pre}") unless $pre_source;
+      my $fetched;
+      unless ($pre_source->primary_columns == grep { exists $prefetch->{$pre}[0]{$_} 
+         and !defined $prefetch->{$pre}[0]{$_} } $pre_source->primary_columns)
+      {
+        $fetched = $pre_source->result_class->inflate_result(
+                      $pre_source, @{$prefetch->{$pre}});      
+      }
+      my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
+      $class->throw_exception("No accessor for prefetched $pre")
+       unless defined $accessor;
+      if ($accessor eq 'single') {
+        $new->{_relationship_data}{$pre} = $fetched;
+      } elsif ($accessor eq 'filter') {
+       $new->{_inflated_column}{$pre} = $fetched;
+      } elsif ($accessor eq 'multi') {
+       $class->throw_exception("Cache must be enabled for has_many prefetch '$pre'");
+      } else {
+       $class->throw_exception("Prefetch not supported with accessor '$accessor'");
+      }
     }
   }
   return $new;