Class-DBI-MockDBD
view release on metacpan or search on metacpan
lib/Class/DBI/MockDBD.pm view on Meta::CPAN
my $meth = "sql_$sth";
$sth = $class->$meth();
}
my $rows = [];
eval { $sth->execute(@$args) unless $sth->{Active};
# set last statement handle to check state in other methods later
$class->mocked_statement_handle($sth);
# set mock::dbd info
$class->mocked_params($sth->{mock_params});
$class->mocked_statement($sth->{mock_statement});
while (my $data = $sth->fetchrow_hashref()) {
push (@$rows, $data);
}
};
return $class->_croak("Class::DBI::MockDBD -- $class can't $sth->{Statement}: $@", err => $@)
if $@;
return $class->_ids_to_objects($rows);
}
sub _insert_row {
my $self = shift;
my $class = ref($self);
my $data = shift;
eval {
my @columns = keys %$data;
my $sth = $self->sql_MakeNewObj(
join(', ', @columns),
join(', ', map $self->_column_placeholder($_), @columns),
);
$self->_bind_param($sth, \@columns);
$sth->execute(values %$data);
# set last statement handle to check state in other methods later
$class->mocked_statement_handle($sth);
# set mock::dbd info
$class->mocked_params($sth->{mock_params});
$class->mocked_statement($sth->{mock_statement});
my @primary_columns = $self->primary_columns;
$data->{ $primary_columns[0] } = $self->_auto_increment_value
if @primary_columns == 1
&& !defined $data->{ $primary_columns[0] };
};
if ($@) {
my $class = ref $self;
return $self->_db_error(
msg => "Can't insert new $class: $@",
err => $@,
method => 'insert'
);
}
return 1;
}
sub update {
my $self = shift;
my $class = ref($self)
or return $self->_croak("Class::DBI::MockDBD -- Can't call update as a class method");
$self->call_trigger('before_update');
return -1 unless my @changed_cols = $self->is_changed;
$self->call_trigger('deflate_for_update');
my @primary_columns = $self->primary_columns;
my $sth = $self->sql_update($self->_update_line);
# set last statement handle to check state in other methods later
$class->mocked_statement_handle($sth);
$class->_bind_param($sth, \@changed_cols);
my $rows = eval { $sth->execute($self->_update_vals, $self->id); };
# set mock::dbd info
$class->mocked_params($sth->{mock_params});
$class->mocked_statement($sth->{mock_statement});
if ($@) {
return $self->_db_error(
msg => "Can't update $self: $@",
err => $@,
method => 'update'
);
}
# enable this once new fixed DBD::SQLite is released:
if (0 and $rows != 1) { # should always only update one row
$self->_croak("Can't update $self: row not found") if $rows == 0;
$self->_croak("Can't update $self: updated more than one row");
}
$self->call_trigger('after_update', discard_columns => \@changed_cols);
# delete columns that changed (in case adding to DB modifies them again)
$self->_attribute_delete(@changed_cols);
delete $self->{__Changed};
return 1;
}
##############################################################################
1;
__END__
=head1 BUGS AND CAVEATS
* rv return value from execute is not correct (DBD::Mock issue)
=head1 SEE ALSO
* Class::DBI
* Mock::DBD
( run in 0.581 second using v1.01-cache-2.11-cpan-13bb782fe5a )