8e2c2ae2e2317a30ae7ccc4a53cff6b8f8264087
[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 _select_columns {
7   return shift->columns('Essential');
8 }
9
10 sub get_column {
11   my ($self, $col) = @_;
12   if ((ref $self) && (!exists $self->{'_column_data'}{$col})
13     && $self->{'_in_database'}) {
14     $self->_flesh(grep { exists $self->_column_groups->{$_}{$col}
15                            && $_ ne 'All' }
16                    keys %{ $self->_column_groups || {} });
17   }
18   $self->NEXT::get_column(@_[1..$#_]);
19 }
20
21 sub _flesh {
22   my ($self, @groups) = @_;
23   @groups = ('All') unless @groups;
24   my %want;
25   $want{$_} = 1 for map { keys %{$self->_column_groups->{$_}} } @groups;
26   if (my @want = grep { !exists $self->{'_column_data'}{$_} } keys %want) {
27     my $cursor = $self->storage->select($self->_table_name, \@want,
28                 \$self->_ident_cond, { bind => [ $self->_ident_values ] });
29     #my $sth = $self->storage->select($self->_table_name, \@want,
30     #                                   $self->ident_condition);
31     # Not sure why the first one works and this doesn't :(
32     my @val = $cursor->next;
33 #warn "Flesh: ".join(', ', @want, '=>', @val);
34     foreach my $w (@want) {
35       $self->{'_column_data'}{$w} = shift @val;
36     }
37   }
38 }
39
40 1;