/[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.5 by joko, Sun Nov 17 07:18:38 2002 UTC
# Line 2  Line 2 
2  ##    $Id$  ##    $Id$
3  ##    --------------------------------------------------------------------------------  ##    --------------------------------------------------------------------------------
4  ##    $Log$  ##    $Log$
5    ##    Revision 1.5  2002/11/17 07:18:38  joko
6    ##    + small modification in hash2sql
7    ##
8    ##    Revision 1.4  2002/10/16 22:36:42  joko
9    ##    + sub testDbServer
10    ##
11    ##    Revision 1.3  2002/07/27 00:28:20  cvsjoko
12    ##    bugfixes
13    ##
14  ##    Revision 1.2  2002/07/20 11:09:58  cvsjoko  ##    Revision 1.2  2002/07/20 11:09:58  cvsjoko
15  ##    + bugfixes  ##    + bugfixes
16  ##    + dont' print sql-errors  ##    + dont' print sql-errors
# Line 29  our @EXPORT = qw( Line 38  our @EXPORT = qw(
38                                dbNow                                dbNow
39                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb
40                                quotesql                                quotesql
41                                testDsnForTables                                testDsnForTables testDbServer
42                                );                                );
43    
44  use constant SQL_INSERT => 10;  use constant SQL_INSERT => 10;
# Line 48  sub testDsn { Line 57  sub testDsn {
57    }    }
58  }  }
59    
60    sub testDbServer {
61      my $dsn = shift;
62      $dsn =~ s/database=(\w+)//;
63      
64      #print "testDbServer: $dsn", "\n";
65      
66      my $result;
67      if ( my $dbh = DBI->connect($dsn, '', '', {
68                                                          PrintError => 0,
69                                                          } ) ) {
70        $dbh->disconnect();
71        return 1;
72      }
73    }
74    
75  sub hash2Sql {  sub hash2Sql {
76        
77    my $table = shift;    my $table = shift;
# Line 56  sub hash2Sql { Line 80  sub hash2Sql {
80    my $crit = shift;    my $crit = shift;
81        
82    my $sql;    my $sql;
83    if ($mode == SQL_INSERT) {    if ($mode eq 'SQL_INSERT' || $mode == SQL_INSERT) {
84      $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";      $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";
85    }    }
86    if ($mode == SQL_UPDATE) {    if ($mode eq 'SQL_UPDATE' || $mode == SQL_UPDATE) {
87      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";
88    }    }
89        
# Line 141  sub sendSql { Line 165  sub sendSql {
165      return 0;      return 0;
166    }    }
167    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {
168      if ($result->execute()) {      #if ($result->execute()) {
169          $result->execute();
170        return $result;        return $result;
171      }      #}
172    }    }
173  }  }
174    
# Line 173  sub sqlDbAction { Line 198  sub sqlDbAction {
198    my $bool_ok;    my $bool_ok;
199    if (connectTarget($dbmeta)) {    if (connectTarget($dbmeta)) {
200      sendSql($sql);      sendSql($sql);
201      #print "state: ", $dbmeta->{dbh}->errstr, "\n";      #print "state: ", $dbmeta->{dbh}->state, "\n";
202      $bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err);      #print "err    ", $dbmeta->{dbh}->err, "\n";
203        #$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err);
204        $bool_ok = ($dbmeta->{dbh} && !$dbmeta->{dbh}->err);
205      disconnectTarget($dbmeta);      disconnectTarget($dbmeta);
206    }    }
207    return $bool_ok;    return $bool_ok;
# Line 187  sub createSqlDb { Line 214  sub createSqlDb {
214    my $sql;    my $sql;
215    $sql = "CREATE DATABASE $dbname;";    $sql = "CREATE DATABASE $dbname;";
216    if ( sqlDbAction($dsn, $sql) ) {    if ( sqlDbAction($dsn, $sql) ) {
217      print "ok";      print "ok", "\n";
218        return 1;
219    } else {    } else {
220      print "failed";      print "failed", "\n";
221        return 0;
222    }    }
   print "\n";  
223  }  }
224    
225  sub dropSqlDb {  sub dropSqlDb {
# Line 205  sub dropSqlDb { Line 233  sub dropSqlDb {
233    
234  sub quotesql {  sub quotesql {
235    my $string = shift;    my $string = shift;
236    $string =~ s/'/\\'/g;    if ($string) {
237        $string =~ s/'/\\'/g;
238      }
239    return $string;    return $string;
240  }  }
241    
# Line 219  sub testDsnForTables { Line 249  sub testDsnForTables {
249    return 1 if ($bool_tablesHere);    return 1 if ($bool_tablesHere);
250  }  }
251    
 1;  
252    1;

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

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