Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::CDBICompat::LazyLoading; |
fe5d862b |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
a9433341 |
7 | sub 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) |
17 | sub update { |
18 | my $self = shift; |
19 | |
20 | my @dirty_columns = keys %{$self->{_dirty_columns}}; |
21 | |
22 | my $ret = $self->next::method(@_); |
23 | |
24 | delete $self->{_column_data}{$_} for @dirty_columns; |
25 | |
26 | return $ret; |
27 | } |
28 | |
29 | |
fe5d862b |
30 | sub get_column { |
31 | my ($self, $col) = @_; |
32 | if ((ref $self) && (!exists $self->{'_column_data'}{$col}) |
8d5134b0 |
33 | && $self->{'_in_storage'}) { |
fe5d862b |
34 | $self->_flesh(grep { exists $self->_column_groups->{$_}{$col} |
35 | && $_ ne 'All' } |
36 | keys %{ $self->_column_groups || {} }); |
37 | } |
147dd158 |
38 | $self->next::method(@_[1..$#_]); |
fe5d862b |
39 | } |
40 | |
e60dc79f |
41 | # CDBI does not explicitly declare auto increment columns, so |
42 | # we just clear out our primary columns before copying. |
43 | sub copy { |
44 | my($self, $changes) = @_; |
45 | |
46 | for my $col ($self->primary_columns) { |
47 | $changes->{$col} = undef unless exists $changes->{$col}; |
48 | } |
49 | |
50 | return $self->next::method($changes); |
51 | } |
52 | |
53 | sub discard_changes { |
54 | my($self) = shift; |
55 | |
56 | delete $self->{_column_data}{$_} for $self->is_changed; |
57 | delete $self->{_dirty_columns}; |
58 | delete $self->{_relationship_data}; |
59 | |
60 | return $self; |
61 | } |
62 | |
831ad24f |
63 | sub _ident_cond { |
64 | my ($class) = @_; |
65 | return join(" AND ", map { "$_ = ?" } $class->primary_columns); |
66 | } |
67 | |
fe5d862b |
68 | sub _flesh { |
69 | my ($self, @groups) = @_; |
b8e1e21f |
70 | @groups = ('All') unless @groups; |
fe5d862b |
71 | my %want; |
72 | $want{$_} = 1 for map { keys %{$self->_column_groups->{$_}} } @groups; |
73 | if (my @want = grep { !exists $self->{'_column_data'}{$_} } keys %want) { |
b98e75f6 |
74 | my $cursor = $self->result_source->storage->select( |
75 | $self->result_source->name, \@want, |
8b445e33 |
76 | \$self->_ident_cond, { bind => [ $self->_ident_values ] }); |
77 | #my $sth = $self->storage->select($self->_table_name, \@want, |
78 | # $self->ident_condition); |
79 | # Not sure why the first one works and this doesn't :( |
223b8fe3 |
80 | my @val = $cursor->next; |
8b445e33 |
81 | #warn "Flesh: ".join(', ', @want, '=>', @val); |
fe5d862b |
82 | foreach my $w (@want) { |
83 | $self->{'_column_data'}{$w} = shift @val; |
84 | } |
85 | } |
86 | } |
87 | |
88 | 1; |