merge upstream
[torrus-plus.git] / src / lib / Torrus / Collector / ExtDBI.pm
blobc8f6bb67a077bc890044bb6365d523f0c4a55dbf
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$
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;
28 use strict;
29 use warnings;
31 use DBI;
32 use Date::Format;
34 use Torrus::SQL::SrvExport;
35 use Torrus::Log;
37 our $VERSION = 1.0;
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
53 our $subtype;
55 my $dbh;
56 my $sth;
58 sub backendInit
60 my $collector = shift;
61 my $token = shift;
64 sub backendOpenSession
66 $dbh = Torrus::SQL::SrvExport->dbh( $subtype );
68 if( defined( $dbh ) )
70 $sth = $dbh->prepare( Torrus::SQL::SrvExport->sqlInsertStatement() );
71 if( not defined( $sth ) )
73 Error('Error preparing the SQL statement: ' . $dbh->errstr);
79 sub backendStoreData
81 my $timestamp = shift;
82 my $serviceid = shift;
83 my $value = shift;
84 my $interval = shift;
86 if( defined( $dbh ) and defined( $sth ) )
88 my $datestr = time2str('%Y-%m-%d', $timestamp);
89 my $timestr = time2str('%H:%M:%S', $timestamp);
90 if( isDebug() )
92 Debug('Updating SQL database: ' .
93 join(', ', $datestr, $timestr,
94 $serviceid, $value, $interval ));
97 if( $sth->execute( $datestr, $timestr,
98 $serviceid, $value, $interval ) )
100 return 1;
102 else
104 Error('Error executing SQL: ' . $dbh->errstr);
108 return;
112 sub backendCloseSession
114 undef $sth;
115 if( defined( $dbh ) )
117 $dbh->commit();
118 $dbh->disconnect();
119 undef $dbh;
128 # Local Variables:
129 # mode: perl
130 # indent-tabs-mode: nil
131 # perl-indent-level: 4
132 # End: