Hi, this combined patch for DBIx-Class/0.08/trunk

On Fri, Apr 11, 2008 at 9:24 PM, Matt S Trout <[EMAIL PROTECTED]> wrote:
> On Fri, Apr 11, 2008 at 12:00:23PM +0400, Oleg Pronin wrote:
>  > Yeah, that would be great! something like 'reload' or 'refresh'. Should we
>  > post a patch for code and test, or somebody else will do this?
>
>  No, reload_columns or refresh_from_storage or something - can't add new
>  one-word methods really for fear of clashing with people's column names.
>
>  I was assuming/hoping you'd combine your existing code+test into a complete
>  patch I can apply before we start the 08100 release cycle :)
>
>
>
>  > 2008/4/10, Matt S Trout <[EMAIL PROTECTED]>:
>  > >
>  > > On Tue, Mar 25, 2008 at 11:54:24AM +0300, Oleg Pronin wrote:
>  > >
>  > > > Hello.
>  > > >
>  > > > I think 'discard_changes' should be upgraded because:
>  > > > - it clears all the data in object. Suppose there was non-db data in 
> the
>  > > > object:
>  > > >      __PACKAGE__->mk_accessors(....);
>  > > >   'discard_changes' clears it all. I think this is not good.
>  > >
>  > >
>  > > But it -is- how we've behaved for a while.
>  > >
>  > > How about adding a 'reload_columns' method that just reloads columns, and
>  > > make discard_changes proxy to that?
>  > >
>  > >
>  > > > - it does not accept $attrs. For example there is often a need to
>  > > refresh
>  > > > object from database with
>  > > >   exclusive lock, like $row->discard_changes({lock_for => 'update'});
>  > > >
>  > > > - I think it should fetch only data without a need of constructing a
>  > > heavy
>  > > > object. I.e.
>  > > >
>  > > >     my ($self, $attrs) = @_;
>  > > >     return unless $self->in_storage;
>  > > >
>  > > >     my $rs = $self->result_source->resultset;
>  > > >     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
>  > > >
>  > > >     my $hash_ref = $rs->find({
>  > > >         map { $_ => $self->get_column($_) } $self->primary_columns,
>  > > >     }, $attrs);
>  > > >
>  > > >     $self->set_columns($hash_ref);
>  > > >     delete $self->{_dirty_columns};
>  > > >
>  > > >
>  > > > With best regards.
>  > >
>  > >
>  > > > _______________________________________________
>  > > > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>  > > > IRC: irc.perl.org#dbix-class
>  > > > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>  > > > Searchable Archive:
>  > > http://www.grokbase.com/group/[EMAIL PROTECTED]
>  > >
>  > >
>  > > --
>  > >
>  > >       Matt S Trout       Need help with your Catalyst or DBIx::Class
>  > > project?
>  > >    Technical Director
>  > > http://www.shadowcat.co.uk/catalyst/
>  > >   Shadowcat Systems Ltd.  Want a managed development or deployment
>  > > platform?
>  > > http://chainsawblues.vox.com/
>  > > http://www.shadowcat.co.uk/servers/
>  > >
>  > > _______________________________________________
>  > > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>  > > IRC: irc.perl.org#dbix-class
>  > > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>  > > Searchable Archive:
>  > > http://www.grokbase.com/group/[EMAIL PROTECTED]
>  > >
>
>  > _______________________________________________
>  > List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>  > IRC: irc.perl.org#dbix-class
>  > SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>  > Searchable Archive: http://www.grokbase.com/group/[EMAIL PROTECTED]
>
>  --
>       Matt S Trout       Need help with your Catalyst or DBIx::Class project?
>    Technical Director                    http://www.shadowcat.co.uk/catalyst/
>   Shadowcat Systems Ltd.  Want a managed development or deployment platform?
>  http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/
>
>  _______________________________________________
>  List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>  IRC: irc.perl.org#dbix-class
>  SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>  Searchable Archive: http://www.grokbase.com/group/[EMAIL PROTECTED]
>
Index: t/reload_columns.t
===================================================================
--- t/reload_columns.t	(revision 0)
+++ t/reload_columns.t	(revision 0)
@@ -0,0 +1,33 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+plan tests => 4;
+
+$schema->class('CD')->mk_group_accessors(simple => qw/external_data/);
+Class::C3->reinitialize;
+
+# external accessors test
+my $cd = $schema->resultset("CD")->find(3);
+$cd->external_data('some value');
+
+is( $cd->external_data, 'some value', 'simple work');
+$cd->reload_columns;
+
+is( $cd->external_data, 'some value', 'work after discard_changes');
+
+# attrs test (we need eval, because sqlite dosn't support FOR UPDATE syntax)
+my @storage_trace = ();
+$schema->storage->debugcb(sub { push @storage_trace, { op => $_[0], info => $_[1] } });
+$schema->storage->debug(1);
+
+$cd = $schema->resultset("CD")->find(3);
+eval { $cd->reload_columns({for => 'update'}) };
+
+is( scalar(@storage_trace), 2, 'we got info for both statements');
+like( $storage_trace[1]->{info}, qr/FOR UPDATE/, 'discard_changes process attrs');
Index: lib/DBIx/Class/PK.pm
===================================================================
--- lib/DBIx/Class/PK.pm	(revision 4257)
+++ lib/DBIx/Class/PK.pm	(working copy)
@@ -2,6 +2,7 @@
 
 use strict;
 use warnings;
+use DBIx::Class::ResultClass::HashRefInflator;
 
 use base qw/DBIx::Class::Row/;
 
@@ -57,6 +58,31 @@
   return $self;
 }
 
+=head2 reload_columns
+
+    $row->reload_columns(\%attrs?)
+
+Reloads from storage.
+
+It's using HashRefInflator to load columns for fast.
+
+=cut
+
+sub reload_columns {
+    my ($self, $attrs) = @_;
+    return unless $self->in_storage;
+
+    my $rs = $self->result_source->resultset;
+    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
+
+    my $hash_ref = $rs->find({
+        map { $_ => $self->get_column($_) } $self->primary_columns,
+    }, $attrs);
+
+    $self->set_columns($hash_ref);
+    delete $self->{_dirty_columns};
+}
+
 =head2 id
 
 Returns the primary key(s) for a row. Can't be called as
Index: lib/DBIx/Class/Row.pm
===================================================================
--- lib/DBIx/Class/Row.pm	(revision 4257)
+++ lib/DBIx/Class/Row.pm	(working copy)
@@ -798,6 +798,12 @@
 changes made since the row was last read from storage. Actually
 implemented in L<DBIx::Class::PK>
 
+=head2 reload_columns
+
+    $row->reload_columns(\%attrs?)
+
+Reloads from storage. Actually in L<DBIx::Class::PK>
+
 =cut
 
 1;
_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[EMAIL PROTECTED]

Reply via email to