--- nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/12/12 02:51:09 1.17 +++ nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2003/02/20 20:20:26 1.28 @@ -1,8 +1,42 @@ ############################################ # -# $Id: Tangram.pm,v 1.17 2002/12/12 02:51:09 joko Exp $ +# $Id: Tangram.pm,v 1.28 2003/02/20 20:20:26 joko Exp $ # # $Log: Tangram.pm,v $ +# Revision 1.28 2003/02/20 20:20:26 joko +# tried to get auto-disconnect working again - failed with that +# +# Revision 1.27 2003/01/31 06:30:59 joko +# + enabled 'sendQuery' +# +# Revision 1.26 2003/01/30 22:29:47 joko +# + fixed module usage (removed dependency on 'libp.pm') +# +# Revision 1.25 2003/01/19 02:30:05 joko +# + fix: modified call to '_initSchema' +# +# Revision 1.24 2002/12/22 14:13:01 joko +# + sub dropDb +# +# Revision 1.23 2002/12/19 16:31:53 joko +# +- renamed sub to 'rebuildDb' +# +# Revision 1.22 2002/12/18 22:28:16 jonen +# + added extended logging at 'getObjectByGuid()' +# +# Revision 1.21 2002/12/16 22:20:49 jonen +# + fixed bug at 'getObjectByGuid()' +# +# Revision 1.20 2002/12/16 20:49:17 jonen +# + added sub 'getObjectByGuid()' +# + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()' +# +# Revision 1.19 2002/12/16 06:46:09 joko +# + attempt to introduce a generic '_patchSchema' - cancelled! +# +# Revision 1.18 2002/12/13 21:48:07 joko +# + fix to 'sub sendQuery' +# # Revision 1.17 2002/12/12 02:51:09 joko # + cosmetics # @@ -10,7 +44,7 @@ # + fix: encapsulated object-loading inside an 'eval' # # Revision 1.15 2002/12/05 13:55:21 joko -# + now utilizing 'object2hash' instead of 'var_deref' +# + now utilizing 'expand' instead of 'var_deref' # + played around with having fresh-objects - no progress.... # # Revision 1.14 2002/12/05 09:40:30 jonen @@ -83,14 +117,15 @@ use base ("Data::Storage::Handler"); use base ("Data::Storage::Handler::Abstract"); -use Tangram; + use Data::Dumper; -use libp qw( getNewPerlObjectByPkgName ); +use Tangram; + +use DesignPattern::Object; use Data::Storage::Result::Tangram; use Data::Compare::Struct qw( isEmpty ); -use Data::Transform::Deep qw( object2hash ); -use Data::Transform::Encode qw( var2utf8 ); - +use Data::Transform::Deep qw( expand ); +#use Data::Transform::Encode qw( var2utf8 ); # get logger instance my $logger = Log::Dispatch::Config->instance; @@ -111,23 +146,46 @@ my $self = shift; $logger->debug( __PACKAGE__ . "->_initSchema()" ); #if (!$schema_tangram) { - my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); + #my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); + my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } ); $schema_tangram = $obj->getSchema(); #} if (!$schema_tangram) { $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" ); return 0; } + #$self->_patchSchema(); return 1; } +sub _patchSchema { + my $self = shift; + foreach (keys %{$schema_tangram->{classes}}) { + next if $schema_tangram->{classes}->{$_}->{abstract}; + #next if ($_ ne 'TsBankAccount'); + #$_ ne 'AbstractAccount' && + print "class: $_", "\n"; +#print Dumper($schema_tangram->{classes}->{$_}); + # create new string property named 'guid' + my $tstring = Tangram::String->new(); + $tstring->{name} = $tstring->{col} = 'guid'; + # inject property into schema + #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring; + print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}); + } +} + sub connect { my $self = shift; my $dsn = shift; + +#print Dumper($self); +#exit; + + # TODO: re-enable $dsn ||= $self->{locator}->{dbi}->{dsn}; - $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); #my $storage = Tangram::Relational->connect( $schema, $dsn ); @@ -139,7 +197,8 @@ # return; # } - return unless $self->_initSchema(); + #return unless $self->_initSchema(); + $self->_initSchema(); # create the main tangram storage object #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn ); @@ -266,6 +325,8 @@ $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); #my $subLayer = $self->_getSubLayerHandle(); + #print Dumper($self); + #exit; # apply configured modifications if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { @@ -331,9 +392,9 @@ return $ok; } -sub rebuildDbAndSchema { +sub rebuildDb { my $self = shift; - $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" ); + $logger->info( __PACKAGE__ . "->rebuildDb()" ); my @results; # sum up results (bool (0/1)) in array @@ -373,7 +434,7 @@ my @results; $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" ); - #print Dumper($filters); +#print Dumper($filters); my @tfilters; @@ -412,6 +473,8 @@ # HACK: build eval-string (sorry) to get filtered list - please give advice here my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; + #print "eval: $evalstring", "\n"; + # get filtered list/set @results = eval($evalstring); die $@ if $@; @@ -429,11 +492,15 @@ sub createSet { my $self = shift; +#print "-" x 80, "\n"; +#print Dumper(@_); my @objects = @_; my $rh = Set::Object->new(); foreach (@objects) { - #print Dumper($_); - $rh->insert($_) if !isEmpty($_); + if (!isEmpty($_)) { +#print Dumper($_); + $rh->insert($_); + } } #print Dumper($rh->members()); my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh ); @@ -457,17 +524,28 @@ my $ident = $query->{criterias}->[0]->{val}; #print "load obj", "\n"; #return $self->createSet() if $ident == 5; + $self->{_COREHANDLE}->unload($ident); my $object = $self->{_COREHANDLE}->load($ident); #print "get id", "\n"; my $oid = $self->{_COREHANDLE}->id($object); +#print Dumper($object); +#print "oid: $oid", "\n"; return $self->createSet($object); #return $self->createSet( $self->{COREHANDLE}->load('300090018') ); } - die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); + my $list = $self->getListFiltered($query->{node}, $query->{criterias}); + #return $self->createSet($object); + #return $self->createSet($list); + return $self->createSet(@$list); + + #die("This should not be reached for now - redirect to \$self->getListFiltered() here!"); - # TODO: do a common tangram query here + + + # try a generic tangram query here + # TODO: try to place an oql on top of that (search.cpan.org!) my @crits; foreach (@{$query->{criterias}}) { my $op = ''; @@ -518,11 +596,47 @@ return $object if $object; } +sub getObjectByGuid { + my $self = shift; + my $guid = shift; + my $options = shift; + + # Guid and Classname is needed + if(!$guid || !$options->{classname}) { + $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" ); + return; + } + + # TODO: create a deep_unload method (currently _all_ objects are unloaded) + # unload($oid) will only unload object, not deep object hashes + $self->{_COREHANDLE}->unload() if ($options->{destroy}); + + # search for object with given Classname and Guid + my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname}); + my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid); + + # we searched for global unique identifer of some object, + # so I think we can trust there would be only one result + if($result[0]) { + return $result[0]; + } else { + $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" ); + return; + } + +} + sub getObjectAsHash { my $self = shift; my $oid = shift; - my $options = shift; - my $obj = $self->getObject($oid, $options); + my $options = shift; + my $obj; + + if($options->{guid}) { + $obj = $self->getObjectByGuid($oid, $options); + } else { + $obj = $self->getObject($oid, $options); + } # build options (a callback to unload autovivified objects) for 'expand' # TODO: use $logger to write to debug here! @@ -539,12 +653,12 @@ } =cut - my $hash = object2hash($obj, $options); + my $hash = expand($obj, $options); #$options->{cb}->{destroy}->($obj); #$self->{_COREHANDLE}->unload($obj); # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML - # now done in object2hash + # now done in expand #var2utf8($hash) if ($options->{utf8}); # old (wrong) attempts to get rid of used instances, if requested @@ -565,4 +679,33 @@ return $self->{_COREHANDLE}; } +sub dropDb { + my $self = shift; + my $storage = $self->_getSubLayerHandle(); + return $storage->dropDb(); +} + +sub testAvailability { + my $self = shift; + my $storage = $self->_getSubLayerHandle(); + return $storage->testAvailability(); +} + +sub disconnect2 { + my $self = shift; + my $storage = $self->_getSubLayerHandle(); + print "DISC!", "\n"; + + my $storage_ll = $storage->{_COREHANDLE}; + $storage_ll->disconnect(); + + print Dumper($storage); + exit; + + #$self->{_COREHANDLE} + #$storage->disconnect(); + $self->{dataStorageLayer}->disconnect(); +} + 1; +__END__