/[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.19 by joko, Tue Feb 18 19:22:11 2003 UTC
# Line 9  Line 9 
9  ## ------------------------------------------------------------------------  ## ------------------------------------------------------------------------
10  ##  ##
11  ##  $Log$  ##  $Log$
12    ##  Revision 1.19  2003/02/18 19:22:11  joko
13    ##  + fixed logging
14    ##
15    ##  Revision 1.18  2003/01/30 22:12:17  joko
16    ##  - removed/refactored old code: ->Data::Storage::Handler::Tangram|DBI
17    ##
18    ##  Revision 1.17  2003/01/30 21:42:22  joko
19    ##  + minor update: renamed method
20    ##
21    ##  Revision 1.16  2003/01/20 16:52:13  joko
22    ##  + now using 'DesignPattern::Object' to create a new 'Data::Storage::Handler::Xyz' on demand - before we did this in a hand-rolled fashion
23    ##
24  ##  Revision 1.15  2003/01/19 03:12:59  joko  ##  Revision 1.15  2003/01/19 03:12:59  joko
25  ##  + modified header  ##  + modified header
26  ##  - removed pod-documentation - now in 'Storage.pod'  ##  - removed pod-documentation - now in 'Storage.pod'
# Line 81  package Data::Storage; Line 93  package Data::Storage;
93  use strict;  use strict;
94  use warnings;  use warnings;
95    
 use Data::Storage::Locator;  
96  use Data::Dumper;  use Data::Dumper;
97    # FIXME: wipe out!
 # TODO: wipe out!  
98  use DBI;  use DBI;
99    
100    use Data::Storage::Locator;
101    use DesignPattern::Object;
102    
103    
104  # TODO: actually implement level (integrate with Log::Dispatch)  # TODO: actually implement level (integrate with Log::Dispatch)
105  my $TRACELEVEL = 0;  my $TRACELEVEL = 0;
106    
# Line 100  sub new { Line 114  sub new {
114    
115    my $arg_locator = shift;    my $arg_locator = shift;
116    my $arg_options = shift;    my $arg_options = shift;
117      #my @args = @_;
118      #@args ||= ();
119    
120    if (!$arg_locator) {    if (!$arg_locator) {
121      $logger->critical( __PACKAGE__ . "->new: No locator passed in!" );      $logger->critical( __PACKAGE__ . "->new: No locator passed in!" );
122      return;      return;
123    }    }
124      
125      #print Dumper($arg_locator);
126    
127    #my $self = { STORAGEHANDLE => undef, @_ };    #my $self = { STORAGEHANDLE => undef, @_ };
128    my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options };    my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options };
129    #$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" );    #$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" );
130    $logger->debug( __PACKAGE__ . "[$arg_locator->{type}]" . "->new(@_)" );    $logger->debug( __PACKAGE__ . "[$arg_locator->{type}]" . "->new()" );
131    return bless $self, $class;    return bless $self, $class;
132  }  }
133    
# Line 133  sub AUTOLOAD { Line 151  sub AUTOLOAD {
151    
152    # advanced logging of AUTOLOAD calls ...    # advanced logging of AUTOLOAD calls ...
153    # ... nice but do it only when TRACING (TODO) is enabled    # ... nice but do it only when TRACING (TODO) is enabled
     if ($TRACELEVEL) {  
154        my $logstring = "";        my $logstring = "";
155        $logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method;        $logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method;
156        #print "count: ", $#_, "\n";        #print "count: ", $#_, "\n";
# Line 143  sub AUTOLOAD { Line 160  sub AUTOLOAD {
160        # TODO: only ok if logstring doesn't contain        # TODO: only ok if logstring doesn't contain
161        #            e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c))          (AUTOLOAD)"        #            e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c))          (AUTOLOAD)"
162        # 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  ;)
163        if ($TRACELEVEL) {
164        $logger->debug( $logstring );        $logger->debug( $logstring );
165        #print join('; ', @_);        #print join('; ', @_);
166      }      }
167            
168    # filtering AUTOLOAD calls and first-time-touch of the actual storage impl    # filtering AUTOLOAD calls and first-time-touch of the actual storage impl
169    if ($self->_filter_AUTOLOAD($method)) {    if ($self->_filter_AUTOLOAD($method)) {
170      #print "_accessStorage\n";      #print "=== FILTER!", "\n";
171      $self->_accessStorage();      $self->_accessStorageHandle();
172      return $self->{STORAGEHANDLE}->$method(@_);      if ($self->{STORAGEHANDLE}) {
173          return $self->{STORAGEHANDLE}->$method(@_);
174        } else {
175          $logger->critical( __PACKAGE__ . "->AUTOLOAD: ERROR: " . $logstring );
176          return;
177        }
178    }    }
179        
180  }  }
# Line 168  sub _filter_AUTOLOAD { Line 191  sub _filter_AUTOLOAD {
191  }  }
192    
193    
194  sub normalizeArgs {  sub _accessStorageHandle {
   my %args = @_;  
   if (!$args{dsn} && $args{meta}{dsn}) {  
     $args{dsn} = $args{meta}{dsn};  
   }  
   my @result = %args;  
   return @result;  
 }  
   
 sub _accessStorage {  
195    my $self = shift;    my $self = shift;
196    # TODO: to some tracelevel!    # TODO: to some tracelevel!
197      #print "=========== _accessStorage", "\n";
198    if ($TRACELEVEL) {    if ($TRACELEVEL) {
199      $logger->debug( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->_accessStorage()" );      $logger->debug( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->_accessStorageHandle()" );
200    }    }
201    if (!$self->{STORAGEHANDLE}) {    if (!$self->{STORAGEHANDLE}) {
202      $self->_createStorageHandle();      return $self->_createStorageHandle();
203    }    }
204  }  }
205    
206    sub _createStorageHandle1 {
207      my $self = shift;
208      my $type = $self->{locator}->{type};
209      $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );
210    
211      my $pkg = "Data::Storage::Handler::" . $type . "";
212      
213      # try to load perl module at runtime
214      my $evalstr = "use $pkg;";
215      eval($evalstr);
216      if ($@) {
217        $logger->error( __PACKAGE__ .  "[$type]" . "->_createStorageHandle(): $@" );
218        return;
219      }
220      
221      # build up some additional arguments to pass on
222      #my @args = %{$self->{locator}};
223      my @args = ();
224    
225      # - create new storage handle object
226      # - propagate arguments to handler
227      # - pass locator by reference to be able to store status- or meta-information in it
228      $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );
229    
230    }
231    
232  sub _createStorageHandle {  sub _createStorageHandle {
233    my $self = shift;    my $self = shift;
234    my $type = $self->{locator}->{type};    my $type = $self->{locator}->{type};
235    $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );    $logger->debug( __PACKAGE__ .  "[$type]" . "->_createStorageHandle()" );
236    
237    #print Dumper($self);
238    #exit;
239    
240    my $pkg = "Data::Storage::Handler::" . $type . "";    my $pkg = "Data::Storage::Handler::" . $type . "";
241        
242    # try to load perl module at runtime    # try to load perl module at runtime
243    =pod
244    my $evalstr = "use $pkg;";    my $evalstr = "use $pkg;";
245    eval($evalstr);    eval($evalstr);
246    if ($@) {    if ($@) {
# Line 211  sub _createStorageHandle { Line 256  sub _createStorageHandle {
256    # - propagate arguments to handler    # - propagate arguments to handler
257    # - 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
258    $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );    $self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args );
259    =cut
260    
261      $self->{STORAGEHANDLE} = DesignPattern::Object->fromPackage( $pkg, locator => $self->{locator} );
262      return 1;
263    
264  }  }
265    
# Line 249  sub removeLogDispatchHandler { Line 298  sub removeLogDispatchHandler {
298    $logger->remove($name);    $logger->remove($name);
299  }  }
300    
 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 );  
   }  
 }  
301    
302  1;  1;
303  __END__  __END__

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

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