[2] | 1 | package LATMOS::Accounts::Bases; |
---|
| 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | use LATMOS::Accounts::Bases::Objects; |
---|
| 7 | |
---|
[3] | 8 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
[2] | 9 | |
---|
[3] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | LATMOS::Accounts::Bases - Base class for account data bases |
---|
| 13 | |
---|
| 14 | =head1 SYNOPSIS |
---|
| 15 | |
---|
| 16 | use LATMOS::Accounts::Bases; |
---|
| 17 | my $base = LATMOS::Accounts::Bases->new('type', %options); |
---|
| 18 | ... |
---|
| 19 | |
---|
| 20 | =head1 DESCRIPTION |
---|
| 21 | |
---|
| 22 | This module provide basic functions for various account base |
---|
| 23 | |
---|
| 24 | =head1 FUNTIONS |
---|
| 25 | |
---|
| 26 | =cut |
---|
| 27 | |
---|
| 28 | =head2 new($type, %options) |
---|
| 29 | |
---|
| 30 | Return, if success, a new data base account object, $type is |
---|
| 31 | account base type, %options to setup the base. |
---|
| 32 | |
---|
| 33 | =cut |
---|
| 34 | |
---|
[2] | 35 | sub new { |
---|
| 36 | my ($class, $type, %options) = @_; |
---|
| 37 | |
---|
| 38 | my $pclass = ucfirst(lc($type)); |
---|
| 39 | eval "require LATMOS::Accounts::Bases::$pclass;"; |
---|
| 40 | if ($@) { return } # error message ? |
---|
| 41 | return "LATMOS::Accounts::Bases::$pclass"->new(%options); |
---|
| 42 | } |
---|
| 43 | |
---|
[6] | 44 | sub _load_obj_class { |
---|
| 45 | my ($self, $otype) = @_; |
---|
| 46 | |
---|
| 47 | # finding perl class: |
---|
| 48 | my $pclass = ref $self; |
---|
| 49 | $pclass .= '::' . ucfirst(lc($otype)); |
---|
| 50 | eval "require $pclass;"; |
---|
| 51 | if ($@) { return } # error message ? |
---|
| 52 | return $pclass; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | =head2 list_canonicals_fields($otype) |
---|
| 56 | |
---|
| 57 | Return the list of supported fields by the database for object type $otype. |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
| 61 | sub list_canonicals_fields { |
---|
| 62 | my ($self, $otype) = @_; |
---|
| 63 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
| 64 | $pclass->canonical_fields; |
---|
| 65 | } |
---|
| 66 | |
---|
[3] | 67 | =head2 get_object($type, $id) |
---|
| 68 | |
---|
| 69 | Return an object of $type (typically user or group) having identifier |
---|
| 70 | $id. |
---|
| 71 | |
---|
| 72 | =cut |
---|
| 73 | |
---|
[2] | 74 | sub get_object { |
---|
| 75 | my ($self, $otype, $id) = @_; |
---|
| 76 | |
---|
| 77 | return LATMOS::Accounts::Bases::Objects->new($self, $otype, $id); |
---|
| 78 | } |
---|
| 79 | |
---|
[5] | 80 | =head2 load |
---|
| 81 | |
---|
| 82 | Make account base loading data into memory if need. |
---|
| 83 | Should always be called, if database fetch data on the fly |
---|
| 84 | (SQL, LDAP), the function just return True. |
---|
| 85 | |
---|
| 86 | =cut |
---|
| 87 | |
---|
| 88 | sub load { 1 } |
---|
| 89 | |
---|
[3] | 90 | =head2 is_transactionnal |
---|
[2] | 91 | |
---|
[3] | 92 | Return True is the database support commit and rollback |
---|
[2] | 93 | |
---|
[3] | 94 | =cut |
---|
[2] | 95 | |
---|
[3] | 96 | sub is_transactionnal { |
---|
| 97 | my ($self) = @_; |
---|
| 98 | return($self->can('_rollback') && $self->can('_commit')); |
---|
| 99 | } |
---|
[2] | 100 | |
---|
[3] | 101 | =head2 commit |
---|
[2] | 102 | |
---|
[3] | 103 | Save change into the database if change are not done immediately. |
---|
| 104 | This should always be called as you don't know when change are applied. |
---|
[2] | 105 | |
---|
[3] | 106 | Return always true if database does not support any transaction. |
---|
[2] | 107 | |
---|
[3] | 108 | The driver should provides a _commit functions to save data. |
---|
[2] | 109 | |
---|
[3] | 110 | =cut |
---|
[2] | 111 | |
---|
[3] | 112 | sub commit { |
---|
| 113 | my ($self) = @_; |
---|
| 114 | return $self->can('_commit') ? $self->_commit : 1; |
---|
| 115 | } |
---|
[2] | 116 | |
---|
[3] | 117 | =head2 rollback |
---|
[2] | 118 | |
---|
[3] | 119 | If database support transaction, rollback changes. Return false |
---|
| 120 | if database does not support. |
---|
[2] | 121 | |
---|
[3] | 122 | If supported, driver should provides a _rollback functions |
---|
[2] | 123 | |
---|
[3] | 124 | =cut |
---|
[2] | 125 | |
---|
[3] | 126 | sub rollback { |
---|
| 127 | my ($self) = @_; |
---|
| 128 | return $self->can('_rollback') ? $self->_commit : 0; |
---|
| 129 | } |
---|
[2] | 130 | |
---|
[3] | 131 | 1; |
---|
[2] | 132 | |
---|
[3] | 133 | __END__ |
---|
[2] | 134 | |
---|
[3] | 135 | =head1 SEE ALSO |
---|
[2] | 136 | |
---|
| 137 | =head1 AUTHOR |
---|
| 138 | |
---|
[3] | 139 | Thauvin Olivier, E<lt>olivier.thauvin.ipsl.fr.thauvin@latmos.ipsl.fr<gt> |
---|
[2] | 140 | |
---|
| 141 | =head1 COPYRIGHT AND LICENSE |
---|
| 142 | |
---|
| 143 | Copyright (C) 2009 by Thauvin Olivier |
---|
| 144 | |
---|
| 145 | This library is free software; you can redistribute it and/or modify |
---|
| 146 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 147 | at your option, any later version of Perl 5 you may have available. |
---|
| 148 | |
---|
| 149 | =cut |
---|