2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## -------------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.10 2003/04/08 23:07:57 joko |
6 |
|
## renamed core database helper functions |
7 |
|
## moved 'hash2Sql' and 'quotesql' to shortcuts::db |
8 |
|
## |
9 |
## Revision 1.9 2003/02/09 04:47:58 joko |
## Revision 1.9 2003/02/09 04:47:58 joko |
10 |
## + minor fix |
## + minor fix |
11 |
## |
## |
45 |
require Exporter; |
require Exporter; |
46 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
47 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
48 |
testDsn hash2Sql |
testDsn |
|
SQL_INSERT SQL_UPDATE |
|
49 |
connectTarget disconnectTarget sendSql |
connectTarget disconnectTarget sendSql |
50 |
dbNow |
dbNow |
51 |
getDbNameByDsn sqlDbAction createSqlDb dropSqlDb |
dsn2dbname sqlDbAction createSqlDb dropSqlDb |
|
quotesql |
|
52 |
testDsnForTables testDbServer |
testDsnForTables testDbServer |
53 |
); |
); |
54 |
|
|
57 |
use DBI; |
use DBI; |
58 |
use Data::Dumper; |
use Data::Dumper; |
59 |
|
|
60 |
use constant SQL_INSERT => 10; |
use shortcuts::db qw( dsn2dbname ); |
|
use constant SQL_UPDATE => 11; |
|
|
use constant SQL_SELECT => 12; |
|
61 |
|
|
62 |
my $dbmeta_ref_cache; |
my $dbmeta_ref_cache; |
63 |
|
|
88 |
} |
} |
89 |
|
|
90 |
|
|
|
# TODO: handle usage of "$crit" in an abstract way somehow |
|
|
sub hash2Sql { |
|
|
|
|
|
my $table = shift; |
|
|
my $hash = shift; |
|
|
my $mode = shift; |
|
|
my $crit = shift; |
|
|
|
|
|
my $sql; |
|
|
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
|
|
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
|
|
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
|
|
|
|
|
if ($mode == SQL_SELECT) { |
|
|
$sql = "SELECT #fields# FROM $table WHERE $crit"; |
|
|
} elsif ($mode == SQL_INSERT) { |
|
|
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
|
|
} elsif ($mode == SQL_UPDATE) { |
|
|
$sql = "UPDATE $table SET #fields-values# WHERE $crit"; |
|
|
} |
|
|
|
|
|
my (@fields, @values); |
|
|
foreach my $key (keys %{$hash}) { |
|
|
push @fields, $key; |
|
|
push @values, $hash->{$key}; |
|
|
} |
|
|
# quote each element |
|
|
map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; |
|
|
|
|
|
my $fields = join(', ', @fields); |
|
|
my $values = join(', ', @values); |
|
|
my $fields_values = ''; |
|
|
my $fc = 0; |
|
|
foreach (@fields) { |
|
|
$fields_values .= $_ . '=' . $values[$fc] . ', '; |
|
|
$fc++; |
|
|
} |
|
|
$fields_values = substr($fields_values, 0, -2); |
|
|
|
|
|
$sql =~ s/#fields#/$fields/; |
|
|
$sql =~ s/#values#/$values/; |
|
|
$sql =~ s/#fields-values#/$fields_values/; |
|
|
|
|
|
return $sql; |
|
|
} |
|
91 |
|
|
92 |
sub patch_dbmeta { |
sub patch_dbmeta { |
93 |
|
|
161 |
} |
} |
162 |
|
|
163 |
|
|
|
sub getDbNameByDsn { |
|
|
my $dsn = shift; |
|
|
$dsn =~ m/database=(.+?);/; |
|
|
my $database_name = $1; |
|
|
return $database_name; |
|
|
} |
|
|
|
|
164 |
sub sqlDbAction { |
sub sqlDbAction { |
165 |
my $dsn = shift; |
my $dsn = shift; |
166 |
$dsn =~ s/database=.+?;//; |
$dsn =~ s/database=.+?;//; |
184 |
|
|
185 |
sub createSqlDb { |
sub createSqlDb { |
186 |
my $dsn = shift; |
my $dsn = shift; |
187 |
my $dbname = getDbNameByDsn($dsn); |
my $dbname = dsn2dbname($dsn); |
188 |
print " - creating rdbms-database $dbname ($dsn) ..."; |
print " - creating rdbms-database $dbname ($dsn) ..."; |
189 |
my $sql; |
my $sql; |
190 |
$sql = "CREATE DATABASE $dbname;"; |
$sql = "CREATE DATABASE $dbname;"; |
199 |
|
|
200 |
sub dropSqlDb { |
sub dropSqlDb { |
201 |
my $dsn = shift; |
my $dsn = shift; |
202 |
my $dbname = getDbNameByDsn($dsn); |
my $dbname = dsn2dbname($dsn); |
203 |
print " - dropping database $dbname", "\n"; |
print " - dropping database $dbname", "\n"; |
204 |
my $sql; |
my $sql; |
205 |
$sql = "DROP DATABASE $dbname;"; |
$sql = "DROP DATABASE $dbname;"; |
206 |
sqlDbAction($dsn, $sql); |
sqlDbAction($dsn, $sql); |
207 |
} |
} |
208 |
|
|
|
sub quotesql { |
|
|
my $string = shift; |
|
|
if ($string) { |
|
|
$string =~ s/'/\\'/g; |
|
|
} |
|
|
return $string; |
|
|
} |
|
|
|
|
209 |
sub testDsnForTables { |
sub testDsnForTables { |
210 |
my $dsn = shift; |
my $dsn = shift; |
211 |
connectTarget($dsn); |
connectTarget($dsn); |