hbjxzl.cublog.cn auxiliary Data Utility Version 1.0.0
This script will auxiliary data and struct. -h - Give help screen -E - for export dict -D export - for export dict's data -T drop - for export table's data Examples: auxiliary.pl -h/H auxiliary.pl -e/E auxiliary.pl -d/D auxiliary.pl -t/T
use Getopt::Long; use Config; use Getopt::Std; use Oraperl; $dbname="dsg"; $dbuser="dsg"; $dbpasswd="dsg"; format HEADER =
hbjxzl.cublog.cn auxiliary Data Utility Version 1.0.0
This script will auxiliary data and struct. -h - Give help screen -E -for export dict -D export -for export dict's data -T drop - for export table's data Examples: auxiliary.pl -h/H auxiliary.pl -e/E auxiliary.pl -d/D auxiliary.pl -t/T .
sub do_help { # Give help screen and exit
$~="HEADER"; write; exit(); }
sub replace_null_to_{ $str=shift; if($str){ $str=~ s//_/g; } return$str; }
sub replace_enter_to_{ $str=shift; if($str){ $str=~ s/\n//g; } return$str; }
sub write_file{ $put_name=shift; $put_trunc_name=shift; $put_drop_name=shift; open(OUTPUT,"> $put_name ") ordie"Couldn't open $put_name for write: $!\n"; if($put_trunc_name){ open(TRUNCATE_OUTPUT,"> $put_trunc_name ") ordie"Couldn't open $put_trunc_name for write: $!\n"; } if($put_drop_name){ open(DROP_OUTPUT,"> $put_drop_name ") ordie"Couldn't open $put_drop_name for write: $!\n"; } } sub close_write_file{ close(OUTPUT); close(TRUNCATE_OUTPUT); close(DROP_OUTPUT); }
sub read_file{ $get_name=shift; open(INPUT,"< $get_name ") ordie"Couldn't open $put_name for read: $!\n"; }
sub close_read_file{ close(INPUT); }
sub connect_db{ my$dbname=shift; my$dbuser=shift; my$dbpasswd=shift; #print " test dbname is ",$dbname," user is ", $dbuser," passwd is ", $dbpasswd,"\n";
sub dis_connect_db{ $dbh->disconnect; } sub print_array{ my@cfg_lines=split(" ",$_); foreach(@cfg_lines){ print$_,"\t"; } print"\n"; return@cfg_lines; }
sub obj_sql{ my$sql_code=shift; my$get_sql=""; if($sql_code==1){ $get_sql="select owner,table_name,column_name,data_type,data_length from dba_tab_columns where owner = ? and table_name = ? "; #for all table's column
}elsif($sql_code==2){ $get_sql="select owner,table_name from dba_tables where owner like ? and cluster_name is null and iot_type is null"; }elsif($sql_code==3){ } return$get_sql; }
sub tab_col_sql{ my$exp_owner=shift; my$exp_tab=shift; my$exp_dict_file=lcshift; my$select_col=""; #print ">>test exp_owner is ",uc $exp_owner,"\n";
#print ">>test exp_tab is ",$exp_tab,"\n";
#print ">>test exp_dict_file is ",$exp_dict_file,"\n";
open(INPUT_COL,"< $exp_dict_file")ordie"Couldn't open $exp_dict_file for read: $!\n"; while(<INPUT_COL>){ chomp; @line_raw=split(" ",$_); $exp_tab=~ s/\$/\_/g; $exp_owner=~ s/\$/\_/g; #print "exp_tab is ",$exp_tab,"\n";
sub owner_all_tab{ my $dbh =shift; my $exp_own =shift; my $oper_p =shift; my $get_obj_sql=&obj_sql(2); #print "test exp_owner is ",uc $exp_own,"\n"; #print "test get_obj_sql is ",$get_obj_sql,"\n"; my $sth = $dbh->prepare($get_obj_sql); $sth->execute($exp_own); my $exp_process_num=0; while (@row= $sth->fetchrow){ print $row[0],"\t",$row[1]," Please waiting...."; &tab_dict($dbh,$row[0],$row[1],$oper_p); $exp_process_num++; print " over \n"; } $sth->finish(); print "\n"; }
sub oper_from_file{ $oper_p =shift; &connect_db($dbname,$dbuser,$dbpasswd); &read_file("exp.dict") or die "Cannot get exp.dict for reading !"; while(){ chomp; if(/^\s*#/ or /^\s*$/){ next; } s/\s*#.*//; tr/\t //d; my @exp_dict= split /\./; my $exp_owner=$exp_dict[0]; my $exp_tab=$exp_dict[1]; my $exp_dict_file =join(".",$exp_owner,"dict"); #print "oper is ",$oper_p," exp_owner is ",$exp_owner," exp_dict_file is ",$exp_dict_file,"\n"; if ($oper_p==1){ &write_file($exp_dict_file); &owner_all_tab($dbh,uc $exp_owner,$oper_p); &close_write_file; }elsif($oper_p == 2){ &owner_all_tab($dbh,uc $exp_owner,$oper_p); }elsif($oper_p==3){ #my $put_file_name=join(".",$exp_owner,$exp_tab,"dat"); #print "exp_owner is $exp_dict[0]\t exp_tab is ",$exp_tab,"\t",$put_file_name,"\n" , #print "Exp table's data!\n"; &tab_dict($dbh,$exp_owner,$exp_tab,2); } } &close_read_file; &dis_connect_db(); }
sub do_opts{ %options=(); getopts("eEdDtThH",\%options); &do_help if ($options{h} or $options{H}); # like the shell getopt, "d:" means d takes an argument if (defined $options{e}){ &oper_from_file(1); }elsif(defined $options{E}){ &oper_from_file(1); }elsif( defined $options{d}){ &oper_from_file(2); }elsif( defined $options{D}){ &oper_from_file(2); }elsif( defined $options{t}){ &oper_from_file(3); }elsif( defined $options{T}){ &oper_from_file(3); }elsif( defined $options{h}){ $do_help; }elsif( defined $options{H}){ $do_help; }else{ print "test opts Err:\n"; } }
$argv=scalar @ARGV; #print "test argv is :",$argv,"\n