/[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.11 by joko, Thu Dec 19 16:31:05 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.11  2002/12/19 16:31:05  joko
7    #  + sub dropDb
8    #  + sub rebuildDb
9    #
10    #  Revision 1.10  2002/12/15 02:02:22  joko
11    #  + fixed logging-message
12    #
13    #  Revision 1.9  2002/12/05 07:58:20  joko
14    #  + now using Tie::SecureHash as a base for the COREHANDLE
15    #  + former public COREHANDLE becomes private _COREHANDLE now
16    #
17    #  Revision 1.8  2002/12/01 22:20:43  joko
18    #  + sub createDb (from Storage.pm)
19    #
20    #  Revision 1.7  2002/12/01 07:09:09  joko
21    #  + sub getListFiltered (dummy redirecting to getListUnfiltered)
22    #
23    #  Revision 1.6  2002/12/01 04:46:01  joko
24    #  + sub eraseAll
25    #
26    #  Revision 1.5  2002/11/29 05:00:26  joko
27    #  + sub getListUnfiltered
28    #  + sub sendQuery
29    #
30    #  Revision 1.4  2002/11/17 08:46:42  jonen
31    #  + wrapped eval around DBI->connect to prevent deaths
32    #
33    #  Revision 1.3  2002/11/17 06:34:39  joko
34    #  + locator metadata can now be reached via ->{locator}
35    #  - sub hash2sql now taken from libdb
36    #
37  #  Revision 1.2  2002/10/25 11:43:27  joko  #  Revision 1.2  2002/10/25 11:43:27  joko
38  #  + enhanced robustness  #  + enhanced robustness
39  #  + more logging for debug-levels  #  + more logging for debug-levels
# Line 21  use warnings; Line 52  use warnings;
52  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
53    
54  use DBI;  use DBI;
55    use Data::Dumper;
56    use libdb qw( getDbNameByDsn hash2Sql );
57    use Data::Storage::Result::DBI;
58    
59  # get logger instance  # get logger instance
60  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
61    
62    
63  our $metainfo = {  sub getMetaInfo {
64    'disconnectMethod' => 'disconnect',    my $self = shift;
65  };    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
66      return {
67        'disconnectMethod' => 'disconnect',
68      };
69    }
70    
71  sub connect {  sub connect {
72    
73      my $self = shift;      my $self = shift;
74            
75      # create handle      # create handle
76        if ( my $dsn = $self->{dbi}->{dsn} ) {        if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
77          #if ( my $dsn = $self->{locator}->{dsn} ) {
78          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
79                    
80          # HACK:          # HACK:
# Line 45  sub connect { Line 84  sub connect {
84                    
85          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
86                    
87          $self->{COREHANDLE} = DBI->connect(          eval {
88            $dsn, '', '', {            $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
89              RaiseError => $self->{dbi}->{RaiseError},            if (!$self->{_COREHANDLE}) {
90              #RaiseError => 1,              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
91              PrintError => $self->{dbi}->{PrintError},              return;
             HandleError => $self->{dbi}->{HandleError},  
92            }            }
93          );          };
94          if (!$self->{COREHANDLE}) {          $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
95            $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );  
           return;  
         }  
96        }        }
97        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
98        
99        $self->{locator}->{status}->{connected} = 1;
100    
101      return 1;      return 1;
102            
103  }  }
# Line 68  sub configureCOREHANDLE { Line 106  sub configureCOREHANDLE {
106    
107    my $self = shift;    my $self = shift;
108    
109    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
110    
111      return if !$self->{_COREHANDLE};
112    
113    # apply configured modifications    # apply configured modifications to DBI-handle
114      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
115        $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
116      }      }
117      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
118        $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
119      }      }
120      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
121        $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
122      }      }
123      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
124        $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
125      }      }
126    
127  }  }
# Line 92  sub _sendSql { Line 132  sub _sendSql {
132        
133    # two-level handling for implicit connect:    # two-level handling for implicit connect:
134    # if there's no corehandle ...    # if there's no corehandle ...
135    if (!$self->{COREHANDLE}) {    if (!$self->{_COREHANDLE}) {
136      # ... try to connect, but ...      # ... try to connect, but ...
137      $self->connect();      $self->connect();
138      # ... if this still fails, there's something wrong probably, so we won't continue      # ... if this still fails, there's something wrong probably, so we won't continue
139      if (!$self->{COREHANDLE}) {      if (!$self->{_COREHANDLE}) {
140        return;        return;
141      }      }
142    }    }
143        
144    my $sth = $self->{COREHANDLE}->prepare($sql);    #print "prepare sql: $sql\n";
145      
146      my $sth = $self->{_COREHANDLE}->prepare($sql);
147    $sth->execute();    $sth->execute();
148    return $sth;    return $sth;
149  }  }
# Line 109  sub _sendSql { Line 151  sub _sendSql {
151  sub sendCommand {  sub sendCommand {
152    my $self = shift;    my $self = shift;
153    my $command = shift;    my $command = shift;
154      # TODO: when tracing: yes, do actually log this
155    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
156    my $cmdHandle = $self->_sendSql($command);    my $cmdHandle = $self->_sendSql($command);
157    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
158    return $result;    return $result;
159  }  }
160    
161  sub quoteSql {  sub getChildNodes {
162    my $self = shift;    my $self = shift;
163    my $string = shift;    my @nodes;
164    if ($string) {    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
165      $string =~ s/'/\\'/g;    my $locator = $self->{locator};
166      #print Dumper($locator); exit;
167      if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
168        my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
169        my $key = "Tables_in_$dbname";
170        while ( my $row = $result->getNextEntry() ) {
171          push @nodes, $row->{$key};
172        }
173    }    }
174    return $string;    return \@nodes;
175  }  }
176    
177  sub hash2Sql {  sub getListUnfiltered {
   
178    my $self = shift;    my $self = shift;
179        my $nodename = shift;
180    my $table = shift;    my @list;
181    my $hash = shift;    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
182    my $mode = shift;    # get list of rows from rdbms by table name
183    my $crit = shift;    my $result = $self->sendCommand("SELECT * FROM $nodename");
184        while ( my $row = $result->getNextEntry() ) {
185    my $sql;      push @list, $row;
   if ($mode eq 'SQL_INSERT') {  
     $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";  
186    }    }
187    if ($mode eq 'SQL_UPDATE') {    return \@list;
188      $sql = "UPDATE $table SET #fields-values# WHERE $crit;";  }
189    }  
190      sub sendQuery {
191    my (@fields, @values);    my $self = shift;
192    foreach my $key (keys %{$hash}) {    my $query = shift;
193      push @fields, $key;  
194      push @values, $hash->{$key};    $logger->debug( __PACKAGE__ . "->sendQuery" );
195    }  
196    # quote each element    #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
197    map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values;    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
198        my @crits;
199    my $fields = join(', ', @fields);    foreach (@{$query->{criterias}}) {
200    my $values = join(', ', @values);      my $op = '';
201    my $fields_values = '';      $op = '=' if lc $_->{op} eq 'eq';
202    my $fc = 0;      push @crits, "$_->{key}$op'$_->{val}'";
   foreach (@fields) {  
     $fields_values .= $_ . '=' . $values[$fc] . ', ';  
     $fc++;  
203    }    }
204    $fields_values = substr($fields_values, 0, -2);    my $subnodes = {};
205        map { $subnodes->{$_}++ } @{$query->{subnodes}};
206    $sql =~ s/#fields#/$fields/;    # HACK: this is hardcoded ;(    expand possibilities!
207    $sql =~ s/#values#/$values/;    my $crit = join(' AND ', @crits);
208    $sql =~ s/#fields-values#/$fields_values/;    my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
209        return $self->sendCommand($sql);
   return $sql;  
210  }  }
211    
212    sub eraseAll {
213      my $self = shift;
214      my $classname = shift;
215      $logger->debug( __PACKAGE__ . "->eraseAll" );
216      my $sql = "DELETE FROM $classname";
217      $self->sendCommand($sql);
218    }
219    
220  sub getChildNodes {  # TODO: actually implement the filtering functionality using $this->sendQuery
221    sub getListFiltered {
222      my $self = shift;
223      my $nodename = shift;
224      return $self->getListUnfiltered($nodename);
225    }
226    
227    # TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)"
228    sub createDb {
229      
230    my $self = shift;    my $self = shift;
231    my @nodes;    
232      # get dsn from Data::Storage::Locator instance
233      my $dsn = $self->{locator}->{dbi}->{dsn};
234    
235    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
236      while ( my $row = $result->_getNextEntry() ) {  
237        push @nodes, $row;    # remove database setting from dsn-string
238      $dsn =~ s/database=(.+?);//;
239      
240      # remember extracted database name to know what actually to create right now
241      my $database_name = $1;
242    
243      # flag to indicate goodness
244      my $ok;
245      
246      # connect to database server - don't select/use any specific database
247      #if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) {
248      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
249    
250        if ($database_name) {
251          if ($dbh->do("CREATE DATABASE $database_name")) {
252            $ok = 1;
253          }
254      }      }
255    
256        $dbh->disconnect();
257    
258    }    }
259        
260    return \@nodes;    return $ok;
261      
262  }  }
263    
264    sub getCOREHANDLE2 {
265      my $self = shift;
266      return $self->{_COREHANDLE};
267    }
268    
269    sub dropDb {
270      my $self = shift;
271      my $dsn = $self->{locator}->{dbi}->{dsn};
272    
273      $logger->debug( __PACKAGE__ .  "->dropDb( dsn $dsn )" );
274    
275  package Data::Storage::Result::DBI;    $dsn =~ s/database=(.+?);//;
276      my $database_name = $1;
277    
278  use strict;    my $ok;
279  use warnings;    
280      if ( my $dbh = DBI->connect($dsn, '', '', {
281                                                          PrintError => 0,
282                                                          } ) ) {
283        if ($database_name) {
284          if ($dbh->do("DROP DATABASE $database_name;")) {
285            $ok = 1;
286          }
287        }
288    
289  use base ("Data::Storage::Result");      $dbh->disconnect();
290    
291  sub DESTROY {    }
292    my $self = shift;    
293    #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );    return $ok;
   $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish();  
294  }  }
295    
296  sub _getNextEntry {  sub rebuildDb {
297    my $self = shift;    my $self = shift;
298    $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref;    $logger->info( __PACKAGE__ . "->rebuildDb()" );
299  }    my @results;
300    
301      # sum up results (bool (0/1)) in array
302      #push @results, $self->retreatSchema();
303      push @results, $self->dropDb();
304      push @results, $self->createDb();
305      #push @results, $self->deploySchema();
306    
307      # scan array for "bad ones"
308      my $res = 1;
309      map {
310        $res = 0 if (!$_);
311      } @results;
312      
313      return $res;
314    }
315    
 1;  
316    1;

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

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