Added some other keys in the configuration file. Now
[breadcrumbs.git] / src / lib / Bcd / Data / StatementsStash.pm
blobdbfd4110c2c5977f540a9783f0a8ed35b6ec6dca
1 package Bcd::Data::StatementsStash;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 use strict;
24 use warnings;
26 use DBI;
27 use Cache::FastMmap;
28 use File::Basename;
29 use File::Find;
30 use File::Spec;
31 use Template;
32 use Bcd::Common::Mail;
33 use Data::Dumper;
34 use FindBin;
36 #this file should simply model the stash which holds the statements
37 #to query and modify the db...
39 sub new {
40 #my ($class, $connection, $cache) = @_;
41 my ($class, $server, $init_file) = @_;
43 my $is_testing = $server->{test};
45 my $self = {};
47 my $conn_string = $server->{conn_string};
48 my $user = $server->{user_db};
49 my $share_file = $server->{share_file};
50 my $expire_time = $server->{expire_time};
52 # if ($is_testing == 0){
53 # $conn_string = 'DBI:Pg:database=breadcrumb';
54 # $user = "";
55 # $share_file = "/tmp/bcd_cache";
56 # } else {
57 # $conn_string = 'DBI:Pg:database=bcdb-test';
58 # $user = "lino";
59 # $share_file = "/tmp/TEST_bcd_cache";
60 # }
62 my $conn = DBI->connect_cached( $conn_string, $user, '',
63 { RaiseError => 1, AutoCommit => 0 });
65 #I have also a cache...
66 my $cache;
69 #The $init file is only a hack. It is defined by the first one
70 #which creates the stash, In this version is the manager of the
71 #bots, because is created whenever the daemon starts.
73 if (defined($init_file)){
74 $cache = Cache::FastMmap->new(
75 share_file => $share_file,
76 init_file => 1,
77 expire_time=> $expire_time,
80 #in the first I initialize the command id
81 $cache->set("current_id_cmd", 0);
82 } else {
83 $cache = Cache::FastMmap->new(
84 share_file => $share_file,
85 expire_time=> $expire_time,
89 $self->{conn} = $conn;
90 $self->{cache} = $cache;
91 $self->{mailer} = Bcd::Common::Mail->new($server);
92 $self->{testing}= 0;
94 bless ($self, $class);
96 $self->_create_the_template($server->{bcd_home});
98 return $self;
101 #this is only used by the tests...
102 sub in_testing{
103 my $self = shift;
104 $self->{testing} = 1;
105 $self->{mailer}->testing_do_not_send_mails();
108 #this gets the bc-root password, hashed
109 sub get_bc_root_password{
110 my $self = shift;
111 if ($self->{testing} == 0){
112 local $/ = "\n";
113 #the user wants to be root. The password is stored in a local file, hashed
114 #otherwise it is stored in the configuration file...
115 my $name = "$FindBin::Bin/../local-admin-passwd";
116 open PASSWD, "< $name" or die "Not found the bc-root password file $name\n";
117 my $password = <PASSWD>;
118 chomp $password;
119 close PASSWD;
120 return $password;
121 } else {
122 #if I am testing the password is fixed to "qpqpqp", and this is the hash...
123 return "55a16180593910a43c0bd2a1344296b6fa4ee802";
127 sub get_next_cmd_id{
128 my $self = shift;
129 #atomically increment and get the value of the next command id
130 return $self->{cache}->get_and_set("current_id_cmd", sub { return ++$_[1]; });
133 sub get_mailer{
134 my $self = shift;
135 return $self->{mailer};
138 sub _create_the_template{
139 my ($self, $bcd_home) = @_;
141 # my $file = __FILE__;
142 # my $abs_path = File::Spec->rel2abs( $file ) ;
143 # my $canon_path = File::Spec->canonpath($abs_path);
144 # my $base = File::Basename::dirname($canon_path);
147 #the templates are inside /templates, and then I put "it" because
148 #in this directory there are the italian templates...
149 my $templates_root_dir = $bcd_home . '/templates/it';
151 my $tt = Template->new({
152 INCLUDE_PATH => $templates_root_dir,
153 INTERPOLATE => 1,
154 WRAPPER => 'mail_wrapper.tt2',
157 $self->{template} = $tt;
160 sub process_this_template{
161 my ($self, $template_name, $vars, $output) = @_;
163 $self->{template}->
164 process($template_name, $vars, $output)
165 || print $self->{template}->error();
167 return 1;
170 #simple pass thru
171 sub prepare_cached{
172 my ($self, $sql) = @_;
173 return $self->{conn}->prepare_cached($sql);
176 sub get_statement{
177 my ($self, $statement, $model) = @_;
179 #I try to get the statement, if not, I will simply try to ask
180 #kindly the model to populate the stash with its commands
182 if ( ! exists($self->{$statement})){
183 $model->populate_the_stash($self);
186 #if it does not exist, return undef...
187 return $self->{$statement};
190 #this function should simply record a statement in the connection for
191 #future reuse, so it is prepared only once.
192 sub record_this_statement{
193 my ($self, $statement_name, $sql_string) = @_;
195 my $sth = $self->{conn}->prepare_cached( $sql_string );
196 $self->{$statement_name} = $sth;
199 sub insert_statement{
200 my ($self, $statement_string, $statement) = @_;
201 $self->{$statement_string} = $statement;
204 sub get_connection{
205 my $self = shift;
206 return $self->{"conn"};
209 sub get_cache{
210 my $self = shift;
211 return $self->{"cache"};
214 sub get_last_personal_data_id {
215 my $self = shift;
216 return $self->_get_last_sequence_value("personal_users_data_id_seq");
219 sub get_id_of_last_user{
220 my $self = shift;
221 return $self->_get_last_sequence_value("users_id_seq");
224 sub get_id_of_last_site{
225 my $self = shift;
226 return $self->_get_last_sequence_value("bc_sites_id_seq");
229 sub get_last_founder_id{
230 my $self = shift;
231 return $self->_get_last_sequence_value("ant_nests_founders_id_user_seq");
234 sub get_last_activity_id{
235 my ($self, $seq) = @_;
236 return $self->_get_last_sequence_value($seq);
239 sub get_last_public_site_presence{
240 my $self = shift;
241 return $self->_get_last_sequence_value("users_in_sites_id_seq");
244 sub get_last_ad_id{
245 my $self = shift;
246 return $self->_get_last_sequence_value("ads_id_seq");
249 sub get_last_invoice_id{
250 my $self = shift;
251 return $self->_get_last_sequence_value("invoices_id_seq");
254 sub _get_last_sequence_value{
255 my ($self, $seq_name) = @_;
257 my $id = $self->{conn}->last_insert_id(undef, undef, undef, undef,
258 {sequence => $seq_name});
259 return $id;
263 #this method returns the post code stored in the session.
264 sub get_session_post_code{
265 my ($self, $session) = @_;
267 my $value = $self->get_cache()->get($session);
268 return $value->{ant_nest};
271 sub get_session_id{
272 my ($self, $session) = @_;
274 my $value = $self->get_cache()->get($session);
275 return $value->{user_id};
278 sub get_session_role{
279 my ($self, $session) = @_;
281 my $value = $self->get_cache()->get($session);
282 return $value->{user_role};
285 sub get_session_nick{
286 my ($self, $session) = @_;
288 my $value = $self->get_cache()->get($session);
289 return $value->{user_connected};
292 #just a convenience function
293 sub select_one_row_arr{
294 my ($self, $st_sql, @pars) = @_;
296 my ($st, $res) = $self->_prepare_bind_execute($st_sql, @pars);
297 my $row = $st->fetch();
298 $st->finish();
300 return $row;
303 sub _prepare_bind_execute{
304 my ($self, $st_sql, @pars) = @_;
306 my $st = $self->prepare_cached($st_sql);
307 my $res = $st->execute(@pars);
309 return ($st, $res);
312 sub select_one_row_hash{
313 my ($self, $st_sql, @pars) = @_;
315 my ($st, $res) = $self->_prepare_bind_execute($st_sql, @pars);
316 my $hash = $st->fetchrow_hashref();
317 $st->finish();
319 return $hash;
322 sub prep_exec{
323 my ($self, $st_sql, @pars) = @_;
325 my ($st, $res) = $self->_prepare_bind_execute($st_sql, @pars);
326 return $res;
329 sub select_all_ds{
330 my ($self, $st_sql, @pars) = @_;
332 my ($st, $res) = $self->_prepare_bind_execute($st_sql, @pars);
334 my $data_set = $st->fetchall_arrayref();
335 my $var = $st->{NAME_lc};
336 unshift (@{$data_set}, $var);
337 return $data_set;
340 sub select_all_arr{
341 my ($self, $st_sql, @pars) = @_;
342 my ($st, $res) = $self->_prepare_bind_execute($st_sql, @pars);
343 my $arr = $st->fetchall_arrayref();
344 return $arr;