Reorder the accessor_name_for() check to get the more likely one first to
[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
c0fcc63f 14
15# Emulate that CDBI throws out all changed columns and reloads them on
16# request in case the database modifies the new value (say, via a trigger)
17sub update {
18 my $self = shift;
19
20 my @dirty_columns = keys %{$self->{_dirty_columns}};
21
22 my $ret = $self->next::method(@_);
1d7e89b8 23 $self->_clear_column_data(@dirty_columns);
c0fcc63f 24
25 return $ret;
26}
27
28
1d7e89b8 29# And again for create
30sub create {
31 my $class = shift;
32 my($data) = @_;
33
34 my @columns = keys %$data;
35
36 my $obj = $class->next::method(@_);
37 return $obj unless defined $obj;
38
39 my %primary_cols = map { $_ => 1 } $class->primary_columns;
40 my @data_cols = grep !$primary_cols{$_}, @columns;
41 $obj->_clear_column_data(@data_cols);
42
43 return $obj;
44}
45
46
47sub _clear_column_data {
48 my $self = shift;
49
50 delete $self->{_column_data}{$_} for @_;
51 delete $self->{_inflated_column}{$_} for @_;
52}
53
54
fe5d862b 55sub get_column {
56 my ($self, $col) = @_;
57 if ((ref $self) && (!exists $self->{'_column_data'}{$col})
8d5134b0 58 && $self->{'_in_storage'}) {
fe5d862b 59 $self->_flesh(grep { exists $self->_column_groups->{$_}{$col}
60 && $_ ne 'All' }
61 keys %{ $self->_column_groups || {} });
62 }
147dd158 63 $self->next::method(@_[1..$#_]);
fe5d862b 64}
65
e60dc79f 66# CDBI does not explicitly declare auto increment columns, so
67# we just clear out our primary columns before copying.
68sub copy {
69 my($self, $changes) = @_;
70
71 for my $col ($self->primary_columns) {
72 $changes->{$col} = undef unless exists $changes->{$col};
73 }
74
75 return $self->next::method($changes);
76}
77
78sub discard_changes {
79 my($self) = shift;
80
81 delete $self->{_column_data}{$_} for $self->is_changed;
82 delete $self->{_dirty_columns};
83 delete $self->{_relationship_data};
84
85 return $self;
86}
87
831ad24f 88sub _ident_cond {
89 my ($class) = @_;
90 return join(" AND ", map { "$_ = ?" } $class->primary_columns);
91}
92
fe5d862b 93sub _flesh {
94 my ($self, @groups) = @_;
b8e1e21f 95 @groups = ('All') unless @groups;
fe5d862b 96 my %want;
97 $want{$_} = 1 for map { keys %{$self->_column_groups->{$_}} } @groups;
98 if (my @want = grep { !exists $self->{'_column_data'}{$_} } keys %want) {
b98e75f6 99 my $cursor = $self->result_source->storage->select(
100 $self->result_source->name, \@want,
8b445e33 101 \$self->_ident_cond, { bind => [ $self->_ident_values ] });
102 #my $sth = $self->storage->select($self->_table_name, \@want,
103 # $self->ident_condition);
104 # Not sure why the first one works and this doesn't :(
223b8fe3 105 my @val = $cursor->next;
e4d970ed 106
107 return unless @val; # object must have been deleted from the database
108
fe5d862b 109 foreach my $w (@want) {
110 $self->{'_column_data'}{$w} = shift @val;
111 }
112 }
113}
114
1151;