/[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.3 by cvsjoko, Sat Jul 27 00:28:20 2002 UTC revision 1.6 by joko, Fri Nov 29 04:53:39 2002 UTC
# Line 2  Line 2 
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  ##    Revision 1.3  2002/07/27 00:28:20  cvsjoko
15  ##    bugfixes  ##    bugfixes
16  ##  ##
# Line 20  package libdb; Line 29  package libdb;
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    
# Line 51  sub testDsn { Line 62  sub testDsn {
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;
# Line 59  sub hash2Sql { Line 87  sub hash2Sql {
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);
# Line 144  sub sendSql { Line 177  sub sendSql {
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    
# Line 211  sub dropSqlDb { Line 245  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    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.6

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