7d139419124d09b8897b611b0d06fcc8c5ed036e
[freeside.git] / torrus / perllib / Torrus / Collector / ExtDBI.pm
1 #  Copyright (C) 2005  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: ExtDBI.pm,v 1.1 2010-12-27 00:03:58 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 ## Pluggable backend module for ExternalStorage
21 ## Stores data in a generic SQL database
22
23 # We use some internals of Torrus::SQL::SrvExport, but
24 # handle the SQL by ourselves, for better efficiency.
25
26 package Torrus::Collector::ExtDBI;
27
28 use strict;
29 use DBI;
30 use Date::Format;
31
32 use Torrus::SQL::SrvExport;
33 use Torrus::Log;
34
35 $Torrus::Collector::ExternalStorage::backendInit =
36     \&Torrus::Collector::ExtDBI::backendInit;
37
38 $Torrus::Collector::ExternalStorage::backendOpenSession =
39     \&Torrus::Collector::ExtDBI::backendOpenSession;
40
41 $Torrus::Collector::ExternalStorage::backendStoreData =
42     \&Torrus::Collector::ExtDBI::backendStoreData;
43
44 $Torrus::Collector::ExternalStorage::backendCloseSession =
45     \&Torrus::Collector::ExtDBI::backendCloseSession;
46
47
48 # Optional SQL connection subtype, configurable from torrus-siteconfig.pl
49 our $subtype;
50
51 my $dbh;
52 my $sth;
53
54 sub backendInit
55 {
56     my $collector = shift;
57     my $token = shift;
58 }
59
60 sub backendOpenSession
61 {
62     $dbh = Torrus::SQL::SrvExport->dbh( $subtype );
63     
64     if( defined( $dbh ) )
65     {
66         $sth = $dbh->prepare( Torrus::SQL::SrvExport->sqlInsertStatement() );
67         if( not defined( $sth ) )
68         {
69             Error('Error preparing the SQL statement: ' . $dbh->errstr);
70         }
71     }
72 }
73
74
75 sub backendStoreData
76 {
77     my $timestamp = shift;
78     my $serviceid = shift;
79     my $value = shift;
80     my $interval = shift;
81     
82     if( defined( $dbh ) and defined( $sth ) )
83     {
84         my $datestr = time2str('%Y-%m-%d', $timestamp);
85         my $timestr = time2str('%H:%M:%S', $timestamp);
86         if( isDebug() )
87         {
88             Debug('Updating SQL database: ' .
89                   join(', ', $datestr, $timestr,
90                        $serviceid, $value, $interval ));
91         }
92
93         if( $sth->execute( $datestr, $timestr,
94                            $serviceid, $value, $interval ) )
95         {
96             return 1;
97         }
98         else
99         {
100             Error('Error executing SQL: ' . $dbh->errstr);
101         }
102     }
103
104     return undef;
105 }
106
107
108 sub backendCloseSession
109 {
110     undef $sth;
111     if( defined( $dbh ) )
112     {
113         $dbh->commit();
114         $dbh->disconnect();
115         undef $dbh;
116     }
117 }
118
119
120     
121 1;
122
123
124 # Local Variables:
125 # mode: perl
126 # indent-tabs-mode: nil
127 # perl-indent-level: 4
128 # End: