| 1 | package Oracle::Loader; | ||
| 2 | our $VERSION = '1.04'; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | my $Debugging = 0; | ||
| 7 | my $modname = "Oracle::Loader"; | ||
| 8 | my $ORA_KEYWORDS = "ACCESS|ADD|ALL|ALTER|AND|ANY|AS|ASC|AUDIT|"; | ||
| 9 | $ORA_KEYWORDS .= "BETWEEN|BY|CHAR|CHECK|CLUSTER|COLUMN|COMMENT|"; | ||
| 10 | $ORA_KEYWORDS .= "COMPRESS|CONNECT|CREATE|CURRENT|"; | ||
| 11 | $ORA_KEYWORDS .= "DATE|DECIMAL|DEFAULT|DELETE|DESC|DISTINCT|DROP|"; | ||
| 12 | $ORA_KEYWORDS .= "ELSE|EXCLUSIVE|EXISTS|FILE|FLOAT|FOR|FROM|GRANT|"; | ||
| 13 | $ORA_KEYWORDS .= "GROUP|HAVING|IDENTIFIED|IMMEDIATE|IN|INCREMENT|"; | ||
| 14 | $ORA_KEYWORDS .= "INDEX|INITIAL|INSERT|INTEGER|INTERSECT|INTO|"; | ||
| 15 | $ORA_KEYWORDS .= "IS|LEVEL|LIKE|LOCK|LONG|MAXEXTENTS|MINUS|"; | ||
| 16 | $ORA_KEYWORDS .= "MLSLABEL|MODE|MODIFY|NOAUDIT|NOCOMPRESS|NOT|"; | ||
| 17 | $ORA_KEYWORDS .= "NOWAIT|NULL|NUMBER|OF|OFFLINE|ON|ONLINE|OPTION|"; | ||
| 18 | $ORA_KEYWORDS .= "OR|ORDER|PCTFREE|PRIOR|PRIVILEGES|PUBLIC|RAW|"; | ||
| 19 | $ORA_KEYWORDS .= "RENAME|RESOURCE|REVOKE|ROW|ROWID|ROWNUM|ROWS|"; | ||
| 20 | $ORA_KEYWORDS .= "SELECT|SET|SHARE|SIZE|SMALLINT|START|SUCCESSFUL|"; | ||
| 21 | $ORA_KEYWORDS .= "SYNONYM|SYSDATE|TABLE|THEN|TO|TRIGGER|UID|"; | ||
| 22 | $ORA_KEYWORDS .= "UNION|UNIQUE|UPDATE|USER|VALIDATE|VALUES|"; | ||
| 23 | $ORA_KEYWORDS .= "VARCHAR|VARCHAR2|VIEW|WHENEVER|WHERE|WITH"; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | Oracle::Loader - Perl extension for creating Oracle PL/SQL and control | ||
| 28 | file. | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use Oracle::Loader; | ||
| 33 | |||
| 34 | $ldr = Oracle::Loader->new; | ||
| 35 | $ldr->init; # only sets vbm(N),direct(N),reset(Y) | ||
| 36 | $ldr->init(%args); # set variables based on hash array | ||
| 37 | $ldr->sync; # syncronize variables | ||
| 38 | $ldr->cols_ref($arf_ref); # column definition array ref | ||
| 39 | $ldr->param->dat_fn($fn); # assign $fn to dat_fn | ||
| 40 | $ldr->conn->Oracle($i, $v); # assign $v to the connection array | ||
| 41 | $ldr->disp_param; # display parameters | ||
| 42 | $ldr->crt_sql; # create PL/SQL file | ||
| 43 | $ldr->crt_ctl; # create control file | ||
| 44 | $ldr->crt_sql($crf,$fh,$apd,$tab,$rst); | ||
| 45 | $ldr->crt_sql($crf,$fn,$apd,$tab,$rst); | ||
| 46 | $ldr->crt_ctl($crf,$fh,$apd,$dat,$rst); | ||
| 47 | $ldr->crt_ctl($crf,$fn,$apd,$dat,$rst); | ||
| 48 | $ldr->create($typ,$cns,$sfn,$phm); | ||
| 49 | $ldr->load($typ,$cns,$ctl,$phm,$log); | ||
| 50 | $ldr->batch($typ,$cns,$sdr,$phm,$ext); | ||
| 51 | $ldr->report_results($typ,$cns,$sdr,$ofn,$ext); | ||
| 52 | $ldr->report_errors($typ,$cns,$sdr,$ofn,$ext); | ||
| 53 | $ldr->read_log($sub,$log,$rno); | ||
| 54 | |||
| 55 | $rv = $ldr->param->sql_fn; # get sql file name | ||
| 56 | $rv = $ldr->param->dat_fn; # get data file name | ||
| 57 | $rv = $ldr->param->vbm; # the same as the above | ||
| 58 | $ary_ref = $ldr->cols_ref; # get column def array ref | ||
| 59 | %ary = $ldr->get_param; # get all the parameters | ||
| 60 | |||
| 61 | Notation and Conventions | ||
| 62 | |||
| 63 | $ldr a display object | ||
| 64 | $crf column definition array reference | ||
| 65 | $fh a file handler | ||
| 66 | $fn an output file name | ||
| 67 | $apd N/Y, append to output file or not | ||
| 68 | $tab table name | ||
| 69 | $dat input data file name | ||
| 70 | $rst Y/N, whether to reset the corresponding variables | ||
| 71 | $typ database type: Oracle, MSSQL, CSV, etc | ||
| 72 | $cns connection string: usr/pwd@db | ||
| 73 | $sfn sql program file name | ||
| 74 | $ctl sqldr control file name | ||
| 75 | $sdr source directory where definition files stored | ||
| 76 | $phm program home directory | ||
| 77 | $log sqlldr log file name | ||
| 78 | $ext definiton file extension such as '.def', '.var', etc. | ||
| 79 | $sub calling sub: result (report_results) or | ||
| 80 | error (report_errors) | ||
| 81 | |||
| 82 | $drh Driver handle object (rarely seen or used in applications) | ||
| 83 | $h Any of the $??h handle types above | ||
| 84 | $rc General Return Code (boolean: true=ok, false=error) | ||
| 85 | $rv General Return Value (typically an integer) | ||
| 86 | @ary List of values returned from the database, typically a row | ||
| 87 | of data | ||
| 88 | $rows Number of rows processed (if available, else -1) | ||
| 89 | $fh A filehandle | ||
| 90 | undef NULL values are represented by undefined values in perl | ||
| 91 | \%attr Reference to a hash of attribute values passed to methods | ||
| 92 | |||
| 93 | =head1 DESCRIPTION | ||
| 94 | |||
| 95 | This is my seocnd object-oriented Perl program. | ||
| 96 | The Loader module creates data definition language (DDL) codes | ||
| 97 | for creating tables and control file to be used to load data | ||
| 98 | into the tables. It creates DDL codes based on column definitons | ||
| 99 | contained in an array or read from a definition file. It also has | ||
| 100 | reporting functions to generate SQL*Load error reports and load | ||
| 101 | result reports. | ||
| 102 | |||
| 103 | The column definition array could be built from Data::Describe module. | ||
| 104 | It is actually an array with hash members and contains these hash | ||
| 105 | elements ('col', 'typ', 'wid', 'max', 'min', 'dec', 'dft', 'req', | ||
| 106 | and 'dsp') | ||
| 107 | for each column. The subscripts in the array are in the format | ||
| 108 | of $ary[$col_seq]{$hash_ele}. The hash elements | ||
| 109 | are: | ||
| 110 | |||
| 111 | col - column name | ||
| 112 | typ - column type, 'N' for numeric, 'C' for characters, | ||
| 113 | 'D' for date | ||
| 114 | max - maximum length of the record in the column | ||
| 115 | wid - column width. It is the max of the column length. If | ||
| 116 | 'wid' presents, the max and min are not needed. | ||
| 117 | min - minimum length of the record in the column | ||
| 118 | dec - maximun decimal length of the record in the column | ||
| 119 | dft - date format string, e.g., YYYY/MM/DD, | ||
| 120 | MON/DD/YYYY HH24:MI:SS | ||
| 121 | req - whether there is null or zero length records in the | ||
| 122 | column only 'NOT NULL' is shown | ||
| 123 | dsp - column description | ||
| 124 | |||
| 125 | The module will use column definitons to create DDL codes and control | ||
| 126 | file using I<crt_sql> and I<crt_ctl> methods. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | =head1 METHODS | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item * the constructor new() | ||
| 135 | |||
| 136 | Without any input, i.e., new(), the constructor generates an empty | ||
| 137 | object. If any argument is provided, the constructor expects them in | ||
| 138 | the hash array format, i.e., in pairs of key and value. | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | use Class::Struct; | ||
| 145 | |||
| 146 | struct ParaType => | ||
| 147 | { | ||
| 148 | sql_fn => '$', # pl/sql file name | ||
| 149 | ctl_fn => '$', # SQL*Loader control file name | ||
| 150 | dat_fn => '$', # data file name for SQL*Loader | ||
| 151 | bad_fn => '$', # bad file name for SQL*Loader | ||
| 152 | dis_fn => '$', # discard file name for SQL*Loader | ||
| 153 | def_fn => '$', # column definition file name | ||
| 154 | def_ex => '$', # definition file name extent | ||
| 155 | log_fn => '$', # log file name for SQL*Loader | ||
| 156 | spool => '$', # spooling file name | ||
| 157 | dbtab => '$', # Oracle table name | ||
| 158 | dbts => '$', # Oracle tablespace name | ||
| 159 | dbsid => '$', # Oracle SID/Database alias | ||
| 160 | dbhome => '$', # Oracle home directory | ||
| 161 | dbconn => '$', # Oracle connection string | ||
| 162 | dbusr => '$', # Oracle user | ||
| 163 | dbpwd => '$', # Oracle password | ||
| 164 | ts_iext => '$', # tablespace initial extent | ||
| 165 | ts_next => '$', # tablespace next extent | ||
| 166 | db_type => '$', # database type: Oracle, MSSQL | ||
| 167 | append => '$', # Y/N/O to append to sql and ctl files | ||
| 168 | drop => '$', # Y/N to drop table in sql and | ||
| 169 | # to append in ctl files | ||
| 170 | vbm => '$', # Y/N to display more message | ||
| 171 | direct => '$', # using direct load method in SQL*Loader | ||
| 172 | overwrite => '$', # over write existing sql and ctl files | ||
| 173 | src_dir => '$', # directory where def files stored | ||
| 174 | DirSep => '$', # directory separator | ||
| 175 | commit => '$', # whether to create tables and load data in | ||
| 176 | # batch load | ||
| 177 | reset => '$', # whether to reset values when new value is | ||
| 178 | # passed in. | ||
| 179 | relax_req => '$', # relax constraint/requirement | ||
| 180 | # for creating tables | ||
| 181 | add_center => '$', # add center number to every plate | ||
| 182 | _counter => '$', # internal counter | ||
| 183 | study_number => '$', # study number | ||
| 184 | }; | ||
| 185 | |||
| 186 | struct ConnType => | ||
| 187 | { | ||
| 188 | Oracle => '@', # Oracle DBI connection | ||
| 189 | CSV => '@', # CSV connection | ||
| 190 | }; | ||
| 191 | |||
| 192 | # define constructor and accessors | ||
| 193 | struct # strunt installs the constructor and accessors into the | ||
| 194 | ( # current package. | ||
| 195 | cols_ref => '$', # ref to column array | ||
| 196 | out_fh => '$', # output sql file handle | ||
| 197 | param => 'ParaType',# parameter type specifier | ||
| 198 | conn => 'ConnType',# connection type | ||
| 199 | ); | ||
| 200 | |||
| 201 | =over 4 | ||
| 202 | |||
| 203 | =item * init(%attr) | ||
| 204 | |||
| 205 | Input variables: | ||
| 206 | |||
| 207 | %attr - argument hash array | ||
| 208 | |||
| 209 | Variables used or methods called: | ||
| 210 | |||
| 211 | param - get attribute value | ||
| 212 | conn - get connection information | ||
| 213 | sync - syncronize the variables | ||
| 214 | |||
| 215 | How to use: | ||
| 216 | |||
| 217 | # use default value to initialize the object | ||
| 218 | $self->init; | ||
| 219 | $self->init(%a); # use %a to initialize | ||
| 220 | |||
| 221 | Return: the initialized object. | ||
| 222 | |||
| 223 | This method initiates the parameters for the object. | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub init { | ||
| 230 | my ($self, %args) = @_; | ||
| 231 | my $pm=$self->param; | ||
| 232 | my $cn=$self->conn; | ||
| 233 | foreach my $attr (keys %args) { | ||
| 234 | no strict "refs"; | ||
| 235 | if ($attr =~ /^(cols_ref|out_fh|conn)/) { | ||
| 236 | $self->$attr( $args{$attr} ); | ||
| 237 | } elsif ($attr =~ /(Oracle|CSV)/) { | ||
| 238 | for my $i (0..$#{$args{$attr}}) { | ||
| 239 | $cn->$attr($i, ${$args{$attr}}[$i]); | ||
| 240 | # print "$i : ${$args{$attr}}[$i]\n"; | ||
| 241 | } | ||
| 242 | } else { | ||
| 243 | $pm->$attr( $args{$attr} ); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | if (!$pm->direct) { $pm->direct('N'); } | ||
| 247 | if (!$pm->reset) { $pm->reset('Y'); } | ||
| 248 | if (!$pm->vbm) { $pm->vbm('N'); } | ||
| 249 | if (!$pm->append) { $pm->append('N'); } | ||
| 250 | if (!$pm->DirSep) { $pm->DirSep('/'); } | ||
| 251 | if (!$pm->commit) { $pm->commit('N'); } | ||
| 252 | if (!$pm->db_type) { $pm->db_type('Oracle'); } | ||
| 253 | if (!$pm->relax_req) { $pm->relax_req('Y'); } | ||
| 254 | $self->_echoMSG(" - Initializing variables in $modname..."); | ||
| 255 | $self->sync; | ||
| 256 | return $self; | ||
| 257 | } | ||
| 258 | |||
| 259 | =over 4 | ||
| 260 | |||
| 261 | =item * sync (%args) | ||
| 262 | |||
| 263 | Input variables: | ||
| 264 | |||
| 265 | %args - argument hash array | ||
| 266 | |||
| 267 | Variables used or methods called: | ||
| 268 | |||
| 269 | param - get attribute value | ||
| 270 | conn - get connection information | ||
| 271 | sync - syncrolize the variables | ||
| 272 | |||
| 273 | How to use: | ||
| 274 | |||
| 275 | # use default value to syncronile the object | ||
| 276 | $self->sync; | ||
| 277 | $self->sync(%a); # use %a to syncronize | ||
| 278 | |||
| 279 | Return: the initialized object. | ||
| 280 | |||
| 281 | This method syncronizes the parameters. | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | sub sync { | ||
| 288 | my ($self, %args) = @_; | ||
| 289 | my $pm=$self->param; | ||
| 290 | my $cn=$self->conn; | ||
| 291 | |||
| 292 | my $vbm = $pm->vbm; | ||
| 293 | my $rst = ($pm->reset eq 'Y')?1:0; | ||
| 294 | $self->_echoMSG(" - Syncronizing variables in $modname..."); | ||
| 295 | # sync Oracle connection parameters | ||
| 296 | my $orf = $cn->Oracle; | ||
| 297 | my $cs = $pm->dbconn; | ||
| 298 | my ($usr, $pwd, $db) = ("", "", ""); | ||
| 299 | if ($#{$orf}<=0) { | ||
| 300 | if ($cs) { | ||
| 301 | ($usr, $pwd, $db) = ($cs =~ m{(\w+)/(\w+)\@(\w+)}); | ||
| 302 | $pm->dbsid($db) if $db; | ||
| 303 | $pm->dbusr($usr) if $usr; | ||
| 304 | $pm->dbpwd($pwd) if $pwd; | ||
| 305 | @{$orf} = ("DBI:Oracle:$db","$usr","$pwd"); | ||
| 306 | } | ||
| 307 | # @{$orf} = ("DBI:Oracle:sid","dbusr","dbpwd"); | ||
| 308 | } | ||
| 309 | my $sid = $pm->dbsid; | ||
| 310 | $sid = "" if ! defined($sid); | ||
| 311 | $usr = $pm->dbusr; | ||
| 312 | $usr = "" if ! defined($usr); | ||
| 313 | $pwd = $pm->dbpwd; | ||
| 314 | $pwd = "" if ! defined($pwd); | ||
| 315 | if ($db ne $sid) { $cn->Oracle(0, "DBI:Oracle:$sid"); } | ||
| 316 | if (defined(${$orf}[1]) && ${$orf}[1] ne $usr) { | ||
| 317 | $cn->Oracle(1,$usr); | ||
| 318 | } | ||
| 319 | if (defined(${$orf}[2]) && ${$orf}[2] ne $pwd) { | ||
| 320 | $cn->Oracle(2,$pwd); | ||
| 321 | } | ||
| 322 | use File::Basename; | ||
| 323 | my $dat = $pm->dat_fn; # data file for SQL*Loader | ||
| 324 | my $sql = $pm->sql_fn; # pl/sql file name | ||
| 325 | my $def = $pm->def_fn; # definition file name | ||
| 326 | if ($dat||$def) { | ||
| 327 | my ($bnm,$dir,$typ); | ||
| 328 | ($bnm,$dir,$typ)=fileparse($dat,'\.\w+$') if ($dat); | ||
| 329 | ($bnm,$dir,$typ)=fileparse($def,'\.\w+$') if ($def); | ||
| 330 | if (!$pm->log_fn||$rst) { $pm->log_fn("$dir${bnm}.log"); } | ||
| 331 | if (!$pm->dis_fn||$rst) { $pm->dis_fn("$dir${bnm}.dis"); } | ||
| 332 | if (!$pm->bad_fn||$rst) { $pm->bad_fn("$dir${bnm}.bad"); } | ||
| 333 | if (!$pm->ctl_fn||$rst) { $pm->ctl_fn("$dir${bnm}.ctl"); } | ||
| 334 | if (!$pm->dat_fn||$rst) { $pm->dat_fn("$dir${bnm}.dat"); } | ||
| 335 | if (!$pm->dbtab) { $pm->dbtab($bnm); } | ||
| 336 | if (!$sql) { | ||
| 337 | $sql="$dir${bnm}.sql"; | ||
| 338 | $pm->sql_fn($sql); | ||
| 339 | } | ||
| 340 | # check the file size of the input data file | ||
| 341 | # 1mb=1048576 bytes | ||
| 342 | if (-f $dat) { | ||
| 343 | my $MB=1048576; | ||
| 344 | my $fsz = -s $dat; | ||
| 345 | my $sz = (int $fsz/1024) + 1; | ||
| 346 | my($sz1,$sz2); | ||
| 347 | if ( $sz > 1024) { | ||
| 348 | $sz1 = sprintf "%dm", (int $sz/1024) + 1; | ||
| 349 | $sz2 = sprintf "%dk", ((int $sz/1024) + 1)*10; | ||
| 350 | } else { | ||
| 351 | $sz1 = sprintf "%dk", $sz; | ||
| 352 | $sz2 = sprintf "%dk", int ($sz/10); | ||
| 353 | } | ||
| 354 | $pm->ts_iext($sz1); | ||
| 355 | $pm->ts_next($sz2); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | if ($sql) { | ||
| 359 | my ($bnm,$dir,$typ)=fileparse($sql,'\.\w+$'); | ||
| 360 | if (!$pm->spool||$rst) { $pm->spool("$dir${bnm}.lst"); } | ||
| 361 | if (!$pm->dbtab && !$dat) { | ||
| 362 | if (!$pm->dbtab) { $pm->dbtab($bnm); } | ||
| 363 | } | ||
| 364 | } | ||
| 365 | $cn = $self->conn; | ||
| 366 | if (!$pm->dbsid) { $pm->dbsid($ENV{'ORACLE_SID'}); } | ||
| 367 | if (!$pm->dbhome) { $pm->dbhome($ENV{'ORACLE_HOME'}); } | ||
| 368 | if (!$pm->dbusr) { $pm->dbusr($ENV{'USER'}); } | ||
| 369 | ($usr, $pwd, $db) = ($pm->dbusr, $pm->dbpwd, $pm->dbsid); | ||
| 370 | if ($usr && $pwd && $db) { | ||
| 371 | $cn->Oracle(0,"DBI:Oracle:${db}"); | ||
| 372 | $cn->Oracle(1,$usr); | ||
| 373 | $cn->Oracle(2,$pwd); | ||
| 374 | $pm->dbconn("$usr/$pwd\@$db"); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | =over 4 | ||
| 379 | |||
| 380 | =item * debug($n) | ||
| 381 | |||
| 382 | Input variables: | ||
| 383 | |||
| 384 | $n - a number between 0 and 100. It specifies the | ||
| 385 | level of messages that you would like to | ||
| 386 | display. The higher the number, the more | ||
| 387 | detailed messages that you will get. | ||
| 388 | |||
| 389 | Variables used or methods called: None. | ||
| 390 | |||
| 391 | How to use: | ||
| 392 | |||
| 393 | $self->debug(2); # set the message level to 2 | ||
| 394 | print $self->debug; # print current message level | ||
| 395 | |||
| 396 | Return: None. | ||
| 397 | |||
| 398 | The debug level will be set to $n. | ||
| 399 | |||
| 400 | =back | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | sub debug { | ||
| 405 | my $self = shift; | ||
| 406 | confess "usage: thing->debug(level)" unless @_ == 1; | ||
| 407 | my $level = shift; | ||
| 408 | if (ref($self)) { | ||
| 409 | $self->{"_DEBUG"} = $level; | ||
| 410 | } else { | ||
| 411 | $Debugging = $level; # whole class | ||
| 412 | } | ||
| 413 | $self->SUPER::debug($Debugging); | ||
| 414 | } | ||
| 415 | |||
| 416 | =over 4 | ||
| 417 | |||
| 418 | =item * disp_param | ||
| 419 | |||
| 420 | Input variables: None | ||
| 421 | |||
| 422 | Variables used or methods called: None. | ||
| 423 | |||
| 424 | How to use: | ||
| 425 | |||
| 426 | $self->display; | ||
| 427 | |||
| 428 | Return: none. | ||
| 429 | |||
| 430 | This method displays the parameters and their values. | ||
| 431 | |||
| 432 | =back | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | sub disp_param { | ||
| 437 | my $self = shift; | ||
| 438 | $self->_echoMSG(" - Displaying parameters in $modname..."); | ||
| 439 | my $fmt = "%15s = %-30s\n"; | ||
| 440 | no strict "refs"; | ||
| 441 | foreach my $p (sort $self->_list_vars) { | ||
| 442 | next if (!defined($p) || ! $p); | ||
| 443 | if ($p =~ /(cols_ref|out_fh)/) { | ||
| 444 | my $c = $self->$p; | ||
| 445 | if (! defined $c) { | ||
| 446 | printf $fmt, $p, 'undef'; | ||
| 447 | } else { | ||
| 448 | printf $fmt, $p, $c; | ||
| 449 | } | ||
| 450 | } elsif ($p =~ /^(conn)/) { | ||
| 451 | printf $fmt, $p, join ",", keys %{$self->$p}; | ||
| 452 | } elsif ($p =~ /(Oracle|CSV)/) { | ||
| 453 | printf $fmt,$p,"[" . (join ",", @{$self->conn->$p}) . "]"; | ||
| 454 | } else { | ||
| 455 | my $v = $self->param->$p; | ||
| 456 | $v = "" if ! defined ($v); | ||
| 457 | printf $fmt, $p, $v; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | =over 4 | ||
| 463 | |||
| 464 | =item * read_definitoin ($dfn, $typ) | ||
| 465 | |||
| 466 | Input variables: | ||
| 467 | |||
| 468 | $dfn - definition file name. If not specified, | ||
| 469 | I<param->def_fn> method will be called. | ||
| 470 | $typ - definition file type. Not implemented at | ||
| 471 | this version. | ||
| 472 | |||
| 473 | Variables used or methods called: None. | ||
| 474 | |||
| 475 | param->def_fn - get definition file name | ||
| 476 | param->reset - reset parameters? | ||
| 477 | cols_ref - get/set column reference | ||
| 478 | |||
| 479 | How to use: | ||
| 480 | |||
| 481 | $self->read_definition($fn); | ||
| 482 | |||
| 483 | Return: none. | ||
| 484 | |||
| 485 | This method reads a column definition file and sets the definition | ||
| 486 | column array. It espects the definiton file to contain one column | ||
| 487 | definition per line with vertical bar delimiting the definition. | ||
| 488 | Here are the definitions: | ||
| 489 | |||
| 490 | 1. SAS Dataset Name and Path| | ||
| 491 | 2. ASCII File Name and Path| | ||
| 492 | 3. Variable Name| | ||
| 493 | 4. Variable Length| | ||
| 494 | 5. Variable Type (1=num 2=char 3=date)| | ||
| 495 | 6. Variable Date Format| | ||
| 496 | 7. Variable Label| | ||
| 497 | 8. All Values Exist? | ||
| 498 | |||
| 499 | Here is an example: | ||
| 500 | |||
| 501 | #SAS|ASCII|VarName|VarLength|VarType|DateFmt|VarLabel|NotNull | ||
| 502 | ||STUDYNO|3|number||Study Number|not null | ||
| 503 | ||CENTERNO|3|number||Center Number| | ||
| 504 | ||PATIENTS|7|number||Center Patients| | ||
| 505 | ||VISITS|7|number||Center Patients| | ||
| 506 | ||RECORDS|7|number||Center Patients| | ||
| 507 | ||Fax_In|6.1|number||Mean # Days from Visit to Fax In| | ||
| 508 | ||DB_Entry|6.1|number||Mean # Days from Visit to DB entry| | ||
| 509 | ||DB_Clean|6.1|number||Mean # Days from Visit to DB clean| | ||
| 510 | ||clean_now|5.1|number||Percent Records Clean Now| | ||
| 511 | ||job_id|9|number||Report Job number|not null | ||
| 512 | |||
| 513 | =back | ||
| 514 | |||
| 515 | =cut | ||
| 516 | |||
| 517 | sub read_definition { | ||
| 518 | my $self = shift; | ||
| 519 | my $pm=$self->param; | ||
| 520 | my $dfn = shift if ($_[0]); # definition file name | ||
| 521 | $dfn = $pm->def_fn if (!$dfn); | ||
| 522 | if ($pm->reset eq 'Y') { $pm->def_fn($dfn) if ($dfn); } | ||
| 523 | my $typ = shift if ($_[0]); # definition file type | ||
| 524 | if (!$dfn) { croak "No definition file name is specified."; } | ||
| 525 | if (! -f $dfn) { croak "Could not find definition file - $dfn."; } | ||
| 526 | $self->_echoMSG(" - Defining column array from $dfn..."); | ||
| 527 | open DEF, "<$dfn" or croak "Could not open file - $dfn: $!"; | ||
| 528 | my (@a,@r); | ||
| 529 | my $i = -1; # def column index | ||
| 530 | my @T=(); $T[1]='N'; $T[2]='C'; $T[3]='D'; | ||
| 531 | my %T=(); $T{'Y'}='NOT NULL'; $T{'N'}=""; | ||
| 532 | while (<DEF>) { | ||
| 533 | chomp; | ||
| 534 | next if ($_ =~ /^#/); # skip comment lines | ||
| 535 | next if (!$_); # skip empty lines | ||
| 536 | ++$i; | ||
| 537 | # 0 - SAS Dataset Name and Path | ||
| 538 | # 1 - ASCII File Name and Path | ||
| 539 | # 2 - Variable Name | ||
| 540 | # 3 - Variable Length | ||
| 541 | # 4 - Variable Type (1=num 2=char 3=date) | ||
| 542 | # 5 - Variable Date Format | ||
| 543 | # 6 - Variable Label | ||
| 544 | # 7 - All Values Exist? | ||
| 545 | @a=split /\|/, $_; | ||
| 546 | $r[$i]{'col'} = $a[2]; | ||
| 547 | if ($a[3] =~ /(\d+)\.(\d+)/) { # such as 6.2 | ||
| 548 | $r[$i]{'wid'} = $1; | ||
| 549 | $r[$i]{'dec'} = $2; | ||
| 550 | } else { # such as 6 | ||
| 551 | $r[$i]{'wid'} = $a[3]; | ||
| 552 | $r[$i]{'dec'} = 0; | ||
| 553 | } | ||
| 554 | $a[4] = "" if ! defined($a[4]); | ||
| 555 | $a[4] =~ s/\s*(.*)\s*/$1/; # trim spaces | ||
| 556 | if ($a[4] =~ /^(1|2|3)$/) { | ||
| 557 | $r[$i]{'typ'} = $T[$a[4]]; | ||
| 558 | } else { | ||
| 559 | $r[$i]{'typ'} = $a[4]; | ||
| 560 | } | ||
| 561 | if ($a[5] =~ /^MMDDYY10/) { | ||
| 562 | $r[$i]{'dft'} = "MM/DD/YYYY"; | ||
| 563 | } elsif ($a[5] =~ /YYMMDD10/) { | ||
| 564 | $r[$i]{'dft'} = "YYYY/MM/DD"; | ||
| 565 | } elsif ($a[5] =~ /MMDDYY8/) { | ||
| 566 | $r[$i]{'dft'} = "MM/DD/YY"; | ||
| 567 | } elsif ($a[5] =~ /YYMMDD8/) { | ||
| 568 | $r[$i]{'dft'} = "YY/MM/DD"; | ||
| 569 | } else { | ||
| 570 | $r[$i]{'dft'} = $a[5]; | ||
| 571 | } | ||
| 572 | $r[$i]{'dsp'} = $a[6]; | ||
| 573 | $a[7] = "" if ! defined($a[7]); | ||
| 574 | $a[7] =~ s/\s*(.*)\s*/$1/; # trim spaces | ||
| 575 | if ($a[7] =~ /^(Y|N)$/) { | ||
| 576 | $r[$i]{'req'} = $T{$a[7]}; | ||
| 577 | } else { | ||
| 578 | $r[$i]{'req'} = $a[7]; | ||
| 579 | } | ||
| 580 | if (uc($a[2]) =~ /^(DFCREATE|DFMODIFY)/) { | ||
| 581 | $r[$i]{'typ'} = 'DATE'; | ||
| 582 | $r[$i]{'dft'} = "YYYY/MM/DD HH24:MI:SS"; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | close DEF; | ||
| 586 | $self->cols_ref(\@r); | ||
| 587 | return $self->cols_ref; | ||
| 588 | } | ||
| 589 | |||
| 590 | =over 4 | ||
| 591 | |||
| 592 | =item * crt_sql($arf,$ofn,$apd,$tab,$rst,$drp) | ||
| 593 | |||
| 594 | Input variables: | ||
| 595 | |||
| 596 | $arf - array ref containing column definitions. | ||
| 597 | If not specified, it defaults to I<cols_ref>. | ||
| 598 | $ofn - output file name. The file will contains | ||
| 599 | the sql codes. It defaults to I<out_fh> or | ||
| 600 | I<sql_fn>. | ||
| 601 | $apd - whether to append if the output file | ||
| 602 | exists. It defaults to I<param->append>. | ||
| 603 | $tab - database table name. It defaults to | ||
| 604 | I<param->dbtab>. | ||
| 605 | $rst - whether to reset parameters based on the | ||
| 606 | specified parameters here. It defaults to | ||
| 607 | I<param->reset>. | ||
| 608 | $drp - whether to drop the table before create it. | ||
| 609 | The default is 'Y'. | ||
| 610 | |||
| 611 | Variables used or methods called: | ||
| 612 | |||
| 613 | param - get parameters | ||
| 614 | |||
| 615 | How to use: | ||
| 616 | |||
| 617 | $self->crt_sql($arf, 'mysql.sql','Y', 'mytab'); | ||
| 618 | |||
| 619 | Return: create PL/SQL codes for creating Oracle tables. | ||
| 620 | |||
| 621 | This method creates PL/SQL codes based on the columns defined in | ||
| 622 | the definition array. You can access the array reference as | ||
| 623 | ${$arf}[$i]{$k}. The $k could be 'col', 'typ', 'wid', 'max', 'min', | ||
| 624 | 'dec', 'dft', and 'req'. Some special keys are stored in the first | ||
| 625 | element of the array, i.e., ${$arf}[0]. They are | ||
| 626 | |||
| 627 | table_name - table name. It is used as the last | ||
| 628 | resource in getting a table name. | ||
| 629 | table_desc - table title/description used to | ||
| 630 | create table comments. | ||
| 631 | |||
| 632 | =back | ||
| 633 | |||
| 634 | =cut | ||
| 635 | |||
| 636 | sub crt_sql { | ||
| 637 | my $self = shift; | ||
| 638 | my $pm=$self->param; | ||
| 639 | # initialize variables and check inputs | ||
| 640 | # (ColRef,SQLFN,Appd,OraTab,ReSet) | ||
| 641 | my($crf,$fh,$fn,$appd,$tab,$rst,$drp)= | ||
| 642 | $self->_getInputs('crt_sql',@_); | ||
| 643 | # print "$crf,$fh,$fn,$appd,$tab,$rst\n"; | ||
| 644 | |||
| 645 | if (exists ${$crf}[0]{'table_name'}) { | ||
| 646 | $tab = ${$crf}[0]{'table_name'} if !$tab; | ||
| 647 | } | ||
| 648 | |||
| 649 | use File::Basename; | ||
| 650 | my ($bnm,$dir,$typ)=fileparse($fn,'\.\w+$'); | ||
| 651 | my $lst = "$dir${bnm}.lst"; | ||
| 652 | my $st = localtime(time); | ||
| 653 | |||
| 654 | my $txt = ""; # SQL codes | ||
| 655 | if ($fn) { $txt .= "REM file name: $fn\n"; | ||
| 656 | } else { $txt .= "REM\n"; } | ||
| 657 | $txt .= "REM created at $st\n"; | ||
| 658 | $txt .= "REM created by Oracle::Loader->crt_sql\nREM\n"; | ||
| 659 | if ($appd eq 'Y') { | ||
| 660 | if ($pm->_counter==1) { $txt .= "spool $lst\n"; } | ||
| 661 | } else { | ||
| 662 | $txt .= "spool $lst\n"; | ||
| 663 | } | ||
| 664 | |||
| 665 | if ($drp eq 'Y') { | ||
| 666 | $txt .= "DROP TABLE $tab;\n"; | ||
| 667 | } else { | ||
| 668 | $txt .= "-- DROP TABLE $tab;\n"; | ||
| 669 | } | ||
| 670 | $txt .= "CREATE TABLE $tab (\n"; | ||
| 671 | |||
| 672 | my $fmt = " %-15s %-23s %12s\n"; | ||
| 673 | my ($col,$wid,$dft,$req,$otp,$orq,$dec,$dsp); | ||
| 674 | my $rlx = $pm->relax_req; | ||
| 675 | for my $i (0..$#{$crf}) { # loop thru each column | ||
| 676 | $col = uc(${$crf}[$i]{'col'}); # column name | ||
| 677 | if ($col =~ /($ORA_KEYWORDS)/) { | ||
| 678 | $col = "\"$col\""; | ||
| 679 | } | ||
| 680 | $typ = uc(${$crf}[$i]{'typ'}); # column type | ||
| 681 | if ($typ =~ /^N$/) { $typ = 'NUMBER'; } | ||
| 682 | if ($typ =~ /^D$/) { $typ = 'DATE'; } | ||
| 683 | if ($typ =~ /^C$/) { $typ = 'VARCHAR2'; } | ||
| 684 | $wid = ""; | ||
| 685 | if (${$crf}[$i]{'wid'}) { # use 'wid' first | ||
| 686 | $wid = uc(${$crf}[$i]{'wid'}); # column width | ||
| 687 | } | ||
| 688 | if (!$wid) { # use 'max' | ||
| 689 | $wid = uc(${$crf}[$i]{'max'}); # column width | ||
| 690 | } | ||
| 691 | $dft = uc(${$crf}[$i]{'dft'}); # column type | ||
| 692 | $req = uc(${$crf}[$i]{'req'}); # requirement/constraint | ||
| 693 | $dec = uc(${$crf}[$i]{'dec'}); # decimal | ||
| 694 | $otp = ""; # Oracle type | ||
| 695 | # print "$col:$typ:$wid:$dec:$req\n"; | ||
| 696 | if ($dec ne "" && $wid && $typ =~ /^N/i) { | ||
| 697 | if ($wid > $dec) { $wid .= ",$dec"; } | ||
| 698 | # print "--$col:$wid\n"; | ||
| 699 | } | ||
| 700 | if ($wid && $typ !~ /^D/i) { $otp = "$typ($wid)"; | ||
| 701 | } else { $otp = $typ; } | ||
| 702 | $orq=""; | ||
| 703 | if ($req && $typ !~ /^D/i && ("$rlx" ne 'Y' || | ||
| 704 | $col =~ /(^ID|ID$)/) ) { $orq = $req; } | ||
| 705 | if ($i == $#{$crf}) { | ||
| 706 | my $t = ""; | ||
| 707 | if ($pm->ts_iext || $pm->ts_next) { | ||
| 708 | $t .= "STORAGE (\n"; | ||
| 709 | if ($self->param->ts_iext) { | ||
| 710 | $t .= " INITIAL " . $pm->ts_iext . "\n"; | ||
| 711 | } | ||
| 712 | if ($pm->ts_next) { | ||
| 713 | $t .= " NEXT " . $pm->ts_next . "\n"; | ||
| 714 | } | ||
| 715 | $t .= ");\n"; | ||
| 716 | } | ||
| 717 | if ($pm->dbts) { | ||
| 718 | $txt .= sprintf $fmt, $col, $otp, $orq . ')'; | ||
| 719 | $txt .= "TABLESPACE " . $pm->dbts . "\n"; | ||
| 720 | if ($t) { $txt .= $t; } else { $txt .= ";\n"; } | ||
| 721 | } else { | ||
| 722 | $txt .= sprintf $fmt, $col, $otp, $orq . ');'; | ||
| 723 | } | ||
| 724 | $txt .= "GRANT SELECT ON $tab TO PUBLIC;\n"; | ||
| 725 | } else { | ||
| 726 | $txt .= sprintf $fmt, $col, $otp, $orq . ','; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | if (exists ${$crf}[0]{'table_desc'}) { | ||
| 730 | $dsp = ${$crf}[0]{'table_desc'}; | ||
| 731 | $dsp =~ s/\'/ /g; # change single quote to blank | ||
| 732 | $dsp =~ s/\&/and/g; # change & sign to 'and' | ||
| 733 | $txt .= "COMMENT ON TABLE ${tab} IS\n '$dsp'}';\n"; | ||
| 734 | } | ||
| 735 | # create comment for columns | ||
| 736 | for my $i (0..$#{$crf}) { # loop thru each column | ||
| 737 | $col = uc(${$crf}[$i]{'col'}); # column name | ||
| 738 | $dsp = ${$crf}[$i]{'dsp'}; # column description | ||
| 739 | $dsp =~ s/\'/ /g if $dsp; # change single quote to blank | ||
| 740 | $dsp =~ s/\&/and/g if $dsp; # change & sign to 'and' | ||
| 741 | next if !$dsp; | ||
| 742 | $txt .= "COMMENT ON COLUMN ${tab}.$col IS\n '$dsp';\n"; | ||
| 743 | } | ||
| 744 | if ($appd eq 'Y') { | ||
| 745 | if ($pm->_counter eq 'N') { $txt .= "spool off\nexit\n"; } | ||
| 746 | } else { $txt .= "spool off\nexit\n"; } | ||
| 747 | print $fh $txt; | ||
| 748 | $fh->close; | ||
| 749 | } | ||
| 750 | |||
| 751 | =over 4 | ||
| 752 | |||
| 753 | =item * crt_ctl ($arf, $ofn, $apd, $dat, $rst, $drp) | ||
| 754 | |||
| 755 | Input variables: | ||
| 756 | |||
| 757 | $arf - array ref containing column definitions. | ||
| 758 | If not specified, it defaults to I<cols_ref>. | ||
| 759 | $ofn - output file name. The file will contains | ||
| 760 | the sql codes. It defaults to I<out_fh> | ||
| 761 | or I<ctl_fn>. | ||
| 762 | $apd - whether to append if the output file | ||
| 763 | exists. It defaults to I<param->append>. | ||
| 764 | $dat - input data file name. It defaults to | ||
| 765 | I<param->dat_fn>. | ||
| 766 | $rst - whether to reset parameters based on the | ||
| 767 | specified parameters here. It defaults to | ||
| 768 | I<param->reset>. | ||
| 769 | $drp - whether drop records before appending | ||
| 770 | |||
| 771 | Variables used or methods called: | ||
| 772 | |||
| 773 | param - get parameters | ||
| 774 | |||
| 775 | How to use: | ||
| 776 | |||
| 777 | $self->crt_sql($arf, 'mysql.ctl','N', 'mytxt.dat'); | ||
| 778 | |||
| 779 | Return: create control file to be used by sql*loader. | ||
| 780 | |||
| 781 | This method creates a SQL*Loader control file. | ||
| 782 | |||
| 783 | =back | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub crt_ctl { | ||
| 788 | my $self = shift; | ||
| 789 | my $pm=$self->param; | ||
| 790 | # initialize variables and check inputs | ||
| 791 | # (ColRef,FH/CTLFN,Appd,DataFile,ReSet) | ||
| 792 | my($crf,$fh,$fn,$appd,$dat,$rst,$drp)= | ||
| 793 | $self->_getInputs('crt_ctl',@_); | ||
| 794 | if (!-f $dat) { carp "Input data file - $dat does not exist."; } | ||
| 795 | |||
| 796 | use File::Basename; | ||
| 797 | my ($bnm,$dir,$typ); | ||
| 798 | if ($dat) { | ||
| 799 | ($bnm,$dir,$typ)=fileparse($dat,'\.\w+$'); | ||
| 800 | } else { | ||
| 801 | ($bnm,$dir,$typ)=fileparse($fn,'\.\w+$'); | ||
| 802 | } | ||
| 803 | my $bad = "$dir${bnm}.bad"; | ||
| 804 | my $dis = "$dir${bnm}.dis"; | ||
| 805 | my $log = "$dir${bnm}.log"; | ||
| 806 | if ($rst) { | ||
| 807 | $pm->bad_fn($bad); | ||
| 808 | $pm->dis_fn($dis); | ||
| 809 | $pm->log_fn($log); | ||
| 810 | $pm->dbtab($bnm) if (!$pm->dbtab); | ||
| 811 | } | ||
| 812 | my $tab = $pm->dbtab; | ||
| 813 | if (!$tab) { carp "Oracle table name is not specified."; } | ||
| 814 | |||
| 815 | my $fmt = " %-15s %-23s %-2s\n"; | ||
| 816 | my $st = localtime(time); | ||
| 817 | |||
| 818 | # start constructing control file | ||
| 819 | my $txt = ""; # SQL codes | ||
| 820 | # if ($fn) { $txt .= "# file name: $fn\n"; | ||
| 821 | # } else { $txt .= "#\n"; } | ||
| 822 | # $txt .= "# created at $st\n"; | ||
| 823 | # $txt .= "# created by Oracle::Loader->crt_ctl\n#\n"; | ||
| 824 | if ($pm->direct eq 'Y') { | ||
| 825 | $txt .= "OPTIONS (ERRORS=1000,SILENT=FEEDBACK,DIRECT=TRUE)\n"; | ||
| 826 | $txt .= "UNRECOVERABLE"; | ||
| 827 | } else { | ||
| 828 | $txt .= "OPTIONS (ERRORS=1000,SILENT=FEEDBACK)\n"; | ||
| 829 | } | ||
| 830 | $txt .= "LOAD DATA\nINFILE \'$dat\'\nBADFILE \'$bad\'\n"; | ||
| 831 | $txt .= "DISCARDFILE \'$dis\'\n"; | ||
| 832 | # $txt .= "LOGFILE \'$log\'\n"; | ||
| 833 | if ($drp eq 'Y') { | ||
| 834 | $txt .= "REPLACE INTO TABLE $tab\n"; | ||
| 835 | } else { | ||
| 836 | $txt .= "APPEND INTO TABLE $tab\n"; | ||
| 837 | } | ||
| 838 | $txt .= "FIELDS TERMINATED BY \"|\" OPTIONALLY ENCLOSED BY "; | ||
| 839 | $txt .= "\"'\"\n TRAILING NULLCOLS\n"; | ||
| 840 | $txt .= "(\n"; | ||
| 841 | my ($col,$wid,$dft,$req,$otp,$dec); | ||
| 842 | for my $i (0..$#{$crf}) { # loop thru each column | ||
| 843 | $col = uc(${$crf}[$i]{'col'}); # column name | ||
| 844 | if ($col =~ /($ORA_KEYWORDS)/) { | ||
| 845 | $col = "\"$col\""; | ||
| 846 | } | ||
| 847 | $typ = uc(${$crf}[$i]{'typ'}); # column type | ||
| 848 | if ($typ =~ /^N$/) { $typ = 'NUMBER'; } | ||
| 849 | if ($typ =~ /^D$/) { $typ = 'DATE'; } | ||
| 850 | if ($typ =~ /^C$/) { $typ = 'VARCHAR2'; } | ||
| 851 | $wid = ""; | ||
| 852 | if (${$crf}[$i]{'wid'}) { # use 'wid' first | ||
| 853 | $wid = uc(${$crf}[$i]{'wid'}); # column width | ||
| 854 | } | ||
| 855 | if (!$wid) { # use 'max' | ||
| 856 | $wid = uc(${$crf}[$i]{'max'}); # column width | ||
| 857 | } | ||
| 858 | $dft = uc(${$crf}[$i]{'dft'}); # column type | ||
| 859 | $req = uc(${$crf}[$i]{'req'}); # requirement/constraint | ||
| 860 | $dec = uc(${$crf}[$i]{'dec'}); # decimal | ||
| 861 | $dft = uc(${$crf}[$i]{'dft'}); # date format | ||
| 862 | if ($dec >=0 && $wid && $typ =~ /^N/i) { | ||
| 863 | if ($wid > $dec) { $wid .= ",$dec"; } | ||
| 864 | } | ||
| 865 | $otp = ""; # Oracle type | ||
| 866 | if ($wid && $typ =~ /^CHAR/i) { $otp = lc("$typ($wid)"); | ||
| 867 | } elsif ( $typ =~ /^D/i) { $otp = lc($typ); } | ||
| 868 | if ($typ =~ /^D/ && $dft) { | ||
| 869 | $otp .= " \"$dft\" NULLIF $col=BLANKS"; | ||
| 870 | } | ||
| 871 | # if ($req && $typ !~ /^D/i) { $otp .= " $req"; } | ||
| 872 | if ($i == $#{$crf}) { | ||
| 873 | $txt .= sprintf $fmt, $col, $otp, ')'; | ||
| 874 | $txt .= "\n"; | ||
| 875 | } else { | ||
| 876 | $txt .= sprintf $fmt, $col, $otp, ','; | ||
| 877 | } | ||
| 878 | } | ||
| 879 | print $fh $txt; | ||
| 880 | $fh->close; | ||
| 881 | } | ||
| 882 | |||
| 883 | =over 4 | ||
| 884 | |||
| 885 | =item * check_infile ($ctl,$typ) | ||
| 886 | |||
| 887 | Input variables: | ||
| 888 | |||
| 889 | $ctl - control file name | ||
| 890 | $typ - routine type: load, create, etc. | ||
| 891 | |||
| 892 | Variables used or methods called: | ||
| 893 | |||
| 894 | echoMSG - echo messages | ||
| 895 | |||
| 896 | How to use: | ||
| 897 | |||
| 898 | $self->check_infile($inf); | ||
| 899 | |||
| 900 | Return: boolean, i.e., 1 for OK, 0 for not OK. | ||
| 901 | |||
| 902 | This method checks whether there is INFILE parameter in control file, | ||
| 903 | whether the infile exisit and has non-zero size. | ||
| 904 | |||
| 905 | =back | ||
| 906 | |||
| 907 | =cut | ||
| 908 | |||
| 909 | sub check_infile { | ||
| 910 | my $self = shift; | ||
| 911 | my ($ctl,$typ) = @_; | ||
| 912 | # | ||
| 913 | # 05/07/2002: htu - added check on infile | ||
| 914 | my $msg = " no CTL file specified."; | ||
| 915 | $self->_echoMSG($msg) if !$ctl; | ||
| 916 | return 0 if !$ctl; | ||
| 917 | open CTL, "<$ctl" or croak "ERR: Could not open $ctl: $!\n"; | ||
| 918 | my $inf = ""; # infile name | ||
| 919 | while (<CTL>) { | ||
| 920 | if ($_ =~ /^INFILE\s+\'(.*)\'/i) { | ||
| 921 | $inf = $1; last; | ||
| 922 | } | ||
| 923 | } | ||
| 924 | close CTL; | ||
| 925 | if ($inf && !-f $inf) { | ||
| 926 | $msg = " INFILE $inf does not exist.\n $typ: skipped."; | ||
| 927 | $self->_echoMSG($msg); | ||
| 928 | return 0; | ||
| 929 | } | ||
| 930 | if ($inf && -z $inf) { | ||
| 931 | $msg = " INFILE $inf is empty.\n $typ: abandoned."; | ||
| 932 | $self->_echoMSG($msg); | ||
| 933 | return 0; | ||
| 934 | } | ||
| 935 | if (!$inf) { | ||
| 936 | $msg = "WARNNING: could not find INFILE file name in $ctl."; | ||
| 937 | $self->_echoMSG($msg); | ||
| 938 | } | ||
| 939 | return 1; | ||
| 940 | } | ||
| 941 | |||
| 942 | =over 4 | ||
| 943 | |||
| 944 | =item * create ($typ, $cns, $sfn, $phm) | ||
| 945 | |||
| 946 | Input variables: | ||
| 947 | |||
| 948 | $typ - DB type: Oracle, MSSQL, etc. It defaults to | ||
| 949 | Oracle | ||
| 950 | $cns - connection string: usr/pwd@db | ||
| 951 | $sfn - sql file name | ||
| 952 | $phm - program (sqlldr) home directory | ||
| 953 | |||
| 954 | Variables used or methods called: | ||
| 955 | |||
| 956 | param - class method to get parameters | ||
| 957 | |||
| 958 | How to use: | ||
| 959 | |||
| 960 | $self->create; | ||
| 961 | $self->create('', 'usr/pwd@db'); | ||
| 962 | |||
| 963 | Return: None. | ||
| 964 | |||
| 965 | This method creates the tables by running SQL*Plus or other program | ||
| 966 | corresponding to its database. | ||
| 967 | |||
| 968 | =back | ||
| 969 | |||
| 970 | =cut | ||
| 971 | |||
| 972 | sub create { | ||
| 973 | my $self = shift; | ||
| 974 | my $pm=$self->param; | ||
| 975 | # creat: $typ, $cns, $sfn, $phm | ||
| 976 | # $typ - DB type: Oracle, MSSQL, | ||
| 977 | # $cns - connection string: usr/pwd@db | ||
| 978 | # $sfn - sql file name | ||
| 979 | # $phm - program (sqlldr) home directory | ||
| 980 | # | ||
| 981 | my ($typ,$cns,$sfn,$hmd)=$self->_getInput2('create', @_); | ||
| 982 | $self->_echoMSG(" - Creating $typ tables\n using $sfn..."); | ||
| 983 | # 05/07/2002: htu - added for infile checking | ||
| 984 | my $ctl = $pm->ctl_fn; | ||
| 985 | return if (!$self->check_infile($ctl, 'create')); | ||
| 986 | if (!$cns) { | ||
| 987 | $self->_echoMSG("ERR: no connection string is defined."); | ||
| 988 | return; | ||
| 989 | } | ||
| 990 | my $cmd = join $pm->DirSep, $hmd, "bin", "sqlplus"; | ||
| 991 | $cmd .= " -s $cns \@$sfn "; | ||
| 992 | # my @a=($sps, " -s ", "$cns ", "\@$sfn"); | ||
| 993 | # system (@a); | ||
| 994 | if ($pm->vbm eq 'Y') { | ||
| 995 | my $tmp=$cmd; | ||
| 996 | $tmp =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}; | ||
| 997 | $tmp =~ s{( \@)}{\n $1}g; | ||
| 998 | print " CMD: $tmp\n"; | ||
| 999 | } | ||
| 1000 | open CMD, "$cmd |" or croak "Could not run sqlplus: $!\n"; | ||
| 1001 | my @a = <CMD>; | ||
| 1002 | close CMD; | ||
| 1003 | if ($pm->vbm eq 'Y') { | ||
| 1004 | for my $i (0..$#a) { print $a[$i]; } | ||
| 1005 | } | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | =over 4 | ||
| 1009 | |||
| 1010 | =item * load ($typ, $cns, $ctl, $phm, $log) | ||
| 1011 | |||
| 1012 | Input variables: | ||
| 1013 | |||
| 1014 | $typ - DB type: Oracle, MSSQL, etc. It defaults to | ||
| 1015 | Oracle | ||
| 1016 | $cns - connection string: usr/pwd@db | ||
| 1017 | $ctl - control file name | ||
| 1018 | $phm - program (sqlldr) home directory | ||
| 1019 | $log - log file name | ||
| 1020 | |||
| 1021 | Variables used or methods called: | ||
| 1022 | |||
| 1023 | param - class method to get parameters | ||
| 1024 | |||
| 1025 | How to use: | ||
| 1026 | |||
| 1027 | $self->load; | ||
| 1028 | $self->load('', 'usr/pwd@db'); | ||
| 1029 | |||
| 1030 | Return: None. | ||
| 1031 | |||
| 1032 | This method loads that data into a corresponding table. For Oracle, | ||
| 1033 | sqlldr is used to load the data into the table. | ||
| 1034 | |||
| 1035 | =back | ||
| 1036 | |||
| 1037 | =cut | ||
| 1038 | |||
| 1039 | sub load { | ||
| 1040 | my $self = shift; | ||
| 1041 | my $pm=$self->param; | ||
| 1042 | # load: $typ, $cns, $ctl, $phm, $log | ||
| 1043 | # $typ - DB type: Oracle, MSSQL, | ||
| 1044 | # $cns - connection string: usr/pwd@db | ||
| 1045 | # $ctl - control file name | ||
| 1046 | # $phm - program (sqlldr) home directory | ||
| 1047 | # $log - log file name | ||
| 1048 | # | ||
| 1049 | my ($typ,$cns,$ctl,$hmd,$log)=$self->_getInput2('load', @_); | ||
| 1050 | my $msg = " - Loading data into $typ tables\n using $ctl...\n"; | ||
| 1051 | $msg .= " logging in $log..."; | ||
| 1052 | $self->_echoMSG($msg); | ||
| 1053 | return if (!$self->check_infile($ctl,'load')); | ||
| 1054 | if (!$cns) { | ||
| 1055 | $self->_echoMSG("ERR: no connection string is specified."); | ||
| 1056 | return; | ||
| 1057 | } | ||
| 1058 | my $cmd = ""; # loader program | ||
| 1059 | if ($typ eq "Oracle") { | ||
| 1060 | $cmd = join $pm->DirSep, $hmd, "bin", "sqlldr"; | ||
| 1061 | $cmd .= " $cns control=$ctl log=$log 2>&1"; | ||
| 1062 | $ENV{'ORACLE_HOME'} = $hmd; | ||
| 1063 | $ENV{'PATH'} = "$hmd/bin:$hmd/lib:/usr/bin:/usr/local/bin:."; | ||
| 1064 | $ENV{'LD_LIBRARY_PATH'} = "$hmd/lib"; | ||
| 1065 | } elsif ($typ eq "MSSQL") { | ||
| 1066 | $cmd = join "\\", $hmd, "bin", "osql"; | ||
| 1067 | } | ||
| 1068 | if ($pm->vbm eq 'Y') { | ||
| 1069 | my $tmp=$cmd; | ||
| 1070 | $tmp =~ s{(\w+)/(\w+)\@}{$1/\*\*\*\@}; | ||
| 1071 | $tmp =~ s{(\w+)=}{\n $1=}g; | ||
| 1072 | print " CMD: $tmp\n"; | ||
| 1073 | } | ||
| 1074 | open CMD, "$cmd |" or croak "Could not run sqlldr: $!\n"; | ||
| 1075 | my @a = <CMD>; | ||
| 1076 | close CMD; | ||
| 1077 | if ($pm->vbm eq 'Y') { | ||
| 1078 | for my $i (0..$#a) { print $a[$i]; } | ||
| 1079 | } | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | =over 4 | ||
| 1083 | |||
| 1084 | =item * batch ($typ, $cns, $sdr, $phm, $ext) | ||
| 1085 | |||
| 1086 | Input variables: | ||
| 1087 | |||
| 1088 | $typ - DB type: Oracle, MSSQL, etc. It defaults to | ||
| 1089 | Oracle | ||
| 1090 | $cns - connection string: usr/pwd@db | ||
| 1091 | $sdr - source directory containing all the definition files | ||
| 1092 | $phm - program (sqlplus, sqlldr, etc.) home directory | ||
| 1093 | $ext - definition file extension such as "def", "var", etc. | ||
| 1094 | It uses 'def_ex' if it is set, otherwise default to | ||
| 1095 | 'def'. | ||
| 1096 | |||
| 1097 | Variables used or methods called: | ||
| 1098 | |||
| 1099 | param - class method to get parameters | ||
| 1100 | crt_sql - create PL/SQL codes | ||
| 1101 | crt_ctl - create Oracle control file | ||
| 1102 | |||
| 1103 | How to use: | ||
| 1104 | |||
| 1105 | $self->batch; | ||
| 1106 | $self->batch('', 'usr/pwd@db', '/my/load/dir'); | ||
| 1107 | |||
| 1108 | Return: None. | ||
| 1109 | |||
| 1110 | This method calls I<read_definition>, I<crt_sql>, I<crt_ctl>, | ||
| 1111 | I<create>, I<load> methods to run through all the definition files | ||
| 1112 | in a source directory. | ||
| 1113 | |||
| 1114 | =back | ||
| 1115 | |||
| 1116 | =cut | ||
| 1117 | |||
| 1118 | sub batch { | ||
| 1119 | my $self = shift; | ||
| 1120 | my $pm=$self->param; | ||
| 1121 | # batch: $typ, $cns, $sdr, $phm, $ext | ||
| 1122 | # $typ - database type: Oracle, MSSQL | ||
| 1123 | # $cns - connection string: usr/pwd@db | ||
| 1124 | # $sdr - source directory containing all the definition files | ||
| 1125 | # $phm - program (sqlplus, sqlldr, etc.) home directory | ||
| 1126 | # $ext - definition file extension such as "def", "var", etc. | ||
| 1127 | # | ||
| 1128 | my ($typ,$cns,$sdr,$hmd,$ext)=$self->_getInput2('batch', @_); | ||
| 1129 | if (!-d $sdr) { croak "Could not find source directory - $sdr."; } | ||
| 1130 | $self->_echoMSG(" - Batch loading data from $sdr..."); | ||
| 1131 | # get a list of def file names | ||
| 1132 | opendir(DIR, "$sdr") || | ||
| 1133 | croak "Unable to open directory - $sdr: $!\n"; | ||
| 1134 | my $dsp = $pm->DirSep; | ||
| 1135 | my @fn = map "$sdr$dsp$_", grep /$ext$/, readdir DIR; | ||
| 1136 | closedir DIR; | ||
| 1137 | if (!@fn) { | ||
| 1138 | print " No definition files in $sdr!\n"; | ||
| 1139 | return; | ||
| 1140 | } | ||
| 1141 | my $apd_cur = $pm->append; | ||
| 1142 | my $rst_cur = $pm->reset; | ||
| 1143 | my $dbtab_cur = $pm->dbtab; | ||
| 1144 | my $dat_fn_cur = $pm->dat_fn; | ||
| 1145 | my $def_fn_cur = $pm->def_fn; | ||
| 1146 | my $sql_fn_cur = $pm->sql_fn; | ||
| 1147 | my $ctl_fn_cur = $pm->ctl_fn; | ||
| 1148 | my $log_fn_cur = $pm->log_fn; | ||
| 1149 | $pm->append('Y'); $pm->reset('Y'); | ||
| 1150 | if ($pm->overwrite eq 'Y' && -f $pm->sql_fn) { | ||
| 1151 | unlink $pm->sql_fn; | ||
| 1152 | } | ||
| 1153 | my ($bnm,$dir,$suf,$sfn,$ctl,$phm,$log); | ||
| 1154 | $phm = $pm->dbhome; | ||
| 1155 | for my $i (0..$#fn) { | ||
| 1156 | if ($pm->vbm eq 'Y') { printf " %2d $fn[$i]\n", $i; } | ||
| 1157 | $self->read_definition($fn[$i]); | ||
| 1158 | $pm->dbtab(""); | ||
| 1159 | $pm->_counter($i+1); | ||
| 1160 | if ($i==$#fn) { $pm->_counter('N'); } | ||
| 1161 | $self->sync; | ||
| 1162 | if ($pm->append eq 'Y' && $i==0) { | ||
| 1163 | unlink $pm->sql_fn; | ||
| 1164 | } | ||
| 1165 | $self->crt_sql; | ||
| 1166 | $self->crt_ctl; | ||
| 1167 | } | ||
| 1168 | if ($pm->append eq 'Y') { # only one pl/sql program to run | ||
| 1169 | if ($pm->commit eq 'Y') { $self->create; } | ||
| 1170 | } | ||
| 1171 | use File::Basename; | ||
| 1172 | if ($pm->commit eq 'Y') { | ||
| 1173 | for my $i (0..$#fn) { | ||
| 1174 | ($bnm,$dir,$suf)=fileparse($fn[$i],'\.\w+$'); | ||
| 1175 | $sfn = "$dir${bnm}.sql"; | ||
| 1176 | $ctl = "$dir${bnm}.ctl"; | ||
| 1177 | $log = "$dir${bnm}.log"; | ||
| 1178 | # we need to create table one by one | ||
| 1179 | if ($pm->append ne 'Y') { | ||
| 1180 | # creat: $typ, $cns, $sfn, $phm | ||
| 1181 | $self->create($typ,$cns,$sfn,$phm); | ||
| 1182 | } | ||
| 1183 | # load: $typ, $cns, $ctl, $phm, $log | ||
| 1184 | $self->load($typ,$cns,$ctl,$phm,$log); | ||
| 1185 | } | ||
| 1186 | } | ||
| 1187 | $pm->append($apd_cur); | ||
| 1188 | $pm->reset($rst_cur); | ||
| 1189 | $pm->dbtab($dbtab_cur); | ||
| 1190 | $pm->dat_fn($dat_fn_cur); | ||
| 1191 | $pm->def_fn($def_fn_cur); | ||
| 1192 | $pm->sql_fn($sql_fn_cur); | ||
| 1193 | $pm->ctl_fn($ctl_fn_cur); | ||
| 1194 | $pm->log_fn($log_fn_cur); | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | =over 4 | ||
| 1198 | |||
| 1199 | =item * read_log ($typ, $ifn, $rno) | ||
| 1200 | |||
| 1201 | Input variables: | ||
| 1202 | |||
| 1203 | $typ - type of information that is extracted from the log file. | ||
| 1204 | The types are: result or error | ||
| 1205 | $ifn - log file name | ||
| 1206 | $rno - record number | ||
| 1207 | |||
| 1208 | Variables used or methods called: | ||
| 1209 | |||
| 1210 | param - class method to get parameters | ||
| 1211 | sort_array - sort a numeric array | ||
| 1212 | compressArray - compress an array of numbers | ||
| 1213 | into a list of range or comma | ||
| 1214 | delimited numbers | ||
| 1215 | |||
| 1216 | How to use: | ||
| 1217 | |||
| 1218 | $self->read_log('','mylog.log'); | ||
| 1219 | |||
| 1220 | Return: None. | ||
| 1221 | |||
| 1222 | This method reads a SQL*Loader log file and return loading result or | ||
| 1223 | loading errors based on request. | ||
| 1224 | |||
| 1225 | =back | ||
| 1226 | |||
| 1227 | =cut | ||
| 1228 | |||
| 1229 | sub read_log { | ||
| 1230 | my $self = shift; | ||
| 1231 | my ($typ, $ifn, $rno) = @_; | ||
| 1232 | # Input variables: | ||
| 1233 | # $typ - type of information that is extracted from the log file. | ||
| 1234 | # The types are: result or error | ||
| 1235 | # $ifn - log file name | ||
| 1236 | # $rno - record number | ||
| 1237 | # | ||
| 1238 | if (!-f $ifn) { | ||
| 1239 | carp "WARNING: file - $ifn does not exist!"; | ||
| 1240 | return; | ||
| 1241 | } | ||
| 1242 | my $msg = sprintf " %3d reading $ifn...", $rno; | ||
| 1243 | $self->_echoMSG($msg); | ||
| 1244 | my $pm=$self->param; | ||
| 1245 | # Purpose: extract load result from SQL*Loader log file and | ||
| 1246 | # generate a bar delimited record with the following columns: | ||
| 1247 | # 1 - Success Rate | ||
| 1248 | # 2 - Oracle table name | ||
| 1249 | # 3 - Rows successfully loaded | ||
| 1250 | # 4 - Rows not loaded due to data errors | ||
| 1251 | # 5 - Rows not loaded because all WHEN clauses were failed | ||
| 1252 | # 6 - Rows not loaded because all fields were null | ||
| 1253 | # 7 - Total logical records skipped | ||
| 1254 | # 8 - Total logical records read | ||
| 1255 | # 9 - Total logical records rejected | ||
| 1256 | # 10 - Total logical records discarded | ||
| 1257 | # 11 - Start time | ||
| 1258 | # 12 - End time | ||
| 1259 | # 13 - Elapsed time | ||
| 1260 | # 14 - CPU time | ||
| 1261 | # | ||
| 1262 | my $hdr = ""; | ||
| 1263 | if ($typ eq 'result') { | ||
| 1264 | $hdr = "# Table columns:\n# 1 - Success Rate\n"; | ||
| 1265 | $hdr .= "# 2 - Oracle table name\n"; | ||
| 1266 | $hdr .= "# 3 - Rows successfully loaded\n"; | ||
| 1267 | $hdr .= "# 4 - Rows not loaded due to data errors\n"; | ||
| 1268 | $hdr .= "# 5 - Rows not loaded because all WHEN "; | ||
| 1269 | $hdr .= "clauses were failed\n"; | ||
| 1270 | $hdr .= "# 6 - Rows not loaded because all fields "; | ||
| 1271 | $hdr .= "were null\n"; | ||
| 1272 | $hdr .= "# 7 - Total logical records skipped\n"; | ||
| 1273 | $hdr .= "# 8 - Total logical records read\n"; | ||
| 1274 | $hdr .= "# 9 - Total logical records rejected\n"; | ||
| 1275 | $hdr .= "# 10 - Total logical records discarded\n"; | ||
| 1276 | $hdr .= "# 11 - Start time\n# 12 - End time\n"; | ||
| 1277 | $hdr .= "# 13 - Elapsed time\n# 14 - CPU time"; | ||
| 1278 | } else { | ||
| 1279 | $hdr = "SQL*Loader error report\n"; | ||
| 1280 | $hdr .= "=" x length($hdr) . "\n"; | ||
| 1281 | $hdr .= "# Output format:\n"; | ||
| 1282 | $hdr .= "# ORA-##### counts\n"; | ||
| 1283 | $hdr .= "# ORA-#####:tabname:colname (count) record range\n"; | ||
| 1284 | } | ||
| 1285 | my %A = (); my @B = (); my %C = (); | ||
| 1286 | $C{'Jan'} = 1; $C{'Feb'} = 2; $C{'Mar'} = 3; $C{'Apr'} = 4; | ||
| 1287 | $C{'May'} = 5; $C{'Jun'} = 6; $C{'Jul'} = 7; $C{'Aug'} = 8; | ||
| 1288 | $C{'Sep'} = 9; $C{'Oct'} = 10; $C{'Nov'} = 11; $C{'Dec'} = 12; | ||
| 1289 | open LOGF, "<$ifn" or croak "Could not open file - $ifn"; | ||
| 1290 | my ($rec,$rec_no,$rec_dsp,$j,$tn,$cn); | ||
| 1291 | if ($typ eq 'error') { | ||
| 1292 | while (<LOGF>) { | ||
| 1293 | next if (!$_ || /^#/); # skip empty and comment lines | ||
| 1294 | # Record 4: Rejected - Error on table S090P035. | ||
| 1295 | # ORA-01401: inserted value too large for column | ||
| 1296 | # | ||
| 1297 | # Record 27: Rejected - Error on table S090P001, column CONS_TM. | ||
| 1298 | # ORA-01401: inserted value too large for column | ||
| 1299 | if (/^Record\s*(\d+):\s*(.+)/) { # reserve record info | ||
| 1300 | $rec_no = $1; # record number | ||
| 1301 | $rec_dsp = $2; # reason for being rejected | ||
| 1302 | if ($rec_dsp =~ /table (.*),/) { $tn = $1; } | ||
| 1303 | if ($rec_dsp =~ /column (.*)\./) { $cn = $1; } | ||
| 1304 | } | ||
| 1305 | if (/^(ORA-\d+):\s*(.+)/) { | ||
| 1306 | $j = $1; # error number | ||
| 1307 | $A{$j}{'dsp'} = $2; # error descriptions | ||
| 1308 | $A{$j}{'id'} = $j; | ||
| 1309 | ++$A{$j}{'cnt'}; # counts for the same errors | ||
| 1310 | if ($A{$j}{'rng'}) { # record range | ||
| 1311 | $A{$j}{'rng'} .= ",$rec_no"; | ||
| 1312 | } else { $A{$j}{'rng'} = $rec_no; } | ||
| 1313 | $A{$j}{'tn'} = $tn; | ||
| 1314 | $A{$j}{'cn'} = $cn; | ||
| 1315 | } | ||
| 1316 | } | ||
| 1317 | } else { | ||
| 1318 | for my $i (0..13) { $B[$i] = 0; }; | ||
| 1319 | while (<LOGF>) { | ||
| 1320 | next if (!$_ || /^#/); # skip empty and comment lines | ||
| 1321 | # get Oracle table name. It has the following format: | ||
| 1322 | # Table S025P036: | ||
| 1323 | if ($_ =~ /^Table\s*(.*):$/) { $B[1] = $1; next; } | ||
| 1324 | # 3 Rows successfully loaded. | ||
| 1325 | if ($_ =~ /\s*(\d+)\s*Row(s?) successfully loaded\.$/) { | ||
| 1326 | $B[2] = $1; next; | ||
| 1327 | } | ||
| 1328 | # 2 Rows not loaded due to data errors. | ||
| 1329 | if (/due to data errors\.$/ && /^\s*(\d+)/ ) | ||
| 1330 | { $B[3] = $1; next; } | ||
| 1331 | # 0 Rows not loaded because all WHEN clauses were failed. | ||
| 1332 | if (/all WHEN clauses were failed\.$/ && /^\s*(\d+)/) { | ||
| 1333 | $B[4] = $1; next; | ||
| 1334 | } | ||
| 1335 | # 0 Rows not loaded because all fields were null. | ||
| 1336 | if (/all fields were null\.$/ && /^\s*(\d+)/) { | ||
| 1337 | $B[5] = $1; next; | ||
| 1338 | } | ||
| 1339 | # Total logical records skipped: 0 | ||
| 1340 | if (/^Total logical records skipped:\s*(\d+)/) { | ||
| 1341 | $B[6] = $1; next; | ||
| 1342 | } | ||
| 1343 | # Total logical records read: 5 | ||
| 1344 | if (/^Total logical records read:\s*(\d+)/) { | ||
| 1345 | $B[7] = $1; next; | ||
| 1346 | } | ||
| 1347 | # Total logical records rejected: 2 | ||
| 1348 | if (/^Total logical records rejected:\s*(\d+)/) { | ||
| 1349 | $B[8] = $1; next; | ||
| 1350 | } | ||
| 1351 | # Total logical records discarded: 0 | ||
| 1352 | if (/^Total logical records discarded:\s*(\d+)/) { | ||
| 1353 | $B[9] = $1; next; | ||
| 1354 | } | ||
| 1355 | # Run began on Fri Jan 26 22:23:15 2001 | ||
| 1356 | if (/^Run began on/) { | ||
| 1357 | if (/(\w+) (\d\d) (\d\d):(\d\d):(\d\d) (\d{1,4})$/) { | ||
| 1358 | $B[10] = sprintf "%04d/%02d/%02d %02d:%02d:%02d", | ||
| 1359 | $6, $C{$1}, $2, $3, $4, $5; | ||
| 1360 | } else { $B[10] = ""; } | ||
| 1361 | next; | ||
| 1362 | } | ||
| 1363 | # Run ended on Fri Jan 26 22:23:16 2001 | ||
| 1364 | if (/^Run ended on/) { | ||
| 1365 | if (/(\w+) (\d\d) (\d\d):(\d\d):(\d\d) (\d{1,4})$/) { | ||
| 1366 | $B[11] = sprintf "%04d/%02d/%02d %02d:%02d:%02d", | ||
| 1367 | $6, $C{$1}, $2, $3, $4, $5; | ||
| 1368 | } else { $B[11] = ""; } | ||
| 1369 | next; | ||
| 1370 | } | ||
| 1371 | # Elapsed time was: 00:00:01.11 | ||
| 1372 | if ($_ =~ /^Elapsed time was:\s*(.+)/) { | ||
| 1373 | $B[12] = $1; next; | ||
| 1374 | } | ||
| 1375 | # CPU time was: 00:00:00.10 | ||
| 1376 | if ($_ =~ /^CPU time was:\s*(.+)/) { | ||
| 1377 | $B[13] = $1; next; | ||
| 1378 | } | ||
| 1379 | } # end of while (<LOGF>) | ||
| 1380 | } | ||
| 1381 | close LOGF; | ||
| 1382 | my $rst = ""; | ||
| 1383 | if ($typ eq 'result') { | ||
| 1384 | if ($B[7] == 0) { $B[0] = sprintf('%6s', ''); | ||
| 1385 | } else { | ||
| 1386 | $B[0] = sprintf('%6.2f', $B[2] / $B[7] * 100); | ||
| 1387 | } | ||
| 1388 | $rst = $B[0]; | ||
| 1389 | for my $i (1..$#B) { $rst .= "|$B[$i]"; } | ||
| 1390 | # print "\@B: @B\n"; | ||
| 1391 | } else { | ||
| 1392 | # use Data::Subs wq(sort_array compressArray); | ||
| 1393 | my $i = 0; | ||
| 1394 | if (%A) { | ||
| 1395 | $rst = "$ifn\n" . "-" x length($ifn) . "\n"; | ||
| 1396 | } | ||
| 1397 | foreach my $k (sort keys %A) { | ||
| 1398 | my @a1 = split ",", $A{$k}{'rng'}; | ||
| 1399 | next if (!@a1); | ||
| 1400 | ++$i; | ||
| 1401 | if ($i>1) { $rst .= "\n"; } | ||
| 1402 | sort_array(\@a1); | ||
| 1403 | # print "RNG: $A{$k}{'rng'}\n"; | ||
| 1404 | # print "\@a1: @a1\n"; | ||
| 1405 | $rst .= sprintf "%-10s%6d\n", $k, $A{$k}{'cnt'}; | ||
| 1406 | $rst .= sprintf "%-10s:%s\n", $k, $A{$k}{'dsp'}; | ||
| 1407 | $rst .= sprintf "%-10s:%-10s:%-10s (%3d) %s\n", $k, | ||
| 1408 | $A{$k}{'tn'}, $A{$k}{'cn'}, $A{$k}{'cnt'}, | ||
| 1409 | &compressArray(\@a1); | ||
| 1410 | } | ||
| 1411 | # print "\%A: " . (keys %A) . "\n"; | ||
| 1412 | } | ||
| 1413 | if ($rno<=0) { $rst = "$hdr\n$rst"; } | ||
| 1414 | if ($rst) { | ||
| 1415 | return "$rst\n"; | ||
| 1416 | } | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | sub sort_array { | ||
| 1420 | my ($arf, $ord) = @_; | ||
| 1421 | # Input variables: | ||
| 1422 | # arr - array containing numbers | ||
| 1423 | # ord - sort order: default - ascending; other is decending | ||
| 1424 | # Local variables: | ||
| 1425 | # i,j - loop indexes | ||
| 1426 | # tmp - temp variable | ||
| 1427 | # Global variables used: None | ||
| 1428 | # Global variables modified/defined: | ||
| 1429 | # arr - sorted array | ||
| 1430 | # Return: None | ||
| 1431 | # | ||
| 1432 | my ($j, $tmp); | ||
| 1433 | for my $i (1..$#{$arf}) { | ||
| 1434 | if ($ord) { # sort numbers in decending order | ||
| 1435 | for ($j=$i; ${$arf}[$j] > ${$arf}[$j-1]; --$j) { | ||
| 1436 | if ($j <= 1) { last; } # j=1 has been compared | ||
| 1437 | $tmp = ${$arf}[$j]; | ||
| 1438 | ${$arf}[$j] = ${$arf}[$j-1]; | ||
| 1439 | ${$arf}[$j-1] = $tmp; | ||
| 1440 | } | ||
| 1441 | } else { | ||
| 1442 | # sort numbers in ascending order | ||
| 1443 | for ($j = $i; ${$arf}[$j-1] > ${$arf}[$j]; --$j) { | ||
| 1444 | $tmp = ${$arf}[$j]; | ||
| 1445 | ${$arf}[$j] = ${$arf}[$j-1]; | ||
| 1446 | ${$arf}[$j-1] = $tmp; | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | } | ||
| 1450 | return; | ||
| 1451 | } | ||
| 1452 | |||
| 1453 | sub compressArray { | ||
| 1454 | my ($arf) = @_; | ||
| 1455 | # Input variables: | ||
| 1456 | # arr - numeric array sorted ascendingly | ||
| 1457 | # Local variables: | ||
| 1458 | # | ||
| 1459 | # Global variables used: None | ||
| 1460 | # Global variables modified: None | ||
| 1461 | # Calls-To: None | ||
| 1462 | # Return: a string | ||
| 1463 | # | ||
| 1464 | my $S = ${$arf}[0]; | ||
| 1465 | my $k = 0; | ||
| 1466 | for my $i (1..$#{$arf}) { | ||
| 1467 | my $j = $i - 1; | ||
| 1468 | # skip the second one if it is the same number as the | ||
| 1469 | # previous one. | ||
| 1470 | next if (${$arf}[$i] == ${$arf}[$j]); | ||
| 1471 | if (${$arf}[$i] == (${$arf}[$j] + 1)) { # if they are adjacent | ||
| 1472 | if ($i == $#{$arf}) { | ||
| 1473 | if (substr($S, length($S)-1, 1) ne '-') { | ||
| 1474 | $S .= "-${$arf}[$i]"; next; | ||
| 1475 | } else { | ||
| 1476 | $S .= ${$arf}[$i]; next; | ||
| 1477 | } | ||
| 1478 | } | ||
| 1479 | ++$k; | ||
| 1480 | if (substr($S, length($S)-1, 1) eq '-') { | ||
| 1481 | next; | ||
| 1482 | } | ||
| 1483 | $S .= '-'; | ||
| 1484 | } else { # if they are separate | ||
| 1485 | if ($k) { | ||
| 1486 | $S .= "${$arf}[$j],${$arf}[$i]"; | ||
| 1487 | } else { | ||
| 1488 | $S .= ",${$arf}[$i]"; | ||
| 1489 | } | ||
| 1490 | $k = 0; | ||
| 1491 | } | ||
| 1492 | } | ||
| 1493 | return $S; | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | |||
| 1497 | =over 4 | ||
| 1498 | |||
| 1499 | =item * report_results ($typ, $cns, $sdr, $ofn, $ext) | ||
| 1500 | |||
| 1501 | Input variables: | ||
| 1502 | |||
| 1503 | $typ - database type: Oracle, MSSQL | ||
| 1504 | $cns - connection string: usr/pwd@db | ||
| 1505 | $sdr - source directory containing all the | ||
| 1506 | definition files | ||
| 1507 | $ofn - output file name | ||
| 1508 | $ext - log file extension such as "log", "lst", | ||
| 1509 | etc. | ||
| 1510 | |||
| 1511 | Variables used or methods called: | ||
| 1512 | |||
| 1513 | param - class method to get parameters | ||
| 1514 | read_log - read an Oracle log file | ||
| 1515 | |||
| 1516 | How to use: | ||
| 1517 | |||
| 1518 | $self->report_results; | ||
| 1519 | |||
| 1520 | Return: None. | ||
| 1521 | |||
| 1522 | This method reads all the SQL*Loader log files in a load directory | ||
| 1523 | and generates a nice report with the following fields: | ||
| 1524 | |||
| 1525 | 1 - Success Rate | ||
| 1526 | 2 - Oracle table name | ||
| 1527 | 3 - Rows successfully loaded | ||
| 1528 | 4 - Rows not loaded due to data errors | ||
| 1529 | 5 - Rows not loaded because all WHEN clauses were | ||
| 1530 | failed | ||
| 1531 | 6 - Rows not loaded because all fields were null | ||
| 1532 | 7 - Total logical records skipped | ||
| 1533 | 8 - Total logical records read | ||
| 1534 | 9 - Total logical records rejected | ||
| 1535 | 10 - Total logical records discarded | ||
| 1536 | 11 - Start time | ||
| 1537 | 12 - End time | ||
| 1538 | 13 - Elapsed time | ||
| 1539 | 14 - CPU time | ||
| 1540 | |||
| 1541 | =back | ||
| 1542 | |||
| 1543 | =cut | ||
| 1544 | |||
| 1545 | sub report_results { | ||
| 1546 | my $self = shift; | ||
| 1547 | my $pm=$self->param; | ||
| 1548 | # get_load_results: $typ, $cns, $sdr, $ofn, $ext | ||
| 1549 | # $typ - database type: Oracle, MSSQL | ||
| 1550 | # $cns - connection string: usr/pwd@db | ||
| 1551 | # $sdr - source directory containing all the definition files | ||
| 1552 | # $ofn - output file name | ||
| 1553 | # $ext - log file extension such as "log", "lst", etc. | ||
| 1554 | # | ||
| 1555 | my ($typ,$cns,$sdr,$ofn,$ext)= | ||
| 1556 | $self->_getInput2('report_results', @_); | ||
| 1557 | if (!-d $sdr) { croak "Could not find source directory - $sdr."; } | ||
| 1558 | $self->_echoMSG(" + Getting load results for log files in $sdr"); | ||
| 1559 | # get a list of def file names | ||
| 1560 | opendir(DIR, "$sdr") || | ||
| 1561 | croak "Unable to open directory - $sdr: $!\n"; | ||
| 1562 | my $dsp = $pm->DirSep; | ||
| 1563 | my @fn = map "$sdr$dsp$_", grep /$ext$/, readdir DIR; | ||
| 1564 | closedir DIR; | ||
| 1565 | if (!@fn) { | ||
| 1566 | print " No log files in $sdr!\n"; | ||
| 1567 | return; | ||
| 1568 | } | ||
| 1569 | my $rpt = (index($ofn, '\/')>-1)?$ofn:join $dsp, $sdr, $ofn; | ||
| 1570 | # open the output file | ||
| 1571 | if (-f $rpt && $pm->overwrite eq 'Y' && $pm->append ne 'Y') { | ||
| 1572 | unlink $rpt; | ||
| 1573 | } | ||
| 1574 | $self->_echoMSG(" to report file $rpt..."); | ||
| 1575 | open OUT, ">>$rpt" or croak "Could not open output file - $rpt."; | ||
| 1576 | for my $i (0..$#fn) { | ||
| 1577 | if (-z $fn[$i]) { | ||
| 1578 | print OUT "# WARNING: no content in $fn[$i]\n"; | ||
| 1579 | next; | ||
| 1580 | } | ||
| 1581 | print OUT $self->read_log('result', $fn[$i], $i); | ||
| 1582 | } | ||
| 1583 | close OUT; | ||
| 1584 | } | ||
| 1585 | |||
| 1586 | =over 4 | ||
| 1587 | |||
| 1588 | =item * report_errors ($typ, $cns, $sdr, $ofn, $ext) | ||
| 1589 | |||
| 1590 | Input variables: | ||
| 1591 | |||
| 1592 | $typ - database type: Oracle, MSSQL | ||
| 1593 | $cns - connection string: usr/pwd@db | ||
| 1594 | $sdr - source directory containing all the | ||
| 1595 | definition files | ||
| 1596 | $ofn - output file name | ||
| 1597 | $ext - log file extension such as "log", "lst", | ||
| 1598 | etc. | ||
| 1599 | |||
| 1600 | Variables used or methods called: | ||
| 1601 | |||
| 1602 | param - class method to get parameters | ||
| 1603 | read_log - read an Oracle log file | ||
| 1604 | |||
| 1605 | How to use: | ||
| 1606 | |||
| 1607 | $self->report_errors; | ||
| 1608 | |||
| 1609 | Return: None. | ||
| 1610 | |||
| 1611 | This method reads all the SQL*Loader log files in a load directory | ||
| 1612 | and generates a nice error report with the following information: | ||
| 1613 | |||
| 1614 | SQL*Loader error report | ||
| 1615 | ======================== | ||
| 1616 | # Output format: | ||
| 1617 | # ORA-##### counts | ||
| 1618 | # ORA-#####:table_name:colum_name (count) record range | ||
| 1619 | |||
| 1620 | =back | ||
| 1621 | |||
| 1622 | =cut | ||
| 1623 | |||
| 1624 | sub report_errors { | ||
| 1625 | my $self = shift; | ||
| 1626 | my $pm=$self->param; | ||
| 1627 | # report_erros: $typ, $cns, $sdr, $ofn, $ext | ||
| 1628 | # $typ - database type: Oracle, MSSQL | ||
| 1629 | # $cns - connection string: usr/pwd@db | ||
| 1630 | # $sdr - source directory containing all the definition files | ||
| 1631 | # $ofn - output file name | ||
| 1632 | # $ext - definition file extension such as "def", "var", etc. | ||
| 1633 | # | ||
| 1634 | my ($typ,$cns,$sdr,$ofn,$ext)= | ||
| 1635 | $self->_getInput2('report_errors', @_); | ||
| 1636 | if (!-d $sdr) { croak "Could not find source directory - $sdr."; } | ||
| 1637 | $self->_echoMSG(" + Getting load errors from $sdr"); | ||
| 1638 | # get a list of def file names | ||
| 1639 | opendir(DIR, "$sdr") || | ||
| 1640 | croak "Unable to open directory - $sdr: $!\n"; | ||
| 1641 | my $dsp = $pm->DirSep; | ||
| 1642 | my @fn = map "$sdr$dsp$_", grep /$ext$/, readdir DIR; | ||
| 1643 | closedir DIR; | ||
| 1644 | if (!@fn) { | ||
| 1645 | print " No definition files in $sdr!\n"; | ||
| 1646 | return; | ||
| 1647 | } | ||
| 1648 | my $rpt = (index($ofn, '\/')>-1)?$ofn:join $dsp, $sdr, $ofn; | ||
| 1649 | # open the output file | ||
| 1650 | if (-f $rpt && $pm->overwrite eq 'Y' && $pm->append ne 'Y') { | ||
| 1651 | unlink $rpt; | ||
| 1652 | } | ||
| 1653 | $self->_echoMSG(" to report file $rpt..."); | ||
| 1654 | open OUT, ">>$rpt" or croak "Could not open output file - $rpt."; | ||
| 1655 | for my $i (0..$#fn) { | ||
| 1656 | print OUT $self->read_log('error', $fn[$i], $i); | ||
| 1657 | } | ||
| 1658 | close OUT; | ||
| 1659 | } | ||
| 1660 | |||
| 1661 | sub _list_vars { | ||
| 1662 | my $vs = "cols_ref,out_fh,sql_fn,ctl_fn,dat_fn,bad_fn,dis_fn,"; | ||
| 1663 | $vs .= "log_fn,def_fn,reset,dbsid,dbusr,dbpwd,dbhome,conn,"; | ||
| 1664 | $vs .= "direct,spool,dbtab,dbts,ts_iext,ts_next,src_dir,"; | ||
| 1665 | $vs .= "append,vbm,Oracle,CSV,overwrite,dbconn,DirSep,commit,"; | ||
| 1666 | $vs .= "db_type,relax_req,add_center,study_number,drop,def_ex"; | ||
| 1667 | my @vars = split /,/, $vs; | ||
| 1668 | return @vars; | ||
| 1669 | } | ||
| 1670 | |||
| 1671 | sub _set_dbconn { | ||
| 1672 | my $self = shift; | ||
| 1673 | my $cns = shift; # in usr/pwd@db | ||
| 1674 | my @a = split /\//, $cns; | ||
| 1675 | my @b = split /\@/, $a[1]; | ||
| 1676 | my ($usr, $pwd, $sid) = ($a[0], $b[0], $b[1]); | ||
| 1677 | my $pm = $self->param; | ||
| 1678 | my $cn=$self->conn; | ||
| 1679 | $pm->dbusr($usr); | ||
| 1680 | $pm->dbpwd($pwd); | ||
| 1681 | $pm->dbsid($sid); | ||
| 1682 | $pm->dbconn("$usr/$pwd\@$sid"); | ||
| 1683 | $cn->Oracle(0,"DBI:Oracle:$sid"); | ||
| 1684 | $cn->Oracle(1,$usr); | ||
| 1685 | $cn->Oracle(2,$pwd); | ||
| 1686 | } | ||
| 1687 | |||
| 1688 | sub _getInputs { | ||
| 1689 | my $self = shift; | ||
| 1690 | my $sub = shift; | ||
| 1691 | # initialize variables and check inputs | ||
| 1692 | # (ColRef,FN,Appd,DataFile/OraTab,Reset) | ||
| 1693 | # crt_sql: (ColRef,SQLFN,Appd,OraTab,ReSet,Drop) | ||
| 1694 | # crt_ctl: (ColRef,FH/CTLFN,Appd,DataFile,ReSet,Drop) | ||
| 1695 | my $pm = $self->param; | ||
| 1696 | my $reset = $_[4] if (defined($_[4])); # Y or N | ||
| 1697 | my $drp = $_[5] if (defined($_[5])); # Y or N | ||
| 1698 | $drp = $pm->drop if !$drp; | ||
| 1699 | $drp = 'Y' if !$drp; | ||
| 1700 | my $rst = 0; # 1 or 0 | ||
| 1701 | if ($reset) { $rst = ($reset eq 'Y')?1:0; | ||
| 1702 | } else { $rst = ($pm->reset eq 'Y')?1:0; } | ||
| 1703 | my $crf = ""; # array ref for column def array | ||
| 1704 | # 1st input: ColRef | ||
| 1705 | $crf = shift | ||
| 1706 | if (ref($_[0]) eq 'ARRAY' || ($_[0] && $_[0] =~ /.*=ARRAY/)); | ||
| 1707 | if ($rst) { | ||
| 1708 | # uncomment the following line if you want to re-set cols_ref | ||
| 1709 | # every time a ref to pass througj crt_sql or crt_ctl | ||
| 1710 | $self->cols_ref($crf) if ($crf); | ||
| 1711 | } | ||
| 1712 | $crf = $self->cols_ref if (!$crf); # or use obj value | ||
| 1713 | my $fh = ""; # 2nd input: FH or | ||
| 1714 | $fh = shift if ($_[0] && (ref($_[0]) =~ /^(GLOB|IO::Handle)/ || | ||
| 1715 | $_[0] =~ /.*=(GLOB|IO::Handle)/)); | ||
| 1716 | if ($rst) { $self->out_fh($fh) if ($fh); } | ||
| 1717 | $fh = $self->out_fh if (!$fh); # or use obj value | ||
| 1718 | my $fn = ""; # 2nd input: FN | ||
| 1719 | $fn = shift if ($_[0] && $_[0] !~ /^(Y|N|O)$/i ); # use input | ||
| 1720 | my $appd = ""; # 3rd input: Y/N/O | ||
| 1721 | $appd = uc shift if ($_[0]); # change to upper case | ||
| 1722 | if ($rst) { $pm->append($appd) if ($appd); } | ||
| 1723 | $appd = $pm->append if (!$appd); # get it from init | ||
| 1724 | my $dat = ""; # 4th input: DataFile | ||
| 1725 | $dat = shift if ($_[0]); # use input | ||
| 1726 | if ($sub eq 'crt_ctl') { | ||
| 1727 | if ($rst) { $pm->dat_fn($dat) if ($dat); } | ||
| 1728 | $dat = $pm->dat_fn if (!$dat); # or use obj value | ||
| 1729 | if ($rst) { $pm->ctl_fn($fn) if ($fn); } | ||
| 1730 | $fn = $pm->ctl_fn if (!$fn); # use obj value | ||
| 1731 | if (!$dat && $fn) { | ||
| 1732 | use File::Basename; | ||
| 1733 | my ($bnm,$dir,$typ)=fileparse($fn,'\.\w+$'); | ||
| 1734 | $dat = "$dir${bnm}.dat"; | ||
| 1735 | } | ||
| 1736 | if ($rst) { $pm->dat_fn($dat) if ($dat); } | ||
| 1737 | } else { | ||
| 1738 | # in crt_sql, we are expecting oracle table name | ||
| 1739 | if ($rst) { $pm->dbtab($dat) if ($dat); } | ||
| 1740 | $dat = $pm->dbtab if (!$dat); | ||
| 1741 | if ($rst) { $pm->sql_fn($fn) if ($fn); } | ||
| 1742 | $fn = $pm->sql_fn if (!$fn); # use obj value | ||
| 1743 | } | ||
| 1744 | if (!$fh) { # if file handler still not being defined | ||
| 1745 | if ($fn) { # if a file name is specified | ||
| 1746 | # check the existance of the file | ||
| 1747 | if (-f $fn && $appd eq 'N') { | ||
| 1748 | if ($pm->overwrite eq 'Y') { | ||
| 1749 | unlink $fn; | ||
| 1750 | } else { | ||
| 1751 | croak "File $fn exist!"; | ||
| 1752 | } | ||
| 1753 | } | ||
| 1754 | use IO::File; | ||
| 1755 | if ($appd eq 'Y' && $sub ne 'crt_ctl') { | ||
| 1756 | # append to the file | ||
| 1757 | $fh = new IO::File ">>$fn"; | ||
| 1758 | } else { | ||
| 1759 | $fh = new IO::File ">$fn"; | ||
| 1760 | } | ||
| 1761 | } else { | ||
| 1762 | $fh = *STDOUT; | ||
| 1763 | } | ||
| 1764 | } | ||
| 1765 | $self->_echoMSG(" - Creating $fn ($sub)..."); | ||
| 1766 | return ($crf,$fh,$fn,$appd,$dat,$rst,$drp); | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | sub _getInput2 { | ||
| 1770 | my $self = shift; | ||
| 1771 | my $sub = shift; | ||
| 1772 | my $pm=$self->param; | ||
| 1773 | # Input variables: | ||
| 1774 | # creat: $typ, $cns, $sfn, $phm | ||
| 1775 | # load: $typ, $cns, $ctl, $phm, $log | ||
| 1776 | # batch: $typ, $cns, $sdr, $phm, $ext | ||
| 1777 | # load_results: $typ, $cns, $sdr, $ofn, $ext | ||
| 1778 | # load_errors: $typ, $cns, $sdr, $ofn, $ext | ||
| 1779 | # | ||
| 1780 | # output var names: $typ, $cns, $ctl, $hmd, $log | ||
| 1781 | # $typ - DB type: Oracle, MSSQL, | ||
| 1782 | # $cns - connection string: usr/pwd@db | ||
| 1783 | # $sfn/$ctl/$sdr - sql/control file name/source directory | ||
| 1784 | # $phm - program (sqlldr) home directory | ||
| 1785 | # $log - log file name | ||
| 1786 | # | ||
| 1787 | my $rst = ($pm->reset eq 'Y')?1:0; | ||
| 1788 | my $typ = "Oracle"; # database type default to Oracle | ||
| 1789 | $typ = shift if ($_[0]); | ||
| 1790 | my $cns = ""; # connection string usr/pwd@db | ||
| 1791 | $cns = shift if ($_[0]); | ||
| 1792 | $cns = $pm->dbconn if (!$cns); | ||
| 1793 | if ($cns ne $pm->dbconn && $rst) { $self->_set_dbconn($cns); } | ||
| 1794 | my $ctl = ""; # control file name | ||
| 1795 | $ctl = shift if ($_[0]); | ||
| 1796 | if ($sub eq 'create') { # get SQL file name | ||
| 1797 | $ctl = $pm->sql_fn if (!$ctl); | ||
| 1798 | if ($ctl ne $pm->sql_fn && $rst) { $pm->sql_fn($ctl); } | ||
| 1799 | } elsif ($sub =~ /^(batch|report_results|report_errors)/) { | ||
| 1800 | $ctl = $pm->src_dir if (!$ctl); | ||
| 1801 | if ($sub =~ /^(batch)/) { | ||
| 1802 | if ($ctl ne $pm->src_dir && $rst) { $pm->src_dir($ctl); } | ||
| 1803 | } | ||
| 1804 | } else { | ||
| 1805 | $ctl = $pm->ctl_fn if (!$ctl); | ||
| 1806 | if ($ctl ne $pm->ctl_fn && $rst) { $pm->ctl_fn($ctl); } | ||
| 1807 | } | ||
| 1808 | my $hmd = ""; # Oracle home directory | ||
| 1809 | $hmd = shift if ($_[0]); | ||
| 1810 | my $log = ""; # log file name | ||
| 1811 | $log = shift if ($_[0]); | ||
| 1812 | if ($sub eq 'load') { | ||
| 1813 | $log = $pm->log_fn if (!$log); | ||
| 1814 | if ($log ne $pm->log_fn && $rst) { $pm->log_fn($log); } | ||
| 1815 | } elsif ($sub eq "batch") { | ||
| 1816 | $log = $self->param->def_ex; | ||
| 1817 | $log = "def" if !$log; | ||
| 1818 | } elsif ($sub =~ /^(report_results|report_errors)/) { | ||
| 1819 | $log = "log"; | ||
| 1820 | } | ||
| 1821 | if ($sub !~ /^(report_results|report_errors)/) { | ||
| 1822 | if ($typ eq "Oracle") { $hmd = $pm->dbhome if (!$hmd); | ||
| 1823 | if ($hmd ne $pm->dbhome && $rst) { $pm->dbhome($hmd); } | ||
| 1824 | } elsif ($typ eq "MSSQL") { $hmd = $pm->msshome if (!$hmd); | ||
| 1825 | if ($hmd ne $pm->msshome && $rst) { $pm->msshome($hmd); } | ||
| 1826 | } else { $hmd = ""; } | ||
| 1827 | } | ||
| 1828 | if (!$hmd) { | ||
| 1829 | my $e1 = "rpt"; | ||
| 1830 | if ($sub =~ /^(report_results)/) { $e1 = "rst"; | ||
| 1831 | } elsif ($sub =~ /^(report_errors)/) { $e1 = "err"; } | ||
| 1832 | if ($pm->study_number) { | ||
| 1833 | $hmd = sprintf "S%03d_ldr.$e1", $pm->study_number; | ||
| 1834 | } else { | ||
| 1835 | my @a = split /\//, $ctl; | ||
| 1836 | $hmd = "$a[$#a-1]_ldr.$e1"; | ||
| 1837 | } | ||
| 1838 | } | ||
| 1839 | return ($typ, $cns, $ctl, $hmd, $log); | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | sub _echoMSG { | ||
| 1843 | my $self = shift; | ||
| 1844 | my $msg = shift; | ||
| 1845 | if ($self->param->vbm eq 'Y') { print "$msg\n"; } | ||
| 1846 | } | ||
| 1847 | |||
| 1848 | 1; # ensure that the module can be successfully used. | ||
| 1849 | |||
| 1850 | __END__ |