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

Last change on this file since 570 was 570, checked in by nanardon, 15 years ago
  • cache some schema data, taking care to delete cache on commit/rollback
  • Property svn:keywords set to Id Rev
File size: 3.5 KB
RevLine 
[29]1package LATMOS::Accounts::Bases::Sql;
[19]2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases);
[297]8use LATMOS::Accounts::Log;
[19]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
[102]51sub DESTROY {
52    my ($self) = @_;
53    $self->{_db} && $self->{_db}->rollback;
54}
55
[19]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            {
[329]66                RaiseError => 1,
[19]67                AutoCommit => 0,
[327]68                PrintWarn => 1,
[19]69                PrintError => 1,
70            }
[297]71        ) or do {
72            $self->log(LA_ERR, "Cannot connect to database");   
73            return;
74        };
[19]75        $self->{_db}->do(q{set DATESTYLE to 'DMY'});
[297]76        $self->log(LA_DEBUG, 'New connection to DB');
[19]77        return $self->{_db};
78    }
79}
80
81=head2 load
82
83Read file and load data into memory
84
85=cut
86
87sub load {
88    my ($self) = @_;
89    return $self->db ? 1 : 0;   
90}
91
92sub commit {
93    my ($self) = @_;
[297]94    if ($ENV{LA_NO_COMMIT}) {
95        $self->log(LA_DEBUG, 'DB::COMMIT (ignore due to LA_NO_COMMIT)');
96        return 1;
97    } else {
98        $self->log(LA_DEBUG, 'DB::COMMIT');
99    }
[570]100    $self->{__cache} = undef;
[19]101    $self->db->commit;
102}
103
104sub rollback {
105    my ($self) = @_;
[297]106    if ($ENV{LA_NO_COMMIT}) {
107        $self->log(LA_DEBUG, 'DB::ROLLBACK (ignore due to LA_NO_COMMIT)');
108        return 1
109    } else {
110        $self->log(LA_DEBUG, 'DB::ROLLBACK');
111    }
[570]112    $self->{__cache} = undef;
[19]113    $self->db->rollback;
114}
115
[132]116sub list_supported_objects {
117    my ($self, @otype) = @_;
118    $self->SUPER::list_supported_objects(qw(site), @otype);
119}
120
[52]121sub current_rev {
122    my ($self) = @_;
123    my $sth = $self->db->prepare_cached(
124        q{select max(rev) from revisions}
125    );
126    $sth->execute;
127    my $res = $sth->fetchrow_hashref;
128    $sth->finish;
129    return ($res->{max});
130} 
131
[413]132sub get_global_value {
133    my ($self, $varname) = @_;
134
135    my $sth = $self->db->prepare_cached(q{
136        select val from settings where varname = ?
137        });
138    $sth->execute($varname);
139    my $res = $sth->fetchrow_hashref;
140    $sth->finish;
141    $res->{val}
142}
143
144sub set_global_value {
145    my ($self, $varname, $value) = @_;
146    my $sth = $self->db->prepare(q{
147        update settings set val = ? where varname = ?
148        });
149    $sth->execute($value, $varname) == 0 and do {
150        my $sth2 = $self->db->prepare(q{
151            insert into settings (val, varname) values (?,?)
152            });
153        $sth2->execute($value, $varname);
154    };
155}
156
[19]1571;
158
159__END__
160
161=head1 SEE ALSO
162
163=head1 AUTHOR
164
165Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
166
167=head1 COPYRIGHT AND LICENSE
168
169Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
170
171This library is free software; you can redistribute it and/or modify
172it under the same terms as Perl itself, either Perl version 5.10.0 or,
173at your option, any later version of Perl 5 you may have available.
174
175
176=cut
Note: See TracBrowser for help on using the repository browser.