Manage UPDATE/INSERT in RDBI/CDBI configuration backends to be compatible with CLI (#694)

This commit is contained in:
Clément Oudot 2014-06-03 16:00:20 +00:00
parent f6cad5438c
commit 2533539072
2 changed files with 35 additions and 10 deletions

View File

@ -4,20 +4,30 @@ use strict;
require Storable;
use Lemonldap::NG::Common::Conf::_DBI;
our $VERSION = '1.1.0';
our $VERSION = '1.4.0';
our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI);
sub store {
my ( $self, $fields ) = @_;
my $cfgNum = $fields->{cfgNum};
my $req;
my $lastCfg = $self->lastCfg;
$fields = Storable::nfreeze($fields);
my $tmp = $self->_dbh->prepare(
"insert into $self->{dbiTable} (cfgNum,data) values (?,?)");
unless ($tmp) {
if ( $lastCfg == $cfgNum ) {
$req = $self->_dbh->prepare(
"UPDATE $self->{dbiTable} SET data=? WHERE cfgNum=?" );
}
else {
$req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (data,cfgNum) VALUES (?,?)" );
}
unless ($req) {
$self->logError;
return UNKNOWN_ERROR;
}
unless ( $tmp->execute( $cfgNum, $fields ) ) {
unless ( $req->execute( $fields, $cfgNum ) ) {
$self->logError;
return UNKNOWN_ERROR;
}

View File

@ -12,17 +12,32 @@ sub store {
$self->{noQuotes} = 1;
$fields = $self->serialize($fields);
my $req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)"
);
my $req;
my $lastCfg = $self->lastCfg;
if ( $lastCfg == $fields->{cfgNum} ) {
$req = $self->_dbh->prepare(
"UPDATE $self->{dbiTable} SET field=?, value=? WHERE cfgNum=? AND field=?"
);
}
else {
$req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)"
);
}
unless ($req) {
$self->logError;
return UNKNOWN_ERROR;
}
$self->_dbh->{AutoCommit} = 0;
while ( my ( $k, $v ) = each %$fields ) {
unless ( $req->execute( $fields->{cfgNum}, $k, $v ) ) {
my @execValues;
if ( $lastCfg == $fields->{cfgNum} ) {
@execValues = ( $k, $v, $fields->{cfgNum}, $k );
}
else { @execValues = ( $fields->{cfgNum}, $k, $v ); }
unless ( $req->execute(@execValues) ) {
$self->logError;
$self->_dbh->do("ROLLBACK");
$self->_dbh->{AutoCommit} = 1;