--- nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/11/29 05:02:30 1.6 +++ nfo/perl/libs/Data/Storage/Handler/Tangram.pm 2002/12/16 06:46:09 1.19 @@ -1,8 +1,51 @@ ############################################ # -# $Id: Tangram.pm,v 1.6 2002/11/29 05:02:30 joko Exp $ +# $Id: Tangram.pm,v 1.19 2002/12/16 06:46:09 joko Exp $ # # $Log: Tangram.pm,v $ +# 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 +# +# Revision 1.16 2002/12/11 06:54:10 joko +# + fix: encapsulated object-loading inside an 'eval' +# +# Revision 1.15 2002/12/05 13:55:21 joko +# + now utilizing 'object2hash' instead of 'var_deref' +# + played around with having fresh-objects - no progress.... +# +# Revision 1.14 2002/12/05 09:40:30 jonen +# + added option->{destroy} at getObject for unloading all instance +# +# 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 @@ -43,6 +86,7 @@ use strict; use warnings; +use base ("Data::Storage::Handler"); use base ("Data::Storage::Handler::Abstract"); use Tangram; @@ -50,11 +94,17 @@ use libp qw( getNewPerlObjectByPkgName ); use Data::Storage::Result::Tangram; use Data::Compare::Struct qw( isEmpty ); +use Data::Transform::Deep qw( object2hash ); +use Data::Transform::Encode qw( var2utf8 ); + # get logger instance my $logger = Log::Dispatch::Config->instance; +# this holds the complete instantiated schema from tangram +my $schema_tangram; + sub getMetaInfo { my $self = shift; $logger->debug( __PACKAGE__ . "->getMetaInfo()" ); @@ -66,17 +116,35 @@ sub _initSchema { my $self = shift; $logger->debug( __PACKAGE__ . "->_initSchema()" ); - #if (!$self->{schema_tangram}) { + #if (!$schema_tangram) { my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } ); - $self->{schema_tangram} = $obj->getSchema(); + $schema_tangram = $obj->getSchema(); #} - if (!$self->{schema_tangram}) { + 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; @@ -99,7 +167,11 @@ # 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(); @@ -135,7 +207,8 @@ #print Dumper($self); #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { - $storage->disconnect(); + # TODO: REVIEW + #$storage->disconnect(); $self->{meta}->{childnodes} = \@nodes; @@ -204,7 +277,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}); } @@ -220,16 +293,16 @@ # 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}; } } @@ -239,19 +312,13 @@ my $args = shift; my $dsn = $self->{locator}->{dbi}->{dsn}; - #my $dsn = $self->{dbi}->{dsn}; $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" ); my $ok; - # TODO: is this DBI->connect okay here like it is? regarding errors.....??? - 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; @@ -261,26 +328,29 @@ 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, - #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; } @@ -311,8 +381,8 @@ 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); + my $object_set = $self->{_COREHANDLE}->remote($nodename); + @results = $self->{_COREHANDLE}->select($object_set); return \@results; } @@ -357,14 +427,14 @@ my $tfilter = join(' & ', @tfilters); # get set of objects from odbms by object name - my $remote = $self->{COREHANDLE}->remote($nodename); + 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 . ');'; + my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');'; # get filtered list/set @results = eval($evalstring); @@ -376,18 +446,22 @@ sub createCursor { my $self = shift; my $node = shift; - my $cmdHandle = $self->{COREHANDLE}->cursor($node); + my $cmdHandle = $self->{_COREHANDLE}->cursor($node); my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle ); return $result; } 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 ); @@ -411,9 +485,12 @@ my $ident = $query->{criterias}->[0]->{val}; #print "load obj", "\n"; #return $self->createSet() if $ident == 5; - my $object = $self->{COREHANDLE}->load($ident); + $self->{_COREHANDLE}->unload($ident); + my $object = $self->{_COREHANDLE}->load($ident); #print "get id", "\n"; - my $oid = $self->{COREHANDLE}->id($object); + my $oid = $self->{_COREHANDLE}->id($object); +#print Dumper($object); +#print "oid: $oid", "\n"; return $self->createSet($object); #return $self->createSet( $self->{COREHANDLE}->load('300090018') ); } @@ -439,4 +516,84 @@ 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; + my $options = shift; + + # 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}); + + # TODO: review this + #if (!$self->{COREHANDLE}) { return; } + + # TODO: review this + my $object = eval('$self->{_COREHANDLE}->load($oid);'); + print $@, "\n" if $@; + + return $object if $object; +} + +sub getObjectAsHash { + my $self = shift; + my $oid = shift; + my $options = shift; + my $obj = $self->getObject($oid, $options); + + # build options (a callback to unload autovivified objects) for 'expand' + # TODO: use $logger to write to debug here! + my $cb; # = sub {}; +=pod + if ($options->{destroy}) { + $options->{cb}->{destroy} = sub { + print "================ DESTROY", "\n"; + my $object = shift; + #print Dumper($object); + $self->{_COREHANDLE}->unload($object); + #undef($object); + }; + } +=cut + + my $hash = object2hash($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 + #var2utf8($hash) if ($options->{utf8}); + + # old (wrong) attempts to get rid of used instances, if requested + #$obj->clear_refs; + #$self->{COREHANDLE}->unload($obj) if($options->{destroy}); + #$obj->DESTROY; + #undef($obj) if($options->{destroy}); + + return $hash; +} + +sub getSchema { + return $schema_tangram; +} + +sub getCOREHANDLE { + my $self = shift; + return $self->{_COREHANDLE}; +} + 1;