1 package DBIx::Class::Row;
8 DBIx::Class::Row - Basic row methods
14 This class is responsible for defining and doing basic operations on rows
15 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 exists $class->_columns->{$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;
108 if (ref $upd eq 'HASH') {
109 $self->$_($upd->{$_}) for keys %$upd;
112 $to_update{$_} = $self->get_column($_) for $self->is_changed;
113 return -1 unless keys %to_update;
114 my $rows = $self->storage->update($self->_table_name, \%to_update,
115 $self->ident_condition);
117 $self->throw( "Can't update ${self}: row not found" );
118 } elsif ($rows > 1) {
119 $self->throw("Can't update ${self}: updated more than one row");
121 $self->{_dirty_columns} = {};
125 sub ident_condition {
128 $cond{$_} = $self->get_column($_) for keys %{$self->_primaries};
136 Deletes the object from the database. The object is still perfectly usable
137 accessor-wise etc. but ->in_storage will now return 0 and the object must
138 be re ->insert'ed before it can be ->update'ed
145 $self->throw( "Not in database" ) unless $self->in_storage;
146 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
147 $self->storage->delete($self->_table_name, $self->ident_condition);
148 $self->in_storage(undef);
149 #$self->store_column($_ => undef) for $self->primary_columns;
150 # Should probably also arrange to trash PK if auto
151 # but if we do, post-delete cascade triggers fail :/
154 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
155 $attrs = { %{ pop(@_) } };
157 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
158 $self->storage->delete($self->_table_name, $query);
165 my $val = $obj->get_column($col);
167 Fetches a column value
172 my ($self, $column) = @_;
173 $self->throw( "Can't fetch data as class method" ) unless ref $self;
174 $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
175 return $self->{_column_data}{$column}
176 if exists $self->{_column_data}{$column};
182 my %data = $obj->get_columns;
184 Fetch all column values at once.
190 return map { $_ => $self->get_column($_) } $self->columns;
195 $obj->set_column($col => $val);
197 Sets a column value; if the new value is different to the old the column
198 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 Set 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 Insert 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 $self->_columns->{$column};
246 $self->throw( "set_column called for ${column} without value" )
248 return $self->{_column_data}{$column} = $value;
252 my ($class, $cols, $row) = @_;
254 $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
255 my $new = bless({ _column_data => \%vals }, ref $class || $class);
261 my ($self, $changes) = @_;
262 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
263 $new->set_column($_ => $changes->{$_}) for keys %$changes;
267 =item insert_or_update
269 $obj->insert_or_update
271 Updates the object if it's already in the db, else inserts it
275 sub insert_or_update {
277 return ($self->in_storage ? $self->update : $self->insert);
282 my @changed_col_names = $obj->is_changed
287 return keys %{shift->{_dirty_columns} || {}};
296 Matt S. Trout <mst@shadowcatsystems.co.uk>
300 You may distribute this code under the same terms as Perl itself.