/[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.12 by joko, Wed Dec 4 11:34:49 2002 UTC revision 1.20 by jonen, Mon Dec 16 20:49:17 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.20  2002/12/16 20:49:17  jonen
7    #  + added sub 'getObjectByGuid()'
8    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
9    #
10    #  Revision 1.19  2002/12/16 06:46:09  joko
11    #  + attempt to introduce a generic '_patchSchema' - cancelled!
12    #
13    #  Revision 1.18  2002/12/13 21:48:07  joko
14    #  + fix to 'sub sendQuery'
15    #
16    #  Revision 1.17  2002/12/12 02:51:09  joko
17    #  + cosmetics
18    #
19    #  Revision 1.16  2002/12/11 06:54:10  joko
20    #  + fix: encapsulated object-loading inside an 'eval'
21    #
22    #  Revision 1.15  2002/12/05 13:55:21  joko
23    #  + now utilizing 'object2hash' instead of 'var_deref'
24    #  + played around with having fresh-objects - no progress....
25    #
26    #  Revision 1.14  2002/12/05 09:40:30  jonen
27    #  + added option->{destroy} at getObject for unloading all instance
28    #
29    #  Revision 1.13  2002/12/05 07:59:04  joko
30    #  + now using Tie::SecureHash as a base for the COREHANDLE
31    #  + former public COREHANDLE becomes private _COREHANDLE now
32    #  + sub getCOREHANDLE
33    #
34  #  Revision 1.12  2002/12/04 11:34:49  joko  #  Revision 1.12  2002/12/04 11:34:49  joko
35  #  - $schema_tangram doesn't have to be in class?  #  - $schema_tangram doesn't have to be in class?
36  #  #
# Line 62  package Data::Storage::Handler::Tangram; Line 90  package Data::Storage::Handler::Tangram;
90  use strict;  use strict;
91  use warnings;  use warnings;
92    
93    use base ("Data::Storage::Handler");
94  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
95    
96  use Tangram;  use Tangram;
# Line 69  use Data::Dumper; Line 98  use Data::Dumper;
98  use libp qw( getNewPerlObjectByPkgName );  use libp qw( getNewPerlObjectByPkgName );
99  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
100  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
101  use Data::Transform::Deep qw( var_deref );  use Data::Transform::Deep qw( object2hash );
102  use Data::Transform::Encode qw( var2utf8 );  use Data::Transform::Encode qw( var2utf8 );
103    
104    
# Line 99  sub _initSchema { Line 128  sub _initSchema {
128      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
129      return 0;      return 0;
130    }    }
131      #$self->_patchSchema();
132    return 1;    return 1;
133  }  }
134    
135    sub _patchSchema {
136      my $self = shift;
137      foreach (keys %{$schema_tangram->{classes}}) {
138        next if $schema_tangram->{classes}->{$_}->{abstract};
139        #next if ($_ ne 'TsBankAccount');
140        #$_ ne 'AbstractAccount' &&
141        print "class: $_", "\n";
142    #print Dumper($schema_tangram->{classes}->{$_});
143        # create new string property named 'guid'
144        my $tstring = Tangram::String->new();
145        $tstring->{name} = $tstring->{col} = 'guid';
146        # inject property into schema
147        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
148        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
149      }
150    }
151    
152  sub connect {  sub connect {
153    
154      my $self = shift;      my $self = shift;
# Line 124  sub connect { Line 171  sub connect {
171    
172      # create the main tangram storage object      # create the main tangram storage object
173      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
174      $self->{COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
175    
176    #print "connect", "\n";
177    #my $core = $self->{_COREHANDLE};
178    #print Dumper($core);
179        
180      # some attempts for configuring the wrapped underlying dbi.....      # some attempts for configuring the wrapped underlying dbi.....
181      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
# Line 230  sub _configureUnderlyingStorage { Line 281  sub _configureUnderlyingStorage {
281    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
282      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
283      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
284      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
285    }    }
286    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
287  }  }
# Line 246  sub configureCOREHANDLE { Line 297  sub configureCOREHANDLE {
297    
298    # apply configured modifications    # apply configured modifications
299      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
300        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
301      }      }
302      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
303        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
304      }      }
305      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
306        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
307      }      }
308      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
309        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
310      }      }
311    
312  }  }
# Line 334  sub getListUnfiltered { Line 385  sub getListUnfiltered {
385    my @results;    my @results;
386    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
387    # get set of objects from odbms by object name    # get set of objects from odbms by object name
388    my $object_set = $self->{COREHANDLE}->remote($nodename);    my $object_set = $self->{_COREHANDLE}->remote($nodename);
389    @results = $self->{COREHANDLE}->select($object_set);    @results = $self->{_COREHANDLE}->select($object_set);
390    return \@results;    return \@results;
391  }  }
392    
# Line 380  sub getListFiltered { Line 431  sub getListFiltered {
431    my $tfilter = join(' & ', @tfilters);    my $tfilter = join(' & ', @tfilters);
432    
433    # get set of objects from odbms by object name    # get set of objects from odbms by object name
434    my $remote = $self->{COREHANDLE}->remote($nodename);    my $remote = $self->{_COREHANDLE}->remote($nodename);
435        
436    # was:    # was:
437    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
438    
439    # is:    # is:
440    # 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
441    my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
442        
443    # get filtered list/set    # get filtered list/set
444    @results = eval($evalstring);    @results = eval($evalstring);
# Line 399  sub getListFiltered { Line 450  sub getListFiltered {
450  sub createCursor {  sub createCursor {
451    my $self = shift;    my $self = shift;
452    my $node = shift;    my $node = shift;
453    my $cmdHandle = $self->{COREHANDLE}->cursor($node);    my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
454    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
455    return $result;    return $result;
456  }  }
457    
458  sub createSet {  sub createSet {
459    my $self = shift;    my $self = shift;
460    #print "-" x 80, "\n";
461    #print Dumper(@_);
462    my @objects = @_;    my @objects = @_;
463    my $rh = Set::Object->new();    my $rh = Set::Object->new();
464    foreach (@objects) {    foreach (@objects) {
465      #print Dumper($_);      if (!isEmpty($_)) {
466      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
467          $rh->insert($_);
468        }
469    }    }
470    #print Dumper($rh->members());    #print Dumper($rh->members());
471    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 434  sub sendQuery { Line 489  sub sendQuery {
489      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
490  #print "load obj", "\n";  #print "load obj", "\n";
491      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
492      my $object = $self->{COREHANDLE}->load($ident);      $self->{_COREHANDLE}->unload($ident);
493        my $object = $self->{_COREHANDLE}->load($ident);
494  #print "get id", "\n";  #print "get id", "\n";
495      my $oid = $self->{COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
496    #print Dumper($object);
497    #print "oid: $oid", "\n";
498      return $self->createSet($object);      return $self->createSet($object);
499      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
500    }    }
# Line 465  sub sendQuery { Line 523  sub sendQuery {
523  sub eraseAll {  sub eraseAll {
524    my $self = shift;    my $self = shift;
525    my $classname = shift;    my $classname = shift;
526    my $remote = $self->{COREHANDLE}->remote($classname);    my $remote = $self->{_COREHANDLE}->remote($classname);
527    my @objs = $self->{COREHANDLE}->select($remote);    my @objs = $self->{_COREHANDLE}->select($remote);
528    $self->{COREHANDLE}->erase(@objs);    $self->{_COREHANDLE}->erase(@objs);
529  }  }
530    
531  sub createDb {  sub createDb {
# Line 479  sub createDb { Line 537  sub createDb {
537  sub getObject {  sub getObject {
538    my $self = shift;    my $self = shift;
539    my $oid = shift;    my $oid = shift;
540      my $options = shift;
541    
542      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
543      # unload($oid) will only unload object, not deep object hashes
544      $self->{_COREHANDLE}->unload() if ($options->{destroy});
545    
546    # TODO: review this    # TODO: review this
547    #if (!$self->{COREHANDLE}) { return; }    #if (!$self->{COREHANDLE}) { return; }
548    return $self->{COREHANDLE}->load($oid);  
549      # TODO: review this
550      my $object = eval('$self->{_COREHANDLE}->load($oid);');
551      print $@, "\n" if $@;
552    
553      return $object if $object;
554    }
555    
556    sub getObjectByGuid {
557      my $self = shift;
558      my $guid = shift;
559      my $options = shift;
560      
561      # Guid and Classname is needed
562      if(!$guid || !$options->{classname}) {
563        return;
564      }
565      
566      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
567      # unload($oid) will only unload object, not deep object hashes
568      $self->{_COREHANDLE}->unload() if ($options->{destroy});
569    
570      # search for object with given Classname and Guid
571      my $obj_tmp = $self->{_COREHANDLE}->remote($classname);
572      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
573      
574      # we searched for global unique identifer of some object,
575      # so it think we can trust there would be only one result
576      if($result[0]) {
577        return $result[0];
578      } else {
579        return "No Object with Classname $classname and GUID $options->{guid} found!";
580      }
581      
582  }  }
583    
584  sub getObjectAsHash {  sub getObjectAsHash {
585    my $self = shift;    my $self = shift;
586    my $oid = shift;    my $oid = shift;
587    my $options = shift;    my $options = shift;  
588    my $obj = $self->getObject($oid);    my $obj;
589    my $deref = var_deref($obj);    
590    var2utf8($deref) if ($options->{utf8});    if($options->{guid}) {
591    undef($obj) if($options->{destroy});      $obj = $self->getObjectByGuid($oid, $options);
592    return $deref;    } else {
593        $obj = $self->getObject($oid, $options);
594      }
595      
596      # build options (a callback to unload autovivified objects) for 'expand'
597      # TODO: use $logger to write to debug here!
598      my $cb; # = sub {};
599    =pod
600      if ($options->{destroy}) {
601        $options->{cb}->{destroy} = sub {
602          print "================ DESTROY", "\n";
603          my $object = shift;
604          #print Dumper($object);
605          $self->{_COREHANDLE}->unload($object);
606          #undef($object);
607        };
608      }
609    =cut
610    
611      my $hash = object2hash($obj, $options);
612      #$options->{cb}->{destroy}->($obj);
613      #$self->{_COREHANDLE}->unload($obj);
614      
615      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
616      # now done in object2hash
617      #var2utf8($hash) if ($options->{utf8});
618      
619      # old (wrong) attempts to get rid of used instances, if requested
620        #$obj->clear_refs;
621        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
622        #$obj->DESTROY;
623        #undef($obj) if($options->{destroy});
624      
625      return $hash;
626  }  }
627    
628  sub getSchema {  sub getSchema {
629    return $schema_tangram;    return $schema_tangram;
630  }  }
631    
632    sub getCOREHANDLE {
633      my $self = shift;
634      return $self->{_COREHANDLE};
635    }
636    
637  1;  1;

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.20

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