Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use DBI;
  4. use Getopt::Long;
  5. my ( $user, $passwd, $db ) = ( "scott", "tiger", "local" );
  6. my ( $id, $blob, $file ) = (1);
  7. my $stat = GetOptions( "u|user=s" => \$user,
  8. "p|password=s" => \$passwd,
  9. "d|database=s" => \$db,
  10. "i|id=s" => \$id,
  11. "h|help|?" => \&usage
  12. );
  13. if ( !$stat ) { usage(); }
  14. my $dbh = db_connect( $user, $passwd, $db );
  15. my $SEL = "SELECT name,data FROM dbi_lob WHERE id=:ID";
  16. my $sth = $dbh->prepare($SEL);
  17. $sth->bind_param( ":ID", $id );
  18. $sth->execute();
  19. ($file,$blob)=$sth->fetchrow_array();
  20. spew($file);
  21. print "File $file written successfully. Len=",length($blob),"\n";
  22.  
  23. END {
  24. $dbh->disconnect() if defined($dbh);
  25. }
  26.  
  27. sub db_connect {
  28. my ( $username, $passwd, $db ) = ( @_, $ENV{"TWO_TASK"} );
  29. my $dbh = DBI->connect( "dbi:Oracle:$db", $username, $passwd )
  30. || die( $DBI::errstr . "\n" );
  31. $dbh->{AutoCommit} = 0;
  32. $dbh->{RaiseError} = 1;
  33. $dbh->{ora_check_sql} = 0;
  34. $dbh->{RowCacheSize} = 16;
  35. $dbh->{LongReadLen} = 5242880;
  36. $dbh->{LongTruncOk} = 0;
  37. return ($dbh);
  38. }
  39.  
  40. sub usage {
  41. print qq(
  42. USAGE:$0
  43. -u <username> -p <password > -d <database>
  44. -i id
  45. -------
  46. This script unloads blob with id defined by -i parameter from the
  47. database. This script was written as an educational tool and is
  48. free to use and modify as needed.
  49. );
  50. exit(0);
  51. }
  52.  
  53. sub spew {
  54. my $file = shift;
  55. if ( !defined($file) ) { usage(); }
  56. open( FL, ">", $file ) or die "Cannot open file $file for writing:$!\n";
  57. printf FL ("%s",$blob);
  58. close FL;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement