]> lifelog.hopto.org Git - PerlCNF.git/commitdiff
Imp. meta _MAP_TO_ for specified tables.
authorWill Budic <redacted>
Fri, 17 May 2024 19:15:36 +0000 (05:15 +1000)
committerWill Budic <redacted>
Fri, 17 May 2024 19:15:36 +0000 (05:15 +1000)
system/modules/CNFParser.pm
system/modules/CNFSQL.pm
tests/testSQL.pl

index 49a97a7efb3d645dc984b4532154f98466f512ac..524dc209f8ace67f2d47e004ef3ff15f2d5772b3 100644 (file)
@@ -691,7 +691,7 @@ sub doDataInstruction_{ my ($self,$e,$v,$t,$d)=@_;
             if(!@hdr && $isHeader){
                 my $ptr = CNFMeta::_metaTranslateDataHeader($isPostgreSQL,@a);
                 @hdr = @{$$ptr}; $isHeader =  0;
-                $self->SQL()->createTable($e,${$hdr[1]},$hdr[2]) if$add_as_SQLTable
+                $self->SQL()->createTable($e,${$hdr[1]},$hdr[2]) if $add_as_SQLTable
             }elsif(scalar @a > 0){
                if($isHeader){
                   $isHeader =  0;
index 799d78f8f8459764a41262758bab60fe0dbdb590..e92f83f293fcbdb139429b708546142c6ddbc51a 100644 (file)
@@ -13,7 +13,7 @@ use Tie::IxHash;
 
 use constant VERSION => '2.0';
 
-our %tables = (); our %tables_id_type = ();
+our (%tables, %tables_id_type, %tables_data_map);
 our %views  = ();
 our %mig    = ();
 our @sql    = ();
@@ -42,8 +42,10 @@ sub isPostgreSQL{shift; return $isPostgreSQL}
 # This subrotine is also a good example why using generic driver is not recomended.
 # Various SQL db server flavours meta info is def. handled differently and not updated in them.
 #
-# $map - In general is binding of an CNF table to its DATA property, header of the DATA instructed property is self column resolving.
-#        If assinged to an array the first element must contain the name,
+# $map - The synch binding of an CNF TABLE to its CNF DATA property,
+#         a header of the DATA instructed property with a meta  _SQL_TABLE_ tag is self column resolving
+#         based on ID not requiring this mapping now. To better explain, CNF data can have several data properties,
+#         with the map we programatically instruct which on is the right one, per various possible tables?
 #        @TODO 20231018 - Specifications page to be provided with examples for this.
 #
 sub initDatabase { my($self, $db, $do_not_auto_synch, $map, $st) = @_;
@@ -115,6 +117,7 @@ try{
                 }
         }
     }}
+    ###
     # By default we automatically data insert synchronize script with database state on every init.
     # If set $do_not_auto_synch = 1 we skip that if table is present, empty or not,
     # and if has been updated dynamically that is good, what we want. It is of external config. implementation choice.
@@ -183,7 +186,13 @@ try{
         if($data){
             my  $data_prp = %$data{$t};
             if(!$data_prp && $self->{data}){
-                $data_prp = %{$self->{data}}{$t};
+                if(%tables_data_map){
+                   $data_prp = %$data{$tables_data_map{$t}};
+                   if(!$data_prp){
+                      $self->{parser} ->error("Invalid data mapping for table $t -> $tables_data_map{$t}")
+                   }
+                }
+                $data_prp = %{$self->{data}}{$t} if !$data_prp;
             }
             if($data_prp){
                 my @hdr;
@@ -270,7 +279,7 @@ try{
                 }
                 $db->commit()
             }else{
-                $self->{parser}->log("CNFParser-> No data collection is available for $tbl\n");
+                $self->{parser}->log("CNFParser-> No data collection is available or mapped to $tbl\n");
             }
         }else{
             $self->{parser}->log("CNFParser-> No data collection scanned for $tbl\n");
@@ -294,7 +303,7 @@ catch{
 }
 return $self->{parser}-> const('$RELEASE_VER');
 }
-
+###
 sub _connectDB {
     my ($user, $pass, $source, $store, $path) = @_;
     if($path && ! -e $path){
@@ -314,6 +323,11 @@ sub _credentialsToArray{
 }
 
 sub createTable { my ($self, $name, $body, $idType) = @_;
+        if($body =~ s/${CNFMeta::_meta('MAP_TO')}/""/sexi){
+           $body =~ m/\s*(\w*)(.*)/gs;
+           $tables_data_map{$name} = $1;
+           $body = $2;
+        }
         $tables{$name} = "CREATE TABLE $name(\n$body);";
         $tables_id_type{$name} = $idType;
 }
index 9e38f4e7e5e939d8c81cc5ae985e3cfa8392ca87..a09a2d8d130d048e1cdb9d493d9033871e981990 100644 (file)
@@ -67,7 +67,7 @@ try{
    $test->case("Test CNFSQL script to data synch and map.");
    $cnf = CNFParser->new(undef,{DO_ENABLED=>1,DEBUG=>1,'%LOG'=>{console=>1}});
    $cnf->parse(undef,q(
-   <<TBL_A<TABLE>
+   <<TBL_A<TABLE>  _MAP_TO_ TBL_A_DATA
         "name"  varchar(28) NOT NULL,
         "ID" INTEGER NOT NULL,
         PRIMARY KEY ("ID" AUTOINCREMENT)
@@ -83,7 +83,8 @@ try{
    my $db = CNFSQL::_connectDB('test','test',$central->{DBI_SQL_SOURCE},$central->{DB}.'.db');
    $sql = $cnf->SQL();
    $sql -> {data } = $sql2->{parser}->data();
-   $sql -> initDatabase($db,0,{'TBL_A' => ['TBL_A_DATA','name','ID']});
+   #$sql -> initDatabase($db,0,{'TBL_A' => ['TBL_A_DATA','name','ID']});
+   $sql -> initDatabase($db,0);
     #
     #
     $test->done();