From 32c22cfd53ff270409238cb91f4ba4eedecf8b9f Mon Sep 17 00:00:00 2001 From: Will Budic Date: Sat, 18 May 2024 05:15:36 +1000 Subject: [PATCH] Imp. meta _MAP_TO_ for specified tables. --- system/modules/CNFParser.pm | 2 +- system/modules/CNFSQL.pm | 26 ++++++++++++++++++++------ tests/testSQL.pl | 5 +++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/system/modules/CNFParser.pm b/system/modules/CNFParser.pm index 49a97a7..524dc20 100644 --- a/system/modules/CNFParser.pm +++ b/system/modules/CNFParser.pm @@ -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; diff --git a/system/modules/CNFSQL.pm b/system/modules/CNFSQL.pm index 799d78f..e92f83f 100644 --- a/system/modules/CNFSQL.pm +++ b/system/modules/CNFSQL.pm @@ -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; } diff --git a/tests/testSQL.pl b/tests/testSQL.pl index 9e38f4e..a09a2d8 100644 --- a/tests/testSQL.pl +++ b/tests/testSQL.pl @@ -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( - < + < _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(); -- 2.34.1