Debian Codegraph @

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

raw
1 package Lemonldap::NG::Common::Conf::Backends::RDBI;
2
3 use strict;
4 use utf8;
5 use Lemonldap::NG::Common::Conf::Serializer;
6 use Lemonldap::NG::Common::Conf::Backends::_DBI;
7
8 our $VERSION = '2.0.12';
9 our @ISA = qw(Lemonldap::NG::Common::Conf::Backends::_DBI);
10
11 sub store {
12 my ( $self, $fields ) = @_;
13 my $cfgNum = $fields->{cfgNum};
14 $self->{noQuotes} = 1;
15 $fields = $self->serialize($fields);
16
17 my $req;
18 my $lastCfg = $self->lastCfg;
19 $self->_dbh->begin_work;
20 $req = $self->_dbh->prepare(
21 "INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)");
22
23 _delete( $self, $cfgNum ) if $lastCfg == $cfgNum;
24 unless ($req) {
25 $self->logError;
26 return UNKNOWN_ERROR;
27 }
28 while ( my ( $k, $v ) = each %$fields ) {
29 my @execValues = ( $cfgNum, $k, $v );
30 my $execute;
31 eval { $execute = $req->execute(@execValues); };
32 print STDERR $@ if $@;
33 unless ($execute) {
34 $self->logError;
35 _delete( $self, $cfgNum ) if $lastCfg != $cfgNum;
36 $self->_dbh->rollback;
37 return UNKNOWN_ERROR;
38 }
39 }
40 $self->_dbh->commit;
41 return $cfgNum;
42 }
43
44 sub load {
45 my ( $self, $cfgNum, $fields ) = @_;
46 $fields = $fields ? join( ",", @$fields ) : '*';
47 my $sth =
48 $self->_dbh->prepare(
49 "SELECT field,value from " . $self->{dbiTable} . " WHERE cfgNum=?" )
50 or $self->logError;
51 $sth->execute($cfgNum) or $self->logError;
52 my ( $res, @row );
53 while ( @row = $sth->fetchrow_array ) {
54 $res->{ $row[0] } = $row[1];
55 }
56 unless ($res) {
57 $Lemonldap::NG::Common::Conf::msg .=
58 "No configuration $cfgNum found \n";
59 return 0;
60 }
61 $res->{cfgNum} = $cfgNum;
62 return $self->unserialize($res);
63 }
64
65 sub _delete {
66 my ( $self, $cfgNum ) = @_;
67 my $r =
68 $self->_dbh->prepare("DELETE FROM $self->{dbiTable} where cfgNum=?");
69 $r->execute($cfgNum);
70 }
71
72 1;