X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FFilterColumn.pm;h=45b798c0f643ee50d3b6f9936e3396f8294d22bf;hb=f43ea814d6c4c181aeaac244c5db17058aac3d5e;hp=2f9b4f113b80a1f54d1d712461ab7e7842b242d3;hpb=85439e0c6d096fb21fdcb1d65140ddef286dfcc9;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/FilterColumn.pm b/lib/DBIx/Class/FilterColumn.pm index 2f9b4f1..45b798c 100644 --- a/lib/DBIx/Class/FilterColumn.pm +++ b/lib/DBIx/Class/FilterColumn.pm @@ -7,6 +7,10 @@ use base qw/DBIx::Class::Row/; sub filter_column { my ($self, $col, $attrs) = @_; + $self->throw_exception("FilterColumn does not work with InflateColumn") + if $self->isa('DBIx::Class::InflateColumn') && + defined $self->column_info($col)->{_inflate_info}; + $self->throw_exception("No such column $col to filter") unless $self->has_column($col); @@ -30,7 +34,7 @@ sub _column_from_storage { return $value unless exists $info->{_filter_info}; my $filter = $info->{_filter_info}{filter_from_storage}; - $self->throw_exception("No inflator for $col") unless defined $filter; + $self->throw_exception("No filter for $col") unless defined $filter; return $self->$filter($value); } @@ -62,7 +66,28 @@ sub get_filtered_column { return $self->{_filtered_column}{$col} = $self->_column_from_storage($col, $val); } -sub set_column { +sub get_column { + my ($self, $col) = @_; + if (exists $self->{_filtered_column}{$col}) { + return $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}); + } + + return $self->next::method ($col); +} + +# sadly a separate codepath in Row.pm ( used by insert() ) +sub get_columns { + my $self = shift; + + foreach my $col (keys %{$self->{_filtered_column}||{}}) { + $self->{_column_data}{$col} ||= $self->_column_to_storage ($col, $self->{_filtered_column}{$col}) + if exists $self->{_filtered_column}{$col}; + } + + $self->next::method (@_); +} + +sub store_column { my ($self, $col) = (shift, @_); # blow cache @@ -74,23 +99,38 @@ sub set_column { sub set_filtered_column { my ($self, $col, $filtered) = @_; - $self->set_column($col, $self->_column_to_storage($col, $filtered)); + # do not blow up the cache via set_column unless necessary + # (filtering may be expensive!) + if (exists $self->{_filtered_column}{$col}) { + return $filtered + if ($self->_eq_column_values ($col, $filtered, $self->{_filtered_column}{$col} ) ); - delete $self->{_filtered_column}{$col}; + $self->make_column_dirty ($col); # so the comparison won't run again + } + + $self->set_column($col, $self->_column_to_storage($col, $filtered)); - return $filtered; + return $self->{_filtered_column}{$col} = $filtered; } sub update { my ($self, $attrs, @rest) = @_; + foreach my $key (keys %{$attrs||{}}) { - if ($self->has_column($key) && - exists $self->column_info($key)->{_filter_info}) { - my $val = delete $attrs->{$key}; - $self->set_filtered_column($key, $val); - $attrs->{$key} = $self->_column_to_storage($key, $val) + if ( + $self->has_column($key) + && + exists $self->column_info($key)->{_filter_info} + ) { + $self->set_filtered_column($key, delete $attrs->{$key}); + + # FIXME update() reaches directly into the object-hash + # and we may *not* have a filtered value there - thus + # the void-ctx filter-trigger + $self->get_column($key) unless exists $self->{_column_data}{$key}; } } + return $self->next::method($attrs, @rest); } @@ -103,10 +143,10 @@ sub new { foreach my $key (keys %{$attrs||{}}) { if ($obj->has_column($key) && exists $obj->column_info($key)->{_filter_info} ) { - my $val = delete $attrs->{$key}; - $obj->set_filtered_column($key, $val); + $obj->set_filtered_column($key, $attrs->{$key}); } } + return $obj; } @@ -163,11 +203,3 @@ Returns the filtered value of the column $obj->set_filtered_column(colname => 'new_value') Sets the filtered value of the column - -=head2 update - -Just overridden to filter changed columns through this component - -=head2 new - -Just overridden to filter columns through this component