/[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.5 by joko, Fri Nov 29 05:00:26 2002 UTC revision 1.12 by joko, Thu Jan 30 22:28:21 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.12  2003/01/30 22:28:21  joko
7    #  + implemented new concrete methods
8    #
9    #  Revision 1.11  2002/12/19 16:31:05  joko
10    #  + sub dropDb
11    #  + sub rebuildDb
12    #
13    #  Revision 1.10  2002/12/15 02:02:22  joko
14    #  + fixed logging-message
15    #
16    #  Revision 1.9  2002/12/05 07:58:20  joko
17    #  + now using Tie::SecureHash as a base for the COREHANDLE
18    #  + former public COREHANDLE becomes private _COREHANDLE now
19    #
20    #  Revision 1.8  2002/12/01 22:20:43  joko
21    #  + sub createDb (from Storage.pm)
22    #
23    #  Revision 1.7  2002/12/01 07:09:09  joko
24    #  + sub getListFiltered (dummy redirecting to getListUnfiltered)
25    #
26    #  Revision 1.6  2002/12/01 04:46:01  joko
27    #  + sub eraseAll
28    #
29  #  Revision 1.5  2002/11/29 05:00:26  joko  #  Revision 1.5  2002/11/29 05:00:26  joko
30  #  + sub getListUnfiltered  #  + sub getListUnfiltered
31  #  + sub sendQuery  #  + sub sendQuery
# Line 65  sub connect { Line 88  sub connect {
88          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
89                    
90          eval {          eval {
91            $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );            $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
92            if (!$self->{COREHANDLE}) {            if (!$self->{_COREHANDLE}) {
93              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
94              return;              return;
95            }            }
# Line 88  sub configureCOREHANDLE { Line 111  sub configureCOREHANDLE {
111    
112    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
113    
114      return if !$self->{_COREHANDLE};
115    
116    # apply configured modifications to DBI-handle    # apply configured modifications to DBI-handle
117      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}) {
118        $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});
119      }      }
120      if (exists $self->{locator}->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
121        $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
122      }      }
123      if (exists $self->{locator}->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
124        $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
125      }      }
126      if (exists $self->{locator}->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
127        $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
128      }      }
129    
130  }  }
# Line 110  sub _sendSql { Line 135  sub _sendSql {
135        
136    # two-level handling for implicit connect:    # two-level handling for implicit connect:
137    # if there's no corehandle ...    # if there's no corehandle ...
138    if (!$self->{COREHANDLE}) {    if (!$self->{_COREHANDLE}) {
139      # ... try to connect, but ...      # ... try to connect, but ...
140      $self->connect();      $self->connect();
141      # ... 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
142      if (!$self->{COREHANDLE}) {      if (!$self->{_COREHANDLE}) {
143        return;        return;
144      }      }
145    }    }
146        
147    #print "prepare sql: $sql\n";    #print "prepare sql: $sql\n";
148        
149    my $sth = $self->{COREHANDLE}->prepare($sql);    my $sth = $self->{_COREHANDLE}->prepare($sql);
150    $sth->execute();    $sth->execute();
151    return $sth;    return $sth;
152  }  }
# Line 140  sub getChildNodes { Line 165  sub getChildNodes {
165    my $self = shift;    my $self = shift;
166    my @nodes;    my @nodes;
167    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
168      my $locator = $self->{locator};
169      #print Dumper($locator); exit;
170    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
171      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
172      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
# Line 166  sub getListUnfiltered { Line 193  sub getListUnfiltered {
193  sub sendQuery {  sub sendQuery {
194    my $self = shift;    my $self = shift;
195    my $query = shift;    my $query = shift;
196    
197      $logger->debug( __PACKAGE__ . "->sendQuery" );
198    
199    #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}';";
200    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
201    my @crits;    my @crits;
# Line 182  sub sendQuery { Line 212  sub sendQuery {
212    return $self->sendCommand($sql);    return $self->sendCommand($sql);
213  }  }
214    
215    sub eraseAll {
216      my $self = shift;
217      my $classname = shift;
218      $logger->debug( __PACKAGE__ . "->eraseAll" );
219      my $sql = "DELETE FROM $classname";
220      $self->sendCommand($sql);
221    }
222    
223    # TODO: actually implement the filtering functionality using $this->sendQuery
224    sub getListFiltered {
225      my $self = shift;
226      my $nodename = shift;
227      return $self->getListUnfiltered($nodename);
228    }
229    
230    # TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)"
231    sub createDb {
232      
233      my $self = shift;
234      
235      # get dsn from Data::Storage::Locator instance
236      my $dsn = $self->{locator}->{dbi}->{dsn};
237    
238      $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
239    
240      # remove database setting from dsn-string
241      $dsn =~ s/database=(.+?);//;
242      
243      # remember extracted database name to know what actually to create right now
244      my $database_name = $1;
245    
246      # flag to indicate goodness
247      my $ok;
248      
249      # connect to database server - don't select/use any specific database
250      #if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) {
251      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
252    
253        if ($database_name) {
254          if ($dbh->do("CREATE DATABASE $database_name")) {
255            $ok = 1;
256          }
257        }
258    
259        $dbh->disconnect();
260    
261      }
262      
263      return $ok;
264      
265    }
266    
267    sub getCOREHANDLE2 {
268      my $self = shift;
269      return $self->{_COREHANDLE};
270    }
271    
272    sub dropDb {
273      my $self = shift;
274      my $dsn = $self->{locator}->{dbi}->{dsn};
275    
276      $logger->debug( __PACKAGE__ .  "->dropDb( dsn $dsn )" );
277    
278      $dsn =~ s/database=(.+?);//;
279      my $database_name = $1;
280    
281      my $ok;
282      
283      if ( my $dbh = DBI->connect($dsn, '', '', {
284                                                          PrintError => 0,
285                                                          } ) ) {
286        if ($database_name) {
287          if ($dbh->do("DROP DATABASE $database_name;")) {
288            $ok = 1;
289          }
290        }
291    
292        $dbh->disconnect();
293    
294      }
295      
296      return $ok;
297    }
298    
299    sub rebuildDb {
300      my $self = shift;
301      $logger->info( __PACKAGE__ . "->rebuildDb()" );
302      my @results;
303    
304      # sum up results (bool (0/1)) in array
305      #push @results, $self->retreatSchema();
306      push @results, $self->dropDb();
307      push @results, $self->createDb();
308      #push @results, $self->deploySchema();
309    
310      # scan array for "bad ones"
311      my $res = 1;
312      map {
313        $res = 0 if (!$_);
314      } @results;
315      
316      return $res;
317    }
318    
319    sub testAvailability {
320      my $self = shift;
321      my $status = $self->testDsn();
322      $self->{locator}->{status}->{available} = $status;
323      return $status;
324    }
325    
326    sub testDsn {
327      my $self = shift;
328      my $dsn = $self->{locator}->{dbi}->{dsn};
329      my $result;
330      if ( my $dbh = DBI->connect($dsn, '', '', {
331                                                          PrintError => 0,
332                                                          } ) ) {
333        
334        # TODO: REVIEW
335        $dbh->disconnect();
336        
337        return 1;
338      } else {
339        $logger->warning( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr );
340      }
341    }
342    
343  1;  1;
344    __END__

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

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