1 # Copyright (C) 2005 Stanislav Sinyagin
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.
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.
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.
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 ## Pluggable backend module for ExternalStorage
21 ## Stores data in a generic SQL database
23 # We use some internals of Torrus::SQL::SrvExport, but
24 # handle the SQL by ourselves, for better efficiency.
26 package Torrus
::Collector
::ExtDBI
;
34 use Torrus
::SQL
::SrvExport
;
39 $Torrus::Collector
::ExternalStorage
::backendInit
=
40 \
&Torrus
::Collector
::ExtDBI
::backendInit
;
42 $Torrus::Collector
::ExternalStorage
::backendOpenSession
=
43 \
&Torrus
::Collector
::ExtDBI
::backendOpenSession
;
45 $Torrus::Collector
::ExternalStorage
::backendStoreData
=
46 \
&Torrus
::Collector
::ExtDBI
::backendStoreData
;
48 $Torrus::Collector
::ExternalStorage
::backendCloseSession
=
49 \
&Torrus
::Collector
::ExtDBI
::backendCloseSession
;
52 # Optional SQL connection subtype, configurable from torrus-siteconfig.pl
60 my $collector = shift;
64 sub backendOpenSession
66 $dbh = Torrus
::SQL
::SrvExport
->dbh( $subtype );
70 $sth = $dbh->prepare( Torrus
::SQL
::SrvExport
->sqlInsertStatement() );
71 if( not defined( $sth ) )
73 Error
('Error preparing the SQL statement: ' . $dbh->errstr);
81 my $timestamp = shift;
82 my $serviceid = shift;
86 if( defined( $dbh ) and defined( $sth ) )
88 my $datestr = time2str
('%Y-%m-%d', $timestamp);
89 my $timestr = time2str
('%H:%M:%S', $timestamp);
92 Debug
('Updating SQL database: ' .
93 join(', ', $datestr, $timestr,
94 $serviceid, $value, $interval ));
97 if( $sth->execute( $datestr, $timestr,
98 $serviceid, $value, $interval ) )
104 Error
('Error executing SQL: ' . $dbh->errstr);
112 sub backendCloseSession
115 if( defined( $dbh ) )
130 # indent-tabs-mode: nil
131 # perl-indent-level: 4