3c629abca639ac2f474e2d91c7ce33e96736ac63
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / LazyLoading.pm
1 package DBIx::Class::CDBICompat::LazyLoading;
2
3 use strict;
4 use warnings;
5
6 sub resultset_instance {
7   my $self = shift;
8   my $rs = $self->next::method(@_);
9   $rs = $rs->search(undef, { columns => [ $self->columns('Essential') ] });
10   return $rs;
11 }
12
13 sub get_column {
14   my ($self, $col) = @_;
15   if ((ref $self) && (!exists $self->{'_column_data'}{$col})
16     && $self->{'_in_storage'}) {
17     $self->_flesh(grep { exists $self->_column_groups->{$_}{$col}
18                            && $_ ne 'All' }
19                    keys %{ $self->_column_groups || {} });
20   }
21   $self->next::method(@_[1..$#_]);
22 }
23
24 sub _flesh {
25   my ($self, @groups) = @_;
26   @groups = ('All') unless @groups;
27   my %want;
28   $want{$_} = 1 for map { keys %{$self->_column_groups->{$_}} } @groups;
29   if (my @want = grep { !exists $self->{'_column_data'}{$_} } keys %want) {
30     my $cursor = $self->result_source->storage->select(
31                 $self->result_source->name, \@want,
32                 \$self->_ident_cond, { bind => [ $self->_ident_values ] });
33     #my $sth = $self->storage->select($self->_table_name, \@want,
34     #                                   $self->ident_condition);
35     # Not sure why the first one works and this doesn't :(
36     my @val = $cursor->next;
37 #warn "Flesh: ".join(', ', @want, '=>', @val);
38     foreach my $w (@want) {
39       $self->{'_column_data'}{$w} = shift @val;
40     }
41   }
42 }
43
44 1;