/[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.13 by joko, Thu Dec 5 07:59:04 2002 UTC revision 1.26 by joko, Thu Jan 30 22:29:47 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.26  2003/01/30 22:29:47  joko
7    #  + fixed module usage (removed dependency on 'libp.pm')
8    #
9    #  Revision 1.25  2003/01/19 02:30:05  joko
10    #  + fix: modified call to '_initSchema'
11    #
12    #  Revision 1.24  2002/12/22 14:13:01  joko
13    #  + sub dropDb
14    #
15    #  Revision 1.23  2002/12/19 16:31:53  joko
16    #  +- renamed sub to 'rebuildDb'
17    #
18    #  Revision 1.22  2002/12/18 22:28:16  jonen
19    #  + added extended logging at 'getObjectByGuid()'
20    #
21    #  Revision 1.21  2002/12/16 22:20:49  jonen
22    #  + fixed bug at 'getObjectByGuid()'
23    #
24    #  Revision 1.20  2002/12/16 20:49:17  jonen
25    #  + added sub 'getObjectByGuid()'
26    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
27    #
28    #  Revision 1.19  2002/12/16 06:46:09  joko
29    #  + attempt to introduce a generic '_patchSchema' - cancelled!
30    #
31    #  Revision 1.18  2002/12/13 21:48:07  joko
32    #  + fix to 'sub sendQuery'
33    #
34    #  Revision 1.17  2002/12/12 02:51:09  joko
35    #  + cosmetics
36    #
37    #  Revision 1.16  2002/12/11 06:54:10  joko
38    #  + fix: encapsulated object-loading inside an 'eval'
39    #
40    #  Revision 1.15  2002/12/05 13:55:21  joko
41    #  + now utilizing 'object2hash' instead of 'var_deref'
42    #  + played around with having fresh-objects - no progress....
43    #
44    #  Revision 1.14  2002/12/05 09:40:30  jonen
45    #  + added option->{destroy} at getObject for unloading all instance
46    #
47  #  Revision 1.13  2002/12/05 07:59:04  joko  #  Revision 1.13  2002/12/05 07:59:04  joko
48  #  + now using Tie::SecureHash as a base for the COREHANDLE  #  + now using Tie::SecureHash as a base for the COREHANDLE
49  #  + former public COREHANDLE becomes private _COREHANDLE now  #  + former public COREHANDLE becomes private _COREHANDLE now
# Line 72  use base ("Data::Storage::Handler::Abstr Line 113  use base ("Data::Storage::Handler::Abstr
113    
114  use Tangram;  use Tangram;
115  use Data::Dumper;  use Data::Dumper;
116  use libp qw( getNewPerlObjectByPkgName );  use DesignPattern::Object;
117  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
118  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
119  use Data::Transform::Deep qw( var_deref );  use Data::Transform::Deep qw( object2hash );
120  use Data::Transform::Encode qw( var2utf8 );  use Data::Transform::Encode qw( var2utf8 );
121    
   
122  # get logger instance  # get logger instance
123  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
124    
# Line 98  sub _initSchema { Line 138  sub _initSchema {
138    my $self = shift;    my $self = shift;
139    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
140    #if (!$schema_tangram) {    #if (!$schema_tangram) {
141      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} } );
142        my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
143      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema();
144    #}    #}
145    if (!$schema_tangram) {    if (!$schema_tangram) {
146      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
147      return 0;      return 0;
148    }    }
149      #$self->_patchSchema();
150    return 1;    return 1;
151  }  }
152    
153    sub _patchSchema {
154      my $self = shift;
155      foreach (keys %{$schema_tangram->{classes}}) {
156        next if $schema_tangram->{classes}->{$_}->{abstract};
157        #next if ($_ ne 'TsBankAccount');
158        #$_ ne 'AbstractAccount' &&
159        print "class: $_", "\n";
160    #print Dumper($schema_tangram->{classes}->{$_});
161        # create new string property named 'guid'
162        my $tstring = Tangram::String->new();
163        $tstring->{name} = $tstring->{col} = 'guid';
164        # inject property into schema
165        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
166        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
167      }
168    }
169    
170  sub connect {  sub connect {
171    
172      my $self = shift;      my $self = shift;
173            
174      my $dsn = shift;      my $dsn = shift;
175    
176    #print Dumper($self);
177    #exit;
178    
179        # TODO: re-enable
180      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
181      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
182            
183      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 126  sub connect { Line 189  sub connect {
189  #      return;  #      return;
190  #    }  #    }
191    
192      return unless $self->_initSchema();      #return unless $self->_initSchema();
193        $self->_initSchema();
194    
195      # create the main tangram storage object      # create the main tangram storage object
196      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 253  sub configureCOREHANDLE { Line 317  sub configureCOREHANDLE {
317    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
318    
319    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
320      #print Dumper($self);
321      #exit;
322    
323    # apply configured modifications    # apply configured modifications
324      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
# Line 318  sub retreatSchema { Line 384  sub retreatSchema {
384    return $ok;    return $ok;
385  }  }
386    
387  sub rebuildDbAndSchema {  sub rebuildDb {
388    my $self = shift;    my $self = shift;
389    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
390    my @results;    my @results;
391    
392    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 416  sub createCursor { Line 482  sub createCursor {
482    
483  sub createSet {  sub createSet {
484    my $self = shift;    my $self = shift;
485    #print "-" x 80, "\n";
486    #print Dumper(@_);
487    my @objects = @_;    my @objects = @_;
488    my $rh = Set::Object->new();    my $rh = Set::Object->new();
489    foreach (@objects) {    foreach (@objects) {
490      #print Dumper($_);      if (!isEmpty($_)) {
491      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
492          $rh->insert($_);
493        }
494    }    }
495    #print Dumper($rh->members());    #print Dumper($rh->members());
496    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 444  sub sendQuery { Line 514  sub sendQuery {
514      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
515  #print "load obj", "\n";  #print "load obj", "\n";
516      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
517        $self->{_COREHANDLE}->unload($ident);
518      my $object = $self->{_COREHANDLE}->load($ident);      my $object = $self->{_COREHANDLE}->load($ident);
519  #print "get id", "\n";  #print "get id", "\n";
520      my $oid = $self->{_COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
521    #print Dumper($object);
522    #print "oid: $oid", "\n";
523      return $self->createSet($object);      return $self->createSet($object);
524      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
525    }    }
# Line 489  sub createDb { Line 562  sub createDb {
562  sub getObject {  sub getObject {
563    my $self = shift;    my $self = shift;
564    my $oid = shift;    my $oid = shift;
565      my $options = shift;
566    
567      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
568      # unload($oid) will only unload object, not deep object hashes
569      $self->{_COREHANDLE}->unload() if ($options->{destroy});
570    
571    # TODO: review this    # TODO: review this
572    #if (!$self->{COREHANDLE}) { return; }    #if (!$self->{COREHANDLE}) { return; }
573    return $self->{_COREHANDLE}->load($oid);  
574      # TODO: review this
575      my $object = eval('$self->{_COREHANDLE}->load($oid);');
576      print $@, "\n" if $@;
577    
578      return $object if $object;
579    }
580    
581    sub getObjectByGuid {
582      my $self = shift;
583      my $guid = shift;
584      my $options = shift;
585      
586      # Guid and Classname is needed
587      if(!$guid || !$options->{classname}) {
588       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
589        return;
590      }
591      
592      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
593      # unload($oid) will only unload object, not deep object hashes
594      $self->{_COREHANDLE}->unload() if ($options->{destroy});
595    
596      # search for object with given Classname and Guid
597      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
598      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
599      
600      # we searched for global unique identifer of some object,
601      # so I think we can trust there would be only one result
602      if($result[0]) {
603        return $result[0];
604      } else {
605        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
606        return;
607      }
608      
609  }  }
610    
611  sub getObjectAsHash {  sub getObjectAsHash {
612    my $self = shift;    my $self = shift;
613    my $oid = shift;    my $oid = shift;
614    my $options = shift;    my $options = shift;  
615    my $obj = $self->getObject($oid);    my $obj;
616    my $deref = var_deref($obj);    
617    var2utf8($deref) if ($options->{utf8});    if($options->{guid}) {
618    undef($obj) if($options->{destroy});      $obj = $self->getObjectByGuid($oid, $options);
619    return $deref;    } else {
620        $obj = $self->getObject($oid, $options);
621      }
622      
623      # build options (a callback to unload autovivified objects) for 'expand'
624      # TODO: use $logger to write to debug here!
625      my $cb; # = sub {};
626    =pod
627      if ($options->{destroy}) {
628        $options->{cb}->{destroy} = sub {
629          print "================ DESTROY", "\n";
630          my $object = shift;
631          #print Dumper($object);
632          $self->{_COREHANDLE}->unload($object);
633          #undef($object);
634        };
635      }
636    =cut
637    
638      my $hash = object2hash($obj, $options);
639      #$options->{cb}->{destroy}->($obj);
640      #$self->{_COREHANDLE}->unload($obj);
641      
642      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
643      # now done in object2hash
644      #var2utf8($hash) if ($options->{utf8});
645      
646      # old (wrong) attempts to get rid of used instances, if requested
647        #$obj->clear_refs;
648        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
649        #$obj->DESTROY;
650        #undef($obj) if($options->{destroy});
651      
652      return $hash;
653  }  }
654    
655  sub getSchema {  sub getSchema {
# Line 514  sub getCOREHANDLE { Line 661  sub getCOREHANDLE {
661    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
662  }  }
663    
664    sub dropDb {
665      my $self = shift;
666      my $storage = $self->_getSubLayerHandle();
667      return $storage->dropDb();
668    }
669    
670    sub testAvailability {
671      my $self = shift;
672      my $storage = $self->_getSubLayerHandle();
673      return $storage->testAvailability();
674    }
675    
676  1;  1;
677    __END__

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.26

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