/[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.6 by joko, Sun Dec 1 04:46:01 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  #  Revision 1.6  2002/12/01 04:46:01  joko
24  #  + sub eraseAll  #  + sub eraseAll
25  #  #
# Line 68  sub connect { Line 85  sub connect {
85          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
86                    
87          eval {          eval {
88            $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );            $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
89            if (!$self->{COREHANDLE}) {            if (!$self->{_COREHANDLE}) {
90              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
91              return;              return;
92            }            }
# Line 91  sub configureCOREHANDLE { Line 108  sub configureCOREHANDLE {
108    
109    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
110    
111      return if !$self->{_COREHANDLE};
112    
113    # apply configured modifications to DBI-handle    # apply configured modifications to DBI-handle
114      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
115        $self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});        $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
116      }      }
117      if (exists $self->{locator}->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
118        $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
119      }      }
120      if (exists $self->{locator}->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
121        $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
122      }      }
123      if (exists $self->{locator}->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
124        $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
125      }      }
126    
127  }  }
# Line 113  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    #print "prepare sql: $sql\n";    #print "prepare sql: $sql\n";
145        
146    my $sth = $self->{COREHANDLE}->prepare($sql);    my $sth = $self->{_COREHANDLE}->prepare($sql);
147    $sth->execute();    $sth->execute();
148    return $sth;    return $sth;
149  }  }
# Line 143  sub getChildNodes { Line 162  sub getChildNodes {
162    my $self = shift;    my $self = shift;
163    my @nodes;    my @nodes;
164    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
165      my $locator = $self->{locator};
166      #print Dumper($locator); exit;
167    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
168      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
169      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
# Line 169  sub getListUnfiltered { Line 190  sub getListUnfiltered {
190  sub sendQuery {  sub sendQuery {
191    my $self = shift;    my $self = shift;
192    my $query = shift;    my $query = shift;
193    
194      $logger->debug( __PACKAGE__ . "->sendQuery" );
195    
196    #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";    #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
197    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
198    my @crits;    my @crits;
# Line 188  sub sendQuery { Line 212  sub sendQuery {
212  sub eraseAll {  sub eraseAll {
213    my $self = shift;    my $self = shift;
214    my $classname = shift;    my $classname = shift;
215      $logger->debug( __PACKAGE__ . "->eraseAll" );
216    my $sql = "DELETE FROM $classname";    my $sql = "DELETE FROM $classname";
217    $self->sendCommand($sql);    $self->sendCommand($sql);
218  }  }
219    
220    # 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;
231      
232      # get dsn from Data::Storage::Locator instance
233      my $dsn = $self->{locator}->{dbi}->{dsn};
234    
235      $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
236    
237      # 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 $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      $dsn =~ s/database=(.+?);//;
276      my $database_name = $1;
277    
278      my $ok;
279      
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        $dbh->disconnect();
290    
291      }
292      
293      return $ok;
294    }
295    
296    sub rebuildDb {
297      my $self = shift;
298      $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    
316  1;  1;

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

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