pod typo fix
[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/;
701da8c4 7use Carp::Clan qw/^DBIx::Class/;
1edd1722 8
097d3227 9__PACKAGE__->load_components(qw/AccessorGroup/);
10
11__PACKAGE__->mk_group_accessors('simple' => 'result_source');
8c49f629 12
7624b19f 13=head1 NAME
14
15DBIx::Class::Row - Basic row methods
16
17=head1 SYNOPSIS
18
19=head1 DESCRIPTION
20
21This class is responsible for defining and doing basic operations on rows
1ea77c14 22derived from L<DBIx::Class::ResultSource> objects.
7624b19f 23
24=head1 METHODS
25
8091aa91 26=head2 new
7624b19f 27
28 my $obj = My::Class->new($attrs);
29
30Creates a new row object from column => value mappings passed as a hash ref
31
32=cut
33
34sub new {
35 my ($class, $attrs) = @_;
36 $class = ref $class if ref $class;
37 my $new = bless({ _column_data => { } }, $class);
38 if ($attrs) {
701da8c4 39 $new->throw_exception("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
7624b19f 40 while (my ($k, $v) = each %{$attrs}) {
701da8c4 41 $new->throw_exception("No such column $k on $class") unless $class->has_column($k);
484c9dda 42 $new->store_column($k => $v);
7624b19f 43 }
44 }
45 return $new;
46}
47
8091aa91 48=head2 insert
7624b19f 49
50 $obj->insert;
51
52Inserts an object into the database if it isn't already in there. Returns
097d3227 53the object itself. Requires the object's result source to be set, or the
54class to have a result_source_instance method.
7624b19f 55
56=cut
57
58sub insert {
59 my ($self) = @_;
60 return $self if $self->in_storage;
097d3227 61 $self->{result_source} ||= $self->result_source_instance
62 if $self->can('result_source_instance');
63 my $source = $self->{result_source};
701da8c4 64 $self->throw_exception("No result_source set on this object; can't insert") unless $source;
7624b19f 65 #use Data::Dumper; warn Dumper($self);
097d3227 66 $source->storage->insert($source->from, { $self->get_columns });
7624b19f 67 $self->in_storage(1);
68 $self->{_dirty_columns} = {};
69 return $self;
70}
71
8091aa91 72=head2 in_storage
7624b19f 73
74 $obj->in_storage; # Get value
75 $obj->in_storage(1); # Set value
76
77Indicated whether the object exists as a row in the database or not
78
79=cut
80
81sub in_storage {
82 my ($self, $val) = @_;
83 $self->{_in_storage} = $val if @_ > 1;
84 return $self->{_in_storage};
85}
86
8091aa91 87=head2 update
7624b19f 88
89 $obj->update;
90
91Must be run on an object that is already in the database; issues an SQL
92UPDATE query to commit any changes to the object to the db if required.
93
94=cut
95
96sub update {
97 my ($self, $upd) = @_;
701da8c4 98 $self->throw_exception( "Not in database" ) unless $self->in_storage;
fc27a867 99 $self->set_columns($upd) if $upd;
d7156e50 100 my %to_update = $self->get_dirty_columns;
c01ab172 101 return $self unless keys %to_update;
4b12b3c2 102 my $ident_cond = $self->ident_condition;
103 $self->throw_exception("Cannot safely update a row in a PK-less table")
104 if ! keys %$ident_cond;
88cb6a1d 105 my $rows = $self->result_source->storage->update(
4b12b3c2 106 $self->result_source->from, \%to_update, $ident_cond);
7624b19f 107 if ($rows == 0) {
701da8c4 108 $self->throw_exception( "Can't update ${self}: row not found" );
7624b19f 109 } elsif ($rows > 1) {
701da8c4 110 $self->throw_exception("Can't update ${self}: updated more than one row");
7624b19f 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) {
701da8c4 129 $self->throw_exception( "Not in database" ) unless $self->in_storage;
4b12b3c2 130 my $ident_cond = $self->ident_condition;
131 $self->throw_exception("Cannot safely delete a row in a PK-less table")
132 if ! keys %$ident_cond;
88cb6a1d 133 $self->result_source->storage->delete(
4b12b3c2 134 $self->result_source->from, $ident_cond);
7624b19f 135 $self->in_storage(undef);
7624b19f 136 } else {
701da8c4 137 $self->throw_exception("Can't do class delete without a ResultSource instance")
097d3227 138 unless $self->can('result_source_instance');
7624b19f 139 my $attrs = { };
140 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
141 $attrs = { %{ pop(@_) } };
142 }
143 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
097d3227 144 $self->result_source_instance->resultset->search(@_)->delete;
7624b19f 145 }
146 return $self;
147}
148
8091aa91 149=head2 get_column
7624b19f 150
151 my $val = $obj->get_column($col);
152
8091aa91 153Gets a column value from a row object. Currently, does not do
154any queries; the column must have already been fetched from
155the database and stored in the object.
7624b19f 156
157=cut
158
159sub get_column {
160 my ($self, $column) = @_;
701da8c4 161 $self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
7624b19f 162 return $self->{_column_data}{$column}
163 if exists $self->{_column_data}{$column};
701da8c4 164 $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column);
7624b19f 165 return undef;
166}
167
8091aa91 168=head2 get_columns
076a6864 169
170 my %data = $obj->get_columns;
171
8091aa91 172Does C<get_column>, for all column values at once.
076a6864 173
174=cut
175
176sub get_columns {
177 my $self = shift;
cb5f2eea 178 return %{$self->{_column_data}};
d7156e50 179}
180
181=head2 get_dirty_columns
182
183 my %data = $obj->get_dirty_columns;
184
185Identical to get_columns but only returns those that have been changed.
186
187=cut
188
189sub get_dirty_columns {
190 my $self = shift;
191 return map { $_ => $self->{_column_data}{$_} }
192 keys %{$self->{_dirty_columns}};
076a6864 193}
194
8091aa91 195=head2 set_column
7624b19f 196
197 $obj->set_column($col => $val);
198
8091aa91 199Sets a column value. If the new value is different from the old one,
200the column is marked as dirty for when you next call $obj->update.
7624b19f 201
202=cut
203
204sub set_column {
205 my $self = shift;
206 my ($column) = @_;
207 my $old = $self->get_column($column);
208 my $ret = $self->store_column(@_);
87772e46 209 $self->{_dirty_columns}{$column} = 1
210 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
7624b19f 211 return $ret;
212}
213
8091aa91 214=head2 set_columns
076a6864 215
dc818523 216 my $copy = $orig->set_columns({ $col => $val, ... });
076a6864 217
8091aa91 218Sets more than one column value at once.
076a6864 219
220=cut
221
222sub set_columns {
223 my ($self,$data) = @_;
224 while (my ($col,$val) = each %$data) {
225 $self->set_column($col,$val);
226 }
c01ab172 227 return $self;
076a6864 228}
229
8091aa91 230=head2 copy
076a6864 231
232 my $copy = $orig->copy({ change => $to, ... });
233
8091aa91 234Inserts a new row with the specified changes.
076a6864 235
236=cut
237
c01ab172 238sub copy {
239 my ($self, $changes) = @_;
240 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
241 $new->set_column($_ => $changes->{$_}) for keys %$changes;
242 return $new->insert;
243}
244
8091aa91 245=head2 store_column
7624b19f 246
247 $obj->store_column($col => $val);
248
8091aa91 249Sets a column value without marking it as dirty.
7624b19f 250
251=cut
252
253sub store_column {
254 my ($self, $column, $value) = @_;
701da8c4 255 $self->throw_exception( "No such column '${column}'" )
d7156e50 256 unless exists $self->{_column_data}{$column} || $self->has_column($column);
701da8c4 257 $self->throw_exception( "set_column called for ${column} without value" )
7624b19f 258 if @_ < 3;
259 return $self->{_column_data}{$column} = $value;
260}
261
b52e9bf8 262=head2 inflate_result
263
c01ab172 264 Class->inflate_result($result_source, \%me, \%prefetch?)
b52e9bf8 265
266Called by ResultSet to inflate a result from storage
267
268=cut
269
270sub inflate_result {
c01ab172 271 my ($class, $source, $me, $prefetch) = @_;
b52e9bf8 272 #use Data::Dumper; print Dumper(@_);
c01ab172 273 my $new = bless({ result_source => $source,
274 _column_data => $me,
275 _in_storage => 1
276 },
277 ref $class || $class);
7fb16f1a 278 my $schema;
b52e9bf8 279 PRE: foreach my $pre (keys %{$prefetch||{}}) {
0dc79249 280 my $pre_source = $source->related_source($pre);
701da8c4 281 $class->throw_exception("Can't prefetch non-existant relationship ${pre}") unless $pre_source;
d20bb28e 282 my $fetched;
283 unless ($pre_source->primary_columns == grep { exists $prefetch->{$pre}[0]{$_}
284 and !defined $prefetch->{$pre}[0]{$_} } $pre_source->primary_columns)
285 {
286 $fetched = $pre_source->result_class->inflate_result(
287 $pre_source, @{$prefetch->{$pre}});
288 }
0dc79249 289 my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
701da8c4 290 $class->throw_exception("No accessor for prefetched $pre")
0dc79249 291 unless defined $accessor;
0dc79249 292 if ($accessor eq 'single') {
b52e9bf8 293 $new->{_relationship_data}{$pre} = $fetched;
0dc79249 294 } elsif ($accessor eq 'filter') {
b52e9bf8 295 $new->{_inflated_column}{$pre} = $fetched;
296 } else {
701da8c4 297 $class->throw_exception("Don't know how to store prefetched $pre");
b52e9bf8 298 }
299 }
7624b19f 300 return $new;
301}
302
8091aa91 303=head2 insert_or_update
7624b19f 304
305 $obj->insert_or_update
306
8091aa91 307Updates the object if it's already in the db, else inserts it.
7624b19f 308
309=cut
310
311sub insert_or_update {
312 my $self = shift;
313 return ($self->in_storage ? $self->update : $self->insert);
314}
315
8091aa91 316=head2 is_changed
7624b19f 317
318 my @changed_col_names = $obj->is_changed
319
320=cut
321
322sub is_changed {
323 return keys %{shift->{_dirty_columns} || {}};
324}
325
097d3227 326=head2 result_source
327
328 Accessor to the ResultSource this object was created from
329
71e65b39 330=head2 register_column($column, $column_info)
1f23a877 331
91b0fbd7 332 Registers a column on the class. If the column_info has an 'accessor' key,
333 creates an accessor named after the value if defined; if there is no such
334 key, creates an accessor with the same name as the column
1f23a877 335
097d3227 336=cut
337
1f23a877 338sub register_column {
339 my ($class, $col, $info) = @_;
91b0fbd7 340 my $acc = $col;
341 if (exists $info->{accessor}) {
342 return unless defined $info->{accessor};
343 $acc = [ $info->{accessor}, $col ];
344 }
345 $class->mk_group_accessors('column' => $acc);
1f23a877 346}
347
701da8c4 348
5160b401 349=head2 throw_exception
701da8c4 350
351See Schema's throw_exception.
352
353=cut
354
355sub throw_exception {
356 my $self=shift;
357 if (ref $self && ref $self->result_source) {
358 $self->result_source->schema->throw_exception(@_);
359 } else {
360 croak(@_);
361 }
362}
363
7624b19f 3641;
365
7624b19f 366=head1 AUTHORS
367
daec44b8 368Matt S. Trout <mst@shadowcatsystems.co.uk>
7624b19f 369
370=head1 LICENSE
371
372You may distribute this code under the same terms as Perl itself.
373
374=cut
375