source: trunk/cgi-bin/IPDB.pm@ 55

Last change on this file since 55 was 16, checked in by Kris Deugau, 20 years ago

/trunk/cgi-bin/IPDB.pm:

Remove CommonWeb.pm dependency from IPDB.pm, so that it
doesn't crash on DB errors.

File size: 1.7 KB
RevLine 
[8]1# ipdb/cgi-bin/IPDB.pm
[4]2# Contains DB-related functions for IPDB
[8]3###
4# SVN revision info
5# $Date$
6# SVN revision $Rev$
7# Last update by $Author$
8###
9
[4]10# Split from CommonWeb.pm 08/13/2004 kdeugau@vianet
11
12package IPDB;
13
14use strict;
15use warnings;
16use Exporter;
17use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
18
19$VERSION = 1.0;
20@ISA = qw(Exporter);
21@EXPORT_OK = qw(&connectDB &checkDBSanity);
22
23@EXPORT = (); # Export nothing by default.
24%EXPORT_TAGS = ( ALL => [qw( &connectDB &checkDBSanity )]
25 );
26
27# Creates connection to IPDB.
28# Default is a PostgreSQL db; could be any DBMS with the
29# right changes. MySQL in comments. Note that some DBMS's don't
30# support transactions, this is a Bad Thing!
31# Returns a handle to the db.
32sub connectDB {
33 my $dbh;
34 my $DSN = "DBI:Pg:dbname=ipdb";
35 my $user = 'ipdb';
36 my $pw = 'ipdbpwd';
37
38# Note that we want to autocommit by default, and we will turn it off locally as necessary.
39 $dbh = DBI->connect($DSN, $user, $pw, { AutoCommit => 1 } )
40 or return undef if(!$dbh);
41
42 return $dbh;
43} # end connectDB
44
45# Quick check to see if the db is responding. A full integrity
46# check will have to be a separate tool to walk the IP allocation trees.
47sub checkDBSanity {
48 my $dbh = connectDB();
49
50 if (!$dbh) {
[16]51 print "Cannot connect to the database!";
[4]52 } else {
53 # it connects, try a stmt.
54 my $sth = $dbh->prepare('select cidr from masterblocks');
55 my $err = $sth->execute();
56
57 if ($sth->fetchrow()) {
58 # all is well.
59 return 1;
60 } else {
[16]61 print "Connected to the database, but could not execute test statement. ".$sth->errstr();
[4]62 }
63 }
64 # Clean up after ourselves.
65 $dbh->disconnect;
66} # end checkDBSanity
67
68# Indicates module loaded OK. Required by Perl.
691;
Note: See TracBrowser for help on using the repository browser.