/[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.7 by joko, Sun Dec 1 22:13:17 2002 UTC
# Line 2  Line 2 
2  ##    $Id$  ##    $Id$
3  ##    --------------------------------------------------------------------------------  ##    --------------------------------------------------------------------------------
4  ##    $Log$  ##    $Log$
5    ##    Revision 1.7  2002/12/01 22:13:17  joko
6    ##    + minor bugfix?
7    ##
8    ##    Revision 1.6  2002/11/29 04:53:39  joko
9    ##    + hash2Sql now knows about SQL_SELECT
10    ##
11    ##    Revision 1.5  2002/11/17 07:18:38  joko
12    ##    + small modification in hash2sql
13    ##
14    ##    Revision 1.4  2002/10/16 22:36:42  joko
15    ##    + sub testDbServer
16    ##
17  ##    Revision 1.3  2002/07/27 00:28:20  cvsjoko  ##    Revision 1.3  2002/07/27 00:28:20  cvsjoko
18  ##    bugfixes  ##    bugfixes
19  ##  ##
# Line 20  package libdb; Line 32  package libdb;
32  use strict;  use strict;
33  use warnings;  use warnings;
34    
 use libp;  
 use DBI;  
   
35  require Exporter;  require Exporter;
36  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
37  our @EXPORT = qw(  our @EXPORT_OK = qw(
38                                testDsn hash2Sql                                testDsn hash2Sql
39                                SQL_INSERT SQL_UPDATE                                SQL_INSERT SQL_UPDATE
40                                connectTarget disconnectTarget sendSql                                connectTarget disconnectTarget sendSql
41                                dbNow                                dbNow
42                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb                                getDbNameByDsn sqlDbAction createSqlDb dropSqlDb
43                                quotesql                                quotesql
44                                testDsnForTables                                testDsnForTables testDbServer
45                                );                                );
46    
47    
48    use libp qw( croak );
49    use DBI;
50    
51  use constant SQL_INSERT => 10;  use constant SQL_INSERT => 10;
52  use constant SQL_UPDATE => 11;  use constant SQL_UPDATE => 11;
53    use constant SQL_SELECT => 12;
54    
55  my $dbmeta_ref_cache;  my $dbmeta_ref_cache;
56    
# Line 51  sub testDsn { Line 65  sub testDsn {
65    }    }
66  }  }
67    
68    sub testDbServer {
69      my $dsn = shift;
70      $dsn =~ s/database=(\w+)//;
71      
72      #print "testDbServer: $dsn", "\n";
73      
74      my $result;
75      if ( my $dbh = DBI->connect($dsn, '', '', {
76                                                          PrintError => 0,
77                                                          } ) ) {
78        $dbh->disconnect();
79        return 1;
80      }
81    }
82    
83    
84    # TODO: handle usage of "$crit" in an abstract way somehow
85  sub hash2Sql {  sub hash2Sql {
86        
87    my $table = shift;    my $table = shift;
# Line 59  sub hash2Sql { Line 90  sub hash2Sql {
90    my $crit = shift;    my $crit = shift;
91        
92    my $sql;    my $sql;
93    if ($mode == SQL_INSERT) {    $mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT');
94      $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";    $mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT');
95    }    $mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE');
96    if ($mode == SQL_UPDATE) {  
97      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";    if ($mode == SQL_SELECT) {
98        $sql = "SELECT #fields# FROM $table WHERE $crit";
99      } elsif ($mode == SQL_INSERT) {
100        $sql = "INSERT INTO $table (#fields#) VALUES (#values#)";
101      } elsif ($mode == SQL_UPDATE) {
102        $sql = "UPDATE $table SET #fields-values# WHERE $crit";
103    }    }
104        
105    my (@fields, @values);    my (@fields, @values);
# Line 129  sub disconnectTarget { Line 165  sub disconnectTarget {
165    #$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();    #$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();
166    my $dbmeta = $dbmeta_ref_cache;    my $dbmeta = $dbmeta_ref_cache;
167    $dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();    $dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();
168    undef($dbmeta_ref_cache);    undef($dbmeta->{dbh});
169      #undef($dbmeta_ref_cache);
170  }  }
171    
172  sub sendSql {  sub sendSql {
# Line 144  sub sendSql { Line 181  sub sendSql {
181      return 0;      return 0;
182    }    }
183    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {    if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {
184      if ($result->execute()) {      #if ($result->execute()) {
185          $result->execute();
186        return $result;        return $result;
187      }      #}
188    }    }
189  }  }
190    
# Line 211  sub dropSqlDb { Line 249  sub dropSqlDb {
249    
250  sub quotesql {  sub quotesql {
251    my $string = shift;    my $string = shift;
252    $string =~ s/'/\\'/g;    if ($string) {
253        $string =~ s/'/\\'/g;
254      }
255    return $string;    return $string;
256  }  }
257    

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

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