Merge 'trunk' into 'DBIx-Class-C3'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Row.pm
CommitLineData
7624b19f 1package DBIx::Class::Row;
2
3use strict;
4use warnings;
5
6=head1 NAME
7
8DBIx::Class::Row - Basic row methods
9
10=head1 SYNOPSIS
11
12=head1 DESCRIPTION
13
14This class is responsible for defining and doing basic operations on rows
15derived from L<DBIx::Class::Table> objects.
16
17=head1 METHODS
18
19=over 4
20
21=item new
22
23 my $obj = My::Class->new($attrs);
24
25Creates a new row object from column => value mappings passed as a hash ref
26
27=cut
28
29sub 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
43=item insert
44
45 $obj->insert;
46
47Inserts an object into the database if it isn't already in there. Returns
48the object itself.
49
50=cut
51
52sub insert {
53 my ($self) = @_;
54 return $self if $self->in_storage;
55 #use Data::Dumper; warn Dumper($self);
56 my %in;
57 $in{$_} = $self->get_column($_)
58 for grep { defined $self->get_column($_) } $self->columns;
59 my %out = %{ $self->storage->insert($self->_table_name, \%in) };
60 $self->store_column($_, $out{$_})
61 for grep { $self->get_column($_) ne $out{$_} } keys %out;
62 $self->in_storage(1);
63 $self->{_dirty_columns} = {};
64 return $self;
65}
66
67=item in_storage
68
69 $obj->in_storage; # Get value
70 $obj->in_storage(1); # Set value
71
72Indicated whether the object exists as a row in the database or not
73
74=cut
75
76sub in_storage {
77 my ($self, $val) = @_;
78 $self->{_in_storage} = $val if @_ > 1;
79 return $self->{_in_storage};
80}
81
82=item create
83
84 my $new = My::Class->create($attrs);
85
86A shortcut for My::Class->new($attrs)->insert;
87
88=cut
89
90sub create {
91 my ($class, $attrs) = @_;
92 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
93 return $class->new($attrs)->insert;
94}
95
96=item update
97
98 $obj->update;
99
100Must be run on an object that is already in the database; issues an SQL
101UPDATE query to commit any changes to the object to the db if required.
102
103=cut
104
105sub update {
106 my ($self, $upd) = @_;
107 $self->throw( "Not in database" ) unless $self->in_storage;
99be059e 108 if (ref $upd eq 'HASH') {
109 $self->$_($upd->{$_}) for keys %$upd;
110 }
111 my %to_update;
7624b19f 112 $to_update{$_} = $self->get_column($_) for $self->is_changed;
113 return -1 unless keys %to_update;
114 my $rows = $self->storage->update($self->_table_name, \%to_update,
115 $self->ident_condition);
116 if ($rows == 0) {
117 $self->throw( "Can't update ${self}: row not found" );
118 } elsif ($rows > 1) {
119 $self->throw("Can't update ${self}: updated more than one row");
120 }
121 $self->{_dirty_columns} = {};
122 return $self;
123}
124
7624b19f 125=item delete
126
127 $obj->delete
128
129Deletes the object from the database. The object is still perfectly usable
130accessor-wise etc. but ->in_storage will now return 0 and the object must
131be re ->insert'ed before it can be ->update'ed
132
133=cut
134
135sub delete {
136 my $self = shift;
137 if (ref $self) {
138 $self->throw( "Not in database" ) unless $self->in_storage;
139 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
140 $self->storage->delete($self->_table_name, $self->ident_condition);
141 $self->in_storage(undef);
142 #$self->store_column($_ => undef) for $self->primary_columns;
143 # Should probably also arrange to trash PK if auto
144 # but if we do, post-delete cascade triggers fail :/
145 } else {
146 my $attrs = { };
147 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
148 $attrs = { %{ pop(@_) } };
149 }
150 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
151 $self->storage->delete($self->_table_name, $query);
152 }
153 return $self;
154}
155
156=item get_column
157
158 my $val = $obj->get_column($col);
159
160Fetches a column value
161
162=cut
163
164sub get_column {
165 my ($self, $column) = @_;
166 $self->throw( "Can't fetch data as class method" ) unless ref $self;
103647d5 167 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
7624b19f 168 return $self->{_column_data}{$column}
169 if exists $self->{_column_data}{$column};
170 return undef;
171}
172
076a6864 173=item get_columns
174
175 my %data = $obj->get_columns;
176
177Fetch all column values at once.
178
179=cut
180
181sub get_columns {
182 my $self = shift;
183 return map { $_ => $self->get_column($_) } $self->columns;
184}
185
7624b19f 186=item set_column
187
188 $obj->set_column($col => $val);
189
190Sets a column value; if the new value is different to the old the column
191is marked as dirty for when you next call $obj->update
192
193=cut
194
195sub set_column {
196 my $self = shift;
197 my ($column) = @_;
198 my $old = $self->get_column($column);
199 my $ret = $self->store_column(@_);
200 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
201 return $ret;
202}
203
076a6864 204=item set_columns
205
dc818523 206 my $copy = $orig->set_columns({ $col => $val, ... });
076a6864 207
208Set more than one column value at once.
209
210=cut
211
212sub set_columns {
213 my ($self,$data) = @_;
214 while (my ($col,$val) = each %$data) {
215 $self->set_column($col,$val);
216 }
217}
218
219=item copy
220
221 my $copy = $orig->copy({ change => $to, ... });
222
223Insert a new row with the specified changes.
224
225=cut
226
7624b19f 227=item store_column
228
229 $obj->store_column($col => $val);
230
231Sets a column value without marking it as dirty
232
233=cut
234
235sub store_column {
236 my ($self, $column, $value) = @_;
237 $self->throw( "No such column '${column}'" )
103647d5 238 unless $self->has_column($column);
7624b19f 239 $self->throw( "set_column called for ${column} without value" )
240 if @_ < 3;
241 return $self->{_column_data}{$column} = $value;
242}
243
1a14aa3f 244sub _row_to_object {
7624b19f 245 my ($class, $cols, $row) = @_;
1a14aa3f 246 my %vals;
247 $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
484c9dda 248 my $new = bless({ _column_data => \%vals }, ref $class || $class);
7624b19f 249 $new->in_storage(1);
250 return $new;
251}
252
7624b19f 253sub copy {
254 my ($self, $changes) = @_;
255 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
256 $new->set_column($_ => $changes->{$_}) for keys %$changes;
257 return $new->insert;
258}
259
260=item insert_or_update
261
262 $obj->insert_or_update
263
264Updates the object if it's already in the db, else inserts it
265
266=cut
267
268sub insert_or_update {
269 my $self = shift;
270 return ($self->in_storage ? $self->update : $self->insert);
271}
272
273=item is_changed
274
275 my @changed_col_names = $obj->is_changed
276
277=cut
278
279sub is_changed {
280 return keys %{shift->{_dirty_columns} || {}};
281}
282
2831;
284
285=back
286
287=head1 AUTHORS
288
daec44b8 289Matt S. Trout <mst@shadowcatsystems.co.uk>
7624b19f 290
291=head1 LICENSE
292
293You may distribute this code under the same terms as Perl itself.
294
295=cut
296