2017-01-06 13:30:41 +01:00
|
|
|
package Lemonldap::NG::Common::Conf::Backends::_DBI;
|
2010-09-17 11:26:03 +02:00
|
|
|
|
|
|
|
use strict;
|
2016-01-07 13:34:34 +01:00
|
|
|
use utf8;
|
2010-09-17 11:26:03 +02:00
|
|
|
use DBI;
|
|
|
|
use Lemonldap::NG::Common::Conf::Constants; #inherits
|
|
|
|
|
2021-06-28 15:36:29 +02:00
|
|
|
our $VERSION = '2.0.12';
|
2010-09-17 11:26:03 +02:00
|
|
|
our @ISA = qw(Lemonldap::NG::Common::Conf::Constants);
|
|
|
|
our ( @EXPORT, %EXPORT_TAGS );
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
*Lemonldap::NG::Common::Conf::_dbh = \&_dbh;
|
2014-11-05 09:54:16 +01:00
|
|
|
push @EXPORT, @Lemonldap::NG::Common::Conf::Constants::EXPORT;
|
|
|
|
%EXPORT_TAGS = %Lemonldap::NG::Common::Conf::Constants::EXPORT_TAGS;
|
2010-09-17 11:26:03 +02:00
|
|
|
push @EXPORT,
|
|
|
|
qw(prereq available lastCfg _dbh lock isLocked unlock delete logError);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub prereq {
|
|
|
|
my $self = shift;
|
|
|
|
unless ( $self->{dbiChain} ) {
|
|
|
|
$Lemonldap::NG::Common::Conf::msg =
|
|
|
|
'"dbiChain" is required in *DBI configuration type';
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-22 17:53:41 +01:00
|
|
|
$Lemonldap::NG::Common::Conf::msg .=
|
|
|
|
'Warning: "dbiUser" parameter is not set'
|
2010-09-17 11:26:03 +02:00
|
|
|
unless ( $self->{dbiUser} );
|
|
|
|
$self->{dbiTable} ||= "lmConfig";
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub available {
|
|
|
|
my $self = shift;
|
|
|
|
my $sth =
|
|
|
|
$self->_dbh->prepare( "SELECT DISTINCT cfgNum from "
|
|
|
|
. $self->{dbiTable}
|
2022-02-16 17:43:29 +01:00
|
|
|
. " order by cfgNum" )
|
|
|
|
or $self->logError;
|
2021-06-25 19:41:36 +02:00
|
|
|
$sth->execute() or $self->logError;
|
2010-09-17 11:26:03 +02:00
|
|
|
my @conf;
|
|
|
|
while ( my @row = $sth->fetchrow_array ) {
|
|
|
|
push @conf, $row[0];
|
|
|
|
}
|
|
|
|
return @conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lastCfg {
|
|
|
|
my $self = shift;
|
|
|
|
my @row = $self->_dbh->selectrow_array(
|
|
|
|
"SELECT max(cfgNum) from " . $self->{dbiTable} );
|
|
|
|
return $row[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _dbh {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{dbiTable} ||= "lmConfig";
|
|
|
|
return $self->{_dbh} if ( $self->{_dbh} and $self->{_dbh}->ping );
|
2017-07-11 14:16:56 +02:00
|
|
|
$self->{_dbh} = DBI->connect_cached( $self->{dbiChain}, $self->{dbiUser},
|
2014-04-04 17:15:14 +02:00
|
|
|
$self->{dbiPassword}, { RaiseError => 1, AutoCommit => 1, } );
|
2017-07-11 14:16:56 +02:00
|
|
|
if ( $self->{dbiChain} =~ /^dbi:sqlite/i ) {
|
|
|
|
$self->{_dbh}->{sqlite_unicode} = 1;
|
|
|
|
}
|
|
|
|
elsif ( $self->{dbiChain} =~ /^dbi:mysql/i ) {
|
2018-05-16 15:03:39 +02:00
|
|
|
eval {
|
|
|
|
$self->{_dbh}->{mysql_enable_utf8} = 1;
|
|
|
|
$self->{_dbh}->do("set names 'utf8'");
|
|
|
|
};
|
2017-07-11 14:16:56 +02:00
|
|
|
}
|
|
|
|
elsif ( $self->{dbiChain} =~ /^dbi:pg/i ) {
|
|
|
|
$self->{_dbh}->{pg_enable_utf8} = 1;
|
|
|
|
}
|
2018-08-23 07:01:22 +02:00
|
|
|
|
|
|
|
# DBD::MariaDB should have o problem here
|
2017-07-11 14:16:56 +02:00
|
|
|
return $self->{_dbh};
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub lock {
|
|
|
|
my $self = shift;
|
2018-08-23 07:01:22 +02:00
|
|
|
if ( $self->{dbiChain} =~ /^dbi:(?:MariaDB|mysql):/i ) {
|
2014-04-04 16:20:43 +02:00
|
|
|
my @row = $self->_dbh->selectrow_array("SELECT GET_LOCK('lmconf', 0)");
|
|
|
|
return $row[0] || 0;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
2014-04-04 16:20:43 +02:00
|
|
|
return 1;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub isLocked {
|
|
|
|
my $self = shift;
|
2018-08-23 07:01:22 +02:00
|
|
|
if ( $self->{dbiChain} =~ /^dbi:(?:MariaDB|mysql):/i ) {
|
2014-04-04 16:20:43 +02:00
|
|
|
my @row = $self->_dbh->selectrow_array("SELECT IS_FREE_LOCK('lmconf')");
|
|
|
|
return $row[0] ? 0 : 1;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
2014-04-04 16:20:43 +02:00
|
|
|
return 0;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unlock {
|
|
|
|
my $self = shift;
|
2018-08-23 07:01:22 +02:00
|
|
|
if ( $self->{dbiChain} =~ /^dbi:(?:MariaDB|mysql):/i ) {
|
2014-04-04 16:20:43 +02:00
|
|
|
my @row = $self->_dbh->selectrow_array("SELECT RELEASE_LOCK('lmconf')");
|
|
|
|
return $row[0] || 0;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
2014-04-04 16:20:43 +02:00
|
|
|
return 1;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub delete {
|
|
|
|
my ( $self, $cfgNum ) = @_;
|
2015-04-05 20:07:44 +02:00
|
|
|
my $req =
|
2022-02-16 17:43:29 +01:00
|
|
|
$self->_dbh->prepare("DELETE FROM $self->{dbiTable} WHERE cfgNum=?")
|
|
|
|
or $self->logError;
|
2021-06-25 19:41:36 +02:00
|
|
|
my $res = $req->execute($cfgNum) or $self->logError;
|
2015-04-05 20:07:44 +02:00
|
|
|
$Lemonldap::NG::Common::Conf::msg .=
|
|
|
|
"Unable to find conf $cfgNum (" . $self->_dbh->errstr . ")"
|
|
|
|
unless ($res);
|
|
|
|
return $res;
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub logError {
|
|
|
|
my $self = shift;
|
|
|
|
$Lemonldap::NG::Common::Conf::msg .=
|
2021-06-25 08:04:49 +02:00
|
|
|
"Database error: " . $DBI::errstr . "\n";
|
2010-09-17 11:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|