/[cvs]/nfo/perl/libs/Data/Storage/Handler/DBI.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/DBI.pm

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

revision 1.2 by joko, Fri Oct 25 11:43:27 2002 UTC revision 1.6 by joko, Sun Dec 1 04:46:01 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.6  2002/12/01 04:46:01  joko
7    #  + sub eraseAll
8    #
9    #  Revision 1.5  2002/11/29 05:00:26  joko
10    #  + sub getListUnfiltered
11    #  + sub sendQuery
12    #
13    #  Revision 1.4  2002/11/17 08:46:42  jonen
14    #  + wrapped eval around DBI->connect to prevent deaths
15    #
16    #  Revision 1.3  2002/11/17 06:34:39  joko
17    #  + locator metadata can now be reached via ->{locator}
18    #  - sub hash2sql now taken from libdb
19    #
20  #  Revision 1.2  2002/10/25 11:43:27  joko  #  Revision 1.2  2002/10/25 11:43:27  joko
21  #  + enhanced robustness  #  + enhanced robustness
22  #  + more logging for debug-levels  #  + more logging for debug-levels
# Line 21  use warnings; Line 35  use warnings;
35  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
36    
37  use DBI;  use DBI;
38    use Data::Dumper;
39    use libdb qw( getDbNameByDsn hash2Sql );
40    use Data::Storage::Result::DBI;
41    
42  # get logger instance  # get logger instance
43  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
44    
45    
46  our $metainfo = {  sub getMetaInfo {
47    'disconnectMethod' => 'disconnect',    my $self = shift;
48  };    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
49      return {
50        'disconnectMethod' => 'disconnect',
51      };
52    }
53    
54  sub connect {  sub connect {
55    
56      my $self = shift;      my $self = shift;
57            
58      # create handle      # create handle
59        if ( my $dsn = $self->{dbi}->{dsn} ) {        if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
60          #if ( my $dsn = $self->{locator}->{dsn} ) {
61          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
62                    
63          # HACK:          # HACK:
# Line 45  sub connect { Line 67  sub connect {
67                    
68          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
69                    
70          $self->{COREHANDLE} = DBI->connect(          eval {
71            $dsn, '', '', {            $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
72              RaiseError => $self->{dbi}->{RaiseError},            if (!$self->{COREHANDLE}) {
73              #RaiseError => 1,              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
74              PrintError => $self->{dbi}->{PrintError},              return;
             HandleError => $self->{dbi}->{HandleError},  
75            }            }
76          );          };
77          if (!$self->{COREHANDLE}) {          $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
78            $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );  
           return;  
         }  
79        }        }
80        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
81        
82        $self->{locator}->{status}->{connected} = 1;
83    
84      return 1;      return 1;
85            
86  }  }
# Line 68  sub configureCOREHANDLE { Line 89  sub configureCOREHANDLE {
89    
90    my $self = shift;    my $self = shift;
91    
92    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
93    
94    # apply configured modifications    # apply configured modifications to DBI-handle
95      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
96        $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
97      }      }
98      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
99        $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
100      }      }
101      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
102        $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError};        $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
103      }      }
104      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
105        $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError};        $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
106      }      }
107    
108  }  }
# Line 101  sub _sendSql { Line 122  sub _sendSql {
122      }      }
123    }    }
124        
125      #print "prepare sql: $sql\n";
126      
127    my $sth = $self->{COREHANDLE}->prepare($sql);    my $sth = $self->{COREHANDLE}->prepare($sql);
128    $sth->execute();    $sth->execute();
129    return $sth;    return $sth;
# Line 109  sub _sendSql { Line 132  sub _sendSql {
132  sub sendCommand {  sub sendCommand {
133    my $self = shift;    my $self = shift;
134    my $command = shift;    my $command = shift;
135      # TODO: when tracing: yes, do actually log this
136    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
137    my $cmdHandle = $self->_sendSql($command);    my $cmdHandle = $self->_sendSql($command);
138    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
139    return $result;    return $result;
140  }  }
141    
 sub quoteSql {  
   my $self = shift;  
   my $string = shift;  
   if ($string) {  
     $string =~ s/'/\\'/g;  
   }  
   return $string;  
 }  
   
 sub hash2Sql {  
   
   my $self = shift;  
     
   my $table = shift;  
   my $hash = shift;  
   my $mode = shift;  
   my $crit = shift;  
     
   my $sql;  
   if ($mode eq 'SQL_INSERT') {  
     $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";  
   }  
   if ($mode eq 'SQL_UPDATE') {  
     $sql = "UPDATE $table SET #fields-values# WHERE $crit;";  
   }  
     
   my (@fields, @values);  
   foreach my $key (keys %{$hash}) {  
     push @fields, $key;  
     push @values, $hash->{$key};  
   }  
   # quote each element  
   map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values;  
     
   my $fields = join(', ', @fields);  
   my $values = join(', ', @values);  
   my $fields_values = '';  
   my $fc = 0;  
   foreach (@fields) {  
     $fields_values .= $_ . '=' . $values[$fc] . ', ';  
     $fc++;  
   }  
   $fields_values = substr($fields_values, 0, -2);  
     
   $sql =~ s/#fields#/$fields/;  
   $sql =~ s/#values#/$values/;  
   $sql =~ s/#fields-values#/$fields_values/;  
     
   return $sql;  
 }  
   
   
142  sub getChildNodes {  sub getChildNodes {
   
143    my $self = shift;    my $self = shift;
144    my @nodes;    my @nodes;
145      $logger->debug( __PACKAGE__ . "->getChildNodes()" );
146    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
147      while ( my $row = $result->_getNextEntry() ) {      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
148        push @nodes, $row;      my $key = "Tables_in_$dbname";
149        while ( my $row = $result->getNextEntry() ) {
150          push @nodes, $row->{$key};
151      }      }
152    }    }
     
153    return \@nodes;    return \@nodes;
   
154  }  }
155    
156    sub getListUnfiltered {
   
   
 package Data::Storage::Result::DBI;  
   
 use strict;  
 use warnings;  
   
 use base ("Data::Storage::Result");  
   
 sub DESTROY {  
157    my $self = shift;    my $self = shift;
158    #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );    my $nodename = shift;
159    $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish();    my @list;
160      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
161      # get list of rows from rdbms by table name
162      my $result = $self->sendCommand("SELECT * FROM $nodename");
163      while ( my $row = $result->getNextEntry() ) {
164        push @list, $row;
165      }
166      return \@list;
167  }  }
168    
169  sub _getNextEntry {  sub sendQuery {
170    my $self = shift;    my $self = shift;
171    $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref;    my $query = shift;
172      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
173      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
174      my @crits;
175      foreach (@{$query->{criterias}}) {
176        my $op = '';
177        $op = '=' if lc $_->{op} eq 'eq';
178        push @crits, "$_->{key}$op'$_->{val}'";
179      }
180      my $subnodes = {};
181      map { $subnodes->{$_}++ } @{$query->{subnodes}};
182      # HACK: this is hardcoded ;(    expand possibilities!
183      my $crit = join(' AND ', @crits);
184      my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
185      return $self->sendCommand($sql);
186  }  }
187    
188    sub eraseAll {
189      my $self = shift;
190      my $classname = shift;
191      my $sql = "DELETE FROM $classname";
192      $self->sendCommand($sql);
193    }
194    
 1;  
195    1;

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

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