/[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.1 by cvsjoko, Thu Oct 10 03:44:07 2002 UTC revision 1.5 by joko, Fri Nov 29 05:00:26 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.5  2002/11/29 05:00:26  joko
7    #  + sub getListUnfiltered
8    #  + sub sendQuery
9    #
10    #  Revision 1.4  2002/11/17 08:46:42  jonen
11    #  + wrapped eval around DBI->connect to prevent deaths
12    #
13    #  Revision 1.3  2002/11/17 06:34:39  joko
14    #  + locator metadata can now be reached via ->{locator}
15    #  - sub hash2sql now taken from libdb
16    #
17    #  Revision 1.2  2002/10/25 11:43:27  joko
18    #  + enhanced robustness
19    #  + more logging for debug-levels
20    #
21  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
22  #  + new  #  + new
23  #  #
# Line 17  use warnings; Line 32  use warnings;
32  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
33    
34  use DBI;  use DBI;
35    use Data::Dumper;
36    use libdb qw( getDbNameByDsn hash2Sql );
37    use Data::Storage::Result::DBI;
38    
39  # get logger instance  # get logger instance
40  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
41    
42    
43  our $metainfo = {  sub getMetaInfo {
44    'disconnectMethod' => 'disconnect',    my $self = shift;
45  };    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
46      return {
47        'disconnectMethod' => 'disconnect',
48      };
49    }
50    
51  sub connect {  sub connect {
52    
53      my $self = shift;      my $self = shift;
54            
55      # create handle      # create handle
56        if ( my $dsn = $self->{dbi}->{dsn} ) {        if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
57          $logger->debug( __PACKAGE__ . "->connect($dsn)" );        #if ( my $dsn = $self->{locator}->{dsn} ) {
58          $self->{COREHANDLE} = DBI->connect($dsn);          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
59            
60            # HACK:
61            # set errorhandler before actually calling DBI->connect
62            # in order to catch errors from the very beginning
63            #DBI->{HandleError} = $self->{dbi}->{HandleError};
64            
65            #use Data::Dumper; print Dumper($self->{dbi});
66            
67            eval {
68              $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
69              if (!$self->{COREHANDLE}) {
70                $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
71                return;
72              }
73            };
74            $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
75    
76        }        }
77        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
78    
79        $self->{locator}->{status}->{connected} = 1;
80    
81        return 1;
82            
83  }  }
84    
# Line 43  sub configureCOREHANDLE { Line 86  sub configureCOREHANDLE {
86    
87    my $self = shift;    my $self = shift;
88    
89    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
90    
91    # apply configured modifications    # apply configured modifications to DBI-handle
92      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
93        $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
94      }      }
95      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
96        $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
97      }      }
98      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
99        $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError};        $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
100      }      }
101      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
102        $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError};        $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
103      }      }
104    
105  }  }
# Line 64  sub configureCOREHANDLE { Line 107  sub configureCOREHANDLE {
107  sub _sendSql {  sub _sendSql {
108    my $self = shift;    my $self = shift;
109    my $sql = shift;    my $sql = shift;
110      
111      # two-level handling for implicit connect:
112      # if there's no corehandle ...
113      if (!$self->{COREHANDLE}) {
114        # ... try to connect, but ...
115        $self->connect();
116        # ... if this still fails, there's something wrong probably, so we won't continue
117        if (!$self->{COREHANDLE}) {
118          return;
119        }
120      }
121      
122      #print "prepare sql: $sql\n";
123      
124    my $sth = $self->{COREHANDLE}->prepare($sql);    my $sth = $self->{COREHANDLE}->prepare($sql);
125    $sth->execute();    $sth->execute();
126    return $sth;    return $sth;
# Line 72  sub _sendSql { Line 129  sub _sendSql {
129  sub sendCommand {  sub sendCommand {
130    my $self = shift;    my $self = shift;
131    my $command = shift;    my $command = shift;
132      # TODO: when tracing: yes, do actually log this
133      #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
134    my $cmdHandle = $self->_sendSql($command);    my $cmdHandle = $self->_sendSql($command);
135    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
136    return $result;    return $result;
137  }  }
138    
 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;  
 }  
   
   
139  sub getChildNodes {  sub getChildNodes {
   
140    my $self = shift;    my $self = shift;
141    my @nodes;    my @nodes;
142      $logger->debug( __PACKAGE__ . "->getChildNodes()" );
143    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
144      while ( my $row = $result->_getNextEntry() ) {      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
145        push @nodes, $row;      my $key = "Tables_in_$dbname";
146        while ( my $row = $result->getNextEntry() ) {
147          push @nodes, $row->{$key};
148      }      }
149    }    }
     
150    return \@nodes;    return \@nodes;
   
151  }  }
152    
153    sub getListUnfiltered {
   
   
 package Data::Storage::Result::DBI;  
   
 use strict;  
 use warnings;  
   
 use base ("Data::Storage::Result");  
   
 sub DESTROY {  
   my $self = shift;  
   #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );  
   $self->{RESULTHANDLE}->finish();  
 }  
   
 sub _getNextEntry {  
154    my $self = shift;    my $self = shift;
155    return $self->{RESULTHANDLE}->fetchrow_hashref;    my $nodename = shift;
156      my @list;
157      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
158      # get list of rows from rdbms by table name
159      my $result = $self->sendCommand("SELECT * FROM $nodename");
160      while ( my $row = $result->getNextEntry() ) {
161        push @list, $row;
162      }
163      return \@list;
164    }
165    
166    sub sendQuery {
167      my $self = shift;
168      my $query = shift;
169      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
170      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
171      my @crits;
172      foreach (@{$query->{criterias}}) {
173        my $op = '';
174        $op = '=' if lc $_->{op} eq 'eq';
175        push @crits, "$_->{key}$op'$_->{val}'";
176      }
177      my $subnodes = {};
178      map { $subnodes->{$_}++ } @{$query->{subnodes}};
179      # HACK: this is hardcoded ;(    expand possibilities!
180      my $crit = join(' AND ', @crits);
181      my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
182      return $self->sendCommand($sql);
183  }  }
184    
   
 1;  
185    1;

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

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