--- nfo/perl/libs/Data/Storage.pm 2002/10/10 03:43:12 1.1 +++ nfo/perl/libs/Data/Storage.pm 2002/10/17 00:04:29 1.2 @@ -1,8 +1,13 @@ ################################# # -# $Id: Storage.pm,v 1.1 2002/10/10 03:43:12 cvsjoko Exp $ +# $Id: Storage.pm,v 1.2 2002/10/17 00:04:29 joko Exp $ # # $Log: Storage.pm,v $ +# Revision 1.2 2002/10/17 00:04:29 joko +# + sub createDb +# + sub isConnected +# + bugfixes regarding "deep recursion" stuff +# # Revision 1.1 2002/10/10 03:43:12 cvsjoko # + new # @@ -15,8 +20,6 @@ use warnings; use Data::Storage::Locator; -use Data::Storage::Handler::DBI; -use Data::Storage::Handler::Tangram; # get logger instance my $logger = Log::Dispatch::Config->instance; @@ -37,6 +40,13 @@ sub AUTOLOAD { + # since this is a core function acting as dispatcher to $self->{STORAGEHANDLE}, + # some sophisticated handling and filtering is needed to avoid things like + # - Deep recursion on subroutine "Data::Storage::AUTOLOAD" + # - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" + # - Deep recursion on anonymous subroutine at [...] + # we also might filter log messages caused by logging itself in "advanced logging of AUTOLOAD calls" + my $self = shift; our $AUTOLOAD; @@ -46,8 +56,16 @@ my $method = $AUTOLOAD; $method =~ s/^.*:://; - #$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method . "(@_)" . " (AUTOLOAD)" ); + # advanced logging of AUTOLOAD calls + my $logstring = __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method . "(@_)"; + my $tabcount = int( (80 - length($logstring)) / 10 ); + $logstring .= "\t" x $tabcount . "(AUTOLOAD)"; + # TODO: only ok if logstring doesn't contain + # e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" + # but that would be way too specific as long as we don't have an abstract handler for this ;) + $logger->debug( $logstring ); + # filtering AUTOLOAD calls if ($self->_filter_AUTOLOAD($method)) { $self->_accessStorage(); $self->{STORAGEHANDLE}->$method(@_); @@ -96,11 +114,13 @@ # propagate args to handler # needs some more thoughts! (not only "dbi" to Tangram, when (in future) db is not more the common case) if ($type eq 'DBI') { + use Data::Storage::Handler::DBI; #my @args = %{$self->{locator}->{dbi}}; my @args = %{$self->{locator}}; $self->{STORAGEHANDLE} = $pkg->new( @args ); } if ($type eq 'Tangram') { + use Data::Storage::Handler::Tangram; #$self->{STORAGEHANDLE} = $pkg->new( dsn => $self->{locator}->{dbi}->{dsn} ); #my @args = %{$self->{locator}->{dbi}}; my @args = %{$self->{locator}}; @@ -167,8 +187,36 @@ $dbh->disconnect(); return 1; } else { - $logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . DBI::errstr ); + $logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); + } +} + +sub createDb { + my $self = shift; + my $dsn = $self->{locator}->{dbi}->{dsn}; + $dsn =~ s/database=(.+?);//; + my $database_name = $1; + + my $ok; + + if ( my $dbh = DBI->connect($dsn, '', '', { + PrintError => 0, + } ) ) { + if ($database_name) { + if ($dbh->do("CREATE DATABASE $database_name;")) { + $ok = 1; + } + } + $dbh->disconnect(); } + + return $ok; + +} + +sub isConnected { + my $self = shift; + return 1 if $self->{STORAGEHANDLE}; } 1; \ No newline at end of file