--- nfo/perl/libs/libdb.pm 2002/07/27 00:28:20 1.3 +++ nfo/perl/libs/libdb.pm 2002/12/01 22:13:17 1.7 @@ -1,7 +1,19 @@ ## -------------------------------------------------------------------------------- -## $Id: libdb.pm,v 1.3 2002/07/27 00:28:20 cvsjoko Exp $ +## $Id: libdb.pm,v 1.7 2002/12/01 22:13:17 joko Exp $ ## -------------------------------------------------------------------------------- ## $Log: libdb.pm,v $ +## Revision 1.7 2002/12/01 22:13:17 joko +## + minor bugfix? +## +## Revision 1.6 2002/11/29 04:53:39 joko +## + hash2Sql now knows about SQL_SELECT +## +## Revision 1.5 2002/11/17 07:18:38 joko +## + small modification in hash2sql +## +## Revision 1.4 2002/10/16 22:36:42 joko +## + sub testDbServer +## ## Revision 1.3 2002/07/27 00:28:20 cvsjoko ## bugfixes ## @@ -20,23 +32,25 @@ use strict; use warnings; -use libp; -use DBI; - require Exporter; our @ISA = qw( Exporter ); -our @EXPORT = qw( +our @EXPORT_OK = qw( testDsn hash2Sql SQL_INSERT SQL_UPDATE connectTarget disconnectTarget sendSql dbNow getDbNameByDsn sqlDbAction createSqlDb dropSqlDb quotesql - testDsnForTables + testDsnForTables testDbServer ); + +use libp qw( croak ); +use DBI; + use constant SQL_INSERT => 10; use constant SQL_UPDATE => 11; +use constant SQL_SELECT => 12; my $dbmeta_ref_cache; @@ -51,6 +65,23 @@ } } +sub testDbServer { + my $dsn = shift; + $dsn =~ s/database=(\w+)//; + + #print "testDbServer: $dsn", "\n"; + + my $result; + if ( my $dbh = DBI->connect($dsn, '', '', { + PrintError => 0, + } ) ) { + $dbh->disconnect(); + return 1; + } +} + + +# TODO: handle usage of "$crit" in an abstract way somehow sub hash2Sql { my $table = shift; @@ -59,11 +90,16 @@ my $crit = shift; my $sql; - if ($mode == SQL_INSERT) { - $sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; - } - if ($mode == SQL_UPDATE) { - $sql = "UPDATE $table SET #fields-values# WHERE $crit;"; + $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); @@ -129,7 +165,8 @@ #$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); my $dbmeta = $dbmeta_ref_cache; $dbmeta->{dbh} && $dbmeta->{dbh}->disconnect(); - undef($dbmeta_ref_cache); + undef($dbmeta->{dbh}); + #undef($dbmeta_ref_cache); } sub sendSql { @@ -144,9 +181,10 @@ return 0; } if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) { - if ($result->execute()) { + #if ($result->execute()) { + $result->execute(); return $result; - } + #} } } @@ -211,7 +249,9 @@ sub quotesql { my $string = shift; - $string =~ s/'/\\'/g; + if ($string) { + $string =~ s/'/\\'/g; + } return $string; }