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 if (ref $upd eq 'HASH') {
108 $self->$_($upd->{$_}) for keys %$upd;
111 $to_update{$_} = $self->get_column($_) for $self->is_changed;
112 return -1 unless keys %to_update;
113 my $rows = $self->storage->update($self->_table_name, \%to_update,
114 $self->ident_condition);
116 $self->throw( "Can't update ${self}: row not found" );
117 } elsif ($rows > 1) {
118 $self->throw("Can't update ${self}: updated more than one row");
120 $self->{_dirty_columns} = {};
124 sub ident_condition {
127 $cond{$_} = $self->get_column($_) for keys %{$self->_primaries};
135 Deletes the object from the database. The object is still perfectly usable
136 accessor-wise etc. but ->in_storage will now return 0 and the object must
137 be re ->insert'ed before it can be ->update'ed
144 $self->throw( "Not in database" ) unless $self->in_storage;
145 #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
146 $self->storage->delete($self->_table_name, $self->ident_condition);
147 $self->in_storage(undef);
148 #$self->store_column($_ => undef) for $self->primary_columns;
149 # Should probably also arrange to trash PK if auto
150 # but if we do, post-delete cascade triggers fail :/
153 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
154 $attrs = { %{ pop(@_) } };
156 my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
157 $self->storage->delete($self->_table_name, $query);
164 my $val = $obj->get_column($col);
166 Fetches a column value
171 my ($self, $column) = @_;
172 $self->throw( "Can't fetch data as class method" ) unless ref $self;
173 $self->throw( "No such column '${column}'" ) unless $self->_columns->{$column};
174 return $self->{_column_data}{$column}
175 if exists $self->{_column_data}{$column};
181 $obj->set_column($col => $val);
183 Sets a column value; if the new value is different to the old the column
184 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 unless defined $old && $old eq $ret;
199 $obj->store_column($col => $val);
201 Sets a column value without marking it as dirty
206 my ($self, $column, $value) = @_;
207 $self->throw( "No such column '${column}'" )
208 unless $self->_columns->{$column};
209 $self->throw( "set_column called for ${column} without value" )
211 return $self->{_column_data}{$column} = $value;
215 my ($class, $cols, $row) = @_;
217 $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
218 my $new = $class->new(\%vals);
225 my $copy = $orig->copy({ change => $to, ... });
230 my ($self, $changes) = @_;
231 my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
232 $new->set_column($_ => $changes->{$_}) for keys %$changes;
236 =item insert_or_update
238 $obj->insert_or_update
240 Updates the object if it's already in the db, else inserts it
244 sub insert_or_update {
246 return ($self->in_storage ? $self->update : $self->insert);
251 my @changed_col_names = $obj->is_changed
256 return keys %{shift->{_dirty_columns} || {}};
265 Matt S. Trout <mst@shadowcatsystems.co.uk>
269 You may distribute this code under the same terms as Perl itself.