a327e07e220dc9a52daf5fed9124964eb2159e7a
[spider.git] / perl / DXSql.pm
1 #
2 # The master SQL module
3 #
4 # $Id$
5 #
6 # Copyright (c) 2006 Dirk Koopman G1TLH
7 #
8
9 package DXSql;
10
11 use strict;
12
13 use vars qw($VERSION $BRANCH);
14 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
15 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
16 $main::build += $VERSION;
17 $main::branch += $BRANCH;
18
19 our $active = 0;
20
21 sub init
22 {
23         return $active if $active;
24         
25         eval { 
26                 require DBI;
27         };
28         unless ($@) {
29                 import DBI;
30                 $active++;
31         }
32         return $active;
33
34
35 sub new
36 {
37         my $class = shift;
38         my $dsn = shift;
39         my $self;
40         
41         return undef unless $active;
42         my $dbh;
43         my ($style) = $dsn =~ /^dbi:(\w+):/;
44         my $newclass = "DXSql::$style";
45         eval "require $newclass";
46         if ($@) {
47                 $active = 0;
48                 return undef;
49         }
50         return bless {}, $newclass;
51 }
52
53 sub connect
54 {
55         my $self = shift; 
56         my $dsn = shift;
57         my $user = shift;
58         my $passwd = shift;
59         
60         my $dbh;
61         eval {
62                 no strict 'refs';
63                 $dbh = DBI->connect($dsn, $user, $passwd); 
64         };
65         unless ($dbh) {
66                 $active = 0;
67                 return undef;
68         }
69         $self->{dbh} = $dbh;
70         return $self;
71 }
72
73 sub finish
74 {
75         my $self = shift;
76         $self->{dbh}->disconnect;
77
78 1;
79