Re: inserted value != actual value in database
[prev]
[thread]
[next]
[Date index for 2005/02/11]
On Thu, 10 Feb 2005 07:57:14 +0000, Tony Bowden <tony-cdbitalk@xxxxx.xxx> wrote:
> On Wed, Feb 09, 2005 at 12:48:14PM -0800, Emile Aben wrote:
> > I have a problem with inserting data via CDBI with a mysql (3.23.38)
> > backend in that I currently have no clue if a value I insert is the
> > same as the value that is actually in the database. Say, I hit the
> > maximum of an INT value, or the maximum length for a VARCHAR, mysql
> > just inserts a value that is different from the value I asked it to
> > insert. What I'd like to have is at least some kind of warning if one
> > or more values I've inserted 'overflows' the column data-type.
>
> After an insert Class::DBI flushes out all the data from the object for
> this exact reason. You can just fetch it back again and compare it to
> see if it's the same.
>
> my $obj = Class->create({ value => $val });
> warn "Value changed" unless $obj->value == $val
Thanks for the advice, because I want to check all values that are
inserted I've created these 'create' and 'update' functions in my base
class:
sub create {
my ($class,$vals_hr) = @_;
my @cols = grep { $_->name } $class->columns;
my %pk = map { $_ => 1 } $class->columns('Primary');
@cols = grep { ! $pk{$_} } @cols;
my @before = map { $vals_hr->{$_} } @cols;
my $obj = $class->SUPER::create($vals_hr);
my @after = map { $obj->get($_) } @cols ;
no warnings qw(uninitialized);
for (my $i=0; $i < scalar(@cols) ; $i++) {
if ( $before[$i] ne $after[$i] ) {
# errorhandling here
});
}
}
use warnings;
return $obj;
}
sub update {
my ($obj) = @_;
no warnings qw(uninitialized);
my @cols = grep { $_->name } $obj->columns;
my @before = $obj->_attrs(@cols);
my $rv = $obj->SUPER::update();
my @after = map { $obj->get($_) } @cols;
for (my $i=0; $i < scalar(@cols) ; $i++) {
if ( $before[$i] ne $after[$i] ) {
# errorhandling here
});
}
}
use warnings;
return $rv;
}