1 |
## -------------------------------------------------------------------------------- |
2 |
## $Id: libdb.pm,v 1.8 2002/12/12 02:48:58 joko Exp $ |
3 |
## -------------------------------------------------------------------------------- |
4 |
## $Log: libdb.pm,v $ |
5 |
## Revision 1.8 2002/12/12 02:48:58 joko |
6 |
## + played with fixing disconnectTarget |
7 |
## |
8 |
## Revision 1.7 2002/12/01 22:13:17 joko |
9 |
## + minor bugfix? |
10 |
## |
11 |
## Revision 1.6 2002/11/29 04:53:39 joko |
12 |
## + hash2Sql now knows about SQL_SELECT |
13 |
## |
14 |
## Revision 1.5 2002/11/17 07:18:38 joko |
15 |
## + small modification in hash2sql |
16 |
## |
17 |
## Revision 1.4 2002/10/16 22:36:42 joko |
18 |
## + sub testDbServer |
19 |
## |
20 |
## Revision 1.3 2002/07/27 00:28:20 cvsjoko |
21 |
## bugfixes |
22 |
## |
23 |
## Revision 1.2 2002/07/20 11:09:58 cvsjoko |
24 |
## + bugfixes |
25 |
## + dont' print sql-errors |
26 |
## |
27 |
## Revision 1.1 2002/07/19 18:14:03 cvsjoko |
28 |
## no message |
29 |
## |
30 |
## |
31 |
## -------------------------------------------------------------------------------- |
32 |
|
33 |
package libdb; |
34 |
|
35 |
use strict; |
36 |
use warnings; |
37 |
|
38 |
require Exporter; |
39 |
our @ISA = qw( Exporter ); |
40 |
our @EXPORT_OK = qw( |
41 |
testDsn hash2Sql |
42 |
SQL_INSERT SQL_UPDATE |
43 |
connectTarget disconnectTarget sendSql |
44 |
dbNow |
45 |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
46 |
quotesql |
47 |
testDsnForTables testDbServer |
48 |
); |
49 |
|
50 |
|
51 |
use Carp; |
52 |
use DBI; |
53 |
use Data::Dumper; |
54 |
|
55 |
use constant SQL_INSERT => 10; |
56 |
use constant SQL_UPDATE => 11; |
57 |
use constant SQL_SELECT => 12; |
58 |
|
59 |
my $dbmeta_ref_cache; |
60 |
|
61 |
sub testDsn { |
62 |
my $dsn = shift; |
63 |
my $result; |
64 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
65 |
PrintError => 0, |
66 |
} ) ) { |
67 |
$dbh->disconnect(); |
68 |
return 1; |
69 |
} |
70 |
} |
71 |
|
72 |
sub testDbServer { |
73 |
my $dsn = shift; |
74 |
$dsn =~ s/database=(\w+)//; |
75 |
|
76 |
#print "testDbServer: $dsn", "\n"; |
77 |
|
78 |
my $result; |
79 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
80 |
PrintError => 0, |
81 |
} ) ) { |
82 |
$dbh->disconnect(); |
83 |
return 1; |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
# TODO: handle usage of "$crit" in an abstract way somehow |
89 |
sub hash2Sql { |
90 |
|
91 |
my $table = shift; |
92 |
my $hash = shift; |
93 |
my $mode = shift; |
94 |
my $crit = shift; |
95 |
|
96 |
my $sql; |
97 |
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
98 |
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
99 |
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
100 |
|
101 |
if ($mode == SQL_SELECT) { |
102 |
$sql = "SELECT #fields# FROM $table WHERE $crit"; |
103 |
} elsif ($mode == SQL_INSERT) { |
104 |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
105 |
} elsif ($mode == SQL_UPDATE) { |
106 |
$sql = "UPDATE $table SET #fields-values# WHERE $crit"; |
107 |
} |
108 |
|
109 |
my (@fields, @values); |
110 |
foreach my $key (keys %{$hash}) { |
111 |
push @fields, $key; |
112 |
push @values, $hash->{$key}; |
113 |
} |
114 |
# quote each element |
115 |
map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; |
116 |
|
117 |
my $fields = join(', ', @fields); |
118 |
my $values = join(', ', @values); |
119 |
my $fields_values = ''; |
120 |
my $fc = 0; |
121 |
foreach (@fields) { |
122 |
$fields_values .= $_ . '=' . $values[$fc] . ', '; |
123 |
$fc++; |
124 |
} |
125 |
$fields_values = substr($fields_values, 0, -2); |
126 |
|
127 |
$sql =~ s/#fields#/$fields/; |
128 |
$sql =~ s/#values#/$values/; |
129 |
$sql =~ s/#fields-values#/$fields_values/; |
130 |
|
131 |
return $sql; |
132 |
} |
133 |
|
134 |
sub patch_dbmeta { |
135 |
|
136 |
my $dbmeta = shift; |
137 |
|
138 |
if ($dbmeta =~ m/^dbi:/) { |
139 |
$dbmeta = { |
140 |
dsn => $dbmeta, |
141 |
trace_level => 0, |
142 |
trace_file => undef, |
143 |
}; |
144 |
} |
145 |
|
146 |
return $dbmeta; |
147 |
|
148 |
} |
149 |
|
150 |
sub connectTarget { |
151 |
my $dbmeta = shift; |
152 |
croak "please supply a dsn or a \"dbmeta\"-hash" if (!$dbmeta); |
153 |
$dbmeta = patch_dbmeta($dbmeta); |
154 |
if (!$dbmeta->{connected}) { |
155 |
if ($dbmeta->{dbh} = DBI->connect($dbmeta->{dsn}, '', '', { PrintError => 0 } )) { |
156 |
$dbmeta->{dbh}->trace($dbmeta->{trace_level}, $dbmeta->{trace_file}); |
157 |
$dbmeta->{dbh}->{PrintError} = 0; |
158 |
$dbmeta->{connected} = 1; |
159 |
$dbmeta_ref_cache = $dbmeta; |
160 |
return 1; |
161 |
} |
162 |
} |
163 |
} |
164 |
|
165 |
sub disconnectTarget { |
166 |
#my $dbmeta = shift; |
167 |
#print "disc\n"; |
168 |
#croak "please supply a \"dbmeta\"-hash" if (!$dbmeta); |
169 |
#$dbmeta = patch_dbmeta($dbmeta); |
170 |
#$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
171 |
my $dbmeta = $dbmeta_ref_cache; |
172 |
#print Dumper($dbmeta); |
173 |
#$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
174 |
$dbmeta->{dbh}->disconnect(); |
175 |
undef($dbmeta->{dbh}); |
176 |
undef($dbmeta); |
177 |
undef($dbmeta_ref_cache); |
178 |
} |
179 |
|
180 |
sub sendSql { |
181 |
my $sql = shift; |
182 |
my $dbmeta_ref = shift; |
183 |
if (!$dbmeta_ref) { |
184 |
$dbmeta_ref = $dbmeta_ref_cache; |
185 |
} |
186 |
#print "sql: $sql", "\n"; |
187 |
if (!$dbmeta_ref->{connected}) { |
188 |
print "not connected!", "\n"; |
189 |
return 0; |
190 |
} |
191 |
if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { |
192 |
#if ($result->execute()) { |
193 |
$result->execute(); |
194 |
return $result; |
195 |
#} |
196 |
} |
197 |
} |
198 |
|
199 |
sub dbNow { |
200 |
#my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
201 |
my $now_string = strftime("%Y-%m-%d %H:%M:%S", localtime); |
202 |
return $now_string; |
203 |
} |
204 |
|
205 |
|
206 |
sub getDbNameByDsn { |
207 |
my $dsn = shift; |
208 |
$dsn =~ m/database=(.+?);/; |
209 |
my $database_name = $1; |
210 |
return $database_name; |
211 |
} |
212 |
|
213 |
sub sqlDbAction { |
214 |
my $dsn = shift; |
215 |
$dsn =~ s/database=.+?;//; |
216 |
my $sql = shift; |
217 |
my $dbmeta = { |
218 |
dsn => $dsn, |
219 |
trace_level => 1, |
220 |
trace_file => 'dbitrace', |
221 |
}; |
222 |
my $bool_ok; |
223 |
if (connectTarget($dbmeta)) { |
224 |
sendSql($sql); |
225 |
#print "state: ", $dbmeta->{dbh}->state, "\n"; |
226 |
#print "err ", $dbmeta->{dbh}->err, "\n"; |
227 |
#$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err); |
228 |
$bool_ok = ($dbmeta->{dbh} && !$dbmeta->{dbh}->err); |
229 |
disconnectTarget($dbmeta); |
230 |
} |
231 |
return $bool_ok; |
232 |
} |
233 |
|
234 |
sub createSqlDb { |
235 |
my $dsn = shift; |
236 |
my $dbname = getDbNameByDsn($dsn); |
237 |
print " - creating rdbms-database $dbname ($dsn) ..."; |
238 |
my $sql; |
239 |
$sql = "CREATE DATABASE $dbname;"; |
240 |
if ( sqlDbAction($dsn, $sql) ) { |
241 |
print "ok", "\n"; |
242 |
return 1; |
243 |
} else { |
244 |
print "failed", "\n"; |
245 |
return 0; |
246 |
} |
247 |
} |
248 |
|
249 |
sub dropSqlDb { |
250 |
my $dsn = shift; |
251 |
my $dbname = getDbNameByDsn($dsn); |
252 |
print " - dropping database $dbname", "\n"; |
253 |
my $sql; |
254 |
$sql = "DROP DATABASE $dbname;"; |
255 |
sqlDbAction($dsn, $sql); |
256 |
} |
257 |
|
258 |
sub quotesql { |
259 |
my $string = shift; |
260 |
if ($string) { |
261 |
$string =~ s/'/\\'/g; |
262 |
} |
263 |
return $string; |
264 |
} |
265 |
|
266 |
sub testDsnForTables { |
267 |
my $dsn = shift; |
268 |
connectTarget($dsn); |
269 |
my $result = sendSql('SHOW TABLES;'); |
270 |
my $bool_tablesHere = $result->fetchrow_hashref(); |
271 |
$result->finish(); |
272 |
disconnectTarget($dsn); |
273 |
return 1 if ($bool_tablesHere); |
274 |
} |
275 |
|
276 |
1; |