From: Michael G Schwern Date: Wed, 13 Feb 2008 05:14:09 +0000 (-0800) Subject: Emulate that CDBI throws out all changed columns and reloads them on X-Git-Tag: v0.08240~541^2~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c0fcc63f9a95819d353c5640ad2cc8d2b956ad63;p=dbsrgits%2FDBIx-Class.git Emulate that CDBI throws out all changed columns and reloads them on request in case the database modifies the new value (say, via a trigger) --- diff --git a/lib/DBIx/Class/CDBICompat/LazyLoading.pm b/lib/DBIx/Class/CDBICompat/LazyLoading.pm index add9390..b3d18e9 100644 --- a/lib/DBIx/Class/CDBICompat/LazyLoading.pm +++ b/lib/DBIx/Class/CDBICompat/LazyLoading.pm @@ -11,6 +11,22 @@ sub resultset_instance { return $rs; } + +# Emulate that CDBI throws out all changed columns and reloads them on +# request in case the database modifies the new value (say, via a trigger) +sub update { + my $self = shift; + + my @dirty_columns = keys %{$self->{_dirty_columns}}; + + my $ret = $self->next::method(@_); + + delete $self->{_column_data}{$_} for @dirty_columns; + + return $ret; +} + + sub get_column { my ($self, $col) = @_; if ((ref $self) && (!exists $self->{'_column_data'}{$col}) diff --git a/t/cdbi-t/04-lazy.t b/t/cdbi-t/04-lazy.t index 69b3549..5fd18f2 100644 --- a/t/cdbi-t/04-lazy.t +++ b/t/cdbi-t/04-lazy.t @@ -13,7 +13,7 @@ BEGIN { next; } eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 25); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 27); } INIT { @@ -79,3 +79,24 @@ eval { # Multiple false columns }; ok($@, $@); + +# Test that update() throws out columns that changed +{ + my $l = Lazy->create({ + this => 99, + that => 2, + oop => 3, + opop => 4, + }); + + $l->oop(32); + $l->update; + + ok $l->db_Main->do(qq{ + UPDATE @{[ $l->table ]} + SET oop = ? + WHERE this = ? + }, undef, 23, $l->this); + + is $l->oop, 23; +}