source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm @ 744

Last change on this file since 744 was 744, checked in by nanardon, 14 years ago
  • use SQL transaction isolation
  • Property svn:keywords set to Id Rev
File size: 3.7 KB
Line 
1package LATMOS::Accounts::Bases::Sql;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases);
8use LATMOS::Accounts::Log;
9use DBI;
10
11our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
12
13=head1 NAME
14
15LATMOS::Ad - Perl extension for blah blah blah
16
17=head1 SYNOPSIS
18
19  use LATMOS::Accounts::Bases;
20  my $base = LATMOS::Accounts::Bases->new('unix');
21  ...
22
23=head1 DESCRIPTION
24
25Account base access over standard unix file format.
26
27=head1 FUNCTIONS
28
29=cut
30
31=head2 new(%options)
32
33Create a new LATMOS::Ad object for windows AD $domain.
34
35domain / server: either the Ad domain or directly the server
36
37ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.
38
39=cut
40
41sub new {
42    my ($class, %options) = @_;
43   
44    my $base = {
45        db_conn => $options{db_conn},
46    };
47
48    bless($base, $class);
49}
50
51sub DESTROY {
52    my ($self) = @_;
53    $self->{_db} && $self->{_db}->rollback;
54}
55
56sub db {
57    my ($self) = @_;
58
59    if ($self->{_db} && $self->{_db}->ping) {
60        return $self->{_db};
61    } else {
62        $self->{_db} = DBI->connect_cached(
63            'dbi:Pg:' . $self->{db_conn},
64            undef, undef,
65            {
66                RaiseError => 1,
67                AutoCommit => 0,
68                PrintWarn => 1,
69                PrintError => 1,
70            }
71        ) or do {
72            $self->log(LA_ERR, "Cannot connect to database");   
73            return;
74        };
75        $self->{_db}->do(q(SET SESSION CHARACTERISTICS AS TRANSACTION
76                    ISOLATION LEVEL SERIALIZABLE));
77        $self->{_db}->do(q{set DATESTYLE to 'DMY'});
78        $self->log(LA_DEBUG, 'New connection to DB');
79        return $self->{_db};
80    }
81}
82
83=head2 load
84
85Read file and load data into memory
86
87=cut
88
89sub load {
90    my ($self) = @_;
91    return $self->db ? 1 : 0;   
92}
93
94sub commit {
95    my ($self) = @_;
96    if ($ENV{LA_NO_COMMIT}) {
97        $self->log(LA_DEBUG, 'DB::COMMIT (ignore due to LA_NO_COMMIT)');
98        return 1;
99    } else {
100        $self->log(LA_DEBUG, 'DB::COMMIT');
101    }
102    $self->{__cache} = undef;
103    $self->db->commit;
104}
105
106sub rollback {
107    my ($self) = @_;
108    if ($ENV{LA_NO_COMMIT}) {
109        $self->log(LA_DEBUG, 'DB::ROLLBACK (ignore due to LA_NO_COMMIT)');
110        return 1
111    } else {
112        $self->log(LA_DEBUG, 'DB::ROLLBACK');
113    }
114    $self->{__cache} = undef;
115    $self->db->rollback;
116}
117
118sub list_supported_objects {
119    my ($self, @otype) = @_;
120    $self->SUPER::list_supported_objects(qw(site), @otype);
121}
122
123sub current_rev {
124    my ($self) = @_;
125    my $sth = $self->db->prepare_cached(
126        q{select max(rev) from revisions}
127    );
128    $sth->execute;
129    my $res = $sth->fetchrow_hashref;
130    $sth->finish;
131    return ($res->{max});
132} 
133
134sub get_global_value {
135    my ($self, $varname) = @_;
136
137    my $sth = $self->db->prepare_cached(q{
138        select val from settings where varname = ?
139        });
140    $sth->execute($varname);
141    my $res = $sth->fetchrow_hashref;
142    $sth->finish;
143    $res->{val}
144}
145
146sub set_global_value {
147    my ($self, $varname, $value) = @_;
148    my $sth = $self->db->prepare(q{
149        update settings set val = ? where varname = ?
150        });
151    $sth->execute($value, $varname) == 0 and do {
152        my $sth2 = $self->db->prepare(q{
153            insert into settings (val, varname) values (?,?)
154            });
155        $sth2->execute($value, $varname);
156    };
157}
158
1591;
160
161__END__
162
163=head1 SEE ALSO
164
165=head1 AUTHOR
166
167Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
172
173This library is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself, either Perl version 5.10.0 or,
175at your option, any later version of Perl 5 you may have available.
176
177
178=cut
Note: See TracBrowser for help on using the repository browser.