Updated main docs, altered mail address in POD for 0.01
[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}) {
36 $new->store_column($k => $v);
37 }
38 }
39 return $new;
40}
41
42=item insert
43
44 $obj->insert;
45
46Inserts an object into the database if it isn't already in there. Returns
47the object itself.
48
49=cut
50
51sub insert {
52 my ($self) = @_;
53 return $self if $self->in_storage;
54 #use Data::Dumper; warn Dumper($self);
55 my %in;
56 $in{$_} = $self->get_column($_)
57 for grep { defined $self->get_column($_) } $self->columns;
58 my %out = %{ $self->storage->insert($self->_table_name, \%in) };
59 $self->store_column($_, $out{$_})
60 for grep { $self->get_column($_) ne $out{$_} } keys %out;
61 $self->in_storage(1);
62 $self->{_dirty_columns} = {};
63 return $self;
64}
65
66=item in_storage
67
68 $obj->in_storage; # Get value
69 $obj->in_storage(1); # Set value
70
71Indicated whether the object exists as a row in the database or not
72
73=cut
74
75sub in_storage {
76 my ($self, $val) = @_;
77 $self->{_in_storage} = $val if @_ > 1;
78 return $self->{_in_storage};
79}
80
81=item create
82
83 my $new = My::Class->create($attrs);
84
85A shortcut for My::Class->new($attrs)->insert;
86
87=cut
88
89sub create {
90 my ($class, $attrs) = @_;
91 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
92 return $class->new($attrs)->insert;
93}
94
95=item update
96
97 $obj->update;
98
99Must be run on an object that is already in the database; issues an SQL
100UPDATE query to commit any changes to the object to the db if required.
101
102=cut
103
104sub update {
105 my ($self, $upd) = @_;
106 $self->throw( "Not in database" ) unless $self->in_storage;
107 my %to_update = %{$upd || {}};
108 $to_update{$_} = $self->get_column($_) for $self->is_changed;
109 return -1 unless keys %to_update;
110 my $rows = $self->storage->update($self->_table_name, \%to_update,
111 $self->ident_condition);
112 if ($rows == 0) {
113 $self->throw( "Can't update ${self}: row not found" );
114 } elsif ($rows > 1) {
115 $self->throw("Can't update ${self}: updated more than one row");
116 }
117 $self->{_dirty_columns} = {};
118 return $self;
119}
120
121sub ident_condition {
122 my ($self) = @_;
123 my %cond;
124 $cond{$_} = $self->get_column($_) for keys %{$self->_primaries};
125 return \%cond;
126}
127
128=item delete
129
130 $obj->delete
131
132Deletes the object from the database. The object is still perfectly usable
133accessor-wise etc. but ->in_storage will now return 0 and the object must
134be re ->insert'ed before it can be ->update'ed
135
136=cut
137
138sub delete {
139 my $self = shift;
140 if (ref $self) {
141 $self->throw( "Not in database" ) unless $self->in_storage;
142 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
143 $self->storage->delete($self->_table_name, $self->ident_condition);
144 $self->in_storage(undef);
145 #$self->store_column($_ => undef) for $self->primary_columns;
146 # Should probably also arrange to trash PK if auto
147 # but if we do, post-delete cascade triggers fail :/
148 } else {
149 my $attrs = { };
150 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
151 $attrs = { %{ pop(@_) } };
152 }
153 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
154 $self->storage->delete($self->_table_name, $query);
155 }
156 return $self;
157}
158
159=item get_column
160
161 my $val = $obj->get_column($col);
162
163Fetches a column value
164
165=cut
166
167sub get_column {
168 my ($self, $column) = @_;
169 $self->throw( "Can't fetch data as class method" ) unless ref $self;
170 $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
171 return $self->{_column_data}{$column}
172 if exists $self->{_column_data}{$column};
173 return undef;
174}
175
176=item set_column
177
178 $obj->set_column($col => $val);
179
180Sets a column value; if the new value is different to the old the column
181is marked as dirty for when you next call $obj->update
182
183=cut
184
185sub set_column {
186 my $self = shift;
187 my ($column) = @_;
188 my $old = $self->get_column($column);
189 my $ret = $self->store_column(@_);
190 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
191 return $ret;
192}
193
194=item store_column
195
196 $obj->store_column($col => $val);
197
198Sets a column value without marking it as dirty
199
200=cut
201
202sub store_column {
203 my ($self, $column, $value) = @_;
204 $self->throw( "No such column '${column}'" )
205 unless $self->_columns->{$column};
206 $self->throw( "set_column called for ${column} without value" )
207 if @_ < 3;
208 return $self->{_column_data}{$column} = $value;
209}
210
211sub _row_to_object { # WARNING: Destructive to @$row
212 my ($class, $cols, $row) = @_;
213 my $new = $class->new;
214 $new->store_column($_, shift @$row) for @$cols;
215 $new->in_storage(1);
216 return $new;
217}
218
219=item copy
220
221 my $copy = $orig->copy({ change => $to, ... });
222
223=cut
224
225sub copy {
226 my ($self, $changes) = @_;
227 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
228 $new->set_column($_ => $changes->{$_}) for keys %$changes;
229 return $new->insert;
230}
231
232=item insert_or_update
233
234 $obj->insert_or_update
235
236Updates the object if it's already in the db, else inserts it
237
238=cut
239
240sub insert_or_update {
241 my $self = shift;
242 return ($self->in_storage ? $self->update : $self->insert);
243}
244
245=item is_changed
246
247 my @changed_col_names = $obj->is_changed
248
249=cut
250
251sub is_changed {
252 return keys %{shift->{_dirty_columns} || {}};
253}
254
2551;
256
257=back
258
259=head1 AUTHORS
260
daec44b8 261Matt S. Trout <mst@shadowcatsystems.co.uk>
7624b19f 262
263=head1 LICENSE
264
265You may distribute this code under the same terms as Perl itself.
266
267=cut
268