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