and my tradditional collection of missing add commands
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / LazyLoading.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::CDBICompat::LazyLoading;
fe5d862b 3
4use strict;
5use warnings;
6
a9433341 7sub resultset_instance {
8 my $self = shift;
9 my $rs = $self->next::method(@_);
5e8b1b2a 10 $rs = $rs->search(undef, { columns => [ $self->columns('Essential') ] });
a9433341 11 return $rs;
fe5d862b 12}
13
14sub get_column {
15 my ($self, $col) = @_;
16 if ((ref $self) && (!exists $self->{'_column_data'}{$col})
8d5134b0 17 && $self->{'_in_storage'}) {
fe5d862b 18 $self->_flesh(grep { exists $self->_column_groups->{$_}{$col}
19 && $_ ne 'All' }
20 keys %{ $self->_column_groups || {} });
21 }
147dd158 22 $self->next::method(@_[1..$#_]);
fe5d862b 23}
24
e60dc79f 25# CDBI does not explicitly declare auto increment columns, so
26# we just clear out our primary columns before copying.
27sub copy {
28 my($self, $changes) = @_;
29
30 for my $col ($self->primary_columns) {
31 $changes->{$col} = undef unless exists $changes->{$col};
32 }
33
34 return $self->next::method($changes);
35}
36
37sub discard_changes {
38 my($self) = shift;
39
40 delete $self->{_column_data}{$_} for $self->is_changed;
41 delete $self->{_dirty_columns};
42 delete $self->{_relationship_data};
43
44 return $self;
45}
46
831ad24f 47sub _ident_cond {
48 my ($class) = @_;
49 return join(" AND ", map { "$_ = ?" } $class->primary_columns);
50}
51
fe5d862b 52sub _flesh {
53 my ($self, @groups) = @_;
b8e1e21f 54 @groups = ('All') unless @groups;
fe5d862b 55 my %want;
56 $want{$_} = 1 for map { keys %{$self->_column_groups->{$_}} } @groups;
57 if (my @want = grep { !exists $self->{'_column_data'}{$_} } keys %want) {
b98e75f6 58 my $cursor = $self->result_source->storage->select(
59 $self->result_source->name, \@want,
8b445e33 60 \$self->_ident_cond, { bind => [ $self->_ident_values ] });
61 #my $sth = $self->storage->select($self->_table_name, \@want,
62 # $self->ident_condition);
63 # Not sure why the first one works and this doesn't :(
223b8fe3 64 my @val = $cursor->next;
8b445e33 65#warn "Flesh: ".join(', ', @want, '=>', @val);
fe5d862b 66 foreach my $w (@want) {
67 $self->{'_column_data'}{$w} = shift @val;
68 }
69 }
70}
71
721;