File: oradump.pl

package info (click to toggle)
libdbd-oracle-perl 1.83-3
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 1,724 kB
  • sloc: ansic: 8,354; perl: 7,868; makefile: 20
file content (42 lines) | stat: -rwxr-xr-x 1,223 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl
#
# Dump the contents of an Oracle table into a set of insert statements.
# Quoting is controlled by the datatypes of each column. (new with DBI)
#
# Usage: oradump <database> <user> <pass> <table>
#
# Author:   Kevin Stock (original oraperl script)
# Date:     28th February 1992
#

use DBI;

use strict;

# Set trace level if '-# trace_level' option is given
DBI->trace( shift ) if 1 < @ARGV && $ARGV[0] =~ /^-#/ && shift;

die "syntax: $0 base user pass table\n" if 4 > @ARGV;
my ( $base, $user, $pass, $table ) = @ARGV;

# Connect to database
my $dbh = DBI->connect( "dbi:Oracle:$base", $user, $pass,
    { AutoCommit => 0, RaiseError => 1, PrintError => 0 } )
    or die $DBI::errstr;

my $sth = $dbh->prepare( "SELECT * FROM $table");
$sth->execute;
my @name = @{$sth->{NAME}};
my @type = @{$sth->{TYPE}};
my $lead = "INSERT INTO $table ( " . join( ', ', @name ) . " ) VALUES ( ";
my ( @data, $i );
$sth->bind_columns( {}, \( @data[0 .. $#name] ) );
while ( $sth->fetch ) {
    $i = 0;
    print $lead . join( ", ", map { $dbh->quote( $_, $type[$i++] ) } @data ) .
  # print $lead . join( ", ", map { $dbh->quote( $_ ) } @data ) . # for old DBI
        " );\n";
}

$sth->finish;
$dbh->disconnect;