| 1 | package CGI::AppBuilder::TaskLoads; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI; | ||
| 10 | use CGI::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | use File::Path; | ||
| 13 | use File::Copy; | ||
| 14 | use File::Basename; | ||
| 15 | use Archive::Tar; | ||
| 16 | use IO::File; | ||
| 17 | use Net::Rexec 'rexec'; | ||
| 18 | |||
| 19 | our $VERSION = 0.12; | ||
| 20 | require Exporter; | ||
| 21 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 22 | our @EXPORT = qw(); | ||
| 23 | our @EXPORT_OK = qw(ld_mdrstd get_ldrcfg build_dml cvt_ar2list cvt_ar2hr | ||
| 24 | get_insert_val get_update_val fmt_list2sel | ||
| 25 | ); | ||
| 26 | our %EXPORT_TAGS = ( | ||
| 27 | all => [@EXPORT_OK] | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | CGI::AppBuilder::TaskLoads - Loading files to a database | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use CGI::AppBuilder::TaskLoads; | ||
| 37 | |||
| 38 | my $sec = CGI::AppBuilder::TaskLoads->new(); | ||
| 39 | my ($sta, $msg) = $sec->exe_sql($ar); | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This class provides methods for reading and parsing configuration | ||
| 44 | files. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 49 | |||
| 50 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 51 | in CGI::AppBuilder for more details. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my ($s, %args) = @_; | ||
| 57 | return $s->SUPER::new(%args); | ||
| 58 | } | ||
| 59 | |||
| 60 | =head2 get_ldrcfg ($ar) | ||
| 61 | |||
| 62 | Input variables: | ||
| 63 | |||
| 64 | $ar - array ref containing the following variables: | ||
| 65 | |||
| 66 | Variables used or routines called: | ||
| 67 | |||
| 68 | None | ||
| 69 | |||
| 70 | How to use: | ||
| 71 | |||
| 72 | Return: array/hash array or its ref {$k}{$e} where $k = [v|1|..|study_id]; | ||
| 73 | $e contains | ||
| 74 | |||
| 75 | rdr - relative directory | ||
| 76 | mfn - meta file name, i.e., the xls contains domain and variable metadata | ||
| 77 | dfn - domain file name containing domain metadata | ||
| 78 | vfn - variable file name containing variable metadata | ||
| 79 | ofd - output file directory | ||
| 80 | dml - data manipulation act: A|D|I|U | ||
| 81 | var - hash array ref containing variables | ||
| 82 | |||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub get_ldrcfg { | ||
| 87 | my ($s, $ar) = @_; | ||
| 88 | |||
| 89 | my $prg = 'get_ldrcfg'; | ||
| 90 | my $ds = ($^O =~ /MSWin/i) ? '\\' : '/'; | ||
| 91 | my $sn = $ar->{sid}; # server id | ||
| 92 | $sn = $ar->{sel_sn1} if !$sn && exists $ar->{sel_sn1}; | ||
| 93 | my $ad = eval $s->set_param('all_dir', $ar); # all dir array | ||
| 94 | my $mlc = $s->set_param('mdr_ldr_cfg', $ar); # meta dataloader cfg file | ||
| 95 | my $dir = $ad->{$sn}{mdr}; # MDR dir | ||
| 96 | my $r = {}; # result array | ||
| 97 | if (!$sn) { | ||
| 98 | $s->echo_msg("ERR: ($prg) server id is not provided.", 0); | ||
| 99 | return wantarray ? %$r : $r; | ||
| 100 | } else { | ||
| 101 | $s->echo_msg("INFO: ($prg) server id is $sn.", 3); | ||
| 102 | } | ||
| 103 | if (! -d $dir) { | ||
| 104 | $s->echo_msg("ERR: ($prg) could not find dir - $dir.", 0); | ||
| 105 | return wantarray ? %$r : $r; | ||
| 106 | } else { | ||
| 107 | $s->echo_msg("INFO: ($prg) spec dir is $dir.", 3); | ||
| 108 | } | ||
| 109 | if (! $mlc || $mlc =~ /^\s*$/) { | ||
| 110 | $s->echo_msg("ERR: ($prg) mdr_ldr_cfg is not defined in the init file.", 0); | ||
| 111 | return wantarray ? %$r : $r; | ||
| 112 | } else { | ||
| 113 | $s->echo_msg("INFO: ($prg) mdr_ldr_cfg file is $dir.", 3); | ||
| 114 | } | ||
| 115 | my $fn = join $ds, $dir, $mlc; | ||
| 116 | if (! -f $fn ) { | ||
| 117 | $s->echo_msg("ERR: ($prg) $fn does not exist.", 0); | ||
| 118 | return wantarray ? %$r : $r; | ||
| 119 | } else { | ||
| 120 | $s->echo_msg("INFO: ($prg) using $fn...", 3); | ||
| 121 | } | ||
| 122 | my $rp = $s->read_cfg_file($fn); | ||
| 123 | |||
| 124 | # opendir DD, "$dir" or die "ERR: could not opendir - $dir: $!\n"; | ||
| 125 | # my @a = sort grep !/\.bak$/, (grep /\.xls$/i, readdir DD); | ||
| 126 | # closedir DD; | ||
| 127 | # get variables | ||
| 128 | my $vr = {}; | ||
| 129 | for my $i (0..$#$rp) { | ||
| 130 | my $k = lc $rp->[$i][0]; # global V | ||
| 131 | next if $k !~ /^V$/i; | ||
| 132 | my $k1 = $rp->[$i][1]; # empty or study_id | ||
| 133 | my $k2 = $rp->[$i][2]; # variable name | ||
| 134 | my $v = $rp->[$i][3]; # variable value | ||
| 135 | next if $k2 =~ /^\s*$/; # skip if the variable name is empty | ||
| 136 | $k = ($k1 =~ /^\s*$/) ? $k : $k1; | ||
| 137 | $vr->{$k}{$k2} = $v; | ||
| 138 | } | ||
| 139 | |||
| 140 | # get study configuration | ||
| 141 | $r->{v} = $vr->{v} if exists $vr->{v}; | ||
| 142 | for my $i (0..$#$rp) { | ||
| 143 | my $k = $rp->[$i][0]; # study_id | ||
| 144 | next if $k =~ /^V$/i; | ||
| 145 | my $rdr = $rp->[$i][1]; # rel_dir | ||
| 146 | $rdr =~ s/^\s*//g if $rdr; | ||
| 147 | $rdr =~ s/\s*$//g if $rdr; | ||
| 148 | my $mfn = ($rp->[$i][2]) ? $rp->[$i][2] : ''; # meta file name | ||
| 149 | $mfn =~ s/^\s*//g if $mfn; | ||
| 150 | $mfn =~ s/\s*$//g if $mfn; | ||
| 151 | my $dfn = $rp->[$i][3]; # domain file or sheet name | ||
| 152 | $dfn =~ s/^\s*//g if $dfn; | ||
| 153 | $dfn =~ s/\s*$//g if $dfn; | ||
| 154 | my $vfn = $rp->[$i][4]; # variable file or sheet name | ||
| 155 | $vfn =~ s/^\s*//g if $vfn; | ||
| 156 | $vfn =~ s/\s*$//g if $vfn; | ||
| 157 | my $ofd = ($rp->[$i][5]) ? $rp->[$i][5] : ''; # output file dir | ||
| 158 | $ofd =~ s/^\s*//g if $ofd; | ||
| 159 | $ofd =~ s/\s*$//g if $ofd; | ||
| 160 | my $act = (exists $rp->[$i][6])?$rp->[$i][6]:''; # DML type | ||
| 161 | $r->{$k} = { rdr=>$rdr, mfn=>$mfn, dfn=>$dfn,vfn=>$vfn,ofd=>$ofd, dml=>$act}; | ||
| 162 | $r->{$k}{var} = $vr->{$k} if exists $vr->{$k}; | ||
| 163 | for my $x (keys %{$vr->{v}}) { | ||
| 164 | next if exists $r->{$k}{var}{$x}; | ||
| 165 | $r->{$k}{var}{$x} = $vr->{v}{$x}; | ||
| 166 | } | ||
| 167 | }; | ||
| 168 | wantarray ? %$r : $r; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | =head2 ld_mdrstd($q,$ar) | ||
| 173 | |||
| 174 | Input variables: | ||
| 175 | |||
| 176 | $q - CGI class | ||
| 177 | $ar - array ref containing the following variables: | ||
| 178 | |||
| 179 | Variables used or routines called: | ||
| 180 | |||
| 181 | None | ||
| 182 | |||
| 183 | Return: $pr will contain the parameters adn output from running the PL/SQL. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | |||
| 188 | sub ld_mdrstd { | ||
| 189 | my ($s, $q, $ar) = @_; | ||
| 190 | |||
| 191 | my $prg = 'TaskLoads::ld_mdrstd'; | ||
| 192 | # 0. prepare parameters and check parameters | ||
| 193 | $s->echo_msg(" 0. running $prg...", 1); | ||
| 194 | my ($sid, $jid, $hid) = (); | ||
| 195 | $sid = $ar->{study_id} if exists $ar->{study_id} | ||
| 196 | && ($ar->{study_id} || $ar->{study_id} == 0); | ||
| 197 | $jid = $ar->{job_id} if exists $ar->{job_id} | ||
| 198 | && ($ar->{job_id} || $ar->{job_id} == 0); | ||
| 199 | $hid = $ar->{hjob_id} if exists $ar->{hjob_id} | ||
| 200 | && ($ar->{hjob_id} || $ar->{hjob_id} == 0); | ||
| 201 | |||
| 202 | if ($sid != 0 && !$sid) { | ||
| 203 | $s->echo_msg("ERR: ($prg) Study ID is required.", 0); return; | ||
| 204 | } | ||
| 205 | |||
| 206 | my $ds = ($^O =~ /MSWin/i) ? '\\' : '/'; | ||
| 207 | my $pid = $ar->{pid}; # parent id | ||
| 208 | my $sn = $ar->{sid}; # server id | ||
| 209 | my $ad = eval $s->set_param('all_dir', $ar); # all dir array | ||
| 210 | |||
| 211 | my $dir = $ad->{$sn}{mdr}; # mdr/map dir | ||
| 212 | # my $sqlout = $ad->{$sn}{sqlout}; # sql output dir | ||
| 213 | # my $odr = "$sqlout$ds$sn"; | ||
| 214 | $s->echo_msg("INFO: ($prg) dir=$dir",1); | ||
| 215 | croak "ERR: ($prg) could not find dir - $dir." if ! -d $dir; | ||
| 216 | |||
| 217 | # 1. get cfg information | ||
| 218 | $s->echo_msg(" 1. get cfg info for study $sid...", 1); | ||
| 219 | |||
| 220 | my $rp = $s->get_ldrcfg($ar); | ||
| 221 | croak("ERR: ($prg) loader cfg is not defined.") if ! %$rp; | ||
| 222 | croak("ERR: ($prg) loader cfg is not defined for study id - $sid.") | ||
| 223 | if ! exists $rp->{$sid}; | ||
| 224 | |||
| 225 | my $vrs = 'dn_tab,vn_tab,sl_col,fk_col,fk_var,dn_key,vn_key'; | ||
| 226 | for my $k (split /,/, $vrs) { | ||
| 227 | croak "ERR: ($prg) missing variable $k." if ! exists $rp->{$sid}{var}{$k}; | ||
| 228 | } | ||
| 229 | my $rdr = join $ds, $dir, $rp->{$sid}{rdr}; | ||
| 230 | my $mfn = $rp->{$sid}{mfn}; | ||
| 231 | $mfn = ($mfn) ? (join $ds,$rdr,$mfn) : ''; | ||
| 232 | my $dfn = ($mfn) ? $rp->{$sid}{dfn} : (join $ds, $rdr,$rp->{$sid}{dfn}); | ||
| 233 | my $vfn = ($mfn) ? $rp->{$sid}{vfn} : (join $ds, $rdr,$rp->{$sid}{vfn}); | ||
| 234 | my $sdr = strftime "%Y$ds%m$ds%d", localtime; | ||
| 235 | my $ofd = join $ds, $dir, $rp->{$sid}{ofd}, $sdr; | ||
| 236 | |||
| 237 | my $dml_type = $rp->{$sid}{dml}; | ||
| 238 | $dml_type = ($dml_type) ? $dml_type : 'A'; | ||
| 239 | |||
| 240 | if (!-d $rdr) { | ||
| 241 | croak "ERR: ($prg) could not find - $rdr<br>\n"; | ||
| 242 | } | ||
| 243 | if (!-d $ofd) { | ||
| 244 | $s->echo_msg("INFO: ($prg) make dir - $ofd.", 2); | ||
| 245 | eval { mkpath($ofd,0,0777) }; | ||
| 246 | # mkdir $ofd; | ||
| 247 | croak "ERR: ($prg) could not mkdir - $ofd: $!: $@<br>\n" if ($@); | ||
| 248 | system("chmod -R ugo+w $ofd") if ($^O !~ /^MSWin/i); # non window | ||
| 249 | } | ||
| 250 | |||
| 251 | if ($mfn && ! -f $mfn) { | ||
| 252 | croak "ERR: ($prg) could not find mfn file - $mfn"; | ||
| 253 | } | ||
| 254 | $s->echo_msg("INFO: ($prg) RDR=$rdr",1); | ||
| 255 | $s->echo_msg("INFO: ($prg) OFD=$ofd",1); | ||
| 256 | $s->echo_msg("INFO: ($prg) MFN=$mfn",1); | ||
| 257 | |||
| 258 | |||
| 259 | # 2. read the XLS file | ||
| 260 | $s->echo_msg(" 2. read configuration files...", 1); | ||
| 261 | my ($dr, $vr) = (); | ||
| 262 | if ($mfn) { | ||
| 263 | my $n1 = $rp->{$sid}{dfn}; | ||
| 264 | my $n2 = $rp->{$sid}{vfn}; | ||
| 265 | $s->echo_msg("INFO: MFN - $mfn ($n1,$n2).", 1); | ||
| 266 | $dr = $s->read_xls($mfn, $n1); | ||
| 267 | $vr = $s->read_xls($mfn, $n2); | ||
| 268 | } else { | ||
| 269 | $s->echo_msg("INFO: DFN - $dfn.", 1); | ||
| 270 | $dr = $s->read_cfg_file($dfn,'A',','); | ||
| 271 | $s->echo_msg("INFO: vFN - $vfn.", 1); | ||
| 272 | $vr = $s->read_cfg_file($vfn,'A',','); | ||
| 273 | } | ||
| 274 | |||
| 275 | # $s->disp_param($dr); | ||
| 276 | # $s->disp_param($vr); | ||
| 277 | |||
| 278 | $s->echo_msg("INFO: ($prg) no of records for domain: $#$dr", 1); | ||
| 279 | $s->echo_msg("INFO: ($prg) no of records for variable: $#$vr", 1); | ||
| 280 | |||
| 281 | $s->echo_msg($dr, 5); | ||
| 282 | $s->echo_msg($vr, 5); | ||
| 283 | |||
| 284 | # 3. build SQL statement for cc_domains | ||
| 285 | $s->echo_msg(" 3. build sql statement for cc_domains...", 1); | ||
| 286 | my $sql = []; # sql array | ||
| 287 | my $rf = {}; # ref for ldr parameter | ||
| 288 | $rf->{tbn} = $rp->{$sid}{var}{dn_tab}; | ||
| 289 | $rf->{kcn} = $rp->{$sid}{var}{dn_key}; | ||
| 290 | $rf->{scn} = $rp->{$sid}{var}{sl_col}; | ||
| 291 | $rf->{scv} = $sid; | ||
| 292 | $rf->{act} = $rp->{$sid}{dml}; | ||
| 293 | |||
| 294 | $rf->{dn_fmt} = ''; | ||
| 295 | $rf->{del_ctb} = ''; | ||
| 296 | $rf->{del_ptb} = ''; | ||
| 297 | if ($rf->{act} =~ /^D/i) { | ||
| 298 | # delete child table first | ||
| 299 | my $ptb = $rp->{$sid}{var}{dn_tab}; | ||
| 300 | my $ctb = $rp->{$sid}{var}{vn_tab}; | ||
| 301 | my $fkc = $rp->{$sid}{var}{fk_col}; | ||
| 302 | my $fkn = $rp->{$sid}{var}{fk_var}; | ||
| 303 | my $scn = $rp->{$sid}{var}{sl_col}; | ||
| 304 | my $t = "PROMPT Deleting $ctb with $scn = $sid...\n"; | ||
| 305 | $t .= "DELETE $ctb WHERE $fkc IN (\n"; | ||
| 306 | $t .= " SELECT $fkc FROM $ptb WHERE $scn = $sid);\n"; | ||
| 307 | $t .= "commit;\n\n"; | ||
| 308 | $rf->{del_ctb} = $t; | ||
| 309 | $t = "PROMPT Deleting $ptb with $scn = $sid...\n"; | ||
| 310 | $t .= "DELETE $ptb WHERE $scn = $sid;\ncommit;\n\n"; | ||
| 311 | $rf->{del_ptb} = $t; | ||
| 312 | # SELECT dn_id FROM cc_domain WHERE study_id = 1 and domain_name = 'AE' | ||
| 313 | my $dn_fmt = "(SELECT MAX($fkc) FROM $ptb WHERE "; | ||
| 314 | $dn_fmt .= "$scn = $sid AND UPPER($fkn) = '%s')"; | ||
| 315 | $rf->{dn_fmt} = $dn_fmt; | ||
| 316 | } | ||
| 317 | |||
| 318 | my $sq1 = $s->build_dml($dr, $rf, $ar); | ||
| 319 | $s->echo_msg("INFO: number of SQL statements: $#$sq1",1); | ||
| 320 | $s->echo_msg($rf->{c1_crf}, 3); | ||
| 321 | $s->echo_msg("SQL: ($prg) @$sq1",5); | ||
| 322 | |||
| 323 | # 4. build SQL statement for cc_variables | ||
| 324 | $s->echo_msg(" 4. build sql statement for cc_variables...", 1); | ||
| 325 | $rf->{tbn} = $rp->{$sid}{var}{vn_tab}; | ||
| 326 | $rf->{kcn} = $rp->{$sid}{var}{vn_key}; | ||
| 327 | $rf->{ptb} = $rp->{$sid}{var}{dn_tab}; # parent table name | ||
| 328 | $rf->{fkn} = $rp->{$sid}{var}{fk_var}; # fk column name: domain_name | ||
| 329 | $rf->{fkc} = $rp->{$sid}{var}{fk_col}; # fk column name: dn_id | ||
| 330 | my $sq2 = $s->build_dml($vr, $rf, $ar); | ||
| 331 | $s->echo_msg("INFO: number of SQL statements: $#$sq2",1); | ||
| 332 | $s->echo_msg($rf->{c2_crf}, 3); | ||
| 333 | $s->echo_msg("SQL: ($prg) @$sq2",5); | ||
| 334 | |||
| 335 | # 5. output to file | ||
| 336 | $s->echo_msg(" 5. output to files...", 1); | ||
| 337 | |||
| 338 | my $rt = ($rp->{$sid}{mfn}) ? $rp->{$sid}{mfn} : $rp->{$sid}{dfn}; | ||
| 339 | $rt =~ s/\.\w*$//i; # get root name | ||
| 340 | my $f1n = $rp->{$sid}{dfn}; | ||
| 341 | my $f2n = $rp->{$sid}{vfn}; | ||
| 342 | |||
| 343 | my $dtm = strftime "%Y%m%d_%H%M%S", localtime; | ||
| 344 | my $ot1 = "${rt}_${f1n}_$dtm.sql"; # parent output file name | ||
| 345 | my $ot2 = "${rt}_${f2n}_$dtm.sql"; # child output file name | ||
| 346 | my $fn1 = join $ds, $ofd,$ot1; # output file full name | ||
| 347 | my $fn2 = join $ds, $ofd,$ot2; # output file full name | ||
| 348 | |||
| 349 | my $m1 = "File - $ot1 was written to $ofd"; | ||
| 350 | my $m2 = "File - $ot2 was written to $ofd"; | ||
| 351 | |||
| 352 | $s->echo_msg("INFO: writing $fn1 ...", 1); | ||
| 353 | open FN, ">$fn1" or croak "ERR: could not write to $fn1: $!\n"; | ||
| 354 | for my $i (0..$#$sq1) { | ||
| 355 | # print FN "PROMPT $i/$#$sql \n"; | ||
| 356 | print FN "$sq1->[$i]"; | ||
| 357 | print FN "commit;\n" if ($i && $i % 100 == 0); | ||
| 358 | } | ||
| 359 | # print FN "exit\n"; | ||
| 360 | close FN; | ||
| 361 | $s->echo_msg($m1, 1); | ||
| 362 | |||
| 363 | $s->echo_msg("INFO: writing $fn2 ...", 1); | ||
| 364 | open FN, ">$fn2" or croak "ERR: could not write to $fn2: $!\n"; | ||
| 365 | for my $i (0..$#$sq2) { | ||
| 366 | # print FN "PROMPT $i/$#$sql \n"; | ||
| 367 | print FN "$sq2->[$i]"; | ||
| 368 | print FN "commit;\n" if ($i % 100 == 0); | ||
| 369 | } | ||
| 370 | # print FN "exit\n"; | ||
| 371 | close FN; | ||
| 372 | $s->echo_msg($m2, 1); | ||
| 373 | |||
| 374 | # 6. run sql | ||
| 375 | $s->echo_msg(" 6. run sql statements ...", 1); | ||
| 376 | |||
| 377 | my $rr = []; | ||
| 378 | push @$rr, "$m1\n"; | ||
| 379 | push @$rr, "$m2\n"; | ||
| 380 | $s->echo_msg("INFO: number of executions: " . ($#$rr+1),1); | ||
| 381 | $s->call_plsql($rr, $ar); | ||
| 382 | |||
| 383 | # wantarray ? @$r : $r; | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | =head2 build_dml($rr, $rf, $ar) | ||
| 388 | |||
| 389 | Input variables: | ||
| 390 | |||
| 391 | $rr - array ([$i]{$k}) with new data | ||
| 392 | $rf - hash array with the following elements | ||
| 393 | tbn - table name: cc_domains | ||
| 394 | kcn - key column name: dn_id | ||
| 395 | scn - select column name: study_id | ||
| 396 | scv - select column value: 1 | ||
| 397 | act - DML type: A-auto, D-delete, I-insert, U-update | ||
| 398 | $ar - hash array for system wide parameters | ||
| 399 | |||
| 400 | Variables used or routines called: | ||
| 401 | |||
| 402 | None | ||
| 403 | |||
| 404 | Return: This procedure returns an array containing DML SQL statements and | ||
| 405 | populate the $rf with the following new elements: | ||
| 406 | |||
| 407 | c1_crf - column definition array [$i]{$k} for the table where $k= | ||
| 408 | column_name,data_type,data_length | ||
| 409 | c1_vars - a list of column names separated by comma in the table | ||
| 410 | c2_crf - column definition array [$i]{$k} for the new data where $k= | ||
| 411 | column_name,data_type,data_length | ||
| 412 | c2_vars - a list of column names separated by comma in the data | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | |||
| 417 | sub build_dml { | ||
| 418 | my ($s, $rr, $rf, $ar) = @_; | ||
| 419 | |||
| 420 | my $prg = 'TaskLoads::build_dml'; | ||
| 421 | my $tbn = $rf->{tbn}; | ||
| 422 | my $kcn = $rf->{kcn}; | ||
| 423 | my $act = $rf->{act}; $act =~ s/^\s*//; $act =~ s/\s*$//; | ||
| 424 | my $scn = $rf->{scn}; | ||
| 425 | my $scv = $rf->{scv}; | ||
| 426 | |||
| 427 | # $s->disp_param($rr); | ||
| 428 | |||
| 429 | # my $cns = lc $rf->{cns}; | ||
| 430 | # $rf->{list_cns} = $s->_list_cns($cns); | ||
| 431 | |||
| 432 | |||
| 433 | # get column names for from the table | ||
| 434 | my $whr = " WHERE table_name = '" . uc($tbn) . "' "; | ||
| 435 | $whr .= " ORDER BY column_id "; | ||
| 436 | my $sel_cns = 'column_name,data_type,data_length' ; | ||
| 437 | my $c1 = $s->run_sqlcmd($ar, $sel_cns, 'user_tab_columns', $whr); | ||
| 438 | for my $i (0..$#$c1) {$c1->[$i]{column_name} = lc $c1->[$i]{column_name};} | ||
| 439 | my $c1_vars = $s->cvt_ar2list($c1); | ||
| 440 | $rf->{c1_vars} = $c1_vars; | ||
| 441 | $rf->{c1_crf} = $c1; | ||
| 442 | |||
| 443 | my $r1 = []; | ||
| 444 | if ($act =~ /^(A|U)/i) { | ||
| 445 | # get data from the table | ||
| 446 | $whr = " WHERE $scn = $scv ORDER BY $scn "; | ||
| 447 | $r1 = $s->run_sqlcmd($ar, $c1_vars, $tbn, $whr); | ||
| 448 | } | ||
| 449 | |||
| 450 | # convert the new data set into the same format | ||
| 451 | my ($r2, $c2) = $s->cvt_ar2hr($rr, $rf->{c1_vars}); | ||
| 452 | my $c2_vars = $s->cvt_ar2list($c2); | ||
| 453 | $rf->{c2_vars} = $c2_vars; | ||
| 454 | $rf->{c2_crf} = $c2; | ||
| 455 | |||
| 456 | |||
| 457 | # convert $r1 to hash hash array | ||
| 458 | my $r3 = {}; | ||
| 459 | if (@{$rf->{c1_crf}} && exists $r1->[0]{$kcn}) { | ||
| 460 | for my $i (0..$#$r1) { | ||
| 461 | my $k = $r1->[$i]{$kcn}; | ||
| 462 | $r3->{$k} = $r1->[$i]; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | my $sql = []; | ||
| 466 | my ($t,$i,$m) = ('',-1,''); | ||
| 467 | if ($act =~ /^D/i) { | ||
| 468 | if (exists $rf->{fkc} || exists $rf->{ptb}) { # child table | ||
| 469 | push @$sql, "/*\n $rf->{del_ctb}*/\n\n" if $rf->{del_ctb}; | ||
| 470 | } else { | ||
| 471 | push @$sql, $rf->{del_ctb} if $rf->{del_ctb}; | ||
| 472 | push @$sql, $rf->{del_ptb} if $rf->{del_ptb}; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | $t = ''; | ||
| 477 | for my $i (1..$#$r2) { # start with 1 since the first row is column names | ||
| 478 | my $k = (exists $r2->[$i]{$kcn}) ? $r2->[$i]{$kcn} : ''; | ||
| 479 | $m = "(" . (uc $act) . "):$k "; | ||
| 480 | $m .= $r2->[$i]{$kcn} if exists $r2->[$i]{$kcn}; | ||
| 481 | $m .= " $r2->[$i]{domain_name}" if exists $r2->[$i]{domain_name}; | ||
| 482 | $m .= "." . $r2->[$i]{var_name} if exists $r2->[$i]{var_name}; | ||
| 483 | |||
| 484 | if ($act =~ /^D/ || ! @{$rf->{c1_crf}}) { | ||
| 485 | $t = "PROMPT $i/$#$r2 - inserting $m...\n"; | ||
| 486 | $t .= $s->get_insert_val($r2->[$i], $rf); | ||
| 487 | } else { | ||
| 488 | if ($k) { # record exists | ||
| 489 | if ($act =~ /^I/) { | ||
| 490 | $t = "PROMPT $i/$#$r2 - record exists skipping $m...\n"; | ||
| 491 | next; | ||
| 492 | } | ||
| 493 | $rf->{kcv} = $k; | ||
| 494 | $t = "PROMPT $i/$#$r2 - updating $m...\n"; | ||
| 495 | $t .= $s->get_update_val($r2->[$i], $rf, $r3->{$k}); | ||
| 496 | } else { | ||
| 497 | if ($act =~ /^U/) { | ||
| 498 | $t = "PROMPT $i/$#$r2 - record does not exist skipping $m...\n"; | ||
| 499 | next; | ||
| 500 | } else { | ||
| 501 | $t = "PROMPT $i/$#$r2 - inserting $m...\n"; | ||
| 502 | $t .= $s->get_insert_val($r2->[$i], $rf); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | push @$sql, $t; | ||
| 507 | } | ||
| 508 | $s->echo_msg("SQL: ($prg) $sql",5); | ||
| 509 | |||
| 510 | wantarray ? @$sql : $sql; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub get_insert_val { | ||
| 514 | my ($s, $hr, $rf) = @_; | ||
| 515 | |||
| 516 | my $prg = 'TaskLoads::get_insert_val'; | ||
| 517 | if (! exists $rf->{c1_vars} && ! exists $rf->{c2_vars}) { | ||
| 518 | $s->echo_msg("ERR: ($prg) c1_vars and c2_vars are missing",0); | ||
| 519 | return ''; | ||
| 520 | } | ||
| 521 | |||
| 522 | if (! exists $rf->{c1_crf} && ! exists $rf->{c2_crf}) { | ||
| 523 | $s->echo_msg("ERR: ($prg) c1_crf and c2_crf are missing",0); | ||
| 524 | return ''; | ||
| 525 | } | ||
| 526 | |||
| 527 | my $tab = $rf->{tbn}; | ||
| 528 | my $cns = (exists $rf->{c1_vars}) ? $rf->{c1_vars} : $rf->{c2_vars}; | ||
| 529 | $cns = $s->fmt_list2sel($cns); | ||
| 530 | my $seq = $rf->{tbn} . "_seq.nextval"; | ||
| 531 | my $cr = (exists $rf->{c1_crf}) ? $rf->{c1_crf} : $rf->{c2_crf}; | ||
| 532 | |||
| 533 | my $dn_id = ''; | ||
| 534 | my $dn_fmt = $rf->{dn_fmt}; | ||
| 535 | |||
| 536 | my $t = ''; | ||
| 537 | foreach my $j (0..$#$cr) { | ||
| 538 | my $k = lc $cr->[$j]{column_name}; | ||
| 539 | my $p = $cr->[$j]{data_type}; | ||
| 540 | my $v = (exists $hr->{$k}) ? $hr->{$k} : ''; | ||
| 541 | $v =~ s/\s*$//g if $v; # remove ending spaces | ||
| 542 | $v =~ s/'/''/g if $v; # double quote | ||
| 543 | $v =~ s/^\s*\n+//mg if $v; # remove multiple line breaks | ||
| 544 | $v =~ s/\&/and/g if $v; # replace & with and | ||
| 545 | $v =~ s/^\s*$//g if $v; # remove all spaces | ||
| 546 | $t .= ($t) ? ' , ' : ' '; | ||
| 547 | if ($k =~ /^$rf->{kcn}/i) { # dn_id/var_id | ||
| 548 | $t .= "$seq\n"; | ||
| 549 | } elsif (exists $rf->{fkc} && $rf->{fkc} && $k =~ /^$rf->{fkc}/i) { # dn_id | ||
| 550 | if (exists $rf->{ptb}) { # it is a child table: cc_variables | ||
| 551 | my $fk = $rf->{fkn}; | ||
| 552 | $dn_id = sprintf $dn_fmt, uc $hr->{$fk}; | ||
| 553 | $t .= ($v) ? "$v\n" : "$dn_id\n"; | ||
| 554 | } else { | ||
| 555 | $t .= "$seq\n"; | ||
| 556 | } | ||
| 557 | } elsif ($k =~ /^$rf->{scn}/i) { # study_id | ||
| 558 | $t .= ($v) ? "$v\n" : "$rf->{scv}\n"; | ||
| 559 | } elsif ($k =~ /^db_user/i) { | ||
| 560 | $t .= ($v) ? "'$v'\n" : "USER\n"; | ||
| 561 | } elsif ($k =~ /^os_user/i) { | ||
| 562 | $t .= ($v) ? "'$v'\n" : "cc_context_pkg.get_os_user\n"; | ||
| 563 | } elsif ($k =~ /^app_user/i) { | ||
| 564 | $t .= ($v) ? "'$v'\n" : "cc_context_pkg.get_app_user\n"; | ||
| 565 | } else { | ||
| 566 | if ($p =~ /^date/i) { | ||
| 567 | $t .= "sysdate\n"; | ||
| 568 | } elsif ($p =~ /^(num|int|float)/i) { | ||
| 569 | $t .= (("$v" eq "0")||($v && $v =~ /^[\d\.]+$/)) ? "$v\n" : "null\n"; | ||
| 570 | } else { | ||
| 571 | $t .= ($v) ? "'$v'\n" : "null\n"; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | } | ||
| 575 | # $t =~ s/^(\s*),/$1 /; | ||
| 576 | $t = "INSERT INTO $tab (\n$cns ) VALUES (\n$t );\n"; | ||
| 577 | return $t; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | sub get_update_val { | ||
| 582 | my ($s, $hr, $rf, $h2) = @_; | ||
| 583 | |||
| 584 | my $cr = (exists $rf->{c1_crf}) ? $rf->{c1_crf} : $rf->{c2_crf}; | ||
| 585 | my $seq = $rf->{tbn} . "_seq.nextval"; | ||
| 586 | |||
| 587 | my $dn_id = ''; | ||
| 588 | my $dn_fmt = $rf->{dn_fmt}; | ||
| 589 | |||
| 590 | my $t = ''; | ||
| 591 | foreach my $j (0..$#$cr) { | ||
| 592 | my $k = lc $cr->[$j]{column_name}; | ||
| 593 | my $p = $cr->[$j]{data_type}; | ||
| 594 | my $v = (exists $hr->{$k}) ? $hr->{$k} : ''; | ||
| 595 | $v =~ s/\s*$//g if $v; # remove ending spaces | ||
| 596 | $v =~ s/'/''/g if $v; # double quote | ||
| 597 | $v =~ s/^\s*\n+//mg if $v; # remove multiple line breaks | ||
| 598 | $v =~ s/\&/and/g if $v; # replace & with and | ||
| 599 | $v =~ s/^\s*$//g if $v; # remove all spaces | ||
| 600 | my $v2 = $h2->{$k}; | ||
| 601 | next if $v =~ /^\s*$/; # skip empty string | ||
| 602 | next if (lc $v) eq (lc $v2); # skip if the value has not changed | ||
| 603 | next if $k eq $rf->{kcn}; # skip the key column: dn_id/var_id | ||
| 604 | |||
| 605 | $v = uc $v; # convert to upper case | ||
| 606 | if (exists $rf->{fkc} && $rf->{fkc} && $k =~ /^$rf->{fkc}/i) { # dn_id | ||
| 607 | if (exists $rf->{ptb}) { # it is a child table: cc_variables | ||
| 608 | my $fk = $rf->{fkn}; | ||
| 609 | $dn_id = sprintf $dn_fmt, uc $hr->{$fk}; | ||
| 610 | $t .= ($v) ? "$v\n" : "$dn_id\n"; | ||
| 611 | } else { | ||
| 612 | $t .= "$seq\n"; | ||
| 613 | } | ||
| 614 | } elsif ($k =~ /^$rf->{scn}/i) { # | ||
| 615 | $t .= ($v) ? "$v\n" : "$rf->{scv}\n"; | ||
| 616 | } elsif ($k =~ /^db_user/i) { | ||
| 617 | $t .= ($v) ? "'$v'\n" : " , USER\n"; | ||
| 618 | } elsif ($k =~ /^os_user/i) { | ||
| 619 | $t .= ($v) ? "'$v'\n" : "cc_context_pkg.get_os_user\n"; | ||
| 620 | } elsif ($k =~ /^app_user/i) { | ||
| 621 | $t .= ($v) ? "'$v'\n" : "cc_context_pkg.get_app_user\n"; | ||
| 622 | } elsif ($k =~ /date$/i) { | ||
| 623 | $t .= "sysdate\n"; | ||
| 624 | } else { | ||
| 625 | if ($p =~ /^date/i) { | ||
| 626 | $t .= "sysdate\n"; | ||
| 627 | } elsif ($p =~ /^(num|int|float)/i) { | ||
| 628 | $t .= ($v==0||($v && $v =~ /^[\d\.]+$/)) ? "$v\n" : "null\n"; | ||
| 629 | } else { | ||
| 630 | $t .= ($v) ? "'$v'\n" : "null\n"; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | } | ||
| 634 | # $t =~ s/^(\s*),/$1 /; | ||
| 635 | $t = "UPDATE $rf->{tbn} $t WHERE $rf->{kcn} = $rf->{kcv};\n"; | ||
| 636 | |||
| 637 | return $t; | ||
| 638 | } | ||
| 639 | |||
| 640 | |||
| 641 | =head2 cvt_ar2list($cr) | ||
| 642 | |||
| 643 | Input variables: | ||
| 644 | |||
| 645 | $cr - array ref containing column definitions ([$i]{$k}) where $k is | ||
| 646 | column_name, data_type, and data_length | ||
| 647 | |||
| 648 | Variables used or routines called: | ||
| 649 | |||
| 650 | None | ||
| 651 | |||
| 652 | Return: a list containing column names separated by comma | ||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub cvt_ar2list { | ||
| 657 | my ($s, $cr) = @_; | ||
| 658 | |||
| 659 | my $t = ''; | ||
| 660 | for my $i (0..$#$cr) { | ||
| 661 | my $k = lc $cr->[$i]{column_name}; | ||
| 662 | $k =~ s/^\s*//; $k =~ s/\s*$//; | ||
| 663 | $t .= ($t) ? ",$k" : $k; | ||
| 664 | } | ||
| 665 | return $t; | ||
| 666 | } | ||
| 667 | |||
| 668 | =head2 fmt_list2sel ($cns) | ||
| 669 | |||
| 670 | Input variables: | ||
| 671 | |||
| 672 | $cns - column names separated by comma | ||
| 673 | |||
| 674 | Variables used or routines called: | ||
| 675 | |||
| 676 | None | ||
| 677 | |||
| 678 | Return: a formated list to be used in SELECT statement. | ||
| 679 | |||
| 680 | =cut | ||
| 681 | |||
| 682 | sub fmt_list2sel { | ||
| 683 | my ($s, $cns) = @_; | ||
| 684 | my $t = ''; | ||
| 685 | foreach my $k (split /,/, $cns) { | ||
| 686 | $t .= ($t) ? " , $k\n" : " $k\n"; | ||
| 687 | } | ||
| 688 | return $t; | ||
| 689 | } | ||
| 690 | |||
| 691 | |||
| 692 | =head2 cvt_ar2hr($ar, $vars) | ||
| 693 | |||
| 694 | Input variables: | ||
| 695 | |||
| 696 | $ar - array ref containing two dimensional data elements in an array | ||
| 697 | $vars - variable names sparated by comma | ||
| 698 | |||
| 699 | Variables used or routines called: | ||
| 700 | |||
| 701 | None | ||
| 702 | |||
| 703 | Return: an array with hash elements $r->[$i]{$k}. | ||
| 704 | |||
| 705 | =cut | ||
| 706 | |||
| 707 | sub cvt_ar2hr { | ||
| 708 | my ($s, $rr, $vars) = @_; | ||
| 709 | |||
| 710 | my $r = []; # record array | ||
| 711 | my $c = []; # column array | ||
| 712 | |||
| 713 | # get column names | ||
| 714 | my $cns = {}; | ||
| 715 | if ($vars) { | ||
| 716 | foreach my $k (split /,/, lc $vars) { $cns->{$k} = 1; } | ||
| 717 | } | ||
| 718 | # the first row contains the column names | ||
| 719 | for my $j (0..$#{$rr->[0]}) { | ||
| 720 | my $k = lc $rr->[0][$j]; | ||
| 721 | $k =~ s/^\s*//; $k =~ s/\s*$//; $k =~ s/ /_/g; | ||
| 722 | if (exists $cns->{$k}) { | ||
| 723 | $r->[0]{$k} = $k; | ||
| 724 | } else { | ||
| 725 | $s->echo_msg(" Col: $k - does not exist in table.", 2) if $vars; | ||
| 726 | } | ||
| 727 | $c->[$j]{column_name} = $k; | ||
| 728 | } | ||
| 729 | for my $i (1..$#$rr) { | ||
| 730 | for my $j (0..$#{$rr->[$i]}) { | ||
| 731 | my $k = lc $c->[$j]{column_name}; | ||
| 732 | my $v = $rr->[$i][$j]; | ||
| 733 | $r->[$i]{$k} = $v; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | # get column type | ||
| 737 | for my $j (0..$#$c) { | ||
| 738 | my $cnt = -1; | ||
| 739 | my $len = -1; | ||
| 740 | for my $i (1..$#$rr) { | ||
| 741 | my $k = lc $c->[$j]{column_name}; | ||
| 742 | my $v = $rr->[$i][$j]; | ||
| 743 | if ($v =~ /^[\d\.]+$/) { ++$cnt; } | ||
| 744 | $len = (length($v) > $len) ? length($v) : $len; | ||
| 745 | } | ||
| 746 | $c->[$j]{data_type} = ($cnt == $#$rr) ? 'number' : 'varchar2'; | ||
| 747 | $c->[$j]{data_length} = ($len) ? $len : 1; | ||
| 748 | } | ||
| 749 | return ($r, $c); | ||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | =head2 coding | ||
| 754 | |||
| 755 | sub prt_txt_file { | ||
| 756 | my ($s, $fn,$ar) = @_; | ||
| 757 | my ($fname, $path, $sfx) = fileparse($fn,qr{\..*}); | ||
| 758 | my $t1 = "<center><b>$fname$sfx</b></center>\n<hr>\n<pre>\n"; | ||
| 759 | my $typ = | ||
| 760 | my $w = | ||
| 761 | my $st = | ||
| 762 | my $t = ""; | ||
| 763 | my $n = $w; | ||
| 764 | open FILE, "<$fn" or die "ERR: could not open $fn: $!\n"; | ||
| 765 | while (<FILE>) { | ||
| 766 | s/</\</g; s/>/\>/g; | ||
| 767 | s//^L/g; # change the non-printable char to printable char | ||
| 768 | if ($st) { | ||
| 769 | s/($st)/<font color=red>$1<\/font>/ig; | ||
| 770 | } | ||
| 771 | my ($tt, $i) = ($_, -1); | ||
| 772 | if (length($tt) < $n || !$w ) { | ||
| 773 | $t .= $tt; next; | ||
| 774 | } | ||
| 775 | while (length($tt) >= $n) { | ||
| 776 | ++$i; | ||
| 777 | if ($i) { # the second line | ||
| 778 | $t .= " "x4 . substr($tt, 0, $n) . "\n"; | ||
| 779 | } else { # first line | ||
| 780 | $t .= substr($tt, 0, $n) . "\n"; | ||
| 781 | } | ||
| 782 | $tt = substr($tt, $n); | ||
| 783 | } | ||
| 784 | $t .= " "x4 . $tt; | ||
| 785 | } | ||
| 786 | close FILE; | ||
| 787 | return $t if $typ; | ||
| 788 | print "$t1$t</pre><br>\n"; | ||
| 789 | } | ||
| 790 | |||
| 791 | =cut | ||
| 792 | |||
| 793 | 1; | ||
| 794 | |||
| 795 | =head1 HISTORY | ||
| 796 | |||
| 797 | =over 4 | ||
| 798 | |||
| 799 | =item * Version 0.10 | ||
| 800 | |||
| 801 | This version was started on 09/28/2011. | ||
| 802 | |||
| 803 | =cut | ||
| 804 | |||
| 805 | =head1 SEE ALSO (some of docs that I check often) | ||
| 806 | |||
| 807 | Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, | ||
| 808 | CGI::AppBuilder::Message | ||
| 809 | |||
| 810 | =head1 AUTHOR | ||
| 811 | |||
| 812 | Copyright (c) 2009 Hanming Tu. All rights reserved. | ||
| 813 | |||
| 814 | This package is free software and is provided "as is" without express | ||
| 815 | or implied warranty. It may be used, redistributed and/or modified | ||
| 816 | under the terms of the Perl Artistic License (see | ||
| 817 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 818 | |||
| 819 | =cut | ||
| 820 | |||
| 821 | 1; |