Merge 'trunk' into 'DBIx-Class-current'
David Kamholz [Wed, 22 Nov 2006 22:10:30 +0000 (22:10 +0000)]
r18925@haferschleim (orig r2919):  ningu | 2006-11-21 22:44:26 -0800
- slight optimization to ident_condition in PK.pm
- get ident_cond in update() before applying arguments, so a column's pk can be updated
r18926@haferschleim (orig r2920):  ningu | 2006-11-21 22:54:10 -0800
revert update() change
r18953@haferschleim (orig r2922):  ningu | 2006-11-22 12:12:43 -0800
re-commit minimal pk-mutation in update(), with test
r18956@haferschleim (orig r2924):  ningu | 2006-11-22 14:09:07 -0800
add shallow copy of $attrs in ResultSet->new

lib/DBIx/Class/PK.pm
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/Row.pm
t/69update.t

index ea22a21..9b9f8a4 100644 (file)
@@ -99,8 +99,8 @@ Produces a condition hash to locate a row based on the primary key(s).
 sub ident_condition {
   my ($self, $alias) = @_;
   my %cond;
-  $cond{(defined $alias ? "${alias}.$_" : $_)} = $self->get_column($_)
-    for $self->primary_columns;
+  my $prefix = defined $alias ? $alias.'.' : '';
+  $cond{$prefix.$_} = $self->get_column($_) for $self->primary_columns;
   return \%cond;
 }
 
index b6fbb05..8eff3f6 100644 (file)
@@ -85,6 +85,7 @@ sub new {
 
   my ($source, $attrs) = @_;
   #weaken $source;
+  $attrs = { %{$attrs||{}} };
 
   if ($attrs->{page}) {
     $attrs->{rows} ||= 10;
index 7d9298d..ae72e6f 100644 (file)
@@ -108,12 +108,12 @@ required.
 sub update {
   my ($self, $upd) = @_;
   $self->throw_exception( "Not in database" ) unless $self->in_storage;
-  $self->set_columns($upd) if $upd;
-  my %to_update = $self->get_dirty_columns;
-  return $self unless keys %to_update;
   my $ident_cond = $self->ident_condition;
   $self->throw_exception("Cannot safely update a row in a PK-less table")
     if ! keys %$ident_cond;
+  $self->set_columns($upd) if $upd;
+  my %to_update = $self->get_dirty_columns;
+  return $self unless keys %to_update;
   my $rows = $self->result_source->storage->update(
                $self->result_source->from, \%to_update, $ident_cond);
   if ($rows == 0) {
index 3372b4f..b11ebde 100644 (file)
@@ -9,7 +9,7 @@ my $schema = DBICTest->init_schema();
 
 BEGIN {
         eval "use DBD::SQLite";
-        plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 3);
+        plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5);
 }                                                                               
 
 my $art = $schema->resultset("Artist")->find(1);
@@ -27,3 +27,6 @@ ok($art->name($name) eq $name, 'update');
 
 $art->discard_changes;
 
+ok($art->update({ artistid => 100 }), 'update allows pk mutation');
+
+is($art->artistid, 100, 'pk mutation applied');