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

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

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