[29] | 1 | package LATMOS::Accounts::Bases::Sql; |
---|
[19] | 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | |
---|
| 7 | use base qw(LATMOS::Accounts::Bases); |
---|
[297] | 8 | use LATMOS::Accounts::Log; |
---|
[19] | 9 | use DBI; |
---|
| 10 | |
---|
| 11 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
| 12 | |
---|
| 13 | =head1 NAME |
---|
| 14 | |
---|
| 15 | LATMOS::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 | |
---|
| 25 | Account base access over standard unix file format. |
---|
| 26 | |
---|
| 27 | =head1 FUNCTIONS |
---|
| 28 | |
---|
| 29 | =cut |
---|
| 30 | |
---|
| 31 | =head2 new(%options) |
---|
| 32 | |
---|
| 33 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
| 34 | |
---|
| 35 | domain / server: either the Ad domain or directly the server |
---|
| 36 | |
---|
| 37 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
| 38 | |
---|
| 39 | =cut |
---|
| 40 | |
---|
| 41 | sub new { |
---|
| 42 | my ($class, %options) = @_; |
---|
| 43 | |
---|
| 44 | my $base = { |
---|
| 45 | db_conn => $options{db_conn}, |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | bless($base, $class); |
---|
| 49 | } |
---|
| 50 | |
---|
[102] | 51 | sub DESTROY { |
---|
| 52 | my ($self) = @_; |
---|
| 53 | $self->{_db} && $self->{_db}->rollback; |
---|
| 54 | } |
---|
| 55 | |
---|
[19] | 56 | sub 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 | |
---|
| 83 | Read file and load data into memory |
---|
| 84 | |
---|
| 85 | =cut |
---|
| 86 | |
---|
| 87 | sub load { |
---|
| 88 | my ($self) = @_; |
---|
| 89 | return $self->db ? 1 : 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | sub 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 | |
---|
| 104 | sub 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] | 116 | sub list_supported_objects { |
---|
| 117 | my ($self, @otype) = @_; |
---|
| 118 | $self->SUPER::list_supported_objects(qw(site), @otype); |
---|
| 119 | } |
---|
| 120 | |
---|
[52] | 121 | sub 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] | 132 | sub 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 | |
---|
| 144 | sub 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] | 157 | 1; |
---|
| 158 | |
---|
| 159 | __END__ |
---|
| 160 | |
---|
| 161 | =head1 SEE ALSO |
---|
| 162 | |
---|
| 163 | =head1 AUTHOR |
---|
| 164 | |
---|
| 165 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 166 | |
---|
| 167 | =head1 COPYRIGHT AND LICENSE |
---|
| 168 | |
---|
| 169 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
| 170 | |
---|
| 171 | This library is free software; you can redistribute it and/or modify |
---|
| 172 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 173 | at your option, any later version of Perl 5 you may have available. |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | =cut |
---|