Database.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 164 CC 14 SCAR 25.0
CC
14
Cov
0%
new
42.0
DESTROY
6.0
state
6.0
CRAP 12.1
1 #!/usr/bin/perl
2  
3 # EvilBoss-api - API for creating FTN-tools
4 #
5 # EBDatabase.pm - work with databases (PostgreSQL/MySQL/mSQL)
6 #
7 # Copyright (c) 2004-2005 Alex Soukhotine, 2:5030/1157
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # $Id$
15  
16 package EvilBoss::Database;
17  
18 require Exporter;
19 require DBI;
20 use Fcntl qw(:flock);
21 require EBConfig;
22  
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24  
25 $VERSION = 1.01;
26  
27 @ISA=qw(Exporter);
28 #@EXPORT = qw(&f1 &f3 &name);
29 %EXPORT_TAGS=();
30  
31 #@EXPORT_OK = qw(&name);
32  
33 my $tmpfile = '';
34 my $SQLConnectStr = '';
35 my $dbh = undef;
36 my $sth = undef;
37 my $state = 0;
38  
39 sub new
40 {
41 my $classname = shift;
42 my $self = {};
43 bless($self,$classname);
44 my $cfg=$_[0];
45 $tmpfile=$cfg->{TempDir}."/EBmSQL.tmp";
46 $SQLConnectStr="DBI:".$cfg->{DBType}.":";
47
48 if ($cfg->{DBType} eq "mSQL")
49 {
50 $SQLConnectStr .= $cfg->{DBName};
51 $cfg->{DBUser} = undef;
52 $cfg->{DBPass} = undef;
53 if (open(TMP,">$tmpfile"))
54 {
55 print "Locking temporary file...\n\n";
56 flock(TMP,LOCK_EX);
57 }
58 }
59 elsif ($cfg->{DBType} eq "mysql")
60 {
61 $SQLConnectStr .= $cfg->{DBName}.":".$cfg->{DBHost}.":".$cfg->{DBPort};
62 }
63 elsif ($cfg->{DBType} eq "Pg")
64 {
65 $SQLConnectStr .= "dbname=".$cfg->{DBName}.";host=".$cfg->{DBHost}.";port=".$cfg->{DBPort};
66 }
67
68 $dbh = DBI->connect($SQLConnectStr,$cfg->{DBUser},$cfg->{DBPass},{ PrintError => 0, RaiseError => 0 });
69
70 $state=1 if ($dbh);
71
72 return $self;
73 }
74  
75 sub DESTROY
76 {
77 my $self = shift;
78 if ($state)
79 {
80 $dbh->disconnect;
81 $state=0;
82 }
83 }
84  
85 sub state
86 {
87 my $self = shift;
88 return 1 if ($state);
89 return 0;
90 }
91  
92 sub dbh
93 {
94 return $dbh;
95 }
96  
97 sub quick_fetchrow
98 {
99 my $self = shift;
100 my $sth,$tmp;
101 if ($dbh)
102 {
103 $sth=$dbh->prepare($_[0]);
104 $sth->execute;
105 $tmp=$sth->fetchrow;
106 $sth->finish;
107 }
108 return $tmp;
109 }
110  
111  
112 sub query
113 {
114 my $self = shift;
115 $sth=$dbh->prepare($_[0]);
116 $sth->execute;
117 }
118  
119 sub end_query
120 {
121 my $self=shift;
122 $sth->finish;
123 $sth=undef;
124 }
125  
126 sub fetchrow
127 {
128 my $self = shift;
129 my $tmp;
130 if ($dbh && $sth)
131 {
132 $tmp=$sth->fetchrow;
133 }
134 return $tmp;
135 }
136  
137 sub fetchrow_array
138 {
139 my $self = shift;
140 my @tmp;
141 if ($dbh && $sth)
142 {
143 @tmp=$sth->fetchrow_array;
144 }
145 return @tmp;
146 }
147  
148 sub fetchrow_hashref
149 {
150 my $self = shift;
151 my %tmp;
152 if ($dbh && $sth)
153 {
154 %tmp=$sth->fetchrow_hashref;
155 }
156 return %tmp;
157 }
158
159  
160 sub quick_fetchrow_array
161 {
162 my $self = shift;
163 my $sth,@tmp;
164 if ($dbh)
165 {
166 $sth=$dbh->prepare($_[0]);
167 $sth->execute;
168 @tmp=$sth->fetchrow_array;
169 $sth->finish;
170 }
171 return @tmp;
172 }
173  
174 sub quick_fetchrow_hashref
175 {
176 my $self = shift;
177 my $sth,%tmp;
178 if ($dbh)
179 {
180 $sth=$dbh->prepare($_[0]);
181 $sth->execute;
182 %tmp=$sth->fetchrow_hashref;
183 $sth->finish;
184 }
185 return %tmp;
186 }
187
188  
189 1;