Removed apparently unnecessary finish statements
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Table.pm
index ed2e86e..d6f65c9 100644 (file)
@@ -15,6 +15,8 @@ __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything
 
 __PACKAGE__->mk_classdata('_cursor_class' => 'DBIx::Class::Cursor');
 
+sub iterator_class { shift->_cursor_class(@_) }
+
 =head1 NAME 
 
 DBIx::Class::Table - Basic table methods
@@ -52,7 +54,6 @@ sub insert {
   my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
                               $self->_table_name, undef);
   $sth->execute(values %{$self->{_column_data}});
-  $sth->finish;
   $self->in_database(1);
   $self->{_dirty_columns} = {};
   return $self;
@@ -79,7 +80,6 @@ sub update {
                               $self->_table_name, $self->_ident_cond);
   my $rows = $sth->execute( (map { $self->{_column_data}{$_} } @to_update),
                   $self->_ident_values );
-  $sth->finish;
   if ($rows == 0) {
     $self->throw( "Can't update $self: row not found" );
   } elsif ($rows > 1) {
@@ -97,7 +97,6 @@ sub delete {
     my $sth = $self->_get_sth('delete', undef,
                                 $self->_table_name, $self->_ident_cond);
     $sth->execute($self->_ident_values);
-    $sth->finish;
     $self->in_database(undef);
   } else {
     my $attrs = { };
@@ -108,7 +107,6 @@ sub delete {
     my ($cond, @param) = $self->_cond_resolve($query, $attrs);
     my $sth = $self->_get_sth('delete', undef, $self->_table_name, $cond);
     $sth->execute(@param);
-    $sth->finish;
   }
   return $self;
 }
@@ -125,8 +123,9 @@ sub get_column {
 sub set_column {
   my $self = shift;
   my ($column) = @_;
+  my $old = $self->get_column($column);
   my $ret = $self->store_column(@_);
-  $self->{_dirty_columns}{$column} = 1;
+  $self->{_dirty_columns}{$column} = 1 unless defined $old && $old eq $ret;
   return $ret;
 }
 
@@ -167,12 +166,36 @@ sub retrieve_from_sql {
   return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond });
 }
 
+sub count_from_sql {
+  my ($class, $cond, @vals) = @_;
+  $cond =~ s/^\s*WHERE//i;
+  my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
+  my @cols = 'COUNT(*)';
+  my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
+  #warn "$cond @vals";
+  $sth->execute(@vals);
+  my ($count) = $sth->fetchrow_array;
+  return $count;
+}
+
+sub count {
+  my $class = shift;
+  my $attrs = { };
+  if (@_ > 1 && ref $_[$#_] eq 'HASH') {
+    $attrs = { %{ pop(@_) } };
+  }
+  my $query    = ref $_[0] eq "HASH" ? shift: {@_};
+  my ($cond, @param)  = $class->_cond_resolve($query, $attrs);
+  return $class->count_from_sql($cond, @param, $attrs);
+}
+
 sub sth_to_objects {
   my ($class, $sth, $args, $cols, $attrs) = @_;
   my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
+  my @args = map { ref $_ ? ''.$_ : $_ } @$args; # Stringify objects
   my $cursor_class = $class->_cursor_class;
   eval "use $cursor_class;";
-  my $cursor = $cursor_class->new($class, $sth, $args, \@cols, $attrs);
+  my $cursor = $cursor_class->new($class, $sth, \@args, \@cols, $attrs);
   return (wantarray ? $cursor->all : $cursor);
 }
 
@@ -238,11 +261,20 @@ sub find_or_create {
   return defined($exists) ? $exists : $class->create($hash);
 }
 
+sub insert_or_update {
+  my $self = shift;
+  return ($self->in_database ? $self->update : $self->insert);
+}
+
 sub retrieve_all {
   my ($class) = @_;
   return $class->retrieve_from_sql( '1' );
 }
 
+sub is_changed {
+  return keys %{shift->{_dirty_columns} || {}};
+}
+
 1;
 
 =back