fixed a stupid typo
[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;
aeb1bf75 37 my $new = bless { _column_data => {} }, $class;
7624b19f 38 if ($attrs) {
aeb1bf75 39 $new->throw_exception("attrs must be a hashref") unless ref($attrs) eq 'HASH';
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};
aeb1bf75 64 $self->throw_exception("No result_source set on this object; can't insert")
65 unless $source;
7624b19f 66 #use Data::Dumper; warn Dumper($self);
097d3227 67 $source->storage->insert($source->from, { $self->get_columns });
7624b19f 68 $self->in_storage(1);
69 $self->{_dirty_columns} = {};
64acc2bc 70 $self->{related_resultsets} = {};
7624b19f 71 return $self;
72}
73
8091aa91 74=head2 in_storage
7624b19f 75
76 $obj->in_storage; # Get value
77 $obj->in_storage(1); # Set value
78
79Indicated whether the object exists as a row in the database or not
80
81=cut
82
83sub in_storage {
84 my ($self, $val) = @_;
85 $self->{_in_storage} = $val if @_ > 1;
86 return $self->{_in_storage};
87}
88
8091aa91 89=head2 update
7624b19f 90
91 $obj->update;
92
93Must be run on an object that is already in the database; issues an SQL
94UPDATE query to commit any changes to the object to the db if required.
95
96=cut
97
98sub update {
99 my ($self, $upd) = @_;
701da8c4 100 $self->throw_exception( "Not in database" ) unless $self->in_storage;
fc27a867 101 $self->set_columns($upd) if $upd;
d7156e50 102 my %to_update = $self->get_dirty_columns;
c01ab172 103 return $self unless keys %to_update;
4b12b3c2 104 my $ident_cond = $self->ident_condition;
105 $self->throw_exception("Cannot safely update a row in a PK-less table")
106 if ! keys %$ident_cond;
88cb6a1d 107 my $rows = $self->result_source->storage->update(
4b12b3c2 108 $self->result_source->from, \%to_update, $ident_cond);
7624b19f 109 if ($rows == 0) {
701da8c4 110 $self->throw_exception( "Can't update ${self}: row not found" );
7624b19f 111 } elsif ($rows > 1) {
701da8c4 112 $self->throw_exception("Can't update ${self}: updated more than one row");
7624b19f 113 }
114 $self->{_dirty_columns} = {};
64acc2bc 115 $self->{related_resultsets} = {};
7624b19f 116 return $self;
117}
118
8091aa91 119=head2 delete
7624b19f 120
121 $obj->delete
122
123Deletes the object from the database. The object is still perfectly usable
124accessor-wise etc. but ->in_storage will now return 0 and the object must
125be re ->insert'ed before it can be ->update'ed
126
127=cut
128
129sub delete {
130 my $self = shift;
131 if (ref $self) {
701da8c4 132 $self->throw_exception( "Not in database" ) unless $self->in_storage;
4b12b3c2 133 my $ident_cond = $self->ident_condition;
134 $self->throw_exception("Cannot safely delete a row in a PK-less table")
135 if ! keys %$ident_cond;
e0f56292 136 foreach my $column (keys %$ident_cond) {
aeb1bf75 137 $self->throw_exception("Can't delete the object unless it has loaded the primary keys")
138 unless exists $self->{_column_data}{$column};
e0f56292 139 }
88cb6a1d 140 $self->result_source->storage->delete(
4b12b3c2 141 $self->result_source->from, $ident_cond);
7624b19f 142 $self->in_storage(undef);
7624b19f 143 } else {
701da8c4 144 $self->throw_exception("Can't do class delete without a ResultSource instance")
097d3227 145 unless $self->can('result_source_instance');
aeb1bf75 146 my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? { %{pop(@_)} } : {};
147 my $query = ref $_[0] eq 'HASH' ? $_[0] : {@_};
097d3227 148 $self->result_source_instance->resultset->search(@_)->delete;
7624b19f 149 }
150 return $self;
151}
152
8091aa91 153=head2 get_column
7624b19f 154
155 my $val = $obj->get_column($col);
156
8091aa91 157Gets a column value from a row object. Currently, does not do
158any queries; the column must have already been fetched from
159the database and stored in the object.
7624b19f 160
161=cut
162
163sub get_column {
164 my ($self, $column) = @_;
701da8c4 165 $self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
aeb1bf75 166 return $self->{_column_data}{$column} if exists $self->{_column_data}{$column};
701da8c4 167 $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column);
7624b19f 168 return undef;
169}
170
def81720 171sub has_column_loaded {
172 my ($self, $column) = @_;
173 $self->throw_exception( "Can't call has_column data as class method" ) unless ref $self;
aeb1bf75 174 return exists $self->{_column_data}{$column};
def81720 175}
176
8091aa91 177=head2 get_columns
076a6864 178
179 my %data = $obj->get_columns;
180
8091aa91 181Does C<get_column>, for all column values at once.
076a6864 182
183=cut
184
185sub get_columns {
186 my $self = shift;
cb5f2eea 187 return %{$self->{_column_data}};
d7156e50 188}
189
190=head2 get_dirty_columns
191
192 my %data = $obj->get_dirty_columns;
193
194Identical to get_columns but only returns those that have been changed.
195
196=cut
197
198sub get_dirty_columns {
199 my $self = shift;
200 return map { $_ => $self->{_column_data}{$_} }
201 keys %{$self->{_dirty_columns}};
076a6864 202}
203
8091aa91 204=head2 set_column
7624b19f 205
206 $obj->set_column($col => $val);
207
8091aa91 208Sets a column value. If the new value is different from the old one,
209the column is marked as dirty for when you next call $obj->update.
7624b19f 210
211=cut
212
213sub set_column {
214 my $self = shift;
215 my ($column) = @_;
216 my $old = $self->get_column($column);
217 my $ret = $self->store_column(@_);
87772e46 218 $self->{_dirty_columns}{$column} = 1
219 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
7624b19f 220 return $ret;
221}
222
8091aa91 223=head2 set_columns
076a6864 224
dc818523 225 my $copy = $orig->set_columns({ $col => $val, ... });
076a6864 226
8091aa91 227Sets more than one column value at once.
076a6864 228
229=cut
230
231sub set_columns {
232 my ($self,$data) = @_;
233 while (my ($col,$val) = each %$data) {
234 $self->set_column($col,$val);
235 }
c01ab172 236 return $self;
076a6864 237}
238
8091aa91 239=head2 copy
076a6864 240
241 my $copy = $orig->copy({ change => $to, ... });
242
8091aa91 243Inserts a new row with the specified changes.
076a6864 244
245=cut
246
c01ab172 247sub copy {
248 my ($self, $changes) = @_;
333cce60 249 $changes ||= {};
fde6e28e 250 my $col_data = { %{$self->{_column_data}} };
251 foreach my $col (keys %$col_data) {
252 delete $col_data->{$col}
253 if $self->result_source->column_info($col)->{is_auto_increment};
254 }
aeb1bf75 255 my $new = bless { _column_data => $col_data }, ref $self;
ecd1f408 256 $new->set_columns($changes);
333cce60 257 $new->insert;
258 foreach my $rel ($self->result_source->relationships) {
259 my $rel_info = $self->result_source->relationship_info($rel);
260 if ($rel_info->{attrs}{cascade_copy}) {
261 my $resolved = $self->result_source->resolve_condition(
262 $rel_info->{cond}, $rel, $new);
263 foreach my $related ($self->search_related($rel)) {
264 $related->copy($resolved);
265 }
266 }
267 }
2c4c67b6 268 return $new;
c01ab172 269}
270
8091aa91 271=head2 store_column
7624b19f 272
273 $obj->store_column($col => $val);
274
8091aa91 275Sets a column value without marking it as dirty.
7624b19f 276
277=cut
278
279sub store_column {
280 my ($self, $column, $value) = @_;
701da8c4 281 $self->throw_exception( "No such column '${column}'" )
d7156e50 282 unless exists $self->{_column_data}{$column} || $self->has_column($column);
701da8c4 283 $self->throw_exception( "set_column called for ${column} without value" )
7624b19f 284 if @_ < 3;
285 return $self->{_column_data}{$column} = $value;
286}
287
b52e9bf8 288=head2 inflate_result
289
c01ab172 290 Class->inflate_result($result_source, \%me, \%prefetch?)
b52e9bf8 291
292Called by ResultSet to inflate a result from storage
293
294=cut
295
296sub inflate_result {
c01ab172 297 my ($class, $source, $me, $prefetch) = @_;
b52e9bf8 298 #use Data::Dumper; print Dumper(@_);
c01ab172 299 my $new = bless({ result_source => $source,
300 _column_data => $me,
301 _in_storage => 1
302 },
303 ref $class || $class);
7fb16f1a 304 my $schema;
64acc2bc 305 foreach my $pre (keys %{$prefetch||{}}) {
306 my $pre_val = $prefetch->{$pre};
f9cc31dd 307 my $pre_source = $source->related_source($pre);
a86b1efe 308 $class->throw_exception("Can't prefetch non-existent relationship ${pre}")
309 unless $pre_source;
0f66a01b 310 if (ref($pre_val->[0]) eq 'ARRAY') { # multi
a86b1efe 311 my @pre_objects;
312 foreach my $pre_rec (@$pre_val) {
313 unless ($pre_source->primary_columns == grep { exists $pre_rec->[0]{$_}
5a5bec6c 314 and defined $pre_rec->[0]{$_} } $pre_source->primary_columns) {
a86b1efe 315 next;
316 }
317 push(@pre_objects, $pre_source->result_class->inflate_result(
318 $pre_source, @{$pre_rec}));
319 }
320 $new->related_resultset($pre)->set_cache(\@pre_objects);
62e87ea8 321 } elsif (defined $pre_val->[0]) {
a86b1efe 322 my $fetched;
323 unless ($pre_source->primary_columns == grep { exists $pre_val->[0]{$_}
324 and !defined $pre_val->[0]{$_} } $pre_source->primary_columns)
325 {
326 $fetched = $pre_source->result_class->inflate_result(
327 $pre_source, @{$pre_val});
328 }
329 my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
330 $class->throw_exception("No accessor for prefetched $pre")
331 unless defined $accessor;
332 if ($accessor eq 'single') {
333 $new->{_relationship_data}{$pre} = $fetched;
334 } elsif ($accessor eq 'filter') {
335 $new->{_inflated_column}{$pre} = $fetched;
336 } else {
337 $class->throw_exception("Prefetch not supported with accessor '$accessor'");
338 }
b52e9bf8 339 }
340 }
7624b19f 341 return $new;
342}
343
9b465d00 344=head2 update_or_insert
7624b19f 345
9b465d00 346 $obj->update_or_insert
7624b19f 347
8091aa91 348Updates the object if it's already in the db, else inserts it.
7624b19f 349
350=cut
351
9b465d00 352*insert_or_update = \&update_or_insert;
353sub update_or_insert {
7624b19f 354 my $self = shift;
355 return ($self->in_storage ? $self->update : $self->insert);
356}
357
8091aa91 358=head2 is_changed
7624b19f 359
360 my @changed_col_names = $obj->is_changed
361
362=cut
363
364sub is_changed {
365 return keys %{shift->{_dirty_columns} || {}};
366}
367
097d3227 368=head2 result_source
369
370 Accessor to the ResultSource this object was created from
371
87c4e602 372=head2 register_column
373
374=head3 Arguments: ($column, $column_info)
1f23a877 375
91b0fbd7 376 Registers a column on the class. If the column_info has an 'accessor' key,
377 creates an accessor named after the value if defined; if there is no such
378 key, creates an accessor with the same name as the column
1f23a877 379
097d3227 380=cut
381
1f23a877 382sub register_column {
383 my ($class, $col, $info) = @_;
91b0fbd7 384 my $acc = $col;
385 if (exists $info->{accessor}) {
386 return unless defined $info->{accessor};
387 $acc = [ $info->{accessor}, $col ];
388 }
389 $class->mk_group_accessors('column' => $acc);
1f23a877 390}
391
701da8c4 392
5160b401 393=head2 throw_exception
701da8c4 394
395See Schema's throw_exception.
396
397=cut
398
399sub throw_exception {
400 my $self=shift;
401 if (ref $self && ref $self->result_source) {
402 $self->result_source->schema->throw_exception(@_);
403 } else {
404 croak(@_);
405 }
406}
407
7624b19f 4081;
409
7624b19f 410=head1 AUTHORS
411
daec44b8 412Matt S. Trout <mst@shadowcatsystems.co.uk>
7624b19f 413
414=head1 LICENSE
415
416You may distribute this code under the same terms as Perl itself.
417
418=cut
419