Merge 'trunk' into 'DBIx-Class-current'
Matt S Trout [Thu, 23 Mar 2006 18:05:16 +0000 (18:05 +0000)]
r9088@obrien (orig r1305):  matthewt | 2006-03-22 11:52:30 +0000
Nuked ancient and unused classdata entry
r9091@obrien (orig r1306):  jguenther | 2006-03-22 18:16:27 +0000
fixed unclear documentation regarding context in txn_do()
r9092@obrien (orig r1307):  castaway | 2006-03-22 22:41:38 +0000
Fix up is_auto_increment doc

r9093@obrien (orig r1308):  castaway | 2006-03-22 22:46:30 +0000
Fixup sequence docs

r9100@obrien (orig r1315):  wdh | 2006-03-23 18:02:43 +0000
add basic cache tests/docs to trunk

lib/DBIx/Class/Relationship/Base.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/Schema.pm
t/run/23cache.tl

index 0c7a141..343c0d0 100644 (file)
@@ -5,8 +5,6 @@ use warnings;
 
 use base qw/DBIx::Class/;
 
-__PACKAGE__->mk_classdata('_relationships', { } );
-
 =head1 NAME 
 
 DBIx::Class::Relationship::Base - Inter-table relationships
index e03a16d..8647918 100644 (file)
@@ -1252,6 +1252,21 @@ A arrayref of columns to group by. Can include columns of joined tables.
 
 Set to 1 to group by all columns.
 
+=head2 cache
+
+Set to 1 to cache search results. This prevents extra SQL queries if you
+revisit rows in your ResultSet:
+
+  my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
+  
+  while( my $artist = $resultset->next ) {
+    ... do stuff ...
+  }
+
+  $rs->first; # without cache, this would issue a query 
+
+By default, searches are not cached.
+
 For more examples of using these attributes, see
 L<DBIx::Class::Manual::Cookbook>.
 
index e2e2d74..1a78f29 100644 (file)
@@ -91,7 +91,8 @@ If the column is allowed to contain NULL values, set a true value
 =item is_auto_increment
 
 Set this to a true value if this is a column that is somehow
-automatically filled. This is currently not used by DBIx::Class.
+automatically filled. This is used to determine which columns to empty
+when cloning objects using C<copy>.
 
 =item is_foreign_key
 
@@ -106,11 +107,9 @@ currently not used by DBIx::Class.
 
 =item sequence
 
-If your column is using a sequence to create it's values, set the name
-of the sequence here, to allow the values to be retrieved
-automatically by the L<DBIx::Class::PK::Auto> module. PK::Auto will
-attempt to retrieve the sequence name from the database, if this value
-is left unset.
+Sets the name of the sequence to use to generate values.  If not 
+specified, L<DBIx::Class::PK::Auto> will attempt to retrieve the 
+name of the sequence from the database automatically.
 
 =back
 
index fc28f79..3d63657 100644 (file)
@@ -455,7 +455,9 @@ sub txn_do {
 
   $self->txn_begin; # If this throws an exception, no rollback is needed
 
-  my $wantarray = wantarray; # Need to save this since it's reset in eval{}
+  my $wantarray = wantarray; # Need to save this since the context
+                            # inside the eval{} block is independent
+                            # of the context that called txn_do()
 
   eval {
     # Need to differentiate between scalar/list context to allow for
index 749ce81..4be8fbd 100644 (file)
@@ -3,7 +3,7 @@ my $schema = shift;
 
 eval "use DBD::SQLite";
 plan skip_all => 'needs DBD::SQLite for testing' if $@;
-plan tests => 17;
+plan tests => 23;
 
 my $rs = $schema->resultset("Artist")->search(
   { artistid => 1 }
@@ -13,6 +13,54 @@ my $artist = $rs->first;
 
 is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
 
+$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
+my $artists = [ $rs->all ];
+
+is( scalar @{$rs->get_cache}, 3, 'all() populates cache for search with cache attribute' );
+
+$rs->clear_cache;
+
+is( scalar @{$rs->get_cache}, 0, 'clear_cache is functional' );
+
+$rs->next;
+
+is( scalar @{$rs->get_cache}, 3, 'next() populates cache for search with cache attribute' );
+
+pop( @$artists );
+$rs->set_cache( $artists );
+
+is( scalar @{$rs->get_cache}, 2, 'set_cache() is functional' );
+
+$cd = $schema->resultset('CD')->find(1);
+
+$rs->clear_cache;
+
+eval {
+  $rs->set_cache( [ $cd ] );
+};
+
+is( scalar @{$rs->get_cache}, 0, 'set_cache() only accepts objects of correct type for the resultset' );
+
+unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
+DBI->trace(1, 't/var/dbic.trace');
+
+$rs = $schema->resultset('Artist')->search( undef, { cache => 1 } );
+while( $artist = $rs->next ) {}
+$artist = $rs->first();
+
+# count the SELECTs
+DBI->trace(0, undef);
+my $selects = 0;
+$trace = IO::File->new('t/var/dbic.trace', '<') 
+    or die "Unable to read trace file";
+while (<$trace>) {
+    $selects++ if /SELECT/;
+}
+$trace->close;
+unlink 't/var/dbic.trace';
+
+is( $selects, 1, 'revisiting a row does not issue a query when cache => 1' );
+
 my @a = $schema->resultset("Artist")->search(
   { },
   {
@@ -57,7 +105,7 @@ is( $artist->count_related('cds'), 3, 'artist->count_related returns correct val
 
 # count the SELECTs
 DBI->trace(0, undef);
-my $selects = 0;
+$selects = 0;
 my $trace = IO::File->new('t/var/dbic.trace', '<') 
     or die "Unable to read trace file";
 while (<$trace>) {