/[cvs]/nfo/perl/libs/libdb.pm
ViewVC logotype

Diff of /nfo/perl/libs/libdb.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by cvsjoko, Sat Jul 20 11:09:58 2002 UTC revision 1.4 by joko, Wed Oct 16 22:36:42 2002 UTC
# Line 2  Line 2 
2  ##    $Id$  ##    $Id$
3  ##    --------------------------------------------------------------------------------  ##    --------------------------------------------------------------------------------
4  ##    $Log$  ##    $Log$
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  ##    Revision 1.2  2002/07/20 11:09:58  cvsjoko
12  ##    + bugfixes  ##    + bugfixes
13  ##    + dont' print sql-errors  ##    + dont' print sql-errors
# Line 29  our @EXPORT = qw( Line 35  our @EXPORT = qw(
35                                dbNow                                dbNow
36                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb
37                                quotesql                                quotesql
38                                testDsnForTables                                testDsnForTables testDbServer
39                                );                                );
40    
41  use constant SQL_INSERT => 10;  use constant SQL_INSERT => 10;
# Line 48  sub testDsn { Line 54  sub testDsn {
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 {  sub hash2Sql {
73        
74    my $table = shift;    my $table = shift;
# Line 56  sub hash2Sql { Line 77  sub hash2Sql {
77    my $crit = shift;    my $crit = shift;
78        
79    my $sql;    my $sql;
80    if ($mode == SQL_INSERT) {    if ($mode == SQL_INSERT || $mode eq 'SQL_INSERT') {
81      $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";      $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";
82    }    }
83    if ($mode == SQL_UPDATE) {    if ($mode == SQL_UPDATE || $mode eq 'SQL_UPDATE') {
84      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";
85    }    }
86        
# Line 141  sub sendSql { Line 162  sub sendSql {
162      return 0;      return 0;
163    }    }
164    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {
165      if ($result->execute()) {      #if ($result->execute()) {
166          $result->execute();
167        return $result;        return $result;
168      }      #}
169    }    }
170  }  }
171    
# Line 173  sub sqlDbAction { Line 195  sub sqlDbAction {
195    my $bool_ok;    my $bool_ok;
196    if (connectTarget($dbmeta)) {    if (connectTarget($dbmeta)) {
197      sendSql($sql);      sendSql($sql);
198      #print "state: ", $dbmeta->{dbh}->errstr, "\n";      #print "state: ", $dbmeta->{dbh}->state, "\n";
199      $bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err);      #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);      disconnectTarget($dbmeta);
203    }    }
204    return $bool_ok;    return $bool_ok;
# Line 187  sub createSqlDb { Line 211  sub createSqlDb {
211    my $sql;    my $sql;
212    $sql = "CREATE DATABASE $dbname;";    $sql = "CREATE DATABASE $dbname;";
213    if ( sqlDbAction($dsn, $sql) ) {    if ( sqlDbAction($dsn, $sql) ) {
214      print "ok";      print "ok", "\n";
215        return 1;
216    } else {    } else {
217      print "failed";      print "failed", "\n";
218        return 0;
219    }    }
   print "\n";  
220  }  }
221    
222  sub dropSqlDb {  sub dropSqlDb {
# Line 205  sub dropSqlDb { Line 230  sub dropSqlDb {
230    
231  sub quotesql {  sub quotesql {
232    my $string = shift;    my $string = shift;
233    $string =~ s/'/\\'/g;    if ($string) {
234        $string =~ s/'/\\'/g;
235      }
236    return $string;    return $string;
237  }  }
238    
# Line 219  sub testDsnForTables { Line 246  sub testDsnForTables {
246    return 1 if ($bool_tablesHere);    return 1 if ($bool_tablesHere);
247  }  }
248    
 1;  
249    1;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.4

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed