File Coverage

blib/lib/Lemonldap/NG/Common/Conf/_DBI.pm
Criterion Covered Total %
statement 18 53 33.9
branch 2 14 14.2
condition 1 11 9.0
subroutine 5 13 38.4
pod 0 8 0.0
total 26 99 26.2


line stmt bran cond sub pod time code
1             package Lemonldap::NG::Common::Conf::_DBI;
2              
3 3     3   13 use strict;
  3         6  
  3         113  
4 3     3   4677 use DBI;
  3         47028  
  3         222  
5 3     3   32 use Lemonldap::NG::Common::Conf::Constants; #inherits
  3         5  
  3         589  
6              
7             our $VERSION = '1.4.0';
8             our @ISA = qw(Lemonldap::NG::Common::Conf::Constants);
9             our ( @EXPORT, %EXPORT_TAGS );
10              
11             BEGIN {
12 3     3   15 *Lemonldap::NG::Common::Conf::_dbh = \&_dbh;
13 3         14 push @EXPORT, @Lemonldap::NG::Common::Conf::Constants::EXPORT;
14 3         14 %EXPORT_TAGS = %Lemonldap::NG::Common::Conf::Constants::EXPORT_TAGS;
15 3         1617 push @EXPORT,
16             qw(prereq available lastCfg _dbh lock isLocked unlock delete logError);
17             }
18              
19             sub prereq {
20 3     3 0 6 my $self = shift;
21 3 50       12 unless ( $self->{dbiChain} ) {
22 0         0 $Lemonldap::NG::Common::Conf::msg =
23             '"dbiChain" is required in *DBI configuration type';
24 0         0 return 0;
25             }
26 3 50       11 print STDERR __PACKAGE__ . 'Warning: "dbiUser" parameter is not set'
27             unless ( $self->{dbiUser} );
28 3   50     19 $self->{dbiTable} ||= "lmConfig";
29 3         14 1;
30             }
31              
32             sub available {
33 0     0 0   my $self = shift;
34 0           my $sth =
35             $self->_dbh->prepare( "SELECT DISTINCT cfgNum from "
36             . $self->{dbiTable}
37             . " order by cfgNum" );
38 0           $sth->execute();
39 0           my @conf;
40 0           while ( my @row = $sth->fetchrow_array ) {
41 0           push @conf, $row[0];
42             }
43 0           return @conf;
44             }
45              
46             sub lastCfg {
47 0     0 0   my $self = shift;
48 0           my @row = $self->_dbh->selectrow_array(
49             "SELECT max(cfgNum) from " . $self->{dbiTable} );
50 0           return $row[0];
51             }
52              
53             sub _dbh {
54 0     0     my $self = shift;
55 0   0       $self->{dbiTable} ||= "lmConfig";
56 0 0 0       return $self->{_dbh} if ( $self->{_dbh} and $self->{_dbh}->ping );
57 0           return DBI->connect_cached( $self->{dbiChain}, $self->{dbiUser},
58             $self->{dbiPassword}, { RaiseError => 1, AutoCommit => 1, } );
59             }
60              
61             sub lock {
62 0     0 0   my $self = shift;
63 0 0         if ( $self->{dbiChain} =~ /^dbi:mysql:/i ) {
64 0           my @row = $self->_dbh->selectrow_array("SELECT GET_LOCK('lmconf', 0)");
65 0   0       return $row[0] || 0;
66             }
67 0           return 1;
68             }
69              
70             sub isLocked {
71 0     0 0   my $self = shift;
72 0 0         if ( $self->{dbiChain} =~ /^dbi:mysql:/i ) {
73 0           my @row = $self->_dbh->selectrow_array("SELECT IS_FREE_LOCK('lmconf')");
74 0 0         return $row[0] ? 0 : 1;
75             }
76 0           return 0;
77             }
78              
79             sub unlock {
80 0     0 0   my $self = shift;
81 0 0         if ( $self->{dbiChain} =~ /^dbi:mysql:/i ) {
82 0           my @row = $self->_dbh->selectrow_array("SELECT RELEASE_LOCK('lmconf')");
83 0   0       return $row[0] || 0;
84             }
85 0           return 1;
86             }
87              
88             sub delete {
89 0     0 0   my ( $self, $cfgNum ) = @_;
90 0           $self->_dbh->do( "DELETE from " . $self->{dbiTable} . " WHERE cfgNum=?",
91             {}, $cfgNum );
92             }
93              
94             sub logError {
95 0     0 0   my $self = shift;
96 0           $Lemonldap::NG::Common::Conf::msg .=
97             "Database error: " . $self->_dbh->errstr . "\n";
98             }
99              
100             1;
101             __END__