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