a4fe4fc754582040cf784d9dd7fa7ab96118daf1
[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.2 2011-04-29 01:13:20 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
67         if ( $dbh->{Driver}->{Name} =~ /^mysql/i ) {
68           $dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');
69           $dbh->commit();
70         }
71
72         $sth = $dbh->prepare( Torrus::SQL::SrvExport->sqlInsertStatement() );
73         if( not defined( $sth ) )
74         {
75             Error('Error preparing the SQL statement: ' . $dbh->errstr);
76         }
77     }
78 }
79
80
81 sub backendStoreData
82 {
83     my $timestamp = shift;
84     my $serviceid = shift;
85     my $value = shift;
86     my $interval = shift;
87     
88     if( defined( $dbh ) and defined( $sth ) )
89     {
90         my $datestr = time2str('%Y-%m-%d', $timestamp);
91         my $timestr = time2str('%H:%M:%S', $timestamp);
92         if( isDebug() )
93         {
94             Debug('Updating SQL database: ' .
95                   join(', ', $datestr, $timestr,
96                        $serviceid, $value, $interval ));
97         }
98
99         if( $sth->execute( $datestr, $timestr,
100                            $serviceid, $value, $interval ) )
101         {
102             return 1;
103         }
104         else
105         {
106             Error('Error executing SQL: ' . $dbh->errstr);
107         }
108     }
109
110     return undef;
111 }
112
113
114 sub backendCloseSession
115 {
116     undef $sth;
117     if( defined( $dbh ) )
118     {
119         $dbh->commit();
120         $dbh->disconnect();
121         undef $dbh;
122     }
123 }
124
125
126     
127 1;
128
129
130 # Local Variables:
131 # mode: perl
132 # indent-tabs-mode: nil
133 # perl-indent-level: 4
134 # End: