register_resultset, Cursor fixes
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Row.pm
index 2354a7a..a03a3d7 100644 (file)
@@ -5,6 +5,10 @@ use warnings;
 
 use base qw/DBIx::Class/;
 
+__PACKAGE__->load_components(qw/AccessorGroup/);
+
+__PACKAGE__->mk_group_accessors('simple' => 'result_source');
+
 =head1 NAME 
 
 DBIx::Class::Row - Basic row methods
@@ -45,20 +49,20 @@ sub new {
   $obj->insert;
 
 Inserts an object into the database if it isn't already in there. Returns
-the object itself.
+the object itself. Requires the object's result source to be set, or the
+class to have a result_source_instance method.
 
 =cut
 
 sub insert {
   my ($self) = @_;
   return $self if $self->in_storage;
+  $self->{result_source} ||= $self->result_source_instance
+    if $self->can('result_source_instance');
+  my $source = $self->{result_source};
+  die "No result_source set on this object; can't insert" unless $source;
   #use Data::Dumper; warn Dumper($self);
-  my %in;
-  $in{$_} = $self->get_column($_)
-    for grep { defined $self->get_column($_) } $self->columns;
-  my %out = %{ $self->storage->insert($self->_table_name, \%in) };
-  $self->store_column($_, $out{$_})
-    for grep { $self->get_column($_) ne $out{$_} } keys %out;
+  $source->storage->insert($source->from, { $self->get_columns });
   $self->in_storage(1);
   $self->{_dirty_columns} = {};
   return $self;
@@ -79,20 +83,6 @@ sub in_storage {
   return $self->{_in_storage};
 }
 
-=head2 create
-
-  my $new = My::Class->create($attrs);
-
-A shortcut for My::Class->new($attrs)->insert;
-
-=cut
-
-sub create {
-  my ($class, $attrs) = @_;
-  $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH';
-  return $class->new($attrs)->insert;
-}
-
 =head2 update
 
   $obj->update;
@@ -105,14 +95,10 @@ UPDATE query to commit any changes to the object to the db if required.
 sub update {
   my ($self, $upd) = @_;
   $self->throw( "Not in database" ) unless $self->in_storage;
-  if (ref $upd eq 'HASH') {
-    $self->$_($upd->{$_}) for keys %$upd;
-  }
-  my %to_update;
-  $to_update{$_} = $self->get_column($_) for $self->is_changed;
-  return -1 unless keys %to_update;
-  my $rows = $self->storage->update($self->_table_name, \%to_update,
-                                      $self->ident_condition);
+  my %to_update = $self->get_dirty_columns;
+  return $self unless keys %to_update;
+  my $rows = $self->result_source->storage->update(
+               $self->result_source->from, \%to_update, $self->ident_condition);
   if ($rows == 0) {
     $self->throw( "Can't update ${self}: row not found" );
   } elsif ($rows > 1) {
@@ -136,19 +122,18 @@ sub delete {
   my $self = shift;
   if (ref $self) {
     $self->throw( "Not in database" ) unless $self->in_storage;
-    #warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
-    $self->storage->delete($self->_table_name, $self->ident_condition);
+    $self->result_source->storage->delete(
+      $self->result_source->from, $self->ident_condition);
     $self->in_storage(undef);
-    #$self->store_column($_ => undef) for $self->primary_columns;
-      # Should probably also arrange to trash PK if auto
-      # but if we do, post-delete cascade triggers fail :/
   } else {
+    die "Can't do class delete without a ResultSource instance"
+      unless $self->can('result_source_instance');
     my $attrs = { };
     if (@_ > 1 && ref $_[$#_] eq 'HASH') {
       $attrs = { %{ pop(@_) } };
     }
     my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_});
-    $self->storage->delete($self->_table_name, $query);
+    $self->result_source_instance->resultset->search(@_)->delete;
   }
   return $self;
 }
@@ -166,9 +151,9 @@ the database and stored in the object.
 sub get_column {
   my ($self, $column) = @_;
   $self->throw( "Can't fetch data as class method" ) unless ref $self;
-  $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
   return $self->{_column_data}{$column}
     if exists $self->{_column_data}{$column};
+  $self->throw( "No such column '${column}'" ) unless $self->has_column($column);
   return undef;
 }
 
@@ -182,7 +167,21 @@ Does C<get_column>, for all column values at once.
 
 sub get_columns {
   my $self = shift;
-  return map { $_ => $self->get_column($_) } $self->columns;
+  return %{$self->{_column_data}};
+}
+
+=head2 get_dirty_columns
+
+  my %data = $obj->get_dirty_columns;
+
+Identical to get_columns but only returns those that have been changed.
+
+=cut
+
+sub get_dirty_columns {
+  my $self = shift;
+  return map { $_ => $self->{_column_data}{$_} }
+           keys %{$self->{_dirty_columns}};
 }
 
 =head2 set_column
@@ -199,7 +198,8 @@ sub set_column {
   my ($column) = @_;
   my $old = $self->get_column($column);
   my $ret = $self->store_column(@_);
-  $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
+  $self->{_dirty_columns}{$column} = 1
+    if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
   return $ret;
 }
 
@@ -216,6 +216,7 @@ sub set_columns {
   while (my ($col,$val) = each %$data) {
     $self->set_column($col,$val);
   }
+  return $self;
 }
 
 =head2 copy
@@ -226,6 +227,13 @@ Inserts a new row with the specified changes.
 
 =cut
 
+sub copy {
+  my ($self, $changes) = @_;
+  my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
+  $new->set_column($_ => $changes->{$_}) for keys %$changes;
+  return $new->insert;
+}
+
 =head2 store_column
 
   $obj->store_column($col => $val);
@@ -237,26 +245,52 @@ Sets a column value without marking it as dirty.
 sub store_column {
   my ($self, $column, $value) = @_;
   $self->throw( "No such column '${column}'" ) 
-    unless $self->has_column($column);
+    unless exists $self->{_column_data}{$column} || $self->has_column($column);
   $self->throw( "set_column called for ${column} without value" ) 
     if @_ < 3;
   return $self->{_column_data}{$column} = $value;
 }
 
-sub _row_to_object {
-  my ($class, $cols, $row) = @_;
-  my %vals;
-  $vals{$cols->[$_]} = $row->[$_] for 0 .. $#$cols;
-  my $new = bless({ _column_data => \%vals }, ref $class || $class);
-  $new->in_storage(1);
-  return $new;
-}
+=head2 inflate_result
 
-sub copy {
-  my ($self, $changes) = @_;
-  my $new = bless({ _column_data => { %{$self->{_column_data}}} }, ref $self);
-  $new->set_column($_ => $changes->{$_}) for keys %$changes;
-  return $new->insert;
+  Class->inflate_result($result_source, \%me, \%prefetch?)
+
+Called by ResultSet to inflate a result from storage
+
+=cut
+
+sub inflate_result {
+  my ($class, $source, $me, $prefetch) = @_;
+  #use Data::Dumper; print Dumper(@_);
+  my $new = bless({ result_source => $source,
+                    _column_data => $me,
+                    _in_storage => 1
+                  },
+                  ref $class || $class);
+  my $schema;
+  PRE: foreach my $pre (keys %{$prefetch||{}}) {
+    my $pre_source = $source->related_source($pre);
+    die "Can't prefetch non-existant relationship ${pre}" unless $pre_source;
+    my $fetched = $pre_source->result_class->inflate_result(
+                    $pre_source, @{$prefetch->{$pre}});
+    my $accessor = $source->relationship_info($pre)->{attrs}{accessor};
+    $class->throw("No accessor for prefetched $pre")
+      unless defined $accessor;
+    PRIMARY: foreach my $pri ($pre_source->primary_columns) {
+      unless (defined $fetched->get_column($pri)) {
+        undef $fetched;
+        last PRIMARY;
+      }
+    }
+    if ($accessor eq 'single') {
+      $new->{_relationship_data}{$pre} = $fetched;
+    } elsif ($accessor eq 'filter') {
+      $new->{_inflated_column}{$pre} = $fetched;
+    } else {
+      $class->throw("Don't know how to store prefetched $pre");
+    }
+  }
+  return $new;
 }
 
 =head2 insert_or_update
@@ -282,6 +316,21 @@ sub is_changed {
   return keys %{shift->{_dirty_columns} || {}};
 }
 
+=head2 result_source
+
+  Accessor to the ResultSource this object was created from
+
+=head2 register_column($column, $column_info)
+
+  Registers a column on the class and creates an accessor for it
+
+=cut
+
+sub register_column {
+  my ($class, $col, $info) = @_;
+  $class->mk_group_accessors('column' => $col);
+}
+
 1;
 
 =head1 AUTHORS