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