Debian Codegraph @

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

raw
1 package Lemonldap::NG::Common::Conf::Backends::CDBI;
2
3 use strict;
4 use utf8;
5 use JSON;
6 use Lemonldap::NG::Common::Conf::Backends::_DBI;
7
8 our $VERSION = '2.22.0';
9 our @ISA = qw(Lemonldap::NG::Common::Conf::Backends::_DBI);
10
11 sub store {
12 my ( $self, $fields ) = @_;
13 my $cfgNum = $fields->{cfgNum};
14 my $req;
15 my $lastCfg = $self->lastCfg;
16
17 $fields = to_json($fields);
18
19 my $query =
20 $lastCfg == $cfgNum
21 ? "UPDATE $self->{dbiTable} SET data=? WHERE cfgNum=?"
22 : "INSERT INTO $self->{dbiTable} (data,cfgNum) VALUES (?,?)";
23 my $res = $self->_execute( $query, $fields, $cfgNum );
24 return $res > 0 ? $cfgNum : $res;
25 }
26
27 sub load {
28 my ( $self, $cfgNum, $fields ) = @_;
29 $fields = $fields ? join( ",", @$fields ) : '*';
30 my ( $res, $row ) = $self->_execute(
31 "SELECT data from " . $self->{dbiTable} . " WHERE cfgNum=?", $cfgNum );
32 if ( $res > 0 ) {
33 $row = $row->fetchrow_arrayref();
34 }
35 else {
36 $self->logError;
37 return 0;
38 }
39 my $r;
40 if ( $row->[0] =~ /^\s*\{/s ) {
41 eval { $r = from_json( $row->[0], { allow_nonref => 1 } ); };
42 }
43 else { # Old format
44 require Storable;
45 eval { $r = Storable::thaw( $row->[0] ); };
46 }
47 if ($@) {
48 $Lemonldap::NG::Common::Conf::msg .=
49 "Bad stored data in conf database: $@ \n";
50 return 0;
51 }
52 return $r;
53 }
54
55 1;
56 __END__