/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by joko, Sun Dec 1 04:46:19 2002 UTC revision 1.21 by jonen, Mon Dec 16 22:20:49 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.21  2002/12/16 22:20:49  jonen
7    #  + fixed bug at 'getObjectByGuid()'
8    #
9    #  Revision 1.20  2002/12/16 20:49:17  jonen
10    #  + added sub 'getObjectByGuid()'
11    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
12    #
13    #  Revision 1.19  2002/12/16 06:46:09  joko
14    #  + attempt to introduce a generic '_patchSchema' - cancelled!
15    #
16    #  Revision 1.18  2002/12/13 21:48:07  joko
17    #  + fix to 'sub sendQuery'
18    #
19    #  Revision 1.17  2002/12/12 02:51:09  joko
20    #  + cosmetics
21    #
22    #  Revision 1.16  2002/12/11 06:54:10  joko
23    #  + fix: encapsulated object-loading inside an 'eval'
24    #
25    #  Revision 1.15  2002/12/05 13:55:21  joko
26    #  + now utilizing 'object2hash' instead of 'var_deref'
27    #  + played around with having fresh-objects - no progress....
28    #
29    #  Revision 1.14  2002/12/05 09:40:30  jonen
30    #  + added option->{destroy} at getObject for unloading all instance
31    #
32    #  Revision 1.13  2002/12/05 07:59:04  joko
33    #  + now using Tie::SecureHash as a base for the COREHANDLE
34    #  + former public COREHANDLE becomes private _COREHANDLE now
35    #  + sub getCOREHANDLE
36    #
37    #  Revision 1.12  2002/12/04 11:34:49  joko
38    #  - $schema_tangram doesn't have to be in class?
39    #
40    #  Revision 1.11  2002/12/04 08:54:08  jonen
41    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
42    #
43    #  Revision 1.10  2002/12/03 15:53:23  joko
44    #  + small bugfix regarding object hierarchy
45    #
46    #  Revision 1.9  2002/12/03 05:29:40  joko
47    #  + sub getObject
48    #  + sub getObjectAsHash
49    #
50    #  Revision 1.8  2002/12/01 22:25:51  joko
51    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
52    #
53  #  Revision 1.7  2002/12/01 04:46:19  joko  #  Revision 1.7  2002/12/01 04:46:19  joko
54  #  + sub eraseAll  #  + sub eraseAll
55  #  #
# Line 46  package Data::Storage::Handler::Tangram; Line 93  package Data::Storage::Handler::Tangram;
93  use strict;  use strict;
94  use warnings;  use warnings;
95    
96    use base ("Data::Storage::Handler");
97  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
98    
99  use Tangram;  use Tangram;
# Line 53  use Data::Dumper; Line 101  use Data::Dumper;
101  use libp qw( getNewPerlObjectByPkgName );  use libp qw( getNewPerlObjectByPkgName );
102  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
103  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
104    use Data::Transform::Deep qw( object2hash );
105    use Data::Transform::Encode qw( var2utf8 );
106    
107    
108  # get logger instance  # get logger instance
109  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
110    
111    
112    # this holds the complete instantiated schema from tangram
113    my $schema_tangram;
114    
115  sub getMetaInfo {  sub getMetaInfo {
116    my $self = shift;    my $self = shift;
117    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
# Line 69  sub getMetaInfo { Line 123  sub getMetaInfo {
123  sub _initSchema {  sub _initSchema {
124    my $self = shift;    my $self = shift;
125    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
126    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
127      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} } );
128      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
129    #}    #}
130    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
131      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
132      return 0;      return 0;
133    }    }
134      #$self->_patchSchema();
135    return 1;    return 1;
136  }  }
137    
138    sub _patchSchema {
139      my $self = shift;
140      foreach (keys %{$schema_tangram->{classes}}) {
141        next if $schema_tangram->{classes}->{$_}->{abstract};
142        #next if ($_ ne 'TsBankAccount');
143        #$_ ne 'AbstractAccount' &&
144        print "class: $_", "\n";
145    #print Dumper($schema_tangram->{classes}->{$_});
146        # create new string property named 'guid'
147        my $tstring = Tangram::String->new();
148        $tstring->{name} = $tstring->{col} = 'guid';
149        # inject property into schema
150        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
151        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
152      }
153    }
154    
155  sub connect {  sub connect {
156    
157      my $self = shift;      my $self = shift;
# Line 102  sub connect { Line 174  sub connect {
174    
175      # create the main tangram storage object      # create the main tangram storage object
176      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
177      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
178    
179    #print "connect", "\n";
180    #my $core = $self->{_COREHANDLE};
181    #print Dumper($core);
182        
183      # some attempts for configuring the wrapped underlying dbi.....      # some attempts for configuring the wrapped underlying dbi.....
184      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
# Line 138  sub getChildNodes { Line 214  sub getChildNodes {
214  #print Dumper($self);  #print Dumper($self);
215    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
216        
217    $storage->disconnect();    # TODO: REVIEW
218      #$storage->disconnect();
219        
220    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
221        
# Line 207  sub _configureUnderlyingStorage { Line 284  sub _configureUnderlyingStorage {
284    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
285      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
286      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
287      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
288    }    }
289    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
290  }  }
# Line 223  sub configureCOREHANDLE { Line 300  sub configureCOREHANDLE {
300    
301    # apply configured modifications    # apply configured modifications
302      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
303        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
304      }      }
305      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
306        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
307      }      }
308      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
309        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
310      }      }
311      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
312        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
313      }      }
314    
315  }  }
# Line 242  sub deploySchema { Line 319  sub deploySchema {
319    my $args = shift;    my $args = shift;
320        
321    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
322    
323    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
324    
325    my $ok;    my $ok;
326    # TODO: is this DBI->connect okay here like it is? regarding errors.....???    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
   if ( my $dbh = DBI->connect($dsn, '', '', {  
                                                       PrintError => 0,  
                                                     } ) ) {  
   
327      return unless $self->_initSchema();      return unless $self->_initSchema();
328            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
329      $dbh->disconnect();      $dbh->disconnect();
330    }    }
331    return $ok;    return $ok;
# Line 264  sub retreatSchema { Line 335  sub retreatSchema {
335    
336    my $self = shift;    my $self = shift;
337    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
338    
339    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
340    
341    my $ok;    my $ok;
342    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
343    
344      return unless $self->_initSchema();      return unless $self->_initSchema();
345            
346      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
347      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
348            
349      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
350      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
351                      # idea: test this by checking for count of tables in database -      # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
352                      #          problem with this: there may be some left not having been included to the schema      # - ... Tangram::Relational->retreat doesn't seem to return a valid status
353        # - possible improvement:
354        #   - test this by checking for count of tables in database
355        #   - problem with this: there may be some left not having been included to the schema
356        #   - maybe better: use "->getChildNodes"?
357        $ok = 2;
358    
359      $dbh->disconnect();      $dbh->disconnect();
360    
361    }    }
362    return $ok;    return $ok;
363  }  }
# Line 314  sub getListUnfiltered { Line 388  sub getListUnfiltered {
388    my @results;    my @results;
389    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
390    # get set of objects from odbms by object name    # get set of objects from odbms by object name
391    my $object_set = $self->{COREHANDLE}->remote($nodename);    my $object_set = $self->{_COREHANDLE}->remote($nodename);
392    @results = $self->{COREHANDLE}->select($object_set);    @results = $self->{_COREHANDLE}->select($object_set);
393    return \@results;    return \@results;
394  }  }
395    
# Line 360  sub getListFiltered { Line 434  sub getListFiltered {
434    my $tfilter = join(' & ', @tfilters);    my $tfilter = join(' & ', @tfilters);
435    
436    # get set of objects from odbms by object name    # get set of objects from odbms by object name
437    my $remote = $self->{COREHANDLE}->remote($nodename);    my $remote = $self->{_COREHANDLE}->remote($nodename);
438        
439    # was:    # was:
440    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
441    
442    # is:    # is:
443    # HACK: build eval-string (sorry) to get filtered list - please give advice here    # HACK: build eval-string (sorry) to get filtered list - please give advice here
444    my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
445        
446    # get filtered list/set    # get filtered list/set
447    @results = eval($evalstring);    @results = eval($evalstring);
# Line 379  sub getListFiltered { Line 453  sub getListFiltered {
453  sub createCursor {  sub createCursor {
454    my $self = shift;    my $self = shift;
455    my $node = shift;    my $node = shift;
456    my $cmdHandle = $self->{COREHANDLE}->cursor($node);    my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
457    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
458    return $result;    return $result;
459  }  }
460    
461  sub createSet {  sub createSet {
462    my $self = shift;    my $self = shift;
463    #print "-" x 80, "\n";
464    #print Dumper(@_);
465    my @objects = @_;    my @objects = @_;
466    my $rh = Set::Object->new();    my $rh = Set::Object->new();
467    foreach (@objects) {    foreach (@objects) {
468      #print Dumper($_);      if (!isEmpty($_)) {
469      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
470          $rh->insert($_);
471        }
472    }    }
473    #print Dumper($rh->members());    #print Dumper($rh->members());
474    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 414  sub sendQuery { Line 492  sub sendQuery {
492      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
493  #print "load obj", "\n";  #print "load obj", "\n";
494      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
495      my $object = $self->{COREHANDLE}->load($ident);      $self->{_COREHANDLE}->unload($ident);
496        my $object = $self->{_COREHANDLE}->load($ident);
497  #print "get id", "\n";  #print "get id", "\n";
498      my $oid = $self->{COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
499    #print Dumper($object);
500    #print "oid: $oid", "\n";
501      return $self->createSet($object);      return $self->createSet($object);
502      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
503    }    }
# Line 445  sub sendQuery { Line 526  sub sendQuery {
526  sub eraseAll {  sub eraseAll {
527    my $self = shift;    my $self = shift;
528    my $classname = shift;    my $classname = shift;
529    my $remote = $self->{storage}->remote($classname);    my $remote = $self->{_COREHANDLE}->remote($classname);
530    my @objs = $self->{storage}->select($remote);    my @objs = $self->{_COREHANDLE}->select($remote);
531    $self->{COREHANDLE}->erase(@objs);    $self->{_COREHANDLE}->erase(@objs);
532    }
533    
534    sub createDb {
535      my $self = shift;
536      my $storage = $self->_getSubLayerHandle();
537      return $storage->createDb();
538    }
539    
540    sub getObject {
541      my $self = shift;
542      my $oid = shift;
543      my $options = shift;
544    
545      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
546      # unload($oid) will only unload object, not deep object hashes
547      $self->{_COREHANDLE}->unload() if ($options->{destroy});
548    
549      # TODO: review this
550      #if (!$self->{COREHANDLE}) { return; }
551    
552      # TODO: review this
553      my $object = eval('$self->{_COREHANDLE}->load($oid);');
554      print $@, "\n" if $@;
555    
556      return $object if $object;
557    }
558    
559    sub getObjectByGuid {
560      my $self = shift;
561      my $guid = shift;
562      my $options = shift;
563      
564      # Guid and Classname is needed
565      if(!$guid || !$options->{classname}) {
566        return;
567      }
568      
569      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
570      # unload($oid) will only unload object, not deep object hashes
571      $self->{_COREHANDLE}->unload() if ($options->{destroy});
572    
573      # search for object with given Classname and Guid
574      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
575      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
576      
577      # we searched for global unique identifer of some object,
578      # so it think we can trust there would be only one result
579      if($result[0]) {
580        return $result[0];
581      } else {
582        return "No Object with Classname $options->{classname} and GUID $options->{guid} found!";
583      }
584      
585    }
586    
587    sub getObjectAsHash {
588      my $self = shift;
589      my $oid = shift;
590      my $options = shift;  
591      my $obj;
592      
593      if($options->{guid}) {
594        $obj = $self->getObjectByGuid($oid, $options);
595      } else {
596        $obj = $self->getObject($oid, $options);
597      }
598      
599      # build options (a callback to unload autovivified objects) for 'expand'
600      # TODO: use $logger to write to debug here!
601      my $cb; # = sub {};
602    =pod
603      if ($options->{destroy}) {
604        $options->{cb}->{destroy} = sub {
605          print "================ DESTROY", "\n";
606          my $object = shift;
607          #print Dumper($object);
608          $self->{_COREHANDLE}->unload($object);
609          #undef($object);
610        };
611      }
612    =cut
613    
614      my $hash = object2hash($obj, $options);
615      #$options->{cb}->{destroy}->($obj);
616      #$self->{_COREHANDLE}->unload($obj);
617      
618      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
619      # now done in object2hash
620      #var2utf8($hash) if ($options->{utf8});
621      
622      # old (wrong) attempts to get rid of used instances, if requested
623        #$obj->clear_refs;
624        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
625        #$obj->DESTROY;
626        #undef($obj) if($options->{destroy});
627      
628      return $hash;
629    }
630    
631    sub getSchema {
632      return $schema_tangram;
633    }
634    
635    sub getCOREHANDLE {
636      my $self = shift;
637      return $self->{_COREHANDLE};
638  }  }
639    
640  1;  1;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.21

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed