2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## -------------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
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 |
## Revision 1.2 2002/07/20 11:09:58 cvsjoko |
21 |
## + bugfixes |
## + bugfixes |
22 |
## + dont' print sql-errors |
## + dont' print sql-errors |
32 |
use strict; |
use strict; |
33 |
use warnings; |
use warnings; |
34 |
|
|
|
use libp; |
|
|
use DBI; |
|
|
|
|
35 |
require Exporter; |
require Exporter; |
36 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
37 |
our @EXPORT = qw( |
our @EXPORT_OK = qw( |
38 |
testDsn hash2Sql |
testDsn hash2Sql |
39 |
SQL_INSERT SQL_UPDATE |
SQL_INSERT SQL_UPDATE |
40 |
connectTarget disconnectTarget sendSql |
connectTarget disconnectTarget sendSql |
41 |
dbNow |
dbNow |
42 |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
43 |
quotesql |
quotesql |
44 |
testDsnForTables |
testDsnForTables testDbServer |
45 |
); |
); |
46 |
|
|
47 |
|
|
48 |
|
use libp qw( croak ); |
49 |
|
use DBI; |
50 |
|
|
51 |
use constant SQL_INSERT => 10; |
use constant SQL_INSERT => 10; |
52 |
use constant SQL_UPDATE => 11; |
use constant SQL_UPDATE => 11; |
53 |
|
use constant SQL_SELECT => 12; |
54 |
|
|
55 |
my $dbmeta_ref_cache; |
my $dbmeta_ref_cache; |
56 |
|
|
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
|
sub testDbServer { |
69 |
|
my $dsn = shift; |
70 |
|
$dsn =~ s/database=(\w+)//; |
71 |
|
|
72 |
|
#print "testDbServer: $dsn", "\n"; |
73 |
|
|
74 |
|
my $result; |
75 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
76 |
|
PrintError => 0, |
77 |
|
} ) ) { |
78 |
|
$dbh->disconnect(); |
79 |
|
return 1; |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
# TODO: handle usage of "$crit" in an abstract way somehow |
85 |
sub hash2Sql { |
sub hash2Sql { |
86 |
|
|
87 |
my $table = shift; |
my $table = shift; |
90 |
my $crit = shift; |
my $crit = shift; |
91 |
|
|
92 |
my $sql; |
my $sql; |
93 |
if ($mode == SQL_INSERT) { |
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
94 |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; |
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
95 |
} |
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
96 |
if ($mode == SQL_UPDATE) { |
|
97 |
$sql = "UPDATE $table SET #fields-values# WHERE $crit;"; |
if ($mode == SQL_SELECT) { |
98 |
|
$sql = "SELECT #fields# FROM $table WHERE $crit"; |
99 |
|
} elsif ($mode == SQL_INSERT) { |
100 |
|
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
101 |
|
} elsif ($mode == SQL_UPDATE) { |
102 |
|
$sql = "UPDATE $table SET #fields-values# WHERE $crit"; |
103 |
} |
} |
104 |
|
|
105 |
my (@fields, @values); |
my (@fields, @values); |
165 |
#$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
#$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
166 |
my $dbmeta = $dbmeta_ref_cache; |
my $dbmeta = $dbmeta_ref_cache; |
167 |
$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); |
168 |
undef($dbmeta_ref_cache); |
undef($dbmeta->{dbh}); |
169 |
|
#undef($dbmeta_ref_cache); |
170 |
} |
} |
171 |
|
|
172 |
sub sendSql { |
sub sendSql { |
181 |
return 0; |
return 0; |
182 |
} |
} |
183 |
if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { |
if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { |
184 |
if ($result->execute()) { |
#if ($result->execute()) { |
185 |
|
$result->execute(); |
186 |
return $result; |
return $result; |
187 |
} |
#} |
188 |
} |
} |
189 |
} |
} |
190 |
|
|
214 |
my $bool_ok; |
my $bool_ok; |
215 |
if (connectTarget($dbmeta)) { |
if (connectTarget($dbmeta)) { |
216 |
sendSql($sql); |
sendSql($sql); |
217 |
#print "state: ", $dbmeta->{dbh}->errstr, "\n"; |
#print "state: ", $dbmeta->{dbh}->state, "\n"; |
218 |
$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err); |
#print "err ", $dbmeta->{dbh}->err, "\n"; |
219 |
|
#$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err); |
220 |
|
$bool_ok = ($dbmeta->{dbh} && !$dbmeta->{dbh}->err); |
221 |
disconnectTarget($dbmeta); |
disconnectTarget($dbmeta); |
222 |
} |
} |
223 |
return $bool_ok; |
return $bool_ok; |
230 |
my $sql; |
my $sql; |
231 |
$sql = "CREATE DATABASE $dbname;"; |
$sql = "CREATE DATABASE $dbname;"; |
232 |
if ( sqlDbAction($dsn, $sql) ) { |
if ( sqlDbAction($dsn, $sql) ) { |
233 |
print "ok"; |
print "ok", "\n"; |
234 |
|
return 1; |
235 |
} else { |
} else { |
236 |
print "failed"; |
print "failed", "\n"; |
237 |
|
return 0; |
238 |
} |
} |
|
print "\n"; |
|
239 |
} |
} |
240 |
|
|
241 |
sub dropSqlDb { |
sub dropSqlDb { |
249 |
|
|
250 |
sub quotesql { |
sub quotesql { |
251 |
my $string = shift; |
my $string = shift; |
252 |
$string =~ s/'/\\'/g; |
if ($string) { |
253 |
|
$string =~ s/'/\\'/g; |
254 |
|
} |
255 |
return $string; |
return $string; |
256 |
} |
} |
257 |
|
|
265 |
return 1 if ($bool_tablesHere); |
return 1 if ($bool_tablesHere); |
266 |
} |
} |
267 |
|
|
|
1; |
|
268 |
|
1; |