2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## -------------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
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 |
## Revision 1.2 2002/07/20 11:09:58 cvsjoko |
18 |
## + bugfixes |
## + bugfixes |
19 |
## + dont' print sql-errors |
## + dont' print sql-errors |
29 |
use strict; |
use strict; |
30 |
use warnings; |
use warnings; |
31 |
|
|
|
use libp; |
|
|
use DBI; |
|
|
|
|
32 |
require Exporter; |
require Exporter; |
33 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
34 |
our @EXPORT = qw( |
our @EXPORT_OK = qw( |
35 |
testDsn hash2Sql |
testDsn hash2Sql |
36 |
SQL_INSERT SQL_UPDATE |
SQL_INSERT SQL_UPDATE |
37 |
connectTarget disconnectTarget sendSql |
connectTarget disconnectTarget sendSql |
38 |
dbNow |
dbNow |
39 |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
40 |
quotesql |
quotesql |
41 |
testDsnForTables |
testDsnForTables testDbServer |
42 |
); |
); |
43 |
|
|
44 |
|
|
45 |
|
use libp qw( croak ); |
46 |
|
use DBI; |
47 |
|
|
48 |
use constant SQL_INSERT => 10; |
use constant SQL_INSERT => 10; |
49 |
use constant SQL_UPDATE => 11; |
use constant SQL_UPDATE => 11; |
50 |
|
use constant SQL_SELECT => 12; |
51 |
|
|
52 |
my $dbmeta_ref_cache; |
my $dbmeta_ref_cache; |
53 |
|
|
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 { |
sub hash2Sql { |
83 |
|
|
84 |
my $table = shift; |
my $table = shift; |
87 |
my $crit = shift; |
my $crit = shift; |
88 |
|
|
89 |
my $sql; |
my $sql; |
90 |
if ($mode == SQL_INSERT) { |
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
91 |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; |
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
92 |
} |
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
93 |
if ($mode == SQL_UPDATE) { |
|
94 |
$sql = "UPDATE $table SET #fields-values# WHERE $crit;"; |
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); |
my (@fields, @values); |
177 |
return 0; |
return 0; |
178 |
} |
} |
179 |
if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { |
if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { |
180 |
if ($result->execute()) { |
#if ($result->execute()) { |
181 |
|
$result->execute(); |
182 |
return $result; |
return $result; |
183 |
} |
#} |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
210 |
my $bool_ok; |
my $bool_ok; |
211 |
if (connectTarget($dbmeta)) { |
if (connectTarget($dbmeta)) { |
212 |
sendSql($sql); |
sendSql($sql); |
213 |
#print "state: ", $dbmeta->{dbh}->errstr, "\n"; |
#print "state: ", $dbmeta->{dbh}->state, "\n"; |
214 |
$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err); |
#print "err ", $dbmeta->{dbh}->err, "\n"; |
215 |
|
#$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err); |
216 |
|
$bool_ok = ($dbmeta->{dbh} && !$dbmeta->{dbh}->err); |
217 |
disconnectTarget($dbmeta); |
disconnectTarget($dbmeta); |
218 |
} |
} |
219 |
return $bool_ok; |
return $bool_ok; |
226 |
my $sql; |
my $sql; |
227 |
$sql = "CREATE DATABASE $dbname;"; |
$sql = "CREATE DATABASE $dbname;"; |
228 |
if ( sqlDbAction($dsn, $sql) ) { |
if ( sqlDbAction($dsn, $sql) ) { |
229 |
print "ok"; |
print "ok", "\n"; |
230 |
|
return 1; |
231 |
} else { |
} else { |
232 |
print "failed"; |
print "failed", "\n"; |
233 |
|
return 0; |
234 |
} |
} |
|
print "\n"; |
|
235 |
} |
} |
236 |
|
|
237 |
sub dropSqlDb { |
sub dropSqlDb { |
245 |
|
|
246 |
sub quotesql { |
sub quotesql { |
247 |
my $string = shift; |
my $string = shift; |
248 |
$string =~ s/'/\\'/g; |
if ($string) { |
249 |
|
$string =~ s/'/\\'/g; |
250 |
|
} |
251 |
return $string; |
return $string; |
252 |
} |
} |
253 |
|
|
261 |
return 1 if ($bool_tablesHere); |
return 1 if ($bool_tablesHere); |
262 |
} |
} |
263 |
|
|
|
1; |
|
264 |
|
1; |