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