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 |
22 | derived from L<DBIx::Class::Table> objects. |
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} = {}; |
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 | |
77 | Indicated whether the object exists as a row in the database or not |
78 | |
79 | =cut |
80 | |
81 | sub 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 | |
91 | Must be run on an object that is already in the database; issues an SQL |
92 | UPDATE query to commit any changes to the object to the db if required. |
93 | |
94 | =cut |
95 | |
96 | sub update { |
97 | my ($self, $upd) = @_; |
701da8c4 |
98 | $self->throw_exception( "Not in database" ) unless $self->in_storage; |
d7156e50 |
99 | my %to_update = $self->get_dirty_columns; |
c01ab172 |
100 | return $self unless keys %to_update; |
88cb6a1d |
101 | my $rows = $self->result_source->storage->update( |
102 | $self->result_source->from, \%to_update, $self->ident_condition); |
7624b19f |
103 | if ($rows == 0) { |
701da8c4 |
104 | $self->throw_exception( "Can't update ${self}: row not found" ); |
7624b19f |
105 | } elsif ($rows > 1) { |
701da8c4 |
106 | $self->throw_exception("Can't update ${self}: updated more than one row"); |
7624b19f |
107 | } |
108 | $self->{_dirty_columns} = {}; |
109 | return $self; |
110 | } |
111 | |
8091aa91 |
112 | =head2 delete |
7624b19f |
113 | |
114 | $obj->delete |
115 | |
116 | Deletes the object from the database. The object is still perfectly usable |
117 | accessor-wise etc. but ->in_storage will now return 0 and the object must |
118 | be re ->insert'ed before it can be ->update'ed |
119 | |
120 | =cut |
121 | |
122 | sub delete { |
123 | my $self = shift; |
124 | if (ref $self) { |
701da8c4 |
125 | $self->throw_exception( "Not in database" ) unless $self->in_storage; |
88cb6a1d |
126 | $self->result_source->storage->delete( |
127 | $self->result_source->from, $self->ident_condition); |
7624b19f |
128 | $self->in_storage(undef); |
7624b19f |
129 | } else { |
701da8c4 |
130 | $self->throw_exception("Can't do class delete without a ResultSource instance") |
097d3227 |
131 | unless $self->can('result_source_instance'); |
7624b19f |
132 | my $attrs = { }; |
133 | if (@_ > 1 && ref $_[$#_] eq 'HASH') { |
134 | $attrs = { %{ pop(@_) } }; |
135 | } |
136 | my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_}); |
097d3227 |
137 | $self->result_source_instance->resultset->search(@_)->delete; |
7624b19f |
138 | } |
139 | return $self; |
140 | } |
141 | |
8091aa91 |
142 | =head2 get_column |
7624b19f |
143 | |
144 | my $val = $obj->get_column($col); |
145 | |
8091aa91 |
146 | Gets a column value from a row object. Currently, does not do |
147 | any queries; the column must have already been fetched from |
148 | the database and stored in the object. |
7624b19f |
149 | |
150 | =cut |
151 | |
152 | sub get_column { |
153 | my ($self, $column) = @_; |
701da8c4 |
154 | $self->throw_exception( "Can't fetch data as class method" ) unless ref $self; |
7624b19f |
155 | return $self->{_column_data}{$column} |
156 | if exists $self->{_column_data}{$column}; |
701da8c4 |
157 | $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column); |
7624b19f |
158 | return undef; |
159 | } |
160 | |
8091aa91 |
161 | =head2 get_columns |
076a6864 |
162 | |
163 | my %data = $obj->get_columns; |
164 | |
8091aa91 |
165 | Does C<get_column>, for all column values at once. |
076a6864 |
166 | |
167 | =cut |
168 | |
169 | sub get_columns { |
170 | my $self = shift; |
cb5f2eea |
171 | return %{$self->{_column_data}}; |
d7156e50 |
172 | } |
173 | |
174 | =head2 get_dirty_columns |
175 | |
176 | my %data = $obj->get_dirty_columns; |
177 | |
178 | Identical to get_columns but only returns those that have been changed. |
179 | |
180 | =cut |
181 | |
182 | sub get_dirty_columns { |
183 | my $self = shift; |
184 | return map { $_ => $self->{_column_data}{$_} } |
185 | keys %{$self->{_dirty_columns}}; |
076a6864 |
186 | } |
187 | |
8091aa91 |
188 | =head2 set_column |
7624b19f |
189 | |
190 | $obj->set_column($col => $val); |
191 | |
8091aa91 |
192 | Sets a column value. If the new value is different from the old one, |
193 | the column is marked as dirty for when you next call $obj->update. |
7624b19f |
194 | |
195 | =cut |
196 | |
197 | sub set_column { |
198 | my $self = shift; |
199 | my ($column) = @_; |
200 | my $old = $self->get_column($column); |
201 | my $ret = $self->store_column(@_); |
87772e46 |
202 | $self->{_dirty_columns}{$column} = 1 |
203 | if (defined $old ^ defined $ret) || (defined $old && $old ne $ret); |
7624b19f |
204 | return $ret; |
205 | } |
206 | |
8091aa91 |
207 | =head2 set_columns |
076a6864 |
208 | |
dc818523 |
209 | my $copy = $orig->set_columns({ $col => $val, ... }); |
076a6864 |
210 | |
8091aa91 |
211 | Sets more than one column value at once. |
076a6864 |
212 | |
213 | =cut |
214 | |
215 | sub set_columns { |
216 | my ($self,$data) = @_; |
217 | while (my ($col,$val) = each %$data) { |
218 | $self->set_column($col,$val); |
219 | } |
c01ab172 |
220 | return $self; |
076a6864 |
221 | } |
222 | |
8091aa91 |
223 | =head2 copy |
076a6864 |
224 | |
225 | my $copy = $orig->copy({ change => $to, ... }); |
226 | |
8091aa91 |
227 | Inserts a new row with the specified changes. |
076a6864 |
228 | |
229 | =cut |
230 | |
c01ab172 |
231 | sub copy { |
232 | my ($self, $changes) = @_; |
233 | my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self); |
234 | $new->set_column($_ => $changes->{$_}) for keys %$changes; |
235 | return $new->insert; |
236 | } |
237 | |
8091aa91 |
238 | =head2 store_column |
7624b19f |
239 | |
240 | $obj->store_column($col => $val); |
241 | |
8091aa91 |
242 | Sets a column value without marking it as dirty. |
7624b19f |
243 | |
244 | =cut |
245 | |
246 | sub store_column { |
247 | my ($self, $column, $value) = @_; |
701da8c4 |
248 | $self->throw_exception( "No such column '${column}'" ) |
d7156e50 |
249 | unless exists $self->{_column_data}{$column} || $self->has_column($column); |
701da8c4 |
250 | $self->throw_exception( "set_column called for ${column} without value" ) |
7624b19f |
251 | if @_ < 3; |
252 | return $self->{_column_data}{$column} = $value; |
253 | } |
254 | |
b52e9bf8 |
255 | =head2 inflate_result |
256 | |
c01ab172 |
257 | Class->inflate_result($result_source, \%me, \%prefetch?) |
b52e9bf8 |
258 | |
259 | Called by ResultSet to inflate a result from storage |
260 | |
261 | =cut |
262 | |
263 | sub inflate_result { |
c01ab172 |
264 | my ($class, $source, $me, $prefetch) = @_; |
b52e9bf8 |
265 | #use Data::Dumper; print Dumper(@_); |
c01ab172 |
266 | my $new = bless({ result_source => $source, |
267 | _column_data => $me, |
268 | _in_storage => 1 |
269 | }, |
270 | ref $class || $class); |
7fb16f1a |
271 | my $schema; |
b52e9bf8 |
272 | PRE: foreach my $pre (keys %{$prefetch||{}}) { |
0dc79249 |
273 | my $pre_source = $source->related_source($pre); |
701da8c4 |
274 | $class->throw_exception("Can't prefetch non-existant relationship ${pre}") unless $pre_source; |
0dc79249 |
275 | my $fetched = $pre_source->result_class->inflate_result( |
276 | $pre_source, @{$prefetch->{$pre}}); |
277 | my $accessor = $source->relationship_info($pre)->{attrs}{accessor}; |
701da8c4 |
278 | $class->throw_exception("No accessor for prefetched $pre") |
0dc79249 |
279 | unless defined $accessor; |
280 | PRIMARY: foreach my $pri ($pre_source->primary_columns) { |
2fb09619 |
281 | unless (defined $fetched->get_column($pri)) { |
282 | undef $fetched; |
283 | last PRIMARY; |
b52e9bf8 |
284 | } |
2fb09619 |
285 | } |
0dc79249 |
286 | if ($accessor eq 'single') { |
b52e9bf8 |
287 | $new->{_relationship_data}{$pre} = $fetched; |
0dc79249 |
288 | } elsif ($accessor eq 'filter') { |
b52e9bf8 |
289 | $new->{_inflated_column}{$pre} = $fetched; |
290 | } else { |
701da8c4 |
291 | $class->throw_exception("Don't know how to store prefetched $pre"); |
b52e9bf8 |
292 | } |
293 | } |
7624b19f |
294 | return $new; |
295 | } |
296 | |
8091aa91 |
297 | =head2 insert_or_update |
7624b19f |
298 | |
299 | $obj->insert_or_update |
300 | |
8091aa91 |
301 | Updates the object if it's already in the db, else inserts it. |
7624b19f |
302 | |
303 | =cut |
304 | |
305 | sub insert_or_update { |
306 | my $self = shift; |
307 | return ($self->in_storage ? $self->update : $self->insert); |
308 | } |
309 | |
8091aa91 |
310 | =head2 is_changed |
7624b19f |
311 | |
312 | my @changed_col_names = $obj->is_changed |
313 | |
314 | =cut |
315 | |
316 | sub is_changed { |
317 | return keys %{shift->{_dirty_columns} || {}}; |
318 | } |
319 | |
097d3227 |
320 | =head2 result_source |
321 | |
322 | Accessor to the ResultSource this object was created from |
323 | |
71e65b39 |
324 | =head2 register_column($column, $column_info) |
1f23a877 |
325 | |
91b0fbd7 |
326 | Registers a column on the class. If the column_info has an 'accessor' key, |
327 | creates an accessor named after the value if defined; if there is no such |
328 | key, creates an accessor with the same name as the column |
1f23a877 |
329 | |
097d3227 |
330 | =cut |
331 | |
1f23a877 |
332 | sub register_column { |
333 | my ($class, $col, $info) = @_; |
91b0fbd7 |
334 | my $acc = $col; |
335 | if (exists $info->{accessor}) { |
336 | return unless defined $info->{accessor}; |
337 | $acc = [ $info->{accessor}, $col ]; |
338 | } |
339 | $class->mk_group_accessors('column' => $acc); |
1f23a877 |
340 | } |
341 | |
701da8c4 |
342 | |
5160b401 |
343 | =head2 throw_exception |
701da8c4 |
344 | |
345 | See Schema's throw_exception. |
346 | |
347 | =cut |
348 | |
349 | sub throw_exception { |
350 | my $self=shift; |
351 | if (ref $self && ref $self->result_source) { |
352 | $self->result_source->schema->throw_exception(@_); |
353 | } else { |
354 | croak(@_); |
355 | } |
356 | } |
357 | |
7624b19f |
358 | 1; |
359 | |
7624b19f |
360 | =head1 AUTHORS |
361 | |
daec44b8 |
362 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
7624b19f |
363 | |
364 | =head1 LICENSE |
365 | |
366 | You may distribute this code under the same terms as Perl itself. |
367 | |
368 | =cut |
369 | |