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.
17 # $Id: ExtDBI.pm,v 1.1 2010-12-27 00:03:58 ivan Exp $
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;
32 use Torrus::SQL::SrvExport;
35 $Torrus::Collector::ExternalStorage::backendInit =
36 \&Torrus::Collector::ExtDBI::backendInit;
38 $Torrus::Collector::ExternalStorage::backendOpenSession =
39 \&Torrus::Collector::ExtDBI::backendOpenSession;
41 $Torrus::Collector::ExternalStorage::backendStoreData =
42 \&Torrus::Collector::ExtDBI::backendStoreData;
44 $Torrus::Collector::ExternalStorage::backendCloseSession =
45 \&Torrus::Collector::ExtDBI::backendCloseSession;
48 # Optional SQL connection subtype, configurable from torrus-siteconfig.pl
56 my $collector = shift;
60 sub backendOpenSession
62 $dbh = Torrus::SQL::SrvExport->dbh( $subtype );
66 $sth = $dbh->prepare( Torrus::SQL::SrvExport->sqlInsertStatement() );
67 if( not defined( $sth ) )
69 Error('Error preparing the SQL statement: ' . $dbh->errstr);
77 my $timestamp = shift;
78 my $serviceid = shift;
82 if( defined( $dbh ) and defined( $sth ) )
84 my $datestr = time2str('%Y-%m-%d', $timestamp);
85 my $timestr = time2str('%H:%M:%S', $timestamp);
88 Debug('Updating SQL database: ' .
89 join(', ', $datestr, $timestr,
90 $serviceid, $value, $interval ));
93 if( $sth->execute( $datestr, $timestr,
94 $serviceid, $value, $interval ) )
100 Error('Error executing SQL: ' . $dbh->errstr);
108 sub backendCloseSession
111 if( defined( $dbh ) )
126 # indent-tabs-mode: nil
127 # perl-indent-level: 4