line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MMM::OracleDump::Table; |
2
|
|
|
|
|
|
|
#$Id: Table.pm,v 1.3 1999/11/24 18:33:13 maxim Exp $ |
3
|
1
|
|
|
1
|
|
513
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1029
|
|
5
|
|
|
|
|
|
|
require Exporter; |
6
|
|
|
|
|
|
|
@ISA = qw(Exporter AutoLoader); |
7
|
|
|
|
|
|
|
@EXPORT = qw( |
8
|
|
|
|
|
|
|
); |
9
|
|
|
|
|
|
|
$VERSION = '0.01'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
0
|
|
my ($pkg, $dbh, $table) = @_; |
14
|
0
|
0
|
0
|
|
|
|
unless (ref($dbh) && $table) { |
15
|
0
|
|
|
|
|
|
die "OracleDump::Table::new() takes 2 arguments: a dbi db handler and a table name ($dbh, $table)\n"; |
16
|
|
|
|
|
|
|
} |
17
|
0
|
|
|
|
|
|
my $this = { |
18
|
|
|
|
|
|
|
Name => $table, |
19
|
|
|
|
|
|
|
Dbh => $dbh, |
20
|
|
|
|
|
|
|
ColInfo => _get_column_info($dbh,$table), |
21
|
|
|
|
|
|
|
}; |
22
|
0
|
|
|
|
|
|
bless $this; |
23
|
0
|
|
|
|
|
|
return $this; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub get_create_sql { |
27
|
0
|
|
|
0
|
0
|
|
my $this = shift; |
28
|
0
|
|
|
|
|
|
my $create_sql = _ddl_create_table ($this->{Name}, $this->{ColInfo}); |
29
|
0
|
|
|
|
|
|
return $create_sql. ";\n"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub dump_sql { |
33
|
0
|
|
|
0
|
0
|
|
my ($this, $fh) = @_; |
34
|
0
|
0
|
|
|
|
|
if (!$fh ) { |
35
|
0
|
|
|
|
|
|
$fh = \*STDOUT; |
36
|
|
|
|
|
|
|
} |
37
|
0
|
|
|
|
|
|
my ($dbh, $table_name, $col_info) = ( $this->{Dbh}, $this->{Name}, $this->{ColInfo} ); |
38
|
0
|
|
|
|
|
|
my $dump_sql = _dml_dump($table_name, $col_info); |
39
|
0
|
|
|
|
|
|
my $insert_sql_fmt = _dml_insert_sql_fmt($table_name, $col_info); |
40
|
0
|
|
|
|
|
|
my $qh = $dbh->prepare($dump_sql); |
41
|
0
|
|
|
|
|
|
$qh->execute(); |
42
|
0
|
|
|
|
|
|
my $row; |
43
|
0
|
|
|
|
|
|
while ( $row = $qh->fetch() ) { |
44
|
0
|
|
|
|
|
|
my @qvalues; |
45
|
0
|
|
|
|
|
|
for (0.. @$row-1) { |
46
|
0
|
0
|
|
|
|
|
if ( defined $row->[$_] ) { |
47
|
0
|
0
|
|
|
|
|
if ( $col_info->[$_]->{Type} !~ /NUMBER/ ) { |
48
|
0
|
|
|
|
|
|
$qvalues[$_] = $dbh->quote($row->[$_]); |
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
|
$qvalues[$_] = $row->[$_]; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} else { |
53
|
0
|
|
|
|
|
|
$qvalues[$_] = 'NULL'; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
my $insert_sql = sprintf($insert_sql_fmt, @qvalues); |
57
|
0
|
|
|
|
|
|
print $fh $insert_sql,";\n"; |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
$qh->finish; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _dml_insert_sql_fmt { |
64
|
0
|
|
|
0
|
|
|
my ($table_name, $col_info) = @_; |
65
|
0
|
|
|
|
|
|
my $sql = "INSERT INTO $table_name VALUES(" . join ( ",", map { $_->{InsertSqlFmt} } @$col_info ) . " )"; |
|
0
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $sql; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _dml_dump { |
70
|
0
|
|
|
0
|
|
|
my ($table_name, $col_info) = @_; |
71
|
0
|
|
|
|
|
|
my $sql = "SELECT " . join ( ",", map { $_->{DumpSql} } @$col_info ) . " FROM $table_name "; |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
return $sql; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _get_column_info{ |
76
|
0
|
|
|
0
|
|
|
my ($dbh, $table) = @_; |
77
|
0
|
|
|
|
|
|
my $sql = qq/ SELECT * FROM USER_TAB_COLUMNS |
78
|
|
|
|
|
|
|
WHERE TABLE_NAME='$table' ORDER BY COLUMN_ID /; |
79
|
0
|
|
|
|
|
|
my $qh = $dbh->prepare($sql); |
80
|
0
|
0
|
|
|
|
|
die "$DBI::errstr\n" unless $qh; |
81
|
0
|
|
|
|
|
|
$qh->execute(); |
82
|
0
|
|
|
|
|
|
my @result = (); |
83
|
0
|
|
|
|
|
|
my $row; |
84
|
0
|
|
|
|
|
|
while ($row = $qh->fetchrow_hashref ) { |
85
|
0
|
|
|
|
|
|
my $data = _get_single_col_info($row); |
86
|
0
|
|
|
|
|
|
push @result, $data; |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
|
$qh->finish; |
89
|
0
|
|
|
|
|
|
return \@result; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _get_single_col_info { |
93
|
0
|
|
|
0
|
|
|
my $h = shift; |
94
|
0
|
|
|
|
|
|
my $name = $h->{COLUMN_NAME}; |
95
|
0
|
|
|
|
|
|
my $type = $h->{DATA_TYPE}; |
96
|
0
|
|
|
|
|
|
my $len = $h->{DATA_LENGTH}; |
97
|
0
|
0
|
|
|
|
|
my $fulltype = $type =~ /DATE/ ? $type : "$type($len)"; |
98
|
0
|
0
|
|
|
|
|
my $notnull = $h->{NULLABLE} =~/Y/i ? "" : "NOT NULL" ; |
99
|
0
|
|
|
|
|
|
my $str = sprintf "%-15s %10s %10s", $name,$fulltype,$notnull ; |
100
|
0
|
0
|
|
|
|
|
my $sel_field = $type eq 'DATE' ? "TO_CHAR($name,'yyyy-mm-dd hh24:mi:ss')" : $name; |
101
|
0
|
0
|
|
|
|
|
my $ins_field_fmt = $type eq 'DATE' ? 'TO_DATE(%s,\'yyyy-mm-dd hh24:mi:ss\')' : '%s'; |
102
|
|
|
|
|
|
|
return { |
103
|
0
|
|
|
|
|
|
Name => $name, |
104
|
|
|
|
|
|
|
Type => $type, |
105
|
|
|
|
|
|
|
CreateSql => $str, |
106
|
|
|
|
|
|
|
DumpSql => $sel_field, |
107
|
|
|
|
|
|
|
InsertSqlFmt => $ins_field_fmt |
108
|
|
|
|
|
|
|
}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
0
|
|
|
sub _ddl_create_table{ "CREATE TABLE $_[0] ( \n\t" . join("\n\t", map { $_->{CreateSql} } @{$_[1]} ) . "\n)" }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
126
|
|
|
|
|
|
|
__END__ |