/[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.27 by joko, Fri Jan 31 06:30:59 2003 UTC revision 1.38 by joko, Tue May 13 16:38:38 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.38  2003/05/13 16:38:38  joko
7    #  problems with "tied" on 5.6.1/win32
8    #
9    #  Revision 1.37  2003/05/10 17:37:39  jonen
10    #  corrected last commit
11    #
12    #  Revision 1.36  2003/05/10 17:31:18  jonen
13    #  + added new functions related to 'create' item
14    #   - createNode()
15    #     # creates non-persistent 'deep dummy filled' object
16    #   - insertChildNode()
17    #     # inserts child node at given parent (child node haven't to exists,
18    #         createNode will be injected transparently)
19    #
20    #  Revision 1.35  2003/04/19 16:09:48  jonen
21    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
22    #
23    #  Revision 1.34  2003/04/11 01:18:53  joko
24    #  sendQuery:
25    #  + introduced crud action 'DELETE'
26    #
27    #  Revision 1.33  2003/04/09 06:07:43  joko
28    #  revamped 'sub sendQuery'
29    #
30    #  Revision 1.32  2003/04/08 22:52:22  joko
31    #  modified 'querySchema': better behaviour regarding filtering result
32    #
33    #  Revision 1.31  2003/04/05 21:24:09  joko
34    #  modified 'sub getChildNodes': now contains code from 'querySchema'
35    #
36    #  Revision 1.30  2003/03/27 15:31:14  joko
37    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
38    #
39    #  Revision 1.29  2003/02/21 01:47:18  joko
40    #  purged old code
41    #  minor cosmetics
42    #
43    #  Revision 1.28  2003/02/20 20:20:26  joko
44    #  tried to get auto-disconnect working again - failed with that
45    #
46  #  Revision 1.27  2003/01/31 06:30:59  joko  #  Revision 1.27  2003/01/31 06:30:59  joko
47  #  + enabled 'sendQuery'  #  + enabled 'sendQuery'
48  #  #
# Line 41  Line 81 
81  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
82  #  #
83  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
84  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
85  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
86  #  #
87  #  Revision 1.14  2002/12/05 09:40:30  jonen  #  Revision 1.14  2002/12/05 09:40:30  jonen
# Line 114  use warnings; Line 154  use warnings;
154  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
155  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
156    
157  use Tangram;  
158  use Data::Dumper;  use Data::Dumper;
159    use Tangram;
160    
161  use DesignPattern::Object;  use DesignPattern::Object;
162  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
163  use Data::Compare::Struct qw( isEmpty );  use Data::Mungle::Compare::Struct qw( isEmpty );
164  use Data::Transform::Deep qw( object2hash );  use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
 use Data::Transform::Encode qw( var2utf8 );  
165    
166  # get logger instance  # get logger instance
167  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 220  sub connect { Line 261  sub connect {
261  sub getChildNodes {  sub getChildNodes {
262    
263    my $self = shift;    my $self = shift;
264    my @nodes;    my $mode = shift;
265      my $filter = shift;
266      
267      $mode ||= 'core';
268      $filter ||= 'all';
269      
270      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
271    
272    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
273    
274    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
275    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
276    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );      # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
277          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
278    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
279    # rather than poorly getting table names from underlying dbi?      
280    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
281    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
282    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
283  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
284  #print Dumper($self);      #$storage->_configureCOREHANDLE();
285    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
286      #print Dumper($self);
287        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
288        
289        # TODO: REVIEW
290        #$storage->disconnect();
291        
292        $self->{meta}->{childnodes} = \@nodes;
293    
294        return \@nodes;
295        
296    # TODO: REVIEW    } elsif ($mode eq 'root') {
297    #$storage->disconnect();      
298        # FIXME: this will return *all* known classes to 'Class::Tangram',
299        # which might not be what you expect since more than one instance
300        # of Tangram may be in memory and Class::Tangram seems to
301        # offer no methods to determine this or filter its result(s) according
302        # to a specific database.
303        my @object_names = Class::Tangram::known_classes();
304        my @concret_names;
305        my $o_cnt;
306        foreach (sort @object_names) {
307          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
308          $o_cnt++;
309        }
310    
311        if ($filter eq 'all') {
312          return \@object_names;
313        } elsif ($filter eq 'concrete') {
314          return \@concret_names;
315        }
316        
317    $self->{meta}->{childnodes} = \@nodes;    }
318        
   return \@nodes;  
319    
320  }  }
321    
   
322  sub testIntegrity {  sub testIntegrity {
323    
324    my $self = shift;    my $self = shift;
# Line 451  sub getListFiltered { Line 523  sub getListFiltered {
523    #    objects => $objects,    #    objects => $objects,
524    #  );    #  );
525        
526      # HACK: build eval-string (sorry) to get filtered list - please give advice here      # TODO: is_op?
527      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      # dispatch un-common operators if exists
528        if($filter->{op} eq "ref") {
529          push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
530        } else {
531          # HACK: build eval-string (sorry) to get filtered list - please give advice here
532          push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
533        }
534    
535    }    }
536    
# Line 505  sub createSet { Line 583  sub createSet {
583  sub sendQuery {  sub sendQuery {
584    my $self = shift;    my $self = shift;
585    my $query = shift;    my $query = shift;
   #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";  
   #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);  
586    
587    #print Dumper($query);    #print Dumper($query);
588    
589    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
590    # just load the object by given id(ent)    my $type = '';
591    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
592      #print "LOAD!!!", "\n";    my $mode = '';
593      #exit;    my $ident = '';
594      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
595      my $ident = $query->{criterias}->[0]->{val};    
596  #print "load obj", "\n";    
597      #return $self->createSet() if $ident == 5;    # dispatch type and mode
598      $self->{_COREHANDLE}->unload($ident);    
599      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
600  #print "get id", "\n";      if ($query->{options}) {
601      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
602  #print Dumper($object);        $crud ||= $query->{options}->{action};
603  #print "oid: $oid", "\n";      }
604      return $self->createSet($object);  
605      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      # defaults - 2
606    }      $type ||= 'TRANSPARENT';
607        $crud ||= 'RETRIEVE';
608    my $list = $self->getListFiltered($query->{node}, $query->{criterias});  
609    #return $self->createSet($object);      if ($query->{options}->{OID}) {
610    #return $self->createSet($list);        $type = 'ITEM';
611    return $self->createSet(@$list);        $mode = 'OID';
612          $ident = $query->{options}->{OID};
613    #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");      
614        } elsif (my $guid = $query->{options}->{GUID}) {
615          $type = 'TRANSPARENT';
616          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
617    
618        # if operator is different (dispatcher for 'getListFiltered')
619        } elsif (my $op = $query->{options}->{op}) {
620          $type = 'TRANSPARENT';
621          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
622    
623        }
624      
625        # HACK: special case: querying by id does not translate into a common tangram query
626        # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
627        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
628          $type = 'ITEM';
629          $mode = 'SPECIAL.SYNC';
630          $ident = $query->{criterias}->[0]->{val};
631        }
632      
633    
634      # execute query
635      my $result;
636    
637      if ($type eq 'ITEM' && $ident) {
638    
639        if ($mode eq 'OID') {    
640          # TODO: review this case!
641          $result = $self->getObject($ident, $query->{options});
642        
643        } elsif ($mode eq 'SPECIAL.SYNC') {
644    
645          # V1 - failed
646          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
647      
648          # hmm....
649          #return $self->createSet() if $ident == 5;
650          
651          # Unload single object before doing any further operations to
652          # expect a "fresh" object from orm when performing the next calls.
653          $self->{_COREHANDLE}->unload($ident);
654          
655          # Load object from orm.
656          my $object = $self->{_COREHANDLE}->load($ident);
657      
658          # determine object identifier (OID)
659          my $oid = $self->{_COREHANDLE}->id($object);
660          
661          # encapsulate into result/response container and return this one
662          $result = $self->createSet($object);
663          
664          # debugging
665          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
666    
667        }
668        
669      
670      } elsif ($type eq 'TRANSPARENT') {
671    
672        if ($crud eq 'RETRIEVE') {
673    
674          my $list = $self->getListFiltered($query->{node}, $query->{criterias});
675          #return $self->createSet($object);
676          #return $self->createSet($list);
677          return $self->createSet(@$list);
678        
679          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
680        
681          # try a generic tangram query here
682          # TODO: try to place an oql on top of that (search.cpan.org!)
683          my @crits;
684          foreach (@{$query->{criterias}}) {
685            my $op = '';
686            $op = '=' if lc $_->{op} eq 'eq';
687            push @crits, "$_->{key}$op'$_->{val}'";
688          }
689          my $subnodes = {};
690          map { $subnodes->{$_}++ } @{$query->{subnodes}};
691          # HACK: this is hardcoded ;(    expand possibilities!
692          #my $crit = join(' AND ', @crits);
693          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
694          #return $self->sendCommand($sql);
695          #my $h = $self->{COREHANDLE}->remote($query->{node});
696          #my $res = $self->{COREHANDLE}->select($h, $h->{);
697          $result = $self->createCursor($query->{node});
698    
699        } elsif ($crud eq 'UPDATE') {
700    
701          # Patch current query to be a loader (e.g. change action, remove payload) ...
702          my $childquery = deep_copy($query);
703          $childquery->{options}->{crud} = 'RETRIEVE';
704          delete $childquery->{payload};
705    
706          # ... to use it to fetch a fresh object using ourselves (sendQuery).
707          my $cursor = $self->sendQuery($childquery);
708          my $status = $cursor->getStatus();
709          my $object = $cursor->getNextEntry();
710    
711          # Merge values and apply value modifiers.
712          my $options = { utf8 => 1, php => 1 };
713          merge_to($object, $query->{payload}, $options);
714    
715          # Execute update operation at orm.
716          $self->update($object);
717        
718        } elsif ($crud eq 'DELETE') {
719    
720          # Patch current query to be a loader (e.g. change action) ...
721          my $childquery = deep_copy($query);
722          $childquery->{options}->{crud} = 'RETRIEVE';
723    
724          # ... to use it to fetch a fresh object using ourselves (sendQuery).
725          my $cursor = $self->sendQuery($childquery);
726          my $status = $cursor->getStatus();
727          my $object = $cursor->getNextEntry();
728    
729          $self->erase($object);
730    
731        } elsif ($crud eq 'CREATE') {
732          
733          my $nodename = $query->{node};      
734          my $newnode = $self->createNode($nodename);
735          my $id = $self->{_COREHANDLE}->insert($newnode);
736      
737          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
738          
739          return $newnode;
740    
741        }
742    
743      }
744    
745      return $result;
746    
   # 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 = '';  
     $op = '=' if lc $_->{op} eq 'eq';  
     push @crits, "$_->{key}$op'$_->{val}'";  
   }  
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
   # HACK: this is hardcoded ;(    expand possibilities!  
   #my $crit = join(' AND ', @crits);  
   #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
   #return $self->sendCommand($sql);  
   #my $h = $self->{COREHANDLE}->remote($query->{node});  
   #my $res = $self->{COREHANDLE}->select($h, $h->{);  
   return $self->createCursor($query->{node});  
747  }  }
748    
749  sub eraseAll {  sub eraseAll {
# Line 591  sub getObject { Line 779  sub getObject {
779    return $object if $object;    return $object if $object;
780  }  }
781    
782  sub getObjectByGuid {  sub getObjectByGuid_old {
783    my $self = shift;    my $self = shift;
784    my $guid = shift;    my $guid = shift;
785    my $options = shift;    my $options = shift;
# Line 621  sub getObjectByGuid { Line 809  sub getObjectByGuid {
809        
810  }  }
811    
812  sub getObjectAsHash {  sub getObjectAsHash_old {
813    my $self = shift;    my $self = shift;
814    my $oid = shift;    my $oid = shift;
815    my $options = shift;      my $options = shift;  
# Line 636  sub getObjectAsHash { Line 824  sub getObjectAsHash {
824    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
825    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
826    my $cb; # = sub {};    my $cb; # = sub {};
827    
828      # deactivated way to get rid of used instances, if requested
829  =pod  =pod
830    if ($options->{destroy}) {      if ($options->{destroy}) {
831      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
832        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
833        my $object = shift;          my $object = shift;
834        #print Dumper($object);          #print Dumper($object);
835        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
836        #undef($object);          #undef($object);
837      };        };
838    }      }
839  =cut  =cut
840    
841    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
842    #$options->{cb}->{destroy}->($obj);  
843    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
844      
845    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
846    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
847    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
848        
849    # old (wrong) attempts to get rid of used instances, if requested      # V2:
850      #$obj->clear_refs;      #$obj->clear_refs;
851      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
852      #$obj->DESTROY;      #$obj->DESTROY;
# Line 686  sub testAvailability { Line 876  sub testAvailability {
876    return $storage->testAvailability();    return $storage->testAvailability();
877  }  }
878    
879    sub disconnect2 {
880      my $self = shift;
881      my $storage = $self->_getSubLayerHandle();
882      print "DISC!", "\n";
883    
884      my $storage_ll = $storage->{_COREHANDLE};
885      $storage_ll->disconnect();
886      
887      print Dumper($storage);
888      exit;
889      
890      #$self->{_COREHANDLE}
891      #$storage->disconnect();
892      $self->{dataStorageLayer}->disconnect();
893    }
894    
895    
896    sub createNode {
897      my $self = shift;
898      my $classname = shift;
899    
900      my $obj = $classname->new();
901      
902      my $attr_options = Class::Tangram::attribute_options($classname);
903      #print "Attribute Options: " . Dumper($attr_options);
904    
905      my $attr_types = Class::Tangram::attribute_types($classname);
906      #print "Attribute Types: " . Dumper($attr_types);
907      
908      foreach(keys %{$attr_types}) {
909        if($attr_types->{$_} eq 'string') {
910          $obj->{$_} = '';
911        } elsif($attr_types->{$_} eq 'int') {
912          $obj->{$_} = 0;
913        } elsif($attr_types->{$_} eq 'real') {
914          $obj->{$_} = 0;
915        } elsif($attr_types->{$_} eq 'rawdatetime') {
916          $obj->{$_} = '0000-00-00 00:00:00';
917        } elsif($attr_types->{$_} eq 'ref') {
918          if($attr_options->{$_}->{class}) {
919            $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
920          } else {
921            #$obj->{$_} = undef();
922          }
923        } elsif($attr_types->{$_} eq 'iarray') {
924            $obj->{$_} = [ ];
925        } elsif($attr_types->{$_} eq 'hash') {
926            $obj->{$_} = {  };
927        } elsif($attr_types->{$_} eq 'flat_hash') {
928            $obj->{$_} = { };
929        }
930      }
931      
932      #print "New Object: " . Dumper($obj);
933      
934      return $obj;
935    }
936    
937    
938      sub insertChildNode {
939        my $self = shift;
940        my $child_entry = shift;
941        my $query_args = shift;
942        
943        my $core = $self->{_COREHANDLE};
944        my $nodename = $query_args->{nodename};
945        
946        # get parent object
947        my $query = {
948              node => $query_args->{parent}->{nodename},
949              options => { GUID => $query_args->{parent}->{guid}, },
950              };
951        my $cursor = $self->sendQuery($query);
952        my $parent = $cursor->getNextEntry();
953    
954        # debug
955        #print "Parent_org: " . Dumper($parent);
956            
957        # Create child node object if isn't already done
958        # ($child_entry have to be the class name then...)
959        if(!ref($child_entry)) {
960          $child_entry = $self->createNode($child_entry);
961          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
962          #$core->insert($child_entry);
963          #print "Create child object [$nodename]: " . Dumper($child_entry);
964        }
965    
966        # get reference of tied node (seems, only on Linux node's are tied!!)
967        my $tied_node = tied $parent->{$nodename};
968    
969        # insert/change child entry at parent
970        #print "reference: " . ref($parent->{$nodename}) . "\n";
971        if(ref($parent->{$nodename}) eq 'ARRAY') {
972          # (seems, only on Linux node's are tied!!)
973          if($tied_node) {
974            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
975            # so a 'PUSH' is not implemented (which could be then done transparently)
976            my $array = $tied_node->FETCH;
977            push @$array, $child_entry;
978            $tied_node->STORE($array);
979            # node will be normaly untied at 'STORE'
980            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
981            else { undef $tied_node; }
982          } else {
983            push @{$parent->{$nodename}}, $child_entry;
984          }
985        }
986        elsif(ref($parent->{$nodename}) eq 'HASH') {
987          if(my $key = $query_args->{hash_key}) {
988            # (seems, only on Linux node's are tied!!)
989            if($tied_node) {
990              # same problem as with 'ARRAY':
991              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
992              my $hash = $tied_node->FETCH;
993              $hash->{$key} = $child_entry;
994              $tied_node->STORE($hash);
995              # node will be normaly untied at 'STORE'
996              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
997              else { undef $tied_node; }
998            } else {
999              $parent->{$nodename}->{$key} = $child_entry;
1000            }
1001          } else {
1002           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1003          }
1004        }
1005        else {
1006          $parent->{$nodename} = $child_entry;
1007        }
1008    
1009        # debug
1010        #print "Parent_new: " . Dumper($parent);
1011        
1012        # save parent
1013        $core->update($parent);
1014        
1015        # debug
1016        #print "Saved Parent: ". Dumper($parent);
1017        
1018        return $child_entry;
1019      }
1020    
1021    
1022  1;  1;
1023  __END__  __END__

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.38

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