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->result_source->storage->insert(
57 $self->_table_name, { $self->get_columns });
59 $self->{_dirty_columns} = {};
65 $obj->in_storage; # Get value
66 $obj->in_storage(1); # Set value
68 Indicated whether the object exists as a row in the database or not
73 my ($self, $val) = @_;
74 $self->{_in_storage} = $val if @_ > 1;
75 return $self->{_in_storage};
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.
88 my ($self, $upd) = @_;
89 $self->throw( "Not in database" ) unless $self->in_storage;
90 my %to_update = $self->get_dirty_columns;
91 return -1 unless keys %to_update;
92 my $rows = $self->result_source->storage->update(
93 $self->result_source->from, \%to_update, $self->ident_condition);
95 $self->throw( "Can't update ${self}: row not found" );
97 $self->throw("Can't update ${self}: updated more than one row");
99 $self->{_dirty_columns} = {};
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
116 $self->throw( "Not in database" ) unless $self->in_storage;
117 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
118 $self->result_source->storage->delete(
119 $self->result_source->from, $self->ident_condition);
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 :/
126 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
127 $attrs = { %{ pop(@_) } };
129 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
130 $self->storage->delete($self->_table_name, $query);
137 my $val = $obj->get_column($col);
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.
146 my ($self, $column) = @_;
147 $self->throw( "Can't fetch data as class method" ) unless ref $self;
148 return $self->{_column_data}{$column}
149 if exists $self->{_column_data}{$column};
150 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
156 my %data = $obj->get_columns;
158 Does C<get_column>, for all column values at once.
164 return return %{$self->{_column_data}};
167 =head2 get_dirty_columns
169 my %data = $obj->get_dirty_columns;
171 Identical to get_columns but only returns those that have been changed.
175 sub get_dirty_columns {
177 return map { $_ => $self->{_column_data}{$_} }
178 keys %{$self->{_dirty_columns}};
183 $obj->set_column($col => $val);
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.
193 my $old = $self->get_column($column);
194 my $ret = $self->store_column(@_);
195 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
201 my $copy = $orig->set_columns({ $col => $val, ... });
203 Sets more than one column value at once.
208 my ($self,$data) = @_;
209 while (my ($col,$val) = each %$data) {
210 $self->set_column($col,$val);
216 my $copy = $orig->copy({ change => $to, ... });
218 Inserts a new row with the specified changes.
224 $obj->store_column($col => $val);
226 Sets a column value without marking it as dirty.
231 my ($self, $column, $value) = @_;
232 $self->throw( "No such column '${column}'" )
233 unless exists $self->{_column_data}{$column} || $self->has_column($column);
234 $self->throw( "set_column called for ${column} without value" )
236 return $self->{_column_data}{$column} = $value;
239 =head2 inflate_result
241 Class->inflate_result(\%me, \%prefetch?)
243 Called by ResultSet to inflate a result from storage
248 my ($class, $me, $prefetch) = @_;
249 #use Data::Dumper; print Dumper(@_);
250 my $new = bless({ _column_data => $me, _in_storage => 1 },
251 ref $class || $class);
253 PRE: foreach my $pre (keys %{$prefetch||{}}) {
254 my $rel_obj = $class->relationship_info($pre);
255 die "Can't prefetch non-eistant relationship ${pre}" unless $rel_obj;
256 $schema ||= $new->result_source->schema;
257 my $pre_class = $schema->class($rel_obj->{class});
258 my $fetched = $pre_class->inflate_result(@{$prefetch->{$pre}});
259 $class->throw("No accessor for prefetched $pre")
260 unless defined $rel_obj->{attrs}{accessor};
261 PRIMARY: foreach my $pri ($rel_obj->{class}->primary_columns) {
262 unless (defined $fetched->get_column($pri)) {
267 if ($rel_obj->{attrs}{accessor} eq 'single') {
268 $new->{_relationship_data}{$pre} = $fetched;
269 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
270 $new->{_inflated_column}{$pre} = $fetched;
272 $class->throw("Don't know how to store prefetched $pre");
279 my ($self, $changes) = @_;
280 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
281 $new->set_column($_ => $changes->{$_}) for keys %$changes;
285 =head2 insert_or_update
287 $obj->insert_or_update
289 Updates the object if it's already in the db, else inserts it.
293 sub insert_or_update {
295 return ($self->in_storage ? $self->update : $self->insert);
300 my @changed_col_names = $obj->is_changed
305 return keys %{shift->{_dirty_columns} || {}};
312 Matt S. Trout <mst@shadowcatsystems.co.uk>
316 You may distribute this code under the same terms as Perl itself.