--- nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/10/17 03:56:55 1.3 +++ nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/10/25 11:44:44 1.4 @@ -1,8 +1,14 @@ ################################# # -# $Id: Tangram.pm,v 1.3 2002/10/17 03:56:55 joko Exp $ +# $Id: Tangram.pm,v 1.4 2002/10/25 11:44:44 joko Exp $ # # $Log: Tangram.pm,v $ +# Revision 1.4 2002/10/25 11:44:44 joko +# + sub _initSchema +# + sub existsChildNode +# + sub testIntegrity +# + sub rebuildDbAndSchema +# # Revision 1.3 2002/10/17 03:56:55 joko # + bugfix: trapped eval error # @@ -46,6 +52,20 @@ return $pkgname->new($args); } +sub _initSchema { + my $self = shift; + $logger->debug( __PACKAGE__ . "->_initSchema()" ); + #if (!$self->{schema_tangram}) { + my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } ); + $self->{schema_tangram} = $obj->getSchema(); + #} + if (!$self->{schema_tangram}) { + $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); + return 0; + } + return 1; +} + sub connect { my $self = shift; @@ -64,9 +84,8 @@ # return; # } - my $obj = getNewPerlObjectByPkgName($self->{schema}, { EXPORT_OBJECTS => $self->{classnames} } ); - $self->{schema_tangram} = $obj->getSchema(); - + return unless $self->_initSchema(); + #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); @@ -75,6 +94,8 @@ #$self->_configureUnderlyingStorage; $self->configureCOREHANDLE(); + return 1; + } sub getChildNodes { @@ -82,6 +103,8 @@ my $self = shift; my @nodes; + $logger->debug( __PACKAGE__ . "->getChildNodes()" ); + # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} }); #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} ); @@ -97,35 +120,77 @@ $storage->disconnect(); + $self->{meta}->{childnodes} = \@nodes; + return \@nodes; } +sub existsChildNode { + my $self = shift; + my $nodename = shift; + $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" ); + $self->getChildNodes() unless $self->{meta}->{childnodes}; + return grep $nodename, @{$self->{meta}->{childnodes}}; +} + + +sub testIntegrity { + + my $self = shift; + + $logger->debug( __PACKAGE__ . "->testIntegrity()" ); + + # 1st test: are there tables? + if (!$self->getChildNodes()) { + $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" ); + return; + } + + # 2nd test: is there a table named "Tangram"? + if (!$self->existsChildNode("Tangram")) { + $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" ); + return; + } + + return 1; + +} + + sub _getSubLayerHandle { my $self = shift; + $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); + use Data::Dumper; #print Dumper($self); + #exit; # hack, make more generic! - if (!$self->{STORAGE_SUBLAYER}) { - my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); - $self->{STORAGE_SUBLAYER} = Data::Storage->new( $loc, { protected => 1 } ); + if (!$self->{dataStorageLayer}) { + $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" ); + #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} ); + my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi} ); + $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); } #print Dumper($self->{STORAGE_UNDER_THE_HOOD}); - return $self->{STORAGE_SUBLAYER}; + return $self->{dataStorageLayer}; } sub _configureUnderlyingStorage { my $self = shift; + + $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" ); + $self->_configureCOREHANDLE_DBI(); return; @@ -142,7 +207,7 @@ my $self = shift; - $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); + $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); #my $subLayer = $self->_getSubLayerHandle(); @@ -166,29 +231,68 @@ my $self = shift; #my $dsn = $self->{locator}->{dbi}->{dsn}; my $dsn = $self->{dbi}->{dsn}; + + $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); + my $ok; if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0, } ) ) { - $ok = Tangram::Relational->deploy($self->{schema}, $dbh ); + + return unless $self->_initSchema(); + + $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); $dbh->disconnect(); } return $ok; } sub retreatSchema { - print "retreat\n"; + my $self = shift; #my $dsn = $self->{locator}->{dbi}->{dsn}; my $dsn = $self->{dbi}->{dsn}; + + $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); + my $ok; if ( my $dbh = DBI->connect($dsn, '', '', { - PrintError => 0, + #PrintError => 0, + #RaiseError => 0, } ) ) { - $ok = Tangram::Relational->retreat($self->{schema}, $dbh ); + + return unless $self->_initSchema(); + + #use Data::Dumper; print Dumper($self); + $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11"); + + $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh ); + $ok = 2; # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status + # idea: test this by checking for count of tables in database - + # problem with this: there may be some left not having been included to the schema $dbh->disconnect(); } return $ok; } +sub rebuildDbAndSchema { + my $self = shift; + $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" ); + my @results; + + # sum up results (bool (0/1)) in array + push @results, $self->retreatSchema(); + push @results, $self->{dataStorageLayer}->dropDb(); + push @results, $self->{dataStorageLayer}->createDb(); + push @results, $self->deploySchema(); + + # scan array for "bad ones" + my $res = 1; + map { + $res = 0 if (!$_); + } @results; + + return $res; +} + 1;