1 | package LATMOS::Accounts::Bases::Sql::objects; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Objects); |
---|
8 | |
---|
9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::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 | |
---|
23 | Account base access over standard unix file format. |
---|
24 | |
---|
25 | =head1 FUNCTIONS |
---|
26 | |
---|
27 | =cut |
---|
28 | |
---|
29 | =head2 new(%options) |
---|
30 | |
---|
31 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
32 | |
---|
33 | domain / server: either the Ad domain or directly the server |
---|
34 | |
---|
35 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
36 | |
---|
37 | =cut |
---|
38 | |
---|
39 | sub 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 | |
---|
58 | sub 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 | |
---|
76 | sub _initial_fields { |
---|
77 | my ($self, $for) = @_; |
---|
78 | $self->_inline_fields($for) |
---|
79 | } |
---|
80 | |
---|
81 | sub _inline_fields { |
---|
82 | my ($self, $for) = @_; |
---|
83 | {} |
---|
84 | } |
---|
85 | |
---|
86 | sub _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 | |
---|
103 | sub _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 | |
---|
119 | sub _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 | |
---|
128 | sub new { |
---|
129 | my ($class, $base, $id) = @_; |
---|
130 | bless({ id => $id, _base => $base }, $class); |
---|
131 | } |
---|
132 | |
---|
133 | sub _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 | |
---|
162 | sub db { |
---|
163 | return $_[0]->base->db; |
---|
164 | } |
---|
165 | |
---|
166 | sub quote_object_table { |
---|
167 | my ($self) = @_; |
---|
168 | my $table = $self->object_table or return; |
---|
169 | $self->db->quote_identifier($table); |
---|
170 | } |
---|
171 | sub quote_key_field { |
---|
172 | my ($self) = @_; |
---|
173 | my $key_field = $self->key_field or return; |
---|
174 | $self->db->quote_identifier($key_field); |
---|
175 | } |
---|
176 | |
---|
177 | sub 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 | |
---|
209 | sub 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 | |
---|
257 | 1; |
---|
258 | |
---|
259 | __END__ |
---|
260 | |
---|
261 | =head1 SEE ALSO |
---|
262 | |
---|
263 | =head1 AUTHOR |
---|
264 | |
---|
265 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
266 | |
---|
267 | =head1 COPYRIGHT AND LICENSE |
---|
268 | |
---|
269 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
270 | |
---|
271 | This library is free software; you can redistribute it and/or modify |
---|
272 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
273 | at your option, any later version of Perl 5 you may have available. |
---|
274 | |
---|
275 | |
---|
276 | =cut |
---|