OracleDump/Table.pm

filter: Statementstmt 15.0% 11 / 73 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 20.0% 2 / 10 Podpod 0.0% 0 / 3 totaltotal 11.5% 13 / 113 CC 14 SCAR 30.5
CC
14
Cov
11%
dump_sql
42.0
_get_single_col_info
30.0
new
12.0
CRAP 21.0
↑ Summary OracleDump.pm »
1 package MMM::OracleDump::Table;
2 #$Id: Table.pm,v 1.3 1999/11/24 18:33:13 maxim Exp $
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 require Exporter;
6 1 @ISA = qw(Exporter AutoLoader);
7 1 @EXPORT = qw(
8 );
9 1 $VERSION = '0.01';
10  
11  
12 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
13 0 my ($pkg, $dbh, $table) = @_;
14 0 ▶ unless (ref($dbh) && $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dbh and $table) 0 0 0
Condition0%0/3
ref $dbh and $table
!ll&&!rl&&r
000
MC/DC0%0/2
ref $dbh and $table
ref $dbh $table
Truth table0%0/3
A and B
A= ref $dbhB= $table
ABresult
0X0
100
111
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 0 ▶sub get_create_sql {
Subroutine get_create_sql: not called CC=1 SCAR=6.9
Pod undocumented
27 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 0 ▶sub dump_sql {
Subroutine dump_sql: not called CC=6 SCAR=37.4
Pod undocumented
33 0 my ($this, $fh) = @_;
34 0 ▶ if (!$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
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() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $qh->fetch) 0 0 0
44 0 my @qvalues;
45 0 for (0.. @$row-1) {
46 0 ▶ if ( defined $row->[$_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->[$_]) { } 0 0 0
47 0 ▶ if ( $col_info->[$_]->{Type} !~ /NUMBER/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $col_info->[$_]{'Type'} =~ /NUMBER/) { } 0 0 0
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 0 ▶sub _dml_insert_sql_fmt {
Subroutine _dml_insert_sql_fmt: not called CC=1 SCAR=6.9
64 0 my ($table_name, $col_info) = @_;
65 0 my $sql = "INSERT INTO $table_name VALUES(" . join ( ",", map { $_->{InsertSqlFmt} } @$col_info ) . " )";
66 0 return $sql;
67 }
68
69 0 ▶sub _dml_dump {
Subroutine _dml_dump: not called CC=1 SCAR=6.9
70 0 my ($table_name, $col_info) = @_;
71 0 my $sql = "SELECT " . join ( ",", map { $_->{DumpSql} } @$col_info ) . " FROM $table_name ";
72 0 return $sql;
73 }
74  
75 0 ▶sub _get_column_info{
Subroutine _get_column_info: not called CC=3 SCAR=24.8
76 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 ▶ die "$DBI::errstr\n" unless $qh;
Branch: 1 branch
BranchTrueFalseTotal
unless $qh 0 0 0
81 0 $qh->execute();
82 0 my @result = ();
83 0 my $row;
84 0 ▶ while ($row = $qh->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $qh->fetchrow_hashref) 0 0 0
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 0 ▶sub _get_single_col_info {
Subroutine _get_single_col_info: not called CC=5 SCAR=34.0
93 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 ▶ my $fulltype = $type =~ /DATE/ ? $type : "$type($len)";
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /DATE/ ? : 0 0 0
98 0 ▶ my $notnull = $h->{NULLABLE} =~/Y/i ? "" : "NOT NULL" ;
Branch: 1 branch
BranchTrueFalseTotal
$h->{'NULLABLE'} =~ /Y/i ? : 0 0 0
99 0 my $str = sprintf "%-15s %10s %10s", $name,$fulltype,$notnull ;
100 0 ▶ my $sel_field = $type eq 'DATE' ? "TO_CHAR($name,'yyyy-mm-dd hh24:mi:ss')" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'DATE' ? : 0 0 0
101 0 ▶ my $ins_field_fmt = $type eq 'DATE' ? 'TO_DATE(%s,\'yyyy-mm-dd hh24:mi:ss\')' : '%s';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'DATE' ? : 0 0 0
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 ▶sub _ddl_create_table{ "CREATE TABLE $_[0] ( \n\t" . join("\n\t", map { $_->{CreateSql} } @{$_[1]} ) . "\n)" };
Subroutine _ddl_create_table: not called CC=1 SCAR=6.9
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125 1 1;
126 __END__
↑ Summary OracleDump.pm »