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.3 2011-05-19 17:30:44 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 );
67 if ( $dbh->{Driver}->{Name} =~ /mysql/i ) {
68 $dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');
72 $sth = $dbh->prepare( Torrus::SQL::SrvExport->sqlInsertStatement() );
73 if( not defined( $sth ) )
75 Error('Error preparing the SQL statement: ' . $dbh->errstr);
83 my $timestamp = shift;
84 my $serviceid = shift;
88 if( defined( $dbh ) and defined( $sth ) )
90 my $datestr = time2str('%Y-%m-%d', $timestamp);
91 my $timestr = time2str('%H:%M:%S', $timestamp);
94 Debug('Updating SQL database: ' .
95 join(', ', $datestr, $timestr,
96 $serviceid, $value, $interval ));
99 if( $sth->execute( $datestr, $timestr,
100 $serviceid, $value, $interval ) )
106 Error('Error executing SQL: ' . $dbh->errstr);
114 sub backendCloseSession
117 if( defined( $dbh ) )
132 # indent-tabs-mode: nil
133 # perl-indent-level: 4