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