--- nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/10/25 11:44:44 1.4 +++ nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/12/05 07:59:04 1.13 @@ -1,8 +1,46 @@ -################################# +############################################ # -# $Id: Tangram.pm,v 1.4 2002/10/25 11:44:44 joko Exp $ +# $Id: Tangram.pm,v 1.13 2002/12/05 07:59:04 joko Exp $ # # $Log: Tangram.pm,v $ +# Revision 1.13 2002/12/05 07:59:04 joko +# + now using Tie::SecureHash as a base for the COREHANDLE +# + former public COREHANDLE becomes private _COREHANDLE now +# + sub getCOREHANDLE +# +# Revision 1.12 2002/12/04 11:34:49 joko +# - $schema_tangram doesn't have to be in class? +# +# Revision 1.11 2002/12/04 08:54:08 jonen +# + untested bugfix: undef($object) after transform to hash at getObjectAsHash +# +# Revision 1.10 2002/12/03 15:53:23 joko +# + small bugfix regarding object hierarchy +# +# Revision 1.9 2002/12/03 05:29:40 joko +# + sub getObject +# + sub getObjectAsHash +# +# Revision 1.8 2002/12/01 22:25:51 joko +# + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode +# +# Revision 1.7 2002/12/01 04:46:19 joko +# + sub eraseAll +# +# Revision 1.6 2002/11/29 05:02:30 joko +# - sub getNewPerlObjectByPkgName (moved to libp.pm) +# + sub getMetaInfo +# - sub existsChildNode (moved to Abstract.pm) +# + sub getListUnfiltered +# + sub getListFiltered +# + sub createCursor +# + sub createSet +# + sub sendQuery +# +# Revision 1.5 2002/11/17 06:35:18 joko +# + locator metadata can now be reached via ->{locator} +# - getChildNodes is now wrapped via COREHANDLE +# # Revision 1.4 2002/10/25 11:44:44 joko # + sub _initSchema # + sub existsChildNode @@ -21,45 +59,49 @@ # Revision 1.1 2002/10/10 03:44:07 cvsjoko # + new # -# -################################# +############################################ + package Data::Storage::Handler::Tangram; use strict; use warnings; +use base ("Data::Storage::Handler"); use base ("Data::Storage::Handler::Abstract"); use Tangram; use Data::Dumper; +use libp qw( getNewPerlObjectByPkgName ); +use Data::Storage::Result::Tangram; +use Data::Compare::Struct qw( isEmpty ); +use Data::Transform::Deep qw( var_deref ); +use Data::Transform::Encode qw( var2utf8 ); + # get logger instance my $logger = Log::Dispatch::Config->instance; -our $metainfo = { - 'disconnectMethod' => 'disconnect', -}; +# this holds the complete instantiated schema from tangram +my $schema_tangram; -sub getNewPerlObjectByPkgName { - my $pkgname = shift; - my $args = shift; - $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" ); - my $evstring = "use $pkgname;"; - eval($evstring); - $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval: " . $@ ); - return $pkgname->new($args); +sub getMetaInfo { + my $self = shift; + $logger->debug( __PACKAGE__ . "->getMetaInfo()" ); + return { + 'disconnectMethod' => 'disconnect', + }; } 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 (!$schema_tangram) { + my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); + $schema_tangram = $obj->getSchema(); #} - if (!$self->{schema_tangram}) { + if (!$schema_tangram) { $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); return 0; } @@ -71,7 +113,7 @@ my $self = shift; my $dsn = shift; - $dsn ||= $self->{dbi}->{dsn}; + $dsn ||= $self->{locator}->{dbi}->{dsn}; $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); @@ -86,14 +128,24 @@ return unless $self->_initSchema(); + # create the main tangram storage object #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); - $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn ); + $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn ); + +#print "connect", "\n"; +#my $core = $self->{_COREHANDLE}; +#print Dumper($core); + # some attempts for configuring the wrapped underlying dbi..... #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage(); #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); #$self->_configureUnderlyingStorage; + + # ..... encapsulation wins! $self->configureCOREHANDLE(); + $self->{locator}->{status}->{connected} = 1; + return 1; } @@ -108,17 +160,18 @@ # 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} ); + + # todo: should we retrieve information from the schema here + # rather than poorly getting table names from underlying dbi? my $storage = $self->_getSubLayerHandle(); + @nodes = @{$storage->getChildNodes()}; #$storage->_configureCOREHANDLE(); - +#print "getchildnodes\n"; +#print Dumper($self); #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { - if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) { - while ( my $row = $result->_getNextEntry() ) { - push @nodes, $row; - } - } - $storage->disconnect(); + # TODO: REVIEW + #$storage->disconnect(); $self->{meta}->{childnodes} = \@nodes; @@ -127,15 +180,6 @@ } -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; @@ -154,6 +198,7 @@ return; } + $self->{locator}->{status}->{integrity} = 1; return 1; } @@ -165,15 +210,13 @@ $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" ); - use Data::Dumper; #print Dumper($self); - #exit; - + # hack, make more generic! 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} ); + my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } ); $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } ); #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE(); #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE(); @@ -197,7 +240,7 @@ foreach my $key (keys %{$self->{dbi}}) { my $val = $self->{dbi}->{$key}; print "entry: $key; $val", "\n"; - $self->{COREHANDLE}->{db}->{$key} = $val; + $self->{_COREHANDLE}->{db}->{$key} = $val; } #print Dumper($self->{COREHANDLE}->{db}); } @@ -213,35 +256,32 @@ # apply configured modifications if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { - $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); + $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); } if (exists $self->{dbi}->{RaiseError}) { - $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; + $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError}; } if (exists $self->{dbi}->{PrintError}) { - $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; + $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError}; } if (exists $self->{dbi}->{HandleError}) { - $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; + $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError}; } } sub deploySchema { my $self = shift; - #my $dsn = $self->{locator}->{dbi}->{dsn}; - my $dsn = $self->{dbi}->{dsn}; + my $args = shift; + + my $dsn = $self->{locator}->{dbi}->{dsn}; $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); my $ok; - if ( my $dbh = DBI->connect($dsn, '', '', { - PrintError => 0, - } ) ) { - + if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { return unless $self->_initSchema(); - - $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh ); + $ok = Tangram::Relational->deploy($schema_tangram, $dbh ); $dbh->disconnect(); } return $ok; @@ -250,27 +290,30 @@ sub retreatSchema { my $self = shift; - #my $dsn = $self->{locator}->{dbi}->{dsn}; - my $dsn = $self->{dbi}->{dsn}; + my $dsn = $self->{locator}->{dbi}->{dsn}; $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" ); my $ok; - if ( my $dbh = DBI->connect($dsn, '', '', { - #PrintError => 0, - #RaiseError => 0, - } ) ) { + if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { 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 + $ok = Tangram::Relational->retreat($schema_tangram, $dbh ); + + # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because... + # - ... Tangram::Relational->retreat doesn't seem to return a valid status + # - possible improvement: + # - 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 + # - maybe better: use "->getChildNodes"? + $ok = 2; + $dbh->disconnect(); + } return $ok; } @@ -295,4 +338,180 @@ return $res; } +sub getListUnfiltered { + my $self = shift; + my $nodename = shift; + my @results; + $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); + # get set of objects from odbms by object name + my $object_set = $self->{_COREHANDLE}->remote($nodename); + @results = $self->{_COREHANDLE}->select($object_set); + return \@results; +} + +sub getListFiltered { + my $self = shift; + + # redirect to unfiltered mode + #return $self->getListUnfiltered(@_); + + my $nodename = shift; + my $filters = shift; + my @results; + $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); + + #print Dumper($filters); + + my @tfilters; + + foreach my $filter (@$filters) { + + # get filter - TODO: for each filter + #my $filter = $filters->[0]; + + # build filter + my $lexpr = $filter->{key}; + #my $op = $filter->{op}; + my $op = '='; + my $rexpr = $filter->{val}; + my $tight = 100; + + # my $tfilter = Tangram::Filter->new( + # expr => "t1.$lexpr $op '$rexpr'", + # tight => $tight, + # objects => $objects, + # ); + + # HACK: build eval-string (sorry) to get filtered list - please give advice here + push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'"; + + } + + my $tfilter = join(' & ', @tfilters); + + # get set of objects from odbms by object name + my $remote = $self->{_COREHANDLE}->remote($nodename); + + # was: + #@results = $self->{COREHANDLE}->select($object_set, $tfilter); + + # is: + # HACK: build eval-string (sorry) to get filtered list - please give advice here + my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; + + # get filtered list/set + @results = eval($evalstring); + die $@ if $@; + + return \@results; +} + +sub createCursor { + my $self = shift; + my $node = shift; + my $cmdHandle = $self->{_COREHANDLE}->cursor($node); + my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); + return $result; +} + +sub createSet { + my $self = shift; + my @objects = @_; + my $rh = Set::Object->new(); + foreach (@objects) { + #print Dumper($_); + $rh->insert($_) if !isEmpty($_); + } + #print Dumper($rh->members()); + my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); + return $result; +} + +sub sendQuery { + my $self = shift; + my $query = shift; + #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; + #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); + + #print Dumper($query); + + # HACK: special case: querying by id does not translate into a common tangram query + # just load the object by given id(ent) + if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') { + #print "LOAD!!!", "\n"; + #exit; + #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) ); + my $ident = $query->{criterias}->[0]->{val}; +#print "load obj", "\n"; + #return $self->createSet() if $ident == 5; + my $object = $self->{_COREHANDLE}->load($ident); +#print "get id", "\n"; + my $oid = $self->{_COREHANDLE}->id($object); + return $self->createSet($object); + #return $self->createSet( $self->{COREHANDLE}->load('300090018') ); + } + + die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); + + # TODO: do a common tangram query here + + my @crits; + foreach (@{$query->{criterias}}) { + my $op = ''; + $op = '=' if lc $_->{op} eq 'eq'; + push @crits, "$_->{key}$op'$_->{val}'"; + } + my $subnodes = {}; + map { $subnodes->{$_}++ } @{$query->{subnodes}}; + # HACK: this is hardcoded ;( expand possibilities! + #my $crit = join(' AND ', @crits); + #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); + #return $self->sendCommand($sql); + #my $h = $self->{COREHANDLE}->remote($query->{node}); + #my $res = $self->{COREHANDLE}->select($h, $h->{); + return $self->createCursor($query->{node}); +} + +sub eraseAll { + my $self = shift; + my $classname = shift; + my $remote = $self->{_COREHANDLE}->remote($classname); + my @objs = $self->{_COREHANDLE}->select($remote); + $self->{_COREHANDLE}->erase(@objs); +} + +sub createDb { + my $self = shift; + my $storage = $self->_getSubLayerHandle(); + return $storage->createDb(); +} + +sub getObject { + my $self = shift; + my $oid = shift; + # TODO: review this + #if (!$self->{COREHANDLE}) { return; } + return $self->{_COREHANDLE}->load($oid); +} + +sub getObjectAsHash { + my $self = shift; + my $oid = shift; + my $options = shift; + my $obj = $self->getObject($oid); + my $deref = var_deref($obj); + var2utf8($deref) if ($options->{utf8}); + undef($obj) if($options->{destroy}); + return $deref; +} + +sub getSchema { + return $schema_tangram; +} + +sub getCOREHANDLE { + my $self = shift; + return $self->{_COREHANDLE}; +} + 1;