/[cvs]/nfo/perl/libs/Data/Storage.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by joko, Sun Jan 19 03:12:59 2003 UTC revision 1.18 by joko, Thu Jan 30 22:12:17 2003 UTC
# Line 9  Line 9 
9  ## ------------------------------------------------------------------------  ## ------------------------------------------------------------------------
10  ##  ##
11  ##  $Log$  ##  $Log$
12    ##  Revision 1.18  2003/01/30 22:12:17  joko
13    ##  - removed/refactored old code: ->Data::Storage::Handler::Tangram|DBI
14    ##
15    ##  Revision 1.17  2003/01/30 21:42:22  joko
16    ##  + minor update: renamed method
17    ##
18    ##  Revision 1.16  2003/01/20 16:52:13  joko
19    ##  + now using 'DesignPattern::Object' to create a new 'Data::Storage::Handler::Xyz' on demand - before we did this in a hand-rolled fashion
20    ##
21  ##  Revision 1.15  2003/01/19 03:12:59  joko  ##  Revision 1.15  2003/01/19 03:12:59  joko
22  ##  + modified header  ##  + modified header
23  ##  - removed pod-documentation - now in 'Storage.pod'  ##  - removed pod-documentation - now in 'Storage.pod'
# Line 81  package Data::Storage; Line 90  package Data::Storage;
90  use strict;  use strict;
91  use warnings;  use warnings;
92    
 use Data::Storage::Locator;  
93  use Data::Dumper;  use Data::Dumper;
94    # FIXME: wipe out!
 # TODO: wipe out!  
95  use DBI;  use DBI;
96    
97    use Data::Storage::Locator;
98    use DesignPattern::Object;
99    
100    
101  # TODO: actually implement level (integrate with Log::Dispatch)  # TODO: actually implement level (integrate with Log::Dispatch)
102  my $TRACELEVEL = 0;  my $TRACELEVEL = 0;
103    
# Line 133  sub AUTOLOAD { Line 144  sub AUTOLOAD {
144    
145    # advanced logging of AUTOLOAD calls ...    # advanced logging of AUTOLOAD calls ...
146    # ... nice but do it only when TRACING (TODO) is enabled    # ... nice but do it only when TRACING (TODO) is enabled
     if ($TRACELEVEL) {  
147        my $logstring = "";        my $logstring = "";
148        $logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method;        $logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method;
149        #print "count: ", $#_, "\n";        #print "count: ", $#_, "\n";
# Line 143  sub AUTOLOAD { Line 153  sub AUTOLOAD {
153        # TODO: only ok if logstring doesn't contain        # TODO: only ok if logstring doesn't contain
154        #            e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c))          (AUTOLOAD)"        #            e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c))          (AUTOLOAD)"
155        # but that would be _way_ too specific as long as we don't have an abstract handler for this  ;)        # but that would be _way_ too specific as long as we don't have an abstract handler for this  ;)
156        if ($TRACELEVEL) {
157        $logger->debug( $logstring );        $logger->debug( $logstring );
158        #print join('; ', @_);        #print join('; ', @_);
159      }      }
160            
161    # filtering AUTOLOAD calls and first-time-touch of the actual storage impl    # filtering AUTOLOAD calls and first-time-touch of the actual storage impl
162    if ($self->_filter_AUTOLOAD($method)) {    if ($self->_filter_AUTOLOAD($method)) {
163      #print "_accessStorage\n";      #print "=== FILTER!", "\n";
164      $self->_accessStorage();      $self->_accessStorageHandle();
165      return $self->{STORAGEHANDLE}->$method(@_);      if ($self->{STORAGEHANDLE}) {
166          return $self->{STORAGEHANDLE}->$method(@_);
167        } else {
168          $logger->critical( __PACKAGE__ . "->AUTOLOAD: ERROR: " . $logstring );
169          return;
170        }
171    }    }
172        
173  }  }
# Line 168  sub _filter_AUTOLOAD { Line 184  sub _filter_AUTOLOAD {
184  }  }
185    
186    
187  sub normalizeArgs {  sub _accessStorageHandle {
   my %args = @_;  
   if (!$args{dsn} && $args{meta}{dsn}) {  
     $args{dsn} = $args{meta}{dsn};  
   }  
   my @result = %args;  
   return @result;  
 }  
   
 sub _accessStorage {  
188    my $self = shift;    my $self = shift;
189    # TODO: to some tracelevel!    # TODO: to some tracelevel!
190      #print "=========== _accessStorage", "\n";
191    if ($TRACELEVEL) {    if ($TRACELEVEL) {
192      $logger->debug( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->_accessStorage()" );      $logger->debug( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->_accessStorageHandle()" );
193    }    }
194    if (!$self->{STORAGEHANDLE}) {    if (!$self->{STORAGEHANDLE}) {
195      $self->_createStorageHandle();      return $self->_createStorageHandle();
196    }    }
197  }  }
198    
199    sub _createStorageHandle1 {
200      my $self = shift;
201      my $type = $self->{locator}->{type};
202      $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );
203    
204      my $pkg = "Data::Storage::Handler::" . $type . "";
205      
206      # try to load perl module at runtime
207      my $evalstr = "use $pkg;";
208      eval($evalstr);
209      if ($@) {
210        $logger->error( __PACKAGE__ .  "[$type]" . "->_createStorageHandle(): $@" );
211        return;
212      }
213      
214      # build up some additional arguments to pass on
215      #my @args = %{$self->{locator}};
216      my @args = ();
217    
218      # - create new storage handle object
219      # - propagate arguments to handler
220      # - pass locator by reference to be able to store status- or meta-information in it
221      $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );
222    
223    }
224    
225  sub _createStorageHandle {  sub _createStorageHandle {
226    my $self = shift;    my $self = shift;
227    my $type = $self->{locator}->{type};    my $type = $self->{locator}->{type};
228    $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );    $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );
229    
230    #print Dumper($self);
231    #exit;
232    
233    my $pkg = "Data::Storage::Handler::" . $type . "";    my $pkg = "Data::Storage::Handler::" . $type . "";
234        
235    # try to load perl module at runtime    # try to load perl module at runtime
236    =pod
237    my $evalstr = "use $pkg;";    my $evalstr = "use $pkg;";
238    eval($evalstr);    eval($evalstr);
239    if ($@) {    if ($@) {
# Line 211  sub _createStorageHandle { Line 249  sub _createStorageHandle {
249    # - propagate arguments to handler    # - propagate arguments to handler
250    # - pass locator by reference to be able to store status- or meta-information in it    # - pass locator by reference to be able to store status- or meta-information in it
251    $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );    $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );
252    =cut
253    
254      $self->{STORAGEHANDLE} = DesignPattern::Object->fromPackage( $pkg, locator => $self->{locator} );
255      return 1;
256    
257  }  }
258    
# Line 249  sub removeLogDispatchHandler { Line 291  sub removeLogDispatchHandler {
291    $logger->remove($name);    $logger->remove($name);
292  }  }
293    
 sub getDbName {  
   my $self = shift;  
   my $dsn = $self->{locator}->{dbi}->{dsn};  
   $dsn =~ m/database=(.+?);/;  
   my $database_name = $1;  
   return $database_name;  
 }  
   
 sub testAvailability {  
   my $self = shift;  
   my $status = $self->testDsn();  
   $self->{locator}->{status}->{available} = $status;  
   return $status;  
 }  
   
 sub isConnected {  
   my $self = shift;  
   # TODO: REVIEW!  
   return 1 if $self->{STORAGEHANDLE};  
 }  
   
 sub testDsn {  
   my $self = shift;  
   my $dsn = $self->{locator}->{dbi}->{dsn};  
   my $result;  
   if ( my $dbh = DBI->connect($dsn, '', '', {  
                                                       PrintError => 0,  
                                                       } ) ) {  
       
     # TODO: REVIEW  
     $dbh->disconnect();  
       
     return 1;  
   } else {  
     $logger->warning( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr );  
   }  
 }  
294    
295  1;  1;
296  __END__  __END__

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.18

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