#!/usr/bin/perl
require '/opt/ws1/run_control.pl'; my $srcip=$ARGV[0]; my $srcuserid=$ARGV[1]; my $srcpass=$ARGV[2]; my $srcdatabase=$ARGV[3]; my $srctable=$ARGV[4]; my $localtable=$ARGV[5]; my $jobid=$ARGV[6]; my $size=@ARGV; if ( $size != 7) { print "Usage: sql2local_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=$srcdatabase;host=$srcip","$srcuserid","$srcpass",{'RaiseError'=>1, 'AutoCommit' => 0}) or die "cannot connect!\n"; $stagedbh->do("SET NAMES 'utf8'"); eval{ $sth=$stagedbh->prepare(qq/select * from $srctable/); $sth->execute(); #$stagedbh->do(qq/delete from $srctable/);
$stagedbh->do("use $localdatabase"); $stagedbh->do("SET NAMES 'utf8'"); $field_count=$stagedbh->selectrow_array(qq/select count(*) from database_field_map where src_tables_name="$localtable"/); #print "localtable:$localtable\n";
#print "field_count:$field_count\n";
$insert_statement=gen_insert($localtable,$field_count); #print "insert_statement:$insert_statement\n";
$inh=$stagedbh->prepare("$insert_statement"); #$inh=$stagedbh->prepare("insert into $localtable 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++){ #print "push:\$row[$i]";
push(@rows,"$row[$i]"); } push(@rows,"$jobid"); push(@rows,"$runid"); push(@rows,"$record_counter"); push(@rows,"$createtime"); push(@rows,0);
$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],$jobid,$runid,$record_counter,$createtime,0);
$record_counter++; } $stagedbh->commit(); };
if($@){ print "Warning: Transaction aborted:$@"; $stagedbh->rollback(); } $sth->finish(); $inh->finish();
$stagedbh->disconnect();
|