1 package DBIx::Class::Row;
6 use base qw/DBIx::Class/;
8 #__PACKAGE__->mk_group_accessors('simple' => 'result_source');
12 DBIx::Class::Row - Basic row methods
18 This class is responsible for defining and doing basic operations on rows
19 derived from L<DBIx::Class::Table> objects.
25 my $obj = My::Class->new($attrs);
27 Creates a new row object from column => value mappings passed as a hash ref
32 my ($class, $attrs) = @_;
33 $class = ref $class if ref $class;
34 my $new = bless({ _column_data => { } }, $class);
36 $new->throw("attrs must be a hashref" ) unless ref($attrs) eq 'HASH';
37 while (my ($k, $v) = each %{$attrs}) {
38 die "No such column $k on $class" unless $class->has_column($k);
39 $new->store_column($k => $v);
49 Inserts an object into the database if it isn't already in there. Returns
56 return $self if $self->in_storage;
57 #use Data::Dumper; warn Dumper($self);
58 $self->result_source->storage->insert(
59 $self->result_source->from, { $self->get_columns });
61 $self->{_dirty_columns} = {};
67 $obj->in_storage; # Get value
68 $obj->in_storage(1); # Set value
70 Indicated whether the object exists as a row in the database or not
75 my ($self, $val) = @_;
76 $self->{_in_storage} = $val if @_ > 1;
77 return $self->{_in_storage};
84 Must be run on an object that is already in the database; issues an SQL
85 UPDATE query to commit any changes to the object to the db if required.
90 my ($self, $upd) = @_;
91 $self->throw( "Not in database" ) unless $self->in_storage;
92 my %to_update = $self->get_dirty_columns;
93 return $self unless keys %to_update;
94 my $rows = $self->result_source->storage->update(
95 $self->result_source->from, \%to_update, $self->ident_condition);
97 $self->throw( "Can't update ${self}: row not found" );
99 $self->throw("Can't update ${self}: updated more than one row");
101 $self->{_dirty_columns} = {};
109 Deletes the object from the database. The object is still perfectly usable
110 accessor-wise etc. but ->in_storage will now return 0 and the object must
111 be re ->insert'ed before it can be ->update'ed
118 $self->throw( "Not in database" ) unless $self->in_storage;
119 $self->result_source->storage->delete(
120 $self->result_source->from, $self->ident_condition);
121 $self->in_storage(undef);
124 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
125 $attrs = { %{ pop(@_) } };
127 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
128 $self->result_source->resultset->search(@_)->delete;
135 my $val = $obj->get_column($col);
137 Gets a column value from a row object. Currently, does not do
138 any queries; the column must have already been fetched from
139 the database and stored in the object.
144 my ($self, $column) = @_;
145 $self->throw( "Can't fetch data as class method" ) unless ref $self;
146 return $self->{_column_data}{$column}
147 if exists $self->{_column_data}{$column};
148 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
154 my %data = $obj->get_columns;
156 Does C<get_column>, for all column values at once.
162 return return %{$self->{_column_data}};
165 =head2 get_dirty_columns
167 my %data = $obj->get_dirty_columns;
169 Identical to get_columns but only returns those that have been changed.
173 sub get_dirty_columns {
175 return map { $_ => $self->{_column_data}{$_} }
176 keys %{$self->{_dirty_columns}};
181 $obj->set_column($col => $val);
183 Sets a column value. If the new value is different from the old one,
184 the column is marked as dirty for when you next call $obj->update.
191 my $old = $self->get_column($column);
192 my $ret = $self->store_column(@_);
193 $self->{_dirty_columns}{$column} = 1
194 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
200 my $copy = $orig->set_columns({ $col => $val, ... });
202 Sets more than one column value at once.
207 my ($self,$data) = @_;
208 while (my ($col,$val) = each %$data) {
209 $self->set_column($col,$val);
216 my $copy = $orig->copy({ change => $to, ... });
218 Inserts a new row with the specified changes.
223 my ($self, $changes) = @_;
224 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
225 $new->set_column($_ => $changes->{$_}) for keys %$changes;
231 $obj->store_column($col => $val);
233 Sets a column value without marking it as dirty.
238 my ($self, $column, $value) = @_;
239 $self->throw( "No such column '${column}'" )
240 unless exists $self->{_column_data}{$column} || $self->has_column($column);
241 $self->throw( "set_column called for ${column} without value" )
243 return $self->{_column_data}{$column} = $value;
246 =head2 inflate_result
248 Class->inflate_result($result_source, \%me, \%prefetch?)
250 Called by ResultSet to inflate a result from storage
255 my ($class, $source, $me, $prefetch) = @_;
256 #use Data::Dumper; print Dumper(@_);
257 my $new = bless({ result_source => $source,
261 ref $class || $class);
263 PRE: foreach my $pre (keys %{$prefetch||{}}) {
264 my $rel_obj = $class->relationship_info($pre);
265 die "Can't prefetch non-eistant relationship ${pre}" unless $rel_obj;
266 $schema ||= $source->schema;
267 my $pre_class = $schema->class($rel_obj->{class});
268 my $fetched = $pre_class->inflate_result(
269 $schema->source($pre_class), @{$prefetch->{$pre}});
270 $class->throw("No accessor for prefetched $pre")
271 unless defined $rel_obj->{attrs}{accessor};
272 PRIMARY: foreach my $pri ($rel_obj->{class}->primary_columns) {
273 unless (defined $fetched->get_column($pri)) {
278 if ($rel_obj->{attrs}{accessor} eq 'single') {
279 $new->{_relationship_data}{$pre} = $fetched;
280 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
281 $new->{_inflated_column}{$pre} = $fetched;
283 $class->throw("Don't know how to store prefetched $pre");
289 =head2 insert_or_update
291 $obj->insert_or_update
293 Updates the object if it's already in the db, else inserts it.
297 sub insert_or_update {
299 return ($self->in_storage ? $self->update : $self->insert);
304 my @changed_col_names = $obj->is_changed
309 return keys %{shift->{_dirty_columns} || {}};
316 Matt S. Trout <mst@shadowcatsystems.co.uk>
320 You may distribute this code under the same terms as Perl itself.