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);
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;
63 $self->{_dirty_columns} = {};
69 $obj->in_storage; # Get value
70 $obj->in_storage(1); # Set value
72 Indicated whether the object exists as a row in the database or not
77 my ($self, $val) = @_;
78 $self->{_in_storage} = $val if @_ > 1;
79 return $self->{_in_storage};
84 my $new = My::Class->create($attrs);
86 A shortcut for My::Class->new($attrs)->insert;
91 my ($class, $attrs) = @_;
92 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
93 return $class->new($attrs)->insert;
100 Must be run on an object that is already in the database; issues an SQL
101 UPDATE query to commit any changes to the object to the db if required.
106 my ($self, $upd) = @_;
107 $self->throw( "Not in database" ) unless $self->in_storage;
109 $to_update{$_} = $self->get_column($_) for $self->is_changed;
110 return -1 unless keys %to_update;
111 my $rows = $self->storage->update($self->_table_name, \%to_update,
112 $self->ident_condition);
114 $self->throw( "Can't update ${self}: row not found" );
115 } elsif ($rows > 1) {
116 $self->throw("Can't update ${self}: updated more than one row");
118 $self->{_dirty_columns} = {};
126 Deletes the object from the database. The object is still perfectly usable
127 accessor-wise etc. but ->in_storage will now return 0 and the object must
128 be re ->insert'ed before it can be ->update'ed
135 $self->throw( "Not in database" ) unless $self->in_storage;
136 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
137 $self->storage->delete($self->_table_name, $self->ident_condition);
138 $self->in_storage(undef);
139 #$self->store_column($_ => undef) for $self->primary_columns;
140 # Should probably also arrange to trash PK if auto
141 # but if we do, post-delete cascade triggers fail :/
144 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
145 $attrs = { %{ pop(@_) } };
147 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
148 $self->storage->delete($self->_table_name, $query);
155 my $val = $obj->get_column($col);
157 Gets a column value from a row object. Currently, does not do
158 any queries; the column must have already been fetched from
159 the database and stored in the object.
164 my ($self, $column) = @_;
165 $self->throw( "Can't fetch data as class method" ) unless ref $self;
166 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
167 return $self->{_column_data}{$column}
168 if exists $self->{_column_data}{$column};
174 my %data = $obj->get_columns;
176 Does C<get_column>, for all column values at once.
182 return map { $_ => $self->get_column($_) } $self->columns;
187 $obj->set_column($col => $val);
189 Sets a column value. If the new value is different from the old one,
190 the column is marked as dirty for when you next call $obj->update.
197 my $old = $self->get_column($column);
198 my $ret = $self->store_column(@_);
199 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
205 my $copy = $orig->set_columns({ $col => $val, ... });
207 Sets more than one column value at once.
212 my ($self,$data) = @_;
213 while (my ($col,$val) = each %$data) {
214 $self->set_column($col,$val);
220 my $copy = $orig->copy({ change => $to, ... });
222 Inserts a new row with the specified changes.
228 $obj->store_column($col => $val);
230 Sets a column value without marking it as dirty.
235 my ($self, $column, $value) = @_;
236 $self->throw( "No such column '${column}'" )
237 unless $self->has_column($column);
238 $self->throw( "set_column called for ${column} without value" )
240 return $self->{_column_data}{$column} = $value;
243 =head2 inflate_result
245 Class->inflate_result(\%me, \%prefetch?)
247 Called by ResultSet to inflate a result from storage
252 my ($class, $me, $prefetch) = @_;
253 #use Data::Dumper; print Dumper(@_);
254 my $new = bless({ _column_data => $me }, ref $class || $class);
256 PRE: foreach my $pre (keys %{$prefetch||{}}) {
257 my $rel_obj = $class->_relationships->{$pre};
258 my $pre_class = $class->resolve_class($rel_obj->{class});
259 my $fetched = $pre_class->inflate_result(@{$prefetch->{$pre}});
260 $class->throw("No accessor for prefetched $pre")
261 unless defined $rel_obj->{attrs}{accessor};
262 if ($rel_obj->{attrs}{accessor} eq 'single') {
263 PRIMARY: foreach my $pri ($rel_obj->{class}->primary_columns) {
264 unless (defined $fetched->get_column($pri)) {
269 $new->{_relationship_data}{$pre} = $fetched;
270 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
271 $new->{_inflated_column}{$pre} = $fetched;
273 $class->throw("Don't know how to store prefetched $pre");
280 my ($self, $changes) = @_;
281 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
282 $new->set_column($_ => $changes->{$_}) for keys %$changes;
286 =head2 insert_or_update
288 $obj->insert_or_update
290 Updates the object if it's already in the db, else inserts it.
294 sub insert_or_update {
296 return ($self->in_storage ? $self->update : $self->insert);
301 my @changed_col_names = $obj->is_changed
306 return keys %{shift->{_dirty_columns} || {}};
313 Matt S. Trout <mst@shadowcatsystems.co.uk>
317 You may distribute this code under the same terms as Perl itself.