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.
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);
59 $in{$_} = $self->get_column($_)
60 for grep { defined $self->get_column($_) } $self->columns;
61 my %out = %{ $self->storage->insert($self->_table_name, \%in) };
62 $self->store_column($_, $out{$_})
63 for grep { $self->get_column($_) ne $out{$_} } keys %out;
65 $self->{_dirty_columns} = {};
71 $obj->in_storage; # Get value
72 $obj->in_storage(1); # Set value
74 Indicated whether the object exists as a row in the database or not
79 my ($self, $val) = @_;
80 $self->{_in_storage} = $val if @_ > 1;
81 return $self->{_in_storage};
86 my $new = My::Class->create($attrs);
88 A shortcut for My::Class->new($attrs)->insert;
93 my ($class, $attrs) = @_;
94 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
95 return $class->new($attrs)->insert;
102 Must be run on an object that is already in the database; issues an SQL
103 UPDATE query to commit any changes to the object to the db if required.
108 my ($self, $upd) = @_;
109 $self->throw( "Not in database" ) unless $self->in_storage;
110 if (ref $upd eq 'HASH') {
111 $self->$_($upd->{$_}) for keys %$upd;
114 $to_update{$_} = $self->get_column($_) for $self->is_changed;
115 return -1 unless keys %to_update;
116 my $rows = $self->storage->update($self->_table_name, \%to_update,
117 $self->ident_condition);
119 $self->throw( "Can't update ${self}: row not found" );
120 } elsif ($rows > 1) {
121 $self->throw("Can't update ${self}: updated more than one row");
123 $self->{_dirty_columns} = {};
131 Deletes the object from the database. The object is still perfectly usable
132 accessor-wise etc. but ->in_storage will now return 0 and the object must
133 be re ->insert'ed before it can be ->update'ed
140 $self->throw( "Not in database" ) unless $self->in_storage;
141 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
142 $self->storage->delete($self->_table_name, $self->ident_condition);
143 $self->in_storage(undef);
144 #$self->store_column($_ => undef) for $self->primary_columns;
145 # Should probably also arrange to trash PK if auto
146 # but if we do, post-delete cascade triggers fail :/
149 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
150 $attrs = { %{ pop(@_) } };
152 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
153 $self->storage->delete($self->_table_name, $query);
160 my $val = $obj->get_column($col);
162 Fetches a column value
167 my ($self, $column) = @_;
168 $self->throw( "Can't fetch data as class method" ) unless ref $self;
169 $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
170 return $self->{_column_data}{$column}
171 if exists $self->{_column_data}{$column};
177 my %data = $obj->get_columns;
179 Fetch all column values at once.
185 return map { $_ => $self->get_column($_) } $self->columns;
190 $obj->set_column($col => $val);
192 Sets a column value; if the new value is different to the old the column
193 is marked as dirty for when you next call $obj->update
200 my $old = $self->get_column($column);
201 my $ret = $self->store_column(@_);
202 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
208 my $copy = $orig->set_columns({ $col => $val, ... });
210 Set more than one column value at once.
215 my ($self,$data) = @_;
216 while (my ($col,$val) = each %$data) {
217 $self->set_column($col,$val);
223 my $copy = $orig->copy({ change => $to, ... });
225 Insert a new row with the specified 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 $self->has_column($column);
241 $self->throw( "set_column called for ${column} without value" )
243 return $self->{_column_data}{$column} = $value;
247 my ($class, $cols, $row) = @_;
249 $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
250 my $new = bless({ _column_data => \%vals }, ref $class || $class);
256 my ($self, $changes) = @_;
257 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
258 $new->set_column($_ => $changes->{$_}) for keys %$changes;
262 =item insert_or_update
264 $obj->insert_or_update
266 Updates the object if it's already in the db, else inserts it
270 sub insert_or_update {
272 return ($self->in_storage ? $self->update : $self->insert);
277 my @changed_col_names = $obj->is_changed
282 return keys %{shift->{_dirty_columns} || {}};
291 Matt S. Trout <mst@shadowcatsystems.co.uk>
295 You may distribute this code under the same terms as Perl itself.