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 $new->store_column($k => $v);
46 Inserts an object into the database if it isn't already in there. Returns
53 return $self if $self->in_storage;
54 #use Data::Dumper; warn Dumper($self);
56 $in{$_} = $self->get_column($_)
57 for grep { defined $self->get_column($_) } $self->columns;
58 my %out = %{ $self->storage->insert($self->_table_name, \%in) };
59 $self->store_column($_, $out{$_})
60 for grep { $self->get_column($_) ne $out{$_} } keys %out;
62 $self->{_dirty_columns} = {};
68 $obj->in_storage; # Get value
69 $obj->in_storage(1); # Set value
71 Indicated whether the object exists as a row in the database or not
76 my ($self, $val) = @_;
77 $self->{_in_storage} = $val if @_ > 1;
78 return $self->{_in_storage};
83 my $new = My::Class->create($attrs);
85 A shortcut for My::Class->new($attrs)->insert;
90 my ($class, $attrs) = @_;
91 $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
92 return $class->new($attrs)->insert;
99 Must be run on an object that is already in the database; issues an SQL
100 UPDATE query to commit any changes to the object to the db if required.
105 my ($self, $upd) = @_;
106 $self->throw( "Not in database" ) unless $self->in_storage;
107 my %to_update = %{$upd || {}};
108 $to_update{$_} = $self->get_column($_) for $self->is_changed;
109 return -1 unless keys %to_update;
110 my $rows = $self->storage->update($self->_table_name, \%to_update,
111 $self->ident_condition);
113 $self->throw( "Can't update ${self}: row not found" );
114 } elsif ($rows > 1) {
115 $self->throw("Can't update ${self}: updated more than one row");
117 $self->{_dirty_columns} = {};
121 sub ident_condition {
124 $cond{$_} = $self->get_column($_) for keys %{$self->_primaries};
132 Deletes the object from the database. The object is still perfectly usable
133 accessor-wise etc. but ->in_storage will now return 0 and the object must
134 be re ->insert'ed before it can be ->update'ed
141 $self->throw( "Not in database" ) unless $self->in_storage;
142 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
143 $self->storage->delete($self->_table_name, $self->ident_condition);
144 $self->in_storage(undef);
145 #$self->store_column($_ => undef) for $self->primary_columns;
146 # Should probably also arrange to trash PK if auto
147 # but if we do, post-delete cascade triggers fail :/
150 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
151 $attrs = { %{ pop(@_) } };
153 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
154 $self->storage->delete($self->_table_name, $query);
161 my $val = $obj->get_column($col);
163 Fetches a column value
168 my ($self, $column) = @_;
169 $self->throw( "Can't fetch data as class method" ) unless ref $self;
170 $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
171 return $self->{_column_data}{$column}
172 if exists $self->{_column_data}{$column};
178 $obj->set_column($col => $val);
180 Sets a column value; if the new value is different to the old the column
181 is marked as dirty for when you next call $obj->update
188 my $old = $self->get_column($column);
189 my $ret = $self->store_column(@_);
190 $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
196 $obj->store_column($col => $val);
198 Sets a column value without marking it as dirty
203 my ($self, $column, $value) = @_;
204 $self->throw( "No such column '${column}'" )
205 unless $self->_columns->{$column};
206 $self->throw( "set_column called for ${column} without value" )
208 return $self->{_column_data}{$column} = $value;
212 my ($class, $cols, $row) = @_;
214 $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
215 my $new = $class->new(\%vals);
222 my $copy = $orig->copy({ change => $to, ... });
227 my ($self, $changes) = @_;
228 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
229 $new->set_column($_ => $changes->{$_}) for keys %$changes;
233 =item insert_or_update
235 $obj->insert_or_update
237 Updates the object if it's already in the db, else inserts it
241 sub insert_or_update {
243 return ($self->in_storage ? $self->update : $self->insert);
248 my @changed_col_names = $obj->is_changed
253 return keys %{shift->{_dirty_columns} || {}};
262 Matt S. Trout <mst@shadowcatsystems.co.uk>
266 You may distribute this code under the same terms as Perl itself.