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