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;
$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) {
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 = { };
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;
}
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;
}
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}} );
return $class->retrieve_from_sql( '1' );
}
+sub is_changed {
+ return keys %{shift->{_dirty_columns} || {}};
+}
+
1;
=back