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