1 package DBIx::Class::Row;
6 use base qw/DBIx::Class/;
10 DBIx::Class::Row - Basic row methods
16 This class is responsible for defining and doing basic operations on rows
17 derived from L<DBIx::Class::Table> objects.
23 my $obj = My::Class->new($attrs);
25 Creates a new row object from column => value mappings passed as a hash ref
30 my ($class, $attrs) = @_;
31 $class = ref $class if ref $class;
32 my $new = bless({ _column_data => { } }, $class);
34 $new->throw("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
35 while (my ($k, $v) = each %{$attrs}) {
36 die "No such column $k on $class" unless $class->has_column($k);
37 $new->store_column($k => $v);
47 Inserts an object into the database if it isn't already in there. Returns
54 return $self if $self->in_storage;
55 #use Data::Dumper; warn Dumper($self);
56 $self->storage->insert($self->_table_name, { $self->get_columns });
58 $self->{_dirty_columns} = {};
64 $obj->in_storage; # Get value
65 $obj->in_storage(1); # Set value
67 Indicated whether the object exists as a row in the database or not
72 my ($self, $val) = @_;
73 $self->{_in_storage} = $val if @_ > 1;
74 return $self->{_in_storage};
79 my $new = My::Class->create($attrs);
81 A shortcut for My::Class->new($attrs)->insert;
86 my ($class, $attrs) = @_;
87 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
88 return $class->new($attrs)->insert;
95 Must be run on an object that is already in the database; issues an SQL
96 UPDATE query to commit any changes to the object to the db if required.
101 my ($self, $upd) = @_;
102 $self->throw( "Not in database" ) unless $self->in_storage;
103 my %to_update = $self->get_dirty_columns;
104 return -1 unless keys %to_update;
105 my $rows = $self->storage->update($self->_table_name, \%to_update,
106 $self->ident_condition);
108 $self->throw( "Can't update ${self}: row not found" );
109 } elsif ($rows > 1) {
110 $self->throw("Can't update ${self}: updated more than one row");
112 $self->{_dirty_columns} = {};
120 Deletes the object from the database. The object is still perfectly usable
121 accessor-wise etc. but ->in_storage will now return 0 and the object must
122 be re ->insert'ed before it can be ->update'ed
129 $self->throw( "Not in database" ) unless $self->in_storage;
130 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
131 $self->storage->delete($self->_table_name, $self->ident_condition);
132 $self->in_storage(undef);
133 #$self->store_column($_ => undef) for $self->primary_columns;
134 # Should probably also arrange to trash PK if auto
135 # but if we do, post-delete cascade triggers fail :/
138 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
139 $attrs = { %{ pop(@_) } };
141 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
142 $self->storage->delete($self->_table_name, $query);
149 my $val = $obj->get_column($col);
151 Gets a column value from a row object. Currently, does not do
152 any queries; the column must have already been fetched from
153 the database and stored in the object.
158 my ($self, $column) = @_;
159 $self->throw( "Can't fetch data as class method" ) unless ref $self;
160 return $self->{_column_data}{$column}
161 if exists $self->{_column_data}{$column};
162 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
168 my %data = $obj->get_columns;
170 Does C<get_column>, for all column values at once.
176 return return %{$self->{_column_data}};
179 =head2 get_dirty_columns
181 my %data = $obj->get_dirty_columns;
183 Identical to get_columns but only returns those that have been changed.
187 sub get_dirty_columns {
189 return map { $_ => $self->{_column_data}{$_} }
190 keys %{$self->{_dirty_columns}};
195 $obj->set_column($col => $val);
197 Sets a column value. If the new value is different from the old one,
198 the column is marked as dirty for when you next call $obj->update.
205 my $old = $self->get_column($column);
206 my $ret = $self->store_column(@_);
207 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
213 my $copy = $orig->set_columns({ $col => $val, ... });
215 Sets more than one column value at once.
220 my ($self,$data) = @_;
221 while (my ($col,$val) = each %$data) {
222 $self->set_column($col,$val);
228 my $copy = $orig->copy({ change => $to, ... });
230 Inserts a new row with the specified changes.
236 $obj->store_column($col => $val);
238 Sets a column value without marking it as dirty.
243 my ($self, $column, $value) = @_;
244 $self->throw( "No such column '${column}'" )
245 unless exists $self->{_column_data}{$column} || $self->has_column($column);
246 $self->throw( "set_column called for ${column} without value" )
248 return $self->{_column_data}{$column} = $value;
251 =head2 inflate_result
253 Class->inflate_result(\%me, \%prefetch?)
255 Called by ResultSet to inflate a result from storage
260 my ($class, $me, $prefetch) = @_;
261 #use Data::Dumper; print Dumper(@_);
262 my $new = bless({ _column_data => $me }, ref $class || $class);
264 PRE: foreach my $pre (keys %{$prefetch||{}}) {
265 my $rel_obj = $class->_relationships->{$pre};
266 my $pre_class = $class->resolve_class($rel_obj->{class});
267 my $fetched = $pre_class->inflate_result(@{$prefetch->{$pre}});
268 $class->throw("No accessor for prefetched $pre")
269 unless defined $rel_obj->{attrs}{accessor};
270 if ($rel_obj->{attrs}{accessor} eq 'single') {
271 PRIMARY: foreach my $pri ($rel_obj->{class}->primary_columns) {
272 unless (defined $fetched->get_column($pri)) {
277 $new->{_relationship_data}{$pre} = $fetched;
278 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
279 $new->{_inflated_column}{$pre} = $fetched;
281 $class->throw("Don't know how to store prefetched $pre");
288 my ($self, $changes) = @_;
289 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
290 $new->set_column($_ => $changes->{$_}) for keys %$changes;
294 =head2 insert_or_update
296 $obj->insert_or_update
298 Updates the object if it's already in the db, else inserts it.
302 sub insert_or_update {
304 return ($self->in_storage ? $self->update : $self->insert);
309 my @changed_col_names = $obj->is_changed
314 return keys %{shift->{_dirty_columns} || {}};
321 Matt S. Trout <mst@shadowcatsystems.co.uk>
325 You may distribute this code under the same terms as Perl itself.