lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm

71 lines
1.9 KiB
Perl
Raw Normal View History

2017-01-06 13:30:41 +01:00
package Lemonldap::NG::Common::Conf::Backends::RDBI;
2009-12-15 17:31:13 +01:00
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;
2017-01-06 13:30:41 +01:00
use Lemonldap::NG::Common::Conf::Backends::_DBI;
2009-12-15 17:31:13 +01:00
2019-02-12 18:21:38 +01:00
our $VERSION = '2.1.0';
2017-01-06 13:30:41 +01:00
our @ISA = qw(Lemonldap::NG::Common::Conf::Backends::_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);
my $req;
my $lastCfg = $self->lastCfg;
$req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)");
2021-06-29 19:30:44 +02:00
_delete($self,$cfgNum) if $lastCfg == $cfgNum;
unless ($req) {
$self->logError;
return UNKNOWN_ERROR;
}
2009-12-15 17:31:13 +01:00
while ( my ( $k, $v ) = each %$fields ) {
my @execValues = ( $cfgNum, $k, $v );
2017-08-29 16:04:40 +02:00
my $execute;
eval { $execute = $req->execute(@execValues); };
print STDERR $@ if $@;
2017-08-29 16:04:40 +02:00
unless ($execute) {
2009-12-15 17:31:13 +01:00
$self->logError;
2021-06-29 19:30:44 +02:00
_delete( $self, $cfgNum ) if $lastCfg != $cfgNum;
$self->_dbh->do("ROLLBACK");
return UNKNOWN_ERROR;
2009-12-15 17:31:13 +01:00
}
}
2016-01-09 21:35:47 +01:00
return $cfgNum;
2009-12-15 17:31:13 +01:00
}
sub load {
my ( $self, $cfgNum, $fields ) = @_;
$fields = $fields ? join( ",", @$fields ) : '*';
my $sth =
2018-06-21 21:35:16 +02:00
$self->_dbh->prepare(
"SELECT field,value from " . $self->{dbiTable} . " WHERE cfgNum=?" )
or $self->logError;
$sth->execute($cfgNum) or $self->logError;
2009-12-15 17:31:13 +01:00
my ( $res, @row );
while ( @row = $sth->fetchrow_array ) {
2018-04-23 17:17:34 +02:00
$res->{ $row[0] } = $row[1];
2009-12-15 17:31:13 +01:00
}
2010-03-01 21:32:28 +01:00
unless ($res) {
$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
}
2021-06-29 19:30:44 +02:00
sub _delete {
my ( $self, $cfgNum ) = @_;
my $r =
$self->_dbh->prepare("DELETE FROM $self->{dbiTable} where cfgNum=?");
$r->execute($cfgNum);
}
2009-12-15 17:31:13 +01:00
1;