After i parse a large .sql
file this is the printed output from a %hashtable
:
Key:AS_LINR
Value:
Name:DS_LSNE_DDD_TS_A
Type:view
Parents:DM_LINE_END MINA_TI_GRP_V
This is the %hash
:
$hashtable{$name}="Name:$name
Type:$type
Parents:@parents"."\n\n
".
"----------------------------"
;
I need to check each parent if he exists as a key
in the %hash
. If he does i need to update it and add a new filed named children: , i will add as a value to the field children the name where i first found the parent .Like in this example :
Key:DM_LINE_END
Value:
Name:DS_LSNE_DDD_TS_A
Type:view
Children:AS_LINR
And i need to do this for each Parent . I want to update a hash by adding new elements to it , and if the key of the has does not exist i have to create one . If i must explain better what i have to do please ask it in comments .
Here is my perl code :
my $var=0;
my @joinparents=();
use warnings;
my %hashtable;
open(DATA,'<','NaViews.sql') or die "Error $!";
open(Writer,'>','ResultFile.txt') or die "Error $!";
open(Writer1,'>','AuxResult.txt') or die "Error $!";
my @create_cmds = ();
my $create_cmd = "";
READ_DATA : while (<DATA>) {
chop;
my $ln = $_;
$ln =~ s/^\s+//;
$ln =~ s/\s+$//;
next READ_DATA if($ln =~ /^\-\-/);
next READ_DATA if($ln =~ /^REM/);
if($create_cmd ne "") {
$create_cmd = $create_cmd." ".$ln;
}
if($ln =~ /^create/i) {
$create_cmd = $ln;
}
elsif($ln =~ /\;$/) {
push @create_cmds, $create_cmd;
$create_cmd = "";
}
}
close DATA;
my @views = ();
foreach my $create_cmd (@create_cmds) {
$create_cmd =~ s/\s+/ /;
$create_cmd =~ s/^\s+//;
$create_cmd =~ s/\s+$//;
my $name = get_view($create_cmd);
my $type = get_type($create_cmd);
my $content = substr($create_cmd, 0, -1);
my @parents =();#get_parents();
my @children = ();#get_children();
#------------------------------------------------------------------------
my @froms = split(/ from\s+/i, $create_cmd);
my @joins = split(/ join /i, $create_cmd);
#parcurge mai multe for in aceeasi structura
#FOR FROM
# body...
foreach my $i (1..@froms-1) {
#print Writer1 "$froms[$i]"."\n\n";
my $from = (split(/ where |select | left | left | right | as /i, $froms[$i])) [0];
$from=~s/^\s+//;
$from=~s/\(+//;
my @Spaces = split(/, | , /,$from);
foreach my $x (0..@Spaces-1) {
my $SpaceFrom = (split(/ /,$Spaces[$x])) [0];
$SpaceFrom=~s/;//;
$SpaceFrom=~s/\)+//;
#print Writer1 $SpaceFrom."\n\n";
push(@parents,$SpaceFrom);
# print "\n\n".$SpaceFrom."\n\n";
# print Writer "\n\n".$SpaceFrom."\n\n";
}
foreach my $x (1..@joins-1){
#print "$joins[$i]"."\n\n";
my $join = (split(/ on /i,$joins[$x])) [0];
my $joinspace = (split(/ /i,$joins[$x])) [0];
#print Writer "\n\n".$join."\n\n";
#print Writer1 $joinspace."\n\n";
#"$joinspace\n\n";
push(@parents,$joinspace);
print Writer1"\n\n".$parents[$_]."\n\n";
}
}
push @views, [$name, $type, $content, @parents, @children];
$hashtable{$name}="[0]Name:$name
[1]Type:$type
[2]Content:$content
[3]Parents:@parents"."\n\n
".
"----------------------------";
}
print Writer "Key:$_
Value:
$hashtable{$_}\n" foreach (keys%hashtable);
#------------------------------------------------------------------------------
print_views(\@views);
exit;
#------------------------------------------------------------------------------
sub get_view {
my $create_cmd = $_[0];
my $tmp = (split(/ view | trigger | table /i, $create_cmd))[1];
$tmp =~ s/^\s+//;
my $view = (split(/\s+/, $tmp))[0];
return $view;
}
#-----------------------------------------------------------------------------
sub get_type{
my $create_cmd = $_[0];
my $tmp = (split(/ replace /i, $create_cmd))[1];
$tmp =~ s/^\s+//;
my $view = (split(/\s+/, $tmp))[0];
return $view;
}
#-----------------------------------------------------------------------------
sub get_parents {
}
sub get_children {
}
get_children();
close Writer1;
close Writer;
This is how a chunk of data i have to parse looks like :
create or replace view MINA_TI_GRP_V
as
select NVL(max(t1.interval_group),(select dm_group from sdate_dm_grp_v)) AS DM_GROUP,
(t2.interval_number) INTERVAL_NUMBER , t2.time_interval_s
from MINA_INTERVAL_CONTROL t2
left join DM_TI_GRP_DATE_TIME t1 on t2.time_interval_s >= t1.time_interval_s
group by t2.interval_number , t2.time_interval_s
order by t2.interval_number;