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

Last change on this file since 59 was 59, checked in by nanardon, 15 years ago
  • support Group/memberUID, eg user in a group
  • Property svn:keywords set to Id Rev
File size: 6.9 KB
Line 
1package LATMOS::Accounts::Bases::Sql::objects;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Objects);
8
9our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
10
11=head1 NAME
12
13LATMOS::Ad - Perl extension for blah blah blah
14
15=head1 SYNOPSIS
16
17  use LATMOS::Accounts::Bases;
18  my $base = LATMOS::Accounts::Bases->new('unix');
19  ...
20
21=head1 DESCRIPTION
22
23Account base access over standard unix file format.
24
25=head1 FUNCTIONS
26
27=cut
28
29=head2 new(%options)
30
31Create a new LATMOS::Ad object for windows AD $domain.
32
33domain / server: either the Ad domain or directly the server
34
35ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.
36
37=cut
38
39sub list {
40    my ($class, $base) = @_;
41
42    my $sth = $base->db->prepare_cached(
43        sprintf(
44            q{select %s as k from %s order by %s},
45            $base->db->quote_identifier($class->key_field),
46            $base->db->quote_identifier($class->object_table),
47            $base->db->quote_identifier($class->key_field),
48        )
49    );
50    $sth->execute;
51    my @keys;
52    while(my $res = $sth->fetchrow_hashref) {
53        push(@keys, $res->{k});
54    }
55    @keys
56}
57
58sub list_from_rev {
59    my ($class, $base, $rev) = @_;
60    my $sth = $base->db->prepare_cached(
61        sprintf(
62            q{select %s as k from %s where rev > ? order by %s},
63            $base->db->quote_identifier($class->key_field),
64            $base->db->quote_identifier($class->object_table),
65            $base->db->quote_identifier($class->key_field),
66        )
67    );
68    $sth->execute($rev);
69    my @keys;
70    while(my $res = $sth->fetchrow_hashref) {
71        push(@keys, $res->{k});
72    }
73    @keys
74}
75
76sub _initial_fields {
77    my ($self, $for) = @_;
78    $self->_inline_fields($for)
79}
80
81sub _inline_fields {
82    my ($self, $for) = @_;
83    {}
84}
85
86sub _canonical_fields {
87    my ($class, $base, $for) = @_;
88    $for ||= 'rw';
89    my $sth = $base->db->prepare_cached(
90        sprintf(
91            q{select canonical from %s order by canonical},
92            $base->db->quote_identifier($class->object_table . '_attributes_list'),
93        )
94    );
95    $sth->execute;
96    my @attr;
97    while (my $res = $sth->fetchrow_hashref) {
98        push(@attr, $res->{canonical});
99    }
100    @attr, keys %{ $class->_inline_fields($for) || {} }
101}
102
103sub _get_field_name_db {
104    my ($class, $c_field, $base) = @_;
105    $class->object_table or return;
106    my $sth = $base->db->prepare_cached(
107        sprintf(
108            q{select ikey from %s where canonical = ?},
109            $base->db->quote_identifier($class->object_table . '_attributes_list'),
110        )
111    );
112    $sth->execute($c_field);
113   
114    my $res = $sth->fetchrow_hashref;
115    $sth->finish;
116    $res->{ikey}
117}
118
119sub _get_field_name {
120    my ($class, $c_field, $base, $for) = @_;
121    $for ||= 'rw';
122    my $inline = $class->_inline_fields($for) || {};
123    return $inline->{$c_field}
124        ? $inline->{$c_field}
125        : $class->_get_field_name_db($c_field, $base);
126}
127
128sub new {
129    my ($class, $base, $id) = @_;
130    bless({ id => $id, _base => $base }, $class);
131}
132
133sub _create {
134    my ($class, $base, $id, %data) = @_;
135
136    # splitting inline from extended
137    my $inl = $class->_initial_fields('w') || {};
138    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
139    my (%first, %second);
140    foreach (keys %data) {
141        if ($inline{$_}) {
142            $first{$_} = $data{$_};
143        } else {
144            $second{$_} = $data{$_};
145        }
146    }
147    $first{$class->key_field} = $id;
148
149    my $sth = $base->db->prepare(
150        sprintf(
151            q{insert into %s (%s) values (%s)},
152            $base->db->quote_identifier($class->object_table),
153            join(', ', map { $base->db->quote_identifier($_) } sort keys %first),
154            join(',', qw(?) x scalar(keys %first)),
155        )
156    );
157    $sth->execute(map { $first{$_} } sort keys %first) or return;
158
159    $class->new($base, $id)->set_fields(%second);
160}
161
162sub db {
163    return $_[0]->base->db;
164}
165
166sub quote_object_table {
167    my ($self) = @_;
168    my $table = $self->object_table or return;
169    $self->db->quote_identifier($table);
170}
171sub quote_key_field { 
172    my ($self) = @_;
173    my $key_field = $self->key_field or return;
174    $self->db->quote_identifier($key_field);
175}
176
177sub get_field {
178    my ($self, $field, $for) = @_;
179    $for ||= 'rw';
180    my $inl = $self->_inline_fields($for) || {};
181    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
182    if ($inline{$field}) {
183    my $sth = $self->db->prepare_cached(
184        sprintf(
185            q{select %s from %s where %s = ?},
186            $self->db->quote_identifier($field),
187            $self->quote_object_table,
188            $self->quote_key_field,
189        )
190    );
191    $sth->execute($self->{id});
192    my $res = $sth->fetchrow_hashref;
193    $sth->finish;
194    return $res->{$field};
195    } else {
196        my $sth = $self->db->prepare_cached(
197            sprintf(
198                q{select value from %s where id = ? and attr = ?},
199                $self->db->quote_identifier($self->object_table. '_attributes'),
200            )
201        );
202        $sth->execute($self->{id}, $field);
203        my $res = $sth->fetchrow_hashref;
204        $sth->finish;
205        return $res->{value};
206    }
207}
208
209sub set_fields {
210    my ($self, %data) = @_;
211    my @fields;
212    my @vals;
213    my %ext;
214    my $inl = $self->_inline_fields('w') || {};
215    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
216    foreach my $field (keys %data) {
217        if ($inline{$field}) {
218        # TODO check fields exists !
219        push(@fields, sprintf("%s = ?", $self->db->quote_identifier($field)));
220        push(@vals, $data{$field});
221        } else {
222            $ext{$field} = $data{$field};
223        }
224    }
225    if (@fields) {
226        my $sth = $self->db->prepare_cached(
227            sprintf(
228                q{update %s set %s where %s = ?},
229                $self->quote_object_table,
230                join(', ', @fields),
231                $self->quote_key_field,
232            )
233        );
234        $sth->execute(@vals, $self->{id});
235    }
236   
237    my $sthd = $self->db->prepare_cached(
238        sprintf(
239            q{delete from %s where id = ? and attr = ?},
240            $self->db->quote_identifier($self->object_table. '_attributes'),
241        ),
242    );
243    my $sthx = $self->db->prepare_cached(
244        sprintf(
245            q{insert into %s (id, attr, value) values (?,?,?)},
246            $self->db->quote_identifier($self->object_table. '_attributes'),
247        )
248    );
249
250    foreach (keys %ext) {
251        $sthd->execute($self->{id}, $_) or return;
252        $ext{$_} or next;
253        $sthx->execute($self->{id}, $_, $ext{$_}) or return;
254    }
255}
256
2571;
258
259__END__
260
261=head1 SEE ALSO
262
263=head1 AUTHOR
264
265Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
266
267=head1 COPYRIGHT AND LICENSE
268
269Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
270
271This library is free software; you can redistribute it and/or modify
272it under the same terms as Perl itself, either Perl version 5.10.0 or,
273at your option, any later version of Perl 5 you may have available.
274
275
276=cut
Note: See TracBrowser for help on using the repository browser.