more dob updates
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Row.pm
1 package DBIx::Class::Row;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class/;
7
8 =head1 NAME 
9
10 DBIx::Class::Row - Basic row methods
11
12 =head1 SYNOPSIS
13
14 =head1 DESCRIPTION
15
16 This class is responsible for defining and doing basic operations on rows
17 derived from L<DBIx::Class::Table> objects.
18
19 =head1 METHODS
20
21 =head2 new
22
23   my $obj = My::Class->new($attrs);
24
25 Creates a new row object from column => value mappings passed as a hash ref
26
27 =cut
28
29 sub new {
30   my ($class, $attrs) = @_;
31   $class = ref $class if ref $class;
32   my $new = bless({ _column_data => { } }, $class);
33   if ($attrs) {
34     $new->throw("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
35     while (my ($k, $v) = each %{$attrs}) {
36       die "No such column $k on $class" unless $class->has_column($k);
37       $new->store_column($k => $v);
38     }
39   }
40   return $new;
41 }
42
43 =head2 insert
44
45   $obj->insert;
46
47 Inserts an object into the database if it isn't already in there. Returns
48 the object itself.
49
50 =cut
51
52 sub insert {
53   my ($self) = @_;
54   return $self if $self->in_storage;
55   #use Data::Dumper; warn Dumper($self);
56   my %in;
57   $in{$_} = $self->get_column($_)
58     for grep { defined $self->get_column($_) } $self->columns;
59   my %out = %{ $self->storage->insert($self->_table_name, \%in) };
60   $self->store_column($_, $out{$_})
61     for grep { $self->get_column($_) ne $out{$_} } keys %out;
62   $self->in_storage(1);
63   $self->{_dirty_columns} = {};
64   return $self;
65 }
66
67 =head2 in_storage
68
69   $obj->in_storage; # Get value
70   $obj->in_storage(1); # Set value
71
72 Indicated whether the object exists as a row in the database or not
73
74 =cut
75
76 sub in_storage {
77   my ($self, $val) = @_;
78   $self->{_in_storage} = $val if @_ > 1;
79   return $self->{_in_storage};
80 }
81
82 =head2 create
83
84   my $new = My::Class->create($attrs);
85
86 A shortcut for My::Class->new($attrs)->insert;
87
88 =cut
89
90 sub create {
91   my ($class, $attrs) = @_;
92   $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
93   return $class->new($attrs)->insert;
94 }
95
96 =head2 update
97
98   $obj->update;
99
100 Must be run on an object that is already in the database; issues an SQL
101 UPDATE query to commit any changes to the object to the db if required.
102
103 =cut
104
105 sub update {
106   my ($self, $upd) = @_;
107   $self->throw( "Not in database" ) unless $self->in_storage;
108   if (ref $upd eq 'HASH') {
109     $self->$_($upd->{$_}) for keys %$upd;
110   }
111   my %to_update;
112   $to_update{$_} = $self->get_column($_) for $self->is_changed;
113   return -1 unless keys %to_update;
114   my $rows = $self->storage->update($self->_table_name, \%to_update,
115                                       $self->ident_condition);
116   if ($rows == 0) {
117     $self->throw( "Can't update ${self}: row not found" );
118   } elsif ($rows > 1) {
119     $self->throw("Can't update ${self}: updated more than one row");
120   }
121   $self->{_dirty_columns} = {};
122   return $self;
123 }
124
125 =head2 delete
126
127   $obj->delete
128
129 Deletes the object from the database. The object is still perfectly usable
130 accessor-wise etc. but ->in_storage will now return 0 and the object must
131 be re ->insert'ed before it can be ->update'ed
132
133 =cut
134
135 sub delete {
136   my $self = shift;
137   if (ref $self) {
138     $self->throw( "Not in database" ) unless $self->in_storage;
139     #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
140     $self->storage->delete($self->_table_name, $self->ident_condition);
141     $self->in_storage(undef);
142     #$self->store_column($_ => undef) for $self->primary_columns;
143       # Should probably also arrange to trash PK if auto
144       # but if we do, post-delete cascade triggers fail :/
145   } else {
146     my $attrs = { };
147     if (@_ > 1 && ref $_[$#_] eq 'HASH') {
148       $attrs = { %{ pop(@_) } };
149     }
150     my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
151     $self->storage->delete($self->_table_name, $query);
152   }
153   return $self;
154 }
155
156 =head2 get_column
157
158   my $val = $obj->get_column($col);
159
160 Gets a column value from a row object. Currently, does not do
161 any queries; the column must have already been fetched from
162 the database and stored in the object.
163
164 =cut
165
166 sub get_column {
167   my ($self, $column) = @_;
168   $self->throw( "Can't fetch data as class method" ) unless ref $self;
169   $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
170   return $self->{_column_data}{$column}
171     if exists $self->{_column_data}{$column};
172   return undef;
173 }
174
175 =head2 get_columns
176
177   my %data = $obj->get_columns;
178
179 Does C<get_column>, for all column values at once.
180
181 =cut
182
183 sub get_columns {
184   my $self = shift;
185   return map { $_ => $self->get_column($_) } $self->columns;
186 }
187
188 =head2 set_column
189
190   $obj->set_column($col => $val);
191
192 Sets a column value. If the new value is different from the old one,
193 the column is marked as dirty for when you next call $obj->update.
194
195 =cut
196
197 sub set_column {
198   my $self = shift;
199   my ($column) = @_;
200   my $old = $self->get_column($column);
201   my $ret = $self->store_column(@_);
202   $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
203   return $ret;
204 }
205
206 =head2 set_columns
207
208   my $copy = $orig->set_columns({ $col => $val, ... });
209
210 Sets more than one column value at once.
211
212 =cut
213
214 sub set_columns {
215   my ($self,$data) = @_;
216   while (my ($col,$val) = each %$data) {
217     $self->set_column($col,$val);
218   }
219 }
220
221 =head2 copy
222
223   my $copy = $orig->copy({ change => $to, ... });
224
225 Inserts a new row with the specified changes.
226
227 =cut
228
229 =head2 store_column
230
231   $obj->store_column($col => $val);
232
233 Sets a column value without marking it as dirty.
234
235 =cut
236
237 sub store_column {
238   my ($self, $column, $value) = @_;
239   $self->throw( "No such column '${column}'" ) 
240     unless $self->has_column($column);
241   $self->throw( "set_column called for ${column} without value" ) 
242     if @_ < 3;
243   return $self->{_column_data}{$column} = $value;
244 }
245
246 sub _row_to_object {
247   my ($class, $cols, $row) = @_;
248   my %vals;
249   $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
250   my $new = bless({ _column_data => \%vals }, ref $class || $class);
251   $new->in_storage(1);
252   return $new;
253 }
254
255 sub copy {
256   my ($self, $changes) = @_;
257   my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
258   $new->set_column($_ => $changes->{$_}) for keys %$changes;
259   return $new->insert;
260 }
261
262 =head2 insert_or_update
263
264   $obj->insert_or_update
265
266 Updates the object if it's already in the db, else inserts it.
267
268 =cut
269
270 sub insert_or_update {
271   my $self = shift;
272   return ($self->in_storage ? $self->update : $self->insert);
273 }
274
275 =head2 is_changed
276
277   my @changed_col_names = $obj->is_changed
278
279 =cut
280
281 sub is_changed {
282   return keys %{shift->{_dirty_columns} || {}};
283 }
284
285 1;
286
287 =head1 AUTHORS
288
289 Matt S. Trout <mst@shadowcatsystems.co.uk>
290
291 =head1 LICENSE
292
293 You may distribute this code under the same terms as Perl itself.
294
295 =cut
296