2009-12-15 17:31:13 +01:00
|
|
|
package Lemonldap::NG::Common::Conf::RDBI;
|
|
|
|
|
|
|
|
use strict;
|
2016-01-07 13:34:34 +01:00
|
|
|
use utf8;
|
2009-12-15 17:31:13 +01:00
|
|
|
use Lemonldap::NG::Common::Conf::Serializer;
|
2010-09-17 11:26:03 +02:00
|
|
|
use Lemonldap::NG::Common::Conf::_DBI;
|
2009-12-15 17:31:13 +01:00
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
our $VERSION = '2.0.0';
|
2010-09-17 11:26:03 +02:00
|
|
|
our @ISA = qw(Lemonldap::NG::Common::Conf::_DBI);
|
2009-12-15 17:31:13 +01:00
|
|
|
|
|
|
|
sub store {
|
|
|
|
my ( $self, $fields ) = @_;
|
2016-01-09 21:35:47 +01:00
|
|
|
my $cfgNum = $fields->{cfgNum};
|
2009-12-15 17:31:13 +01:00
|
|
|
$self->{noQuotes} = 1;
|
|
|
|
$fields = $self->serialize($fields);
|
2014-04-04 17:15:14 +02:00
|
|
|
|
2014-06-03 18:00:20 +02:00
|
|
|
my $req;
|
|
|
|
my $lastCfg = $self->lastCfg;
|
|
|
|
|
2016-01-09 21:35:47 +01:00
|
|
|
$self->_dbh->begin_work;
|
|
|
|
eval { $self->_dbh->do("BEGIN") };
|
|
|
|
if ( $lastCfg == $cfgNum ) {
|
2014-06-03 18:00:20 +02:00
|
|
|
$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 (?,?,?)"
|
|
|
|
);
|
|
|
|
}
|
2014-04-04 17:15:14 +02:00
|
|
|
unless ($req) {
|
|
|
|
$self->logError;
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
2009-12-15 17:31:13 +01:00
|
|
|
while ( my ( $k, $v ) = each %$fields ) {
|
2014-06-03 18:00:20 +02:00
|
|
|
my @execValues;
|
2016-01-09 21:35:47 +01:00
|
|
|
if ( $lastCfg == $cfgNum ) {
|
|
|
|
@execValues = ( $k, $v, $cfgNum, $k );
|
2014-06-03 18:00:20 +02:00
|
|
|
}
|
2016-01-09 21:35:47 +01:00
|
|
|
else { @execValues = ( $cfgNum, $k, $v ); }
|
2014-06-03 18:00:20 +02:00
|
|
|
unless ( $req->execute(@execValues) ) {
|
2009-12-15 17:31:13 +01:00
|
|
|
$self->logError;
|
2014-04-04 17:15:14 +02:00
|
|
|
$self->_dbh->do("ROLLBACK");
|
|
|
|
return UNKNOWN_ERROR;
|
2009-12-15 17:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 21:35:47 +01:00
|
|
|
eval { $self->_dbh->do("COMMIT") };
|
|
|
|
return $cfgNum;
|
2009-12-15 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub load {
|
|
|
|
my ( $self, $cfgNum, $fields ) = @_;
|
|
|
|
$fields = $fields ? join( ",", @$fields ) : '*';
|
|
|
|
my $sth =
|
2016-01-22 17:53:41 +01:00
|
|
|
$self->_dbh->prepare( "SELECT cfgNum,field,value from "
|
2009-12-15 17:31:13 +01:00
|
|
|
. $self->{dbiTable}
|
2013-07-12 09:54:35 +02:00
|
|
|
. " WHERE cfgNum=?" );
|
|
|
|
$sth->execute($cfgNum);
|
2009-12-15 17:31:13 +01:00
|
|
|
my ( $res, @row );
|
|
|
|
while ( @row = $sth->fetchrow_array ) {
|
|
|
|
$res->{ $row[1] } = $row[2];
|
|
|
|
}
|
2010-03-01 21:32:28 +01:00
|
|
|
unless ($res) {
|
2011-07-07 17:08:45 +02:00
|
|
|
$Lemonldap::NG::Common::Conf::msg .=
|
|
|
|
"No configuration $cfgNum found \n";
|
2009-12-15 17:31:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$res->{cfgNum} = $cfgNum;
|
2016-02-08 06:53:18 +01:00
|
|
|
return $self->unserialize($res);
|
2009-12-15 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|