| 1 | package Oracle::Sqlldr; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Oracle::Sqlldr - Perl wrapper around Oracle's sqlldr utility. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Oracle::Sqlldr; | ||
| 10 | my $sqlldr = Oracle::Sqlldr->new(); # get new sqlldr object | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | Oracle::Sqlldr is an object-oriented class that provides a convenient | ||
| 15 | Perl wrapper around Oracle's sqlldr utility. | ||
| 16 | |||
| 17 | SQL*Loader (I<sqlldr>) is the utility to use for high performance | ||
| 18 | data loading from a text file into a an Oracle database. | ||
| 19 | |||
| 20 | =head1 LIMITATIONS | ||
| 21 | |||
| 22 | =over | ||
| 23 | |||
| 24 | =item No WIN32 support | ||
| 25 | |||
| 26 | =item No fixed format record support | ||
| 27 | |||
| 28 | =item Assumes table owner and user to load data as are the same | ||
| 29 | |||
| 30 | =item No support for parameter file | ||
| 31 | |||
| 32 | =head1 CAUTION | ||
| 33 | |||
| 34 | Whilst you are calling the method C<execute()>, Oracle::Sqlldr is calling C<sqlldr> and displaying your user/pass to the world, or at least readable within C<`ps -deaf`>. | ||
| 35 | |||
| 36 | =head1 PERFORMANCE | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =item Bulk uploads will be faster if indexes are disabled and built after loading. | ||
| 41 | |||
| 42 | =item Disable Archiving, only do this if the DBA is at lunch. | ||
| 43 | |||
| 44 | =item Use fixed width data - unsupported. | ||
| 45 | |||
| 46 | =head1 EXAMPLE | ||
| 47 | |||
| 48 | use strict; | ||
| 49 | use warnings; | ||
| 50 | use Oracle::Sqlldr; | ||
| 51 | |||
| 52 | my $sqlldr = Oracle::Sqlldr->new(-db=>'thedb'); | ||
| 53 | |||
| 54 | $sqlldr->warnings(-status=>'on'); | ||
| 55 | $sqlldr->table(-name=>'animals'); | ||
| 56 | $sqlldr->user(-name=>'scott'); | ||
| 57 | $sqlldr->pass(-word=>'tiger'); | ||
| 58 | $sqlldr->fieldsterminatedby(-symbol=>','); | ||
| 59 | $sqlldr->datafile(-file=>'animals.dat'); | ||
| 60 | $sqlldr->controlfile(-file=>'animals.ctr'); | ||
| 61 | $sqlldr->logfile(-file=>'animals.log'); | ||
| 62 | $sqlldr->badfile(-file=>'animals.bad'); | ||
| 63 | $sqlldr->discardfile(-file=>'animals.dis'); | ||
| 64 | |||
| 65 | $sqlldr->create_controlfile() or die "cannot create the controlfile"; | ||
| 66 | $sqlldr->write_controlfile() or die "cannot write controlfile"; | ||
| 67 | |||
| 68 | my $r = $sqlldr->execute() or die "cannot execute sqlldr"; | ||
| 69 | |||
| 70 | print "output from Oracle::Sqlldr: $r\n"; | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Andrew McGregor, E<lt>mcgregor@cpan.orgE<gt> | ||
| 75 | |||
| 76 | =head1 SEE ALSO | ||
| 77 | |||
| 78 | C<Oracle::SQLLoader> | ||
| 79 | |||
| 80 | =head1 METHODS | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | require 5.006; | ||
| 87 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 88 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 89 | 1 | ▶ | use Carp qw/carp croak/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 90 | 0 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 91 | use vars qw /$VERSION $bin/; | ||
| 92 | |||
| 93 | #require Exporter; | ||
| 94 | |||
| 95 | #our @ISA = qw(Exporter); | ||
| 96 | |||
| 97 | # This allows declaration use Oracle::Sqlldr ':all'; | ||
| 98 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 99 | # will save memory. | ||
| 100 | #our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); | ||
| 101 | #our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 102 | #our @EXPORT = qw( ); | ||
| 103 | |||
| 104 | $VERSION = '0.13'; | ||
| 105 | |||
| 106 | $bin = 'sqlldr'; | ||
| 107 | |||
| 108 | |||
| 109 | =item new | ||
| 110 | |||
| 111 | constructor to create a new instance of the Sqlldr. | ||
| 112 | |||
| 113 | C<my $sqlldr = Oracle->Sqlldr;> | ||
| 114 | |||
| 115 | C<my $sqlldr = Oracle->Sqlldr(-warnings=>'on');> | ||
| 116 | |||
| 117 | C<my $sqlldr = Oracle->Sqlldr(-warnings => 'off',> | ||
| 118 | C< -parameterfile => 'parameterfile.par',> | ||
| 119 | C< -datafile => 'datafile.csv',> | ||
| 120 | C< -controlfile => 'controlfile.ctr',> | ||
| 121 | C< -logfile => 'logfile.log',> | ||
| 122 | C< -discardfile => 'discardfile.dis',> | ||
| 123 | C< -badfile => 'badfile.bad',> | ||
| 124 | C< -table => 'table_name',> | ||
| 125 | C< -fieldsterminatedby => ',',> | ||
| 126 | C< -user => 'scott',> | ||
| 127 | C< -pass => 'tiger',> | ||
| 128 | C< -db => 'foo',> | ||
| 129 | C< );> | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | sub new { | ||
| 134 | my ($pkg, %params) = @_; | ||
| 135 | my $self = {}; | ||
| 136 | |||
| 137 | # check for any critical errors before instantiation | ||
| 138 | |||
| 139 | ## TODO | ||
| 140 | ## perhaps this should be in the Makefile.PL to stop | ||
| 141 | ## MS users installing it only to find out they are | ||
| 142 | ## unsupported. | ||
| 143 | if ($^O =~ /win32/i) { | ||
| 144 | croak __PACKAGE__."::new: WIN32 is unsupported."; | ||
| 145 | } | ||
| 146 | |||
| 147 | $self->{CSTR} = 'DBI:Oracle:'; | ||
| 148 | |||
| 149 | # instantiate the object | ||
| 150 | bless($self, $pkg); | ||
| 151 | |||
| 152 | # check for any none critical warnings | ||
| 153 | |||
| 154 | # enable or disable warnings depending on signature | ||
| 155 | ## TODO | ||
| 156 | ## warnings should be in a more generic package | ||
| 157 | ## which can he inherited by us. | ||
| 158 | ## TODO | ||
| 159 | ## when I call an external facing method | ||
| 160 | ## that expects its first parameter to be $self | ||
| 161 | ## how do I call it from within the object without | ||
| 162 | ## explicitly typing $self every time? | ||
| 163 | warnings($self, -status=>$params{-warnings}) if $params{-warnings}; | ||
| 164 | |||
| 165 | # check for $ORACLE_HOME, if warnings are enabled | ||
| 166 | carp "env \$ORACLE_HOME is not defined" | ||
| 167 | if not $ENV{ORACLE_HOME} and $self->warnings(); | ||
| 168 | |||
| 169 | # check for $ORACLE_BASE, if warnings are enabled | ||
| 170 | carp "env \$ORACLE_BASE is not defined" | ||
| 171 | if not $ENV{ORACLE_BASE} and $self->warnings(); | ||
| 172 | |||
| 173 | parameterfile($self, -file=>$params{-parameterfile}); | ||
| 174 | |||
| 175 | datafile($self, -file=>$params{-datafile}); | ||
| 176 | |||
| 177 | controlfile($self, -file=>$params{-controlfile}); | ||
| 178 | |||
| 179 | logfile($self, -file=>$params{-logfile}); | ||
| 180 | |||
| 181 | discardfile($self, -file=>$params{-discardfile}); | ||
| 182 | |||
| 183 | badfile($self, -file=>$params{-badfile}); | ||
| 184 | |||
| 185 | user($self, -name=>$params{-user}); | ||
| 186 | |||
| 187 | pass($self, -word=>$params{-pass}); | ||
| 188 | |||
| 189 | table($self, -name=>$params{-table}); | ||
| 190 | |||
| 191 | db($self, -name=>$params{-db}); | ||
| 192 | |||
| 193 | if ($params{-fieldsterminatedby}) { | ||
| 194 | fieldsterminatedby($self, -symbol=>$params{-fieldsterminatedby}); | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | carp "fixed format records are unsupported, specify a field terminator." | ||
| 198 | if $self->warnings(); | ||
| 199 | } | ||
| 200 | |||
| 201 | return $self; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | =item warnings | ||
| 208 | |||
| 209 | turn warnings on or or off | ||
| 210 | return status, 1 = on, 0 = off | ||
| 211 | |||
| 212 | C<my $status = $sqlldr->warnings(-status=>'on');> | ||
| 213 | C<my $status = $sqlldr->warnings(-status=>'off');> | ||
| 214 | C< $sqlldr->warnings(-status=>1);> | ||
| 215 | C<my $status = $sqlldr->warnings(-status=>0);> | ||
| 216 | C<my $status = $sqlldr->warnings();> | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub warnings { | ||
| 221 | my ($self, %params) = @_; | ||
| 222 | |||
| 223 | if (defined $params{-status}) { | ||
| 224 | |||
| 225 | no warnings; | ||
| 226 | |||
| 227 | if ($params{-status} > 0 or $params{-status} =~ /on/i) { | ||
| 228 | warnings_on($self); | ||
| 229 | } | ||
| 230 | elsif ($params{-status} == 0 or $params{-status} =~ /off/i) { | ||
| 231 | warnings_off($self); | ||
| 232 | } | ||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | return $self->{WARNINGS}; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | =item warnings_on | ||
| 243 | |||
| 244 | turn warnings on | ||
| 245 | |||
| 246 | C<$sqlldr->warnings_on();> | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub warnings_on { | ||
| 251 | my ($self) = @_; | ||
| 252 | $self->{WARNINGS} = 1; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | =item warnings_off | ||
| 259 | |||
| 260 | turn warnings off | ||
| 261 | |||
| 262 | C<$sqlldr->warnings_off();> | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub warnings_off { | ||
| 267 | my ($self) = @_; | ||
| 268 | $self->{WARNINGS} = 0; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | =item logfile | ||
| 275 | |||
| 276 | set the logfile to load. | ||
| 277 | |||
| 278 | C<$sqlldr->logfile(-file=>'load.log');> | ||
| 279 | |||
| 280 | SQL*Loader writes messages to this log file during loading. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub logfile { | ||
| 285 | my ($self, %params) = @_; | ||
| 286 | |||
| 287 | if ($params{-file}) { | ||
| 288 | $self->{LOGFILE} = $params{-file}; | ||
| 289 | } | ||
| 290 | |||
| 291 | return $self->{LOGFILE}; | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | =item discardfile | ||
| 298 | |||
| 299 | set the discardfile to load. | ||
| 300 | |||
| 301 | C<$sqlldr->discardfile(-file=>'load.dis');> | ||
| 302 | |||
| 303 | SQL*Loader writes discarded rows to this discard file during loading. | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub discardfile { | ||
| 308 | my ($self, %params) = @_; | ||
| 309 | |||
| 310 | if ($params{-file}) { | ||
| 311 | $self->{DISCARDFILE} = $params{-file}; | ||
| 312 | } | ||
| 313 | |||
| 314 | return $self->{DISCARDFILE}; | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | |||
| 320 | =item badfile | ||
| 321 | |||
| 322 | set the badfile to load. | ||
| 323 | |||
| 324 | C<$sqlldr->badfile(-file=>'load.bad');> | ||
| 325 | |||
| 326 | SQL*Loader writes bad rows to this bad file during loading. | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | sub badfile { | ||
| 331 | my ($self, %params) = @_; | ||
| 332 | |||
| 333 | if ($params{-file}) { | ||
| 334 | $self->{BADFILE} = $params{-file}; | ||
| 335 | } | ||
| 336 | |||
| 337 | return $self->{BADFILE}; | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | =item controlfile | ||
| 344 | |||
| 345 | set the controlfile to load. | ||
| 346 | |||
| 347 | C<$sqlldr->controlfile(-file=>'controlfile.ctr');> | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | sub controlfile { | ||
| 352 | my ($self, %params) = @_; | ||
| 353 | |||
| 354 | if ($params{-file}) { | ||
| 355 | $self->{CONTROLFILE} = $params{-file}; | ||
| 356 | } | ||
| 357 | |||
| 358 | return $self->{CONTROLFILE}; | ||
| 359 | } | ||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | =item datafile | ||
| 365 | |||
| 366 | set the datafile to load. | ||
| 367 | |||
| 368 | C<$sqlldr->datafile(-file=>'datafile.ctr');> | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | sub datafile { | ||
| 373 | my ($self, %params) = @_; | ||
| 374 | |||
| 375 | if ($params{-file}) { | ||
| 376 | $self->{DATAFILE} = $params{-file}; | ||
| 377 | |||
| 378 | unless (-f $self->{DATAFILE}) { | ||
| 379 | carp "datafile ", $self->{DATAFILE}, " does not exist therefore cannot be loaded" | ||
| 380 | if $self->warnings; | ||
| 381 | } | ||
| 382 | |||
| 383 | } | ||
| 384 | |||
| 385 | return $self->{DATAFILE}; | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | =item parameterfile | ||
| 392 | |||
| 393 | set the parameterfile to load. | ||
| 394 | |||
| 395 | C<$sqlldr->parameterfile(-file=>'parameterfile.dat');> | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | sub parameterfile { | ||
| 400 | my ($self, %params) = @_; | ||
| 401 | |||
| 402 | ## TODO support this method! | ||
| 403 | |||
| 404 | carp __PACKAGE__."::parameterfile: this method is not yet supported"; | ||
| 405 | return 0; | ||
| 406 | |||
| 407 | |||
| 408 | if ($params{-file}) { | ||
| 409 | $self->{PARAMETERFILE} = $params{-file}; | ||
| 410 | } | ||
| 411 | |||
| 412 | return $self->{PARAMETERFILE}; | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | |||
| 418 | =item table | ||
| 419 | |||
| 420 | set the table to load into. | ||
| 421 | |||
| 422 | C<$sqlldr->table(-name=>'table_name');> | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub table { | ||
| 427 | my ($self, %params) = @_; | ||
| 428 | |||
| 429 | if ($params{-name}) { | ||
| 430 | $self->{TABLE} = $params{-name}; | ||
| 431 | } | ||
| 432 | |||
| 433 | return $self->{TABLE}; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | =item fieldsterminatedby | ||
| 440 | |||
| 441 | set the field to terminate the datafile. | ||
| 442 | |||
| 443 | C<$sqlldr->fieldsterminatedby(-symbol=>',');> | ||
| 444 | C<my $t = $sqlldr->fieldsterminatedby;> | ||
| 445 | |||
| 446 | if you don't set this or pass null assumes | ||
| 447 | records are fixed format .. unsupported :( | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | sub fieldsterminatedby { | ||
| 452 | my ($self, %params) = @_; | ||
| 453 | |||
| 454 | if ($params{-symbol}) { | ||
| 455 | $self->{FIELDSTERMINATEDBY} = $params{-symbol}; | ||
| 456 | } | ||
| 457 | |||
| 458 | return $self->{FIELDSTERMINATEDBY}; | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | =item pass | ||
| 465 | |||
| 466 | set or get the password | ||
| 467 | |||
| 468 | C<$sqlldr->pass(-word=>'tiger');> | ||
| 469 | C<my $pass = $sqlldr->pass;> | ||
| 470 | |||
| 471 | =cut | ||
| 472 | |||
| 473 | sub pass { | ||
| 474 | my ($self, %params) = @_; | ||
| 475 | |||
| 476 | if ($params{-word}) { | ||
| 477 | $self->{PASS} = $params{-word}; | ||
| 478 | } | ||
| 479 | |||
| 480 | return $self->{PASS}; | ||
| 481 | } | ||
| 482 | |||
| 483 | |||
| 484 | |||
| 485 | |||
| 486 | =item user | ||
| 487 | |||
| 488 | set or get the username | ||
| 489 | |||
| 490 | C<$sqlldr->user(-name=>'scott');> | ||
| 491 | C<my $user = $sqlldr->user;> | ||
| 492 | |||
| 493 | =cut | ||
| 494 | |||
| 495 | sub user { | ||
| 496 | my ($self, %params) = @_; | ||
| 497 | |||
| 498 | if ($params{-name}) { | ||
| 499 | $self->{USER} = $params{-name}; | ||
| 500 | } | ||
| 501 | |||
| 502 | return $self->{USER}; | ||
| 503 | } | ||
| 504 | |||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | =item cstr | ||
| 509 | |||
| 510 | get or set the connection string used | ||
| 511 | |||
| 512 | C<my $user = $sqlldr->cstr;> | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | sub cstr { | ||
| 517 | my ($self, %params) = @_; | ||
| 518 | |||
| 519 | if ($params{-name}) { | ||
| 520 | $self->{USER} = $params{-name}; | ||
| 521 | } | ||
| 522 | |||
| 523 | return $self->{CSTR}; | ||
| 524 | } | ||
| 525 | |||
| 526 | |||
| 527 | |||
| 528 | |||
| 529 | =item db | ||
| 530 | |||
| 531 | set or get the db | ||
| 532 | |||
| 533 | C<$sqlldr->db(-name=>'foo:');> | ||
| 534 | C<my $user = $sqlldr->db;> | ||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub db { | ||
| 539 | my ($self, %params) = @_; | ||
| 540 | |||
| 541 | if ($params{-name}) { | ||
| 542 | $self->{DB} = $params{-name}; | ||
| 543 | } | ||
| 544 | |||
| 545 | return $self->{DB}; | ||
| 546 | } | ||
| 547 | |||
| 548 | |||
| 549 | |||
| 550 | =item create_controlfile | ||
| 551 | |||
| 552 | creates the controlfile from DB | ||
| 553 | |||
| 554 | =cut | ||
| 555 | |||
| 556 | sub create_controlfile { | ||
| 557 | my ($self, %params, $controlfile) = @_; | ||
| 558 | |||
| 559 | eval { | ||
| 560 | |||
| 561 | my $datafile = $self->datafile | ||
| 562 | or die "datafile is not defined"; | ||
| 563 | my $table = $self->table | ||
| 564 | or die "table name is not defined"; | ||
| 565 | my $user = $self->user | ||
| 566 | or die "user is not defined"; | ||
| 567 | my $pass = $self->pass | ||
| 568 | or die "pass is not defined"; | ||
| 569 | my $cstr = $self->cstr | ||
| 570 | or die "cstr is not defined"; | ||
| 571 | my $db = $self->db | ||
| 572 | or die "db is not defined"; | ||
| 573 | my $fieldsterminatedby = $self->fieldsterminatedby | ||
| 574 | or die "fields terminated by is not defined"; | ||
| 575 | |||
| 576 | my $dbh = DBI->connect($self->cstr . $db, $user, $pass) | ||
| 577 | or die "cannot connect to DB"; | ||
| 578 | |||
| 579 | ## TODO handle fields terminated | ||
| 580 | |||
| 581 | ## TODO get some SQL guru to explain this code | ||
| 582 | |||
| 583 | ## TODO give credit to SQL author | ||
| 584 | ## http://www.oracleutilities.com/OSUtil/sqlldr.html | ||
| 585 | |||
| 586 | my $SQL1 = <<__SQL1__; | ||
| 587 | select 'LOAD DATA' || chr(10) || | ||
| 588 | 'INFILE ''$datafile''' || chr(10) || | ||
| 589 | 'INTO TABLE '|| table_name || chr(10) || | ||
| 590 | 'FIELDS TERMINATED BY '','''||chr(10) || | ||
| 591 | 'TRAILING NULLCOLS' || chr(10) || '(' | ||
| 592 | from user_tables | ||
| 593 | where table_name = upper ('$table') | ||
| 594 | __SQL1__ | ||
| 595 | |||
| 596 | my $SQL2 = <<__SQL2__; | ||
| 597 | select decode (rownum, 1, ' ', ' , ') || | ||
| 598 | rpad (column_name, 33, ' ') || | ||
| 599 | decode (data_type, | ||
| 600 | 'VARCHAR2', 'CHAR NULLIF ('||column_name||'=BLANKS)', | ||
| 601 | 'FLOAT', 'DECIMAL EXTERNAL NULLIF('||column_name||'=BLANKS)', | ||
| 602 | 'NUMBER', decode (data_precision, 0, | ||
| 603 | 'INTEGER EXTERNAL NULLIF ('||column_name|| | ||
| 604 | '=BLANKS)', decode (data_scale, 0, | ||
| 605 | 'INTEGER EXTERNAL NULLIF ('|| | ||
| 606 | column_name||'=BLANKS)', | ||
| 607 | 'DECIMAL EXTERNAL NULLIF ('|| | ||
| 608 | column_name||'=BLANKS)')), | ||
| 609 | 'DATE', 'DATE "DD/MM/YYYY" NULLIF ('||column_name||'=BLANKS)', null) | ||
| 610 | from user_tab_columns | ||
| 611 | where table_name = upper ('$table') | ||
| 612 | order by column_id | ||
| 613 | __SQL2__ | ||
| 614 | |||
| 615 | my $SQL3 = <<__SQL3__; | ||
| 616 | select ')' | ||
| 617 | from dual | ||
| 618 | __SQL3__ | ||
| 619 | |||
| 620 | ## TODO | ||
| 621 | ## The table must exist!! | ||
| 622 | |||
| 623 | my $sth1 = $dbh->prepare($SQL1) or die "cannot prepare sql1: $!\n$SQL1"; | ||
| 624 | my $sth2 = $dbh->prepare($SQL2) or die "cannot prepare sql2: $!\n$SQL2"; | ||
| 625 | my $sth3 = $dbh->prepare($SQL3) or die "cannot prepare sql3: $!\n$SQL3"; | ||
| 626 | |||
| 627 | $sth1->execute() or die "cannot execute sth1: $!\n$SQL1"; | ||
| 628 | $sth2->execute() or die "cannot execute sth2: $!\n$SQL2"; | ||
| 629 | $sth3->execute() or die "cannot execute sth3: $!\n$SQL3"; | ||
| 630 | |||
| 631 | $controlfile .= $sth1->fetchrow(); | ||
| 632 | |||
| 633 | while (my $col = $sth2->fetchrow()) { | ||
| 634 | $controlfile .= $col; | ||
| 635 | } | ||
| 636 | |||
| 637 | $controlfile .= $sth3->fetchrow(); | ||
| 638 | |||
| 639 | $self->{CONTROL} = "$controlfile\n"; | ||
| 640 | |||
| 641 | $sth1->finish(); | ||
| 642 | $sth2->finish(); | ||
| 643 | $sth3->finish(); | ||
| 644 | |||
| 645 | $dbh->disconnect(); | ||
| 646 | |||
| 647 | }; if ($@) { | ||
| 648 | carp $@; | ||
| 649 | return undef; | ||
| 650 | } else { return $self->{CONTROL} } | ||
| 651 | |||
| 652 | } | ||
| 653 | |||
| 654 | |||
| 655 | |||
| 656 | |||
| 657 | =item write_controlfile | ||
| 658 | |||
| 659 | writes the control file to disk | ||
| 660 | |||
| 661 | =cut | ||
| 662 | |||
| 663 | sub write_controlfile { | ||
| 664 | my ($self, %params) = @_; | ||
| 665 | |||
| 666 | eval { | ||
| 667 | my $controlfile = $self->controlfile | ||
| 668 | or die "controlfile is not defined"; | ||
| 669 | |||
| 670 | open (my $fh, ">$self->{CONTROLFILE}") | ||
| 671 | or die "cannot open " . $self->{CONTROLFILE} . ": $!"; | ||
| 672 | |||
| 673 | print $fh $self->{CONTROL}; | ||
| 674 | |||
| 675 | close $fh | ||
| 676 | or die "cannot close " . $self->{CONTROLFILE} . ": $!"; | ||
| 677 | |||
| 678 | }; if($@) { | ||
| 679 | carp $@; | ||
| 680 | return undef; | ||
| 681 | } else { return $self->{CONTROL} } | ||
| 682 | |||
| 683 | } | ||
| 684 | |||
| 685 | |||
| 686 | |||
| 687 | |||
| 688 | =item execute | ||
| 689 | |||
| 690 | call and execute the sqlldr utility. | ||
| 691 | |||
| 692 | =cut | ||
| 693 | |||
| 694 | sub execute { | ||
| 695 | my ($self, %params, $return) = @_; | ||
| 696 | |||
| 697 | eval { | ||
| 698 | |||
| 699 | ## TODO could this be an internal method? | ||
| 700 | |||
| 701 | my $user = $self->user | ||
| 702 | or die "user is not defined"; # -- ORACLE username | ||
| 703 | my $table = $self->table | ||
| 704 | or die "table is not defined"; # -- table name | ||
| 705 | my $fieldsterminatedby = $self->fieldsterminatedby | ||
| 706 | or die "fields terminated by is not defined"; # -- FIELDSTERMINATEDBY | ||
| 707 | my $controlfile = $self->controlfile | ||
| 708 | or die "control file by is not defined"; # -- Control file name | ||
| 709 | my $logfile = $self->logfile | ||
| 710 | or die "log file is not defined"; # -- Log file name | ||
| 711 | my $badfile = $self->badfile | ||
| 712 | or die "bad file is not defined"; # -- Bad file name | ||
| 713 | my $datafile = $self->datafile | ||
| 714 | or die "datafile does not exist"; # -- Data file name | ||
| 715 | my $discardfile = $self->discardfile | ||
| 716 | or die "discard file is not defined"; # -- Discard file name | ||
| 717 | ## TODO | ||
| 718 | ## see sub param | ||
| 719 | # my $parameterfile = $self->parameterfile | ||
| 720 | # or die "parameter file name is not defined"; # -- parameter specifications file name | ||
| 721 | |||
| 722 | ## TODO WARNING | ||
| 723 | ## The user's password can be snooped on with `ps` | ||
| 724 | ## give the user an option to key their password on demand | ||
| 725 | my $pass = $self->pass | ||
| 726 | or die "user is not defined"; # -- ORACLE password | ||
| 727 | |||
| 728 | =for TODO support these.. | ||
| 729 | |||
| 730 | discardmax -- Number of discards to allow (Default all) | ||
| 731 | skip -- Number of logical records to skip (Default 0) | ||
| 732 | load -- Number of logical records to load (Default all) | ||
| 733 | errors -- Number of errors to allow (Default 50) | ||
| 734 | rows -- Number of rows in conventional path bind array or between direct path data saves | ||
| 735 | (Default: Conventional path 64, Direct path all) | ||
| 736 | bindsize -- Size of conventional path bind array in bytes (Default 65536) | ||
| 737 | silent -- Suppress messages during run (header,feedback,errors,discards,partitions) | ||
| 738 | direct -- use direct path (Default FALSE) | ||
| 739 | parallel -- do parallel load (Default FALSE) | ||
| 740 | file -- File to allocate extents from | ||
| 741 | skip_unusable_indexes -- disallow/allow unusable indexes or index partitions (Default FALSE) | ||
| 742 | skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable (Default FALSE) | ||
| 743 | commit_discontinued -- commit loaded rows when load is discontinued (Default FALSE) | ||
| 744 | readsize -- Size of Read buffer (Default 1048576) | ||
| 745 | |||
| 746 | =cut | ||
| 747 | |||
| 748 | # Usage: SQLLOAD keyword=value [,keyword=value,...] | ||
| 749 | my $cmd = "$ENV{ORACLE_HOME}/bin/$bin userid=$user/$pass control=$controlfile log=$logfile bad=$badfile" | ||
| 750 | . " data=$datafile discard=$discardfile"; | ||
| 751 | |||
| 752 | ## TODO | ||
| 753 | # my $cmd = "$ENV{ORACLE_HOME}/bin/$bin userid=$user control=$controlfile log=$logfile bad=$badfile" | ||
| 754 | # . " data=$datafile discard=$discardfile parfile=$parameterfile"; | ||
| 755 | |||
| 756 | $return = `$cmd`; | ||
| 757 | |||
| 758 | }; if ($@) { | ||
| 759 | croak __PACKAGE__."::execute: $@."; | ||
| 760 | } else { | ||
| 761 | return $return; | ||
| 762 | } | ||
| 763 | |||
| 764 | } | ||
| 765 | |||
| 766 | |||
| 767 | |||
| 768 | |||
| 769 | =item cleanup | ||
| 770 | |||
| 771 | delete parameter, control, bad, discard and log files. | ||
| 772 | |||
| 773 | =cut | ||
| 774 | |||
| 775 | sub cleanup { | ||
| 776 | my $self = shift; | ||
| 777 | |||
| 778 | ## TODO | ||
| 779 | |||
| 780 | unlink $self->{CONTROLFILE} if $self->{CONTROLFILE}; | ||
| 781 | |||
| 782 | } | ||
| 783 | |||
| 784 | |||
| 785 | |||
| 786 | |||
| 787 | =item DESTROY | ||
| 788 | |||
| 789 | cleanup this instance | ||
| 790 | |||
| 791 | =cut | ||
| 792 | |||
| 793 | # TODO | ||
| 794 | # DESTROY is currently called once per instance | ||
| 795 | # but do I want it called once after the final instance | ||
| 796 | # is destroyed? | ||
| 797 | |||
| 798 | sub DESTROY { | ||
| 799 | my $self = shift; | ||
| 800 | } | ||
| 801 | |||
| 802 | |||
| 803 | |||
| 804 | |||
| 805 | =head1 SQL*Loader Options | ||
| 806 | |||
| 807 | SQL*Loader provides the following options, which can be specified either on the command line or within a parameter file: | ||
| 808 | |||
| 809 | |||
| 810 | |||
| 811 | · bad . A file that is created when at least one record from the input file is rejected. The rejected data records are placed in this file. A record could be rejected for many reasons, including a non-unique key or a required column being null. | ||
| 812 | |||
| 813 | · bindsize . [256000] The size of the bind array in bytes. | ||
| 814 | |||
| 815 | · columnarrayrows . [5000] Specifies the number of rows to allocate for direct path column arrays. | ||
| 816 | |||
| 817 | · control . The name of the control file. This file specifies the format of the data to be loaded. | ||
| 818 | |||
| 819 | · data . The name of the file that contains the data to load. | ||
| 820 | |||
| 821 | · direct . [FALSE] Specifies whether or not to use a direct path load or conventional. | ||
| 822 | |||
| 823 | · discard . The name of the file that contains the discarded rows. Discarded rows are those that fail the WHEN clause condition when selectively loading records. | ||
| 824 | |||
| 825 | · discardmax . [ALL] The maximum number of discards to allow. | ||
| 826 | |||
| 827 | · errors . [50] The number of errors to allow on the load. | ||
| 828 | |||
| 829 | · external_table . [NOT_USED] Determines whether or not any data will be loaded using external tables. The other valid options include GENERATE_ONLY and EXECUTE. | ||
| 830 | |||
| 831 | · file . Used only with parallel loads, this parameter specifies the file to allocate extents from. | ||
| 832 | |||
| 833 | · load . [ALL] The number of logical records to load. | ||
| 834 | |||
| 835 | · log . The name of the file used by SQL*Loader to log results. | ||
| 836 | |||
| 837 | · multithreading . The default is TRUE on multiple CPU systems and FALSE on single CPU systems. | ||
| 838 | |||
| 839 | · parfile . [Y] The name of the file that contains the parameter options for SQL*Loader. | ||
| 840 | |||
| 841 | · parallel . [FALSE] Specifies a filename that contains index creation statements. | ||
| 842 | |||
| 843 | · readsize . The size of the buffer used by SQL*Loader when reading data from the input file. This value should match that of bindsize. | ||
| 844 | |||
| 845 | · resumable . [N] Enables and disables resumable space allocation. When .Y., the parameters resumable_name and resumable_timeout are utilized. | ||
| 846 | |||
| 847 | · resumable_name . User defined string that helps identify a resumable statement that has been suspended. This parameter is ignored unless resumable = Y. | ||
| 848 | |||
| 849 | · resumable_timeout . [7200 seconds] The time period in which an error must be fixed. This parameter is ignored unless resumable = Y. | ||
| 850 | |||
| 851 | · rows . [64] The number of rows to load before a commit is issued (conventional path only). For direct path loads, rows are the number of rows to read from the data file before saving the data in the datafiles. | ||
| 852 | |||
| 853 | · silent . Suppress errors during data load. A value of ALL will suppress all load messages. Other options include DISCARDS, ERRORS, FEEDBACK, HEADER, and PARTITIONS. | ||
| 854 | |||
| 855 | · skip . [0] Allows the skipping of the specified number of logical records. | ||
| 856 | |||
| 857 | · skip_unusable_indexes . [FALSE] Determines whether SQL*Loader skips the building of indexes that are in an unusable state. | ||
| 858 | |||
| 859 | · skip_index_maintenance . [FALSE] Stops index maintenance for direct path loads only. | ||
| 860 | |||
| 861 | · streamsize . [256000] Specifies the size of direct path streams in bytes. | ||
| 862 | |||
| 863 | · userid . The Oracle username and password. | ||
| 864 | |||
| 865 | |||
| 866 | |||
| 867 | =cut | ||
| 868 | |||
| 869 | |||
| 870 | |||
| 871 | |||
| 872 | 1; | ||
| 873 | __END__ | ||