X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FLazyLoading.pm;h=a9e41af5178e0422220484a1282d8eb6755d8914;hb=5e0eea3522876a30453af24097507198bbbc9409;hp=8a7c17b533da57a2ce65251eba073fda1766017c;hpb=a9433341369da32eafd7509f49477b44c84bcbeb;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/LazyLoading.pm b/lib/DBIx/Class/CDBICompat/LazyLoading.pm index 8a7c17b..a9e41af 100644 --- a/lib/DBIx/Class/CDBICompat/LazyLoading.pm +++ b/lib/DBIx/Class/CDBICompat/LazyLoading.pm @@ -1,15 +1,59 @@ -package DBIx::Class::CDBICompat::LazyLoading; +package # hide from PAUSE + DBIx::Class::CDBICompat::LazyLoading; use strict; use warnings; +use base 'DBIx::Class'; + sub resultset_instance { my $self = shift; my $rs = $self->next::method(@_); - $rs = $rs->search(undef, { cols => [ $self->columns('Essential') ] }); + $rs = $rs->search(undef, { columns => [ $self->columns('Essential') ] }); 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(@_); + $self->_clear_column_data(@dirty_columns); + + return $ret; +} + + +# And again for create +sub create { + my $class = shift; + my($data) = @_; + + my @columns = keys %$data; + + my $obj = $class->next::method(@_); + return $obj unless defined $obj; + + my %primary_cols = map { $_ => 1 } $class->primary_columns; + my @data_cols = grep !$primary_cols{$_}, @columns; + $obj->_clear_column_data(@data_cols); + + return $obj; +} + + +sub _clear_column_data { + my $self = shift; + + delete $self->{_column_data}{$_} for @_; + delete $self->{_inflated_column}{$_} for @_; +} + + sub get_column { my ($self, $col) = @_; if ((ref $self) && (!exists $self->{'_column_data'}{$col}) @@ -21,6 +65,33 @@ sub get_column { $self->next::method(@_[1..$#_]); } +# CDBI does not explicitly declare auto increment columns, so +# we just clear out our primary columns before copying. +sub copy { + my($self, $changes) = @_; + + for my $col ($self->primary_columns) { + $changes->{$col} = undef unless exists $changes->{$col}; + } + + return $self->next::method($changes); +} + +sub discard_changes { + my($self) = shift; + + delete $self->{_column_data}{$_} for $self->is_changed; + delete $self->{_dirty_columns}; + delete $self->{_relationship_data}; + + return $self; +} + +sub _ident_cond { + my ($class) = @_; + return join(" AND ", map { "$_ = ?" } $class->primary_columns); +} + sub _flesh { my ($self, @groups) = @_; @groups = ('All') unless @groups; @@ -34,7 +105,9 @@ sub _flesh { # $self->ident_condition); # Not sure why the first one works and this doesn't :( my @val = $cursor->next; -#warn "Flesh: ".join(', ', @want, '=>', @val); + + return unless @val; # object must have been deleted from the database + foreach my $w (@want) { $self->{'_column_data'}{$w} = shift @val; }