removed ->reload_row from storage, changed this to a method based on the actual row...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / PK.pm
1 package DBIx::Class::PK;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Row/;
7
8 =head1 NAME
9
10 DBIx::Class::PK - Primary Key class
11
12 =head1 SYNOPSIS
13
14 =head1 DESCRIPTION
15
16 This class contains methods for handling primary keys and methods
17 depending on them.
18
19 =head1 METHODS
20
21 =cut
22
23 sub _ident_values {
24   my ($self) = @_;
25   return (map { $self->{_column_data}{$_} } $self->primary_columns);
26 }
27
28 =head2 discard_changes
29
30 Re-selects the row from the database, losing any changes that had
31 been made.
32
33 This method can also be used to refresh from storage, retrieving any
34 changes made since the row was last read from storage.
35
36 =cut
37
38 sub discard_changes {
39   my ($self) = @_;
40   delete $self->{_dirty_columns};
41   return unless $self->in_storage; # Don't reload if we aren't real!
42   
43   if( my $current_storage = $self->get_current_storage) {
44         
45     # Set $self to the current.
46         %$self = %$current_storage;
47         
48     # Avoid a possible infinite loop with
49     # sub DESTROY { $_[0]->discard_changes }
50     bless $current_storage, 'Do::Not::Exist';
51     
52     return $self;       
53   } else {
54     $self->in_storage(0);
55     return $self;       
56   }
57 }
58
59 =head2 id
60
61 Returns the primary key(s) for a row. Can't be called as
62 a class method.
63
64 =cut
65
66 sub id {
67   my ($self) = @_;
68   $self->throw_exception( "Can't call id() as a class method" )
69     unless ref $self;
70   my @pk = $self->_ident_values;
71   return (wantarray ? @pk : $pk[0]);
72 }
73
74 =head2 ID
75
76 Returns a unique id string identifying a row object by primary key.
77 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
78 L<DBIx::Class::ObjectCache>.
79
80 =cut
81
82 sub ID {
83   my ($self) = @_;
84   $self->throw_exception( "Can't call ID() as a class method" )
85     unless ref $self;
86   return undef unless $self->in_storage;
87   return $self->_create_ID(map { $_ => $self->{_column_data}{$_} }
88                              $self->primary_columns);
89 }
90
91 sub _create_ID {
92   my ($self,%vals) = @_;
93   return undef unless 0 == grep { !defined } values %vals;
94   return join '|', ref $self || $self, $self->result_source->name,
95     map { $_ . '=' . $vals{$_} } sort keys %vals;
96 }
97
98 =head2 ident_condition
99
100   my $cond = $result_source->ident_condition();
101
102   my $cond = $result_source->ident_condition('alias');
103
104 Produces a condition hash to locate a row based on the primary key(s).
105
106 =cut
107
108 sub ident_condition {
109   my ($self, $alias) = @_;
110   my %cond;
111   my $prefix = defined $alias ? $alias.'.' : '';
112   $cond{$prefix.$_} = $self->get_column($_) for $self->primary_columns;
113   return \%cond;
114 }
115
116 1;
117
118 =head1 AUTHORS
119
120 Matt S. Trout <mst@shadowcatsystems.co.uk>
121
122 =head1 LICENSE
123
124 You may distribute this code under the same terms as Perl itself.
125
126 =cut
127