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

Last change on this file since 81 was 81, checked in by nanardon, 15 years ago
  • provide id functions to objects
  • Property svn:keywords set to Id Rev
File size: 8.0 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 has_extended_attributes { 0 }
77
78sub _initial_fields {
79    my ($self, $for) = @_;
80    $self->_inline_fields($for)
81}
82
83sub _inline_fields {
84    my ($self, $for) = @_;
85    {}
86}
87
88sub _canonical_fields {
89    my ($class, $base, $for) = @_;
90    $for ||= 'rw';
91    my @attr;
92    if ($class->has_extended_attributes) {
93        my $sth = $base->db->prepare_cached(
94            sprintf(
95                q{select canonical from %s order by canonical},
96                $base->db->quote_identifier($class->object_table . '_attributes_list'),
97            )
98        );
99        $sth->execute;
100        while (my $res = $sth->fetchrow_hashref) {
101            push(@attr, $res->{canonical});
102        }
103    }
104    @attr, keys %{ $class->_inline_fields($for) || {} }
105}
106
107sub _get_field_name_db {
108    my ($class, $c_field, $base) = @_;
109    $class->has_extended_attributes or return;
110    $class->object_table or return;
111    my $sth = $base->db->prepare_cached(
112        sprintf(
113            q{select ikey from %s where canonical = ?},
114            $base->db->quote_identifier($class->object_table . '_attributes_list'),
115        )
116    );
117    $sth->execute($c_field);
118   
119    my $res = $sth->fetchrow_hashref;
120    $sth->finish;
121    $res->{ikey}
122}
123
124sub _get_field_name {
125    my ($class, $c_field, $base, $for) = @_;
126    $for ||= 'rw';
127    my $inline = $class->_inline_fields($for) || {};
128    return $inline->{$c_field}
129        ? $inline->{$c_field}
130        : $class->_get_field_name_db($c_field, $base);
131}
132
133sub new {
134    my ($class, $base, $id) = @_;
135    bless({ _id => $id, _base => $base }, $class);
136}
137
138sub _create {
139    my ($class, $base, $id, %data) = @_;
140
141    # splitting inline from extended
142    my $inl = $class->_initial_fields('w') || {};
143    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
144    my (%first, %second);
145    foreach (keys %data) {
146        if ($inline{$_}) {
147            $first{$_} = $data{$_};
148        } else {
149            $second{$_} = $data{$_};
150        }
151    }
152    $first{$class->key_field} = $id;
153
154    my $sth = $base->db->prepare(
155        sprintf(
156            q{insert into %s (%s) values (%s)},
157            $base->db->quote_identifier($class->object_table),
158            join(', ', map { $base->db->quote_identifier($_) } sort keys %first),
159            join(',', qw(?) x scalar(keys %first)),
160        )
161    );
162    $sth->execute(map { $first{$_} } sort keys %first) or return;
163
164    $class->new($base, $id)->set_fields(%second);
165
166    1;
167}
168
169sub _delete {
170    my ($class, $base, $id) = @_;
171
172    my $sthd = $base->db->prepare_cached(
173        sprintf(
174            q{delete from %s where %s = ?},
175            $base->db->quote_identifier($class->object_table),
176            $base->db->quote_identifier($class->key_field),
177        )
178    );
179    $sthd->execute($id);
180}
181
182sub db {
183    return $_[0]->base->db;
184}
185
186sub quote_object_table {
187    my ($self) = @_;
188    my $table = $self->object_table or return;
189    $self->db->quote_identifier($table);
190}
191sub quote_key_field { 
192    my ($self) = @_;
193    my $key_field = $self->key_field or return;
194    $self->db->quote_identifier($key_field);
195}
196
197sub get_field {
198    my ($self, $field) = @_;
199    my $inl = $self->_inline_fields('r') || {};
200    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
201    if ($inline{$field}) {
202    my $sth = $self->db->prepare_cached(
203        sprintf(
204            q{select %s from %s where %s = ?},
205            $self->db->quote_identifier($field),
206            $self->quote_object_table,
207            $self->quote_key_field,
208        )
209    );
210    $sth->execute($self->id);
211    my $res = $sth->fetchrow_hashref;
212    $sth->finish;
213    return $res->{$field};
214    } elsif ($self->has_extended_attributes) { # else, then we mandatory have extend attr
215        my $sth = $self->db->prepare_cached(
216            sprintf(
217                q{select value from %s where id = ? and attr = ?},
218                $self->db->quote_identifier($self->object_table. '_attributes'),
219            )
220        );
221        $sth->execute($self->id, $field);
222        my $res = $sth->fetchrow_hashref;
223        $sth->finish;
224        return $res->{value};
225    }
226}
227
228sub set_fields {
229    my ($self, %data) = @_;
230    my @fields;
231    my @vals;
232    my %ext;
233    my $inl = $self->_inline_fields('w') || {};
234    my %inline = map { $inl->{$_} => 1 }  keys %{ $inl || {}};
235    foreach my $field (keys %data) {
236        my $oldval = $self->get_field($field);
237        next if (($data{$field} || '') eq ($oldval || ''));
238        if ($inline{$field}) {
239        # TODO check fields exists !
240        push(@fields, sprintf("%s = ?", $self->db->quote_identifier($field)));
241        push(@vals, $data{$field});
242        } else {
243            $ext{$field} = $data{$field};
244        }
245    }
246    if (@fields) {
247        my $sth = $self->db->prepare_cached(
248            sprintf(
249                q{update %s set %s where %s = ?},
250                $self->quote_object_table,
251                join(', ', @fields),
252                $self->quote_key_field,
253            )
254        );
255        $sth->execute(@vals, $self->id);
256    }
257   
258    if ($self->has_extended_attributes) {
259        my $sthd = $self->db->prepare_cached(
260            sprintf(
261                q{delete from %s where id = ? and attr = ?},
262                $self->db->quote_identifier($self->object_table. '_attributes'),
263            ),
264        );
265        my $sthx = $self->db->prepare_cached(
266            sprintf(
267                q{insert into %s (id, attr, value) values (?,?,?)},
268                $self->db->quote_identifier($self->object_table. '_attributes'),
269            )
270        );
271        my $sthu = $self->db->prepare_cached(
272            sprintf(
273                q{update %s set value = ? where id = ? and attr = ?},
274                $self->db->quote_identifier($self->object_table. '_attributes'),
275            )
276        );
277
278        foreach (keys %ext) {
279            if ($ext{$_}) {
280                $sthu->execute($ext{$_}, $self->id, $_) ||
281                $sthx->execute($self->id, $_, $ext{$_}) or return;
282            } else {
283                $sthd->execute($self->id, $_) or return;
284            }
285        }
286    }
287
288    1;
289}
290
2911;
292
293__END__
294
295=head1 SEE ALSO
296
297=head1 AUTHOR
298
299Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
300
301=head1 COPYRIGHT AND LICENSE
302
303Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
304
305This library is free software; you can redistribute it and/or modify
306it under the same terms as Perl itself, either Perl version 5.10.0 or,
307at your option, any later version of Perl 5 you may have available.
308
309
310=cut
Note: See TracBrowser for help on using the repository browser.