#!/usr/bin/perl
require '/opt/ws1/run_control.pl'; my $destip=$ARGV[0]; my $destuserid=$ARGV[1]; my $destpass=$ARGV[2]; my $destdatabase=$ARGV[3]; my $destable=$ARGV[4]; my $localtable=$ARGV[5]; my $jobid=$ARGV[6]; my $size=@ARGV; if ( $size != 7) { print "Usage: local2sql_direct.pl \n"; exit(1); } if (!($jobid=~m/\d+/)) { print "Error: please input jobid\n"; exit(1); } #my $runid=get_runid($jobid);
#my $createtime=get_datetime();
#my $record_counter=0;
my $stagedbh=DBI->connect("DBI:mysql:database=$destdatabase;host=$destip","$destuserid","$destpass",{'RaiseError'=>1, 'AutoCommit' => 0}) or die "cannot connect!\n"; $stagedbh->do("SET NAMES 'utf8'"); eval{ $stagedbh->do("use $localdatabase"); $sth=$stagedbh->prepare(qq/select * from $localtable/); $sth->execute(); #$stagedbh->do(qq/delete from $srctable/);
$stagedbh->do("SET NAMES 'utf8'"); $field_count=$stagedbh->selectrow_array(qq/select count(*) from database_field_map where target_tables_name="$localtable"/); #print "field_count:$field_count";
$stagedbh->do("use $destdatabase"); $insert_statement=reverte_insert($destable,$field_count); #print "insert_statement:$insert_statement\n";
$inh=$stagedbh->prepare("$insert_statement"); #$inh=$stagedbh->prepare("insert into $destable values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
while(@row=$sth->fetchrow_array){ #sleep(10);
#print "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],]";
#print "start insert\n";
for (my $i=0;$i<$field_count;$i++){ push (@rows,"$row[$i]"); } $inh->execute(@rows); #$inh->execute($row[0],$row[1],$row[2],,$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],,$row[9],$row[10],$row[11],$row[12],$row[13],$row[14],,$row[15],$row[16]);
$record_counter++; } $stagedbh->commit(); };
if($@){ print "Warning: Transaction aborted:$@"; $stagedbh->rollback(); } $sth->finish(); $inh->finish();
$stagedbh->disconnect();
|