Debian Codegraph @

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

raw
1 package Lemonldap::NG::Common::Conf::Backends::_DBI;
2
3 use strict;
4 no strict 'refs';
5 use utf8;
6 use DBI;
7 use Lemonldap::NG::Common::Conf::Constants;
8 use Lemonldap::NG::Common::Lib::DBI qw(check_dbh);
9
10 our $VERSION = '2.22.2';
11 our @ISA = qw(Lemonldap::NG::Common::Conf::Constants);
12 our ( @EXPORT, %EXPORT_TAGS );
13
14 BEGIN {
15 *Lemonldap::NG::Common::Conf::_dbh = \&_dbh;
16 *Lemonldap::NG::Common::Conf::_execute = \&_execute;
17 push @EXPORT, @Lemonldap::NG::Common::Conf::Constants::EXPORT;
18 %EXPORT_TAGS = %Lemonldap::NG::Common::Conf::Constants::EXPORT_TAGS;
19 push @EXPORT,
20 qw(prereq available lastCfg _dbh lock isLocked unlock delete logError);
21 }
22
23 sub prereq {
24 my $self = shift;
25 unless ( $self->{dbiChain} ) {
26 $Lemonldap::NG::Common::Conf::msg =
27 '"dbiChain" is required in *DBI configuration type';
28 return 0;
29 }
30 $Lemonldap::NG::Common::Conf::msg .=
31 'Warning: "dbiUser" parameter is not set'
32 unless ( $self->{dbiUser} );
33 $self->{dbiTable} ||= "lmConfig";
34 1;
35 }
36
37 sub available {
38 my $self = shift;
39 my $sth =
40 $self->_execute( "SELECT DISTINCT cfgNum from "
41 . $self->{dbiTable}
42 . " order by cfgNum" );
43 my @conf;
44 while ( my @row = $sth->fetchrow_array ) {
45 push @conf, $row[0];
46 }
47 return @conf;
48 }
49
50 sub lastCfg {
51 my $self = shift;
52 my $sth = $self->_execute( "SELECT max(cfgNum) from " . $self->{dbiTable} );
53 my @row = $sth->fetchrow_array();
54 return $row[0];
55 }
56
57 sub _dbh {
58 my $self = shift;
59 $self->{dbiTable} ||= "lmConfig";
60 my $dbh = check_dbh( $self->{_dbh} );
61 return $dbh if $dbh;
62 delete $self->{_dbh};
63 eval {
64 $self->{_dbh} =
65 DBI->connect_cached( $self->{dbiChain}, $self->{dbiUser},
66 $self->{dbiPassword}, { RaiseError => 1, AutoCommit => 1, } );
67 die unless $self->{_dbh}->ping;
68 };
69 if ( $@
70 and $self->{type}->can('beforeRetry')
71 and $self->{type}->can('beforeRetry')->($self) )
72 {
73 eval {
74 $self->{_dbh} =
75 DBI->connect_cached( $self->{dbiChain}, $self->{dbiUser},
76 $self->{dbiPassword}, { RaiseError => 1, AutoCommit => 1, } );
77 };
78 }
79 if ($@) {
80 die $@;
81 }
82 if ( $self->{dbiChain} =~ /^dbi:sqlite/i ) {
83 $self->{_dbh}->{sqlite_unicode} = 1;
84 }
85 elsif ( $self->{dbiChain} =~ /^dbi:mysql/i ) {
86 eval {
87 $self->{_dbh}->{mysql_enable_utf8} = 1;
88 $self->{_dbh}->do("set names 'utf8'");
89 };
90 }
91 elsif ( $self->{dbiChain} =~ /^dbi:pg/i ) {
92 $self->{_dbh}->{pg_enable_utf8} = 1;
93 }
94
95 # DBD::MariaDB should have o problem here
96 return $self->{_dbh};
97 }
98
99 sub lock {
100 my $self = shift;
101 return 1;
102 }
103
104 sub isLocked {
105 my $self = shift;
106 return 0;
107 }
108
109 sub unlock {
110 my $self = shift;
111 return 1;
112 }
113
114 sub delete {
115 my ( $self, $cfgNum ) = @_;
116 my $res = $self->_execute( "DELETE FROM $self->{dbiTable} WHERE cfgNum=?",
117 $cfgNum );
118 return ( $res > 0 );
119 }
120
121 sub logError {
122 my $self = shift;
123 $Lemonldap::NG::Common::Conf::msg .=
124 "Database error: " . $DBI::errstr . "\n";
125 }
126
127 sub _execute {
128 my ( $self, $query, @prms ) = @_;
129 my $req = $self->_dbh->prepare($query);
130 unless ($req) {
131 $self->logError;
132 return UNKNOWN_ERROR;
133 }
134 my $execute = eval { $req->execute(@prms) };
135 if ( !$execute
136 and $self->{type}->can('beforeRetry')
137 and $self->{type}->can('beforeRetry')->($self) )
138 {
139 $req = $self->_dbh->prepare($query);
140 $execute = eval { $req->execute(@prms) };
141 }
142 unless ($execute) {
143 $self->logError;
144 return UNKNOWN_ERROR;
145 }
146 return ( 1, $req );
147 }
148
149 sub beforeRetry {
150 return !$_[0]->{noRetry};
151 }
152
153 1;