Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32666
  • 博文数量: 8
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-26 14:10
文章分类

全部博文(8)

文章存档

2014年(1)

2013年(3)

2012年(4)

我的朋友

分类: PERL

2013-06-20 19:13:41

1. sql.txt中有多条sql语句,如下:

点击(此处)折叠或打开

  1. SELECT * FROM TESTA
  2. SELECT * FROM TESTB
  3. SELECT * FROM TESTC
  4. SELECT * FROM TESTD
  5. SELECT * FROM TESTE
  6. SELECT * FROM TESTF
2. perl脚本如下,执行的结果会放在与表对应的csv文件中

点击(此处)折叠或打开

  1. use strict;
  2. use warnings;
  3. use DBI;

  4. main();

  5. sub main
  6. {
  7.     my $dbh_oracle;
  8.     my $dbh_mysql;
  9.     open (IN, "sql.txt") or die "$!, opening sql.txt\n";
  10.     open (FAIL, ">00fail.csv") or die 'Unable to create diff file for 00fail.csv $!';
  11.     while (<IN>)
  12.     {
  13.         chomp;
  14.         # Oracle
  15.         $dbh_oracle=DBI->connect("DBI:Oracle:host=xxx.xx.xx.xxx;service_name=xxx;",'xxx','xxx') || die "cannot connect to Oracle:$!\n";
  16.         my @oracle = getResult($dbh_oracle, $_);
  17.                 
  18.         # Mysql
  19.         $dbh_mysql=DBI->connect('DBI:mysql:database=xxxx;host=xxx.xx.xx.xxx','xxx','xxx') || die "cannot connect to mysql:$!\n";
  20.         my @mysql = getResult($dbh_mysql, $_);
  21.                 
  22.         # SqlServer
  23.         # my $dbh_sqlServr=DBI->connect("DBI:ADO:driver={SQL Server};Server=localhost; database=xxxx;", 'xxx', 'xxxx') || die "cannot connect to mysql:$!\n";
  24.         # my @sqlserver = getResult($dbh_sqlServr, $_);
  25.         
  26.         my @output = split/ /;
  27.         my $outputname = $output[-1];
  28.         compare(\@oracle, \@mysql, $outputname);
  29.     }
  30.     
  31.     $dbh_oracle->disconnect();
  32.     $dbh_mysql->disconnect();
  33.     
  34.     close IN;
  35.     close FAIL;
  36. }

  37. sub getResult
  38. {
  39.     my $dbh = shift;
  40.     my $sql = shift;
  41.     
  42.     my @A;
  43.     
  44.     my $sth = $dbh->prepare($sql);
  45.     $sth->execute;
  46.     my $numFields = $sth->{'NUM_OF_FIELDS'};
  47.     
  48.     while (my $ref = $sth->fetchrow_arrayref) {
  49.     my $line = "";
  50.         for (my $i = 0; $i < $numFields; $i++) {
  51.             $line .= $$ref[$i] if(defined $$ref[$i]);
  52.             $line .= ",";
  53.         }
  54.         # print OUT "\n";
  55.         push @A, $line;
  56.     }
  57.     $sth->finish;
  58.     return @A;
  59. }

  60. sub compare
  61. {
  62.     my ($A_ref, $B_ref, $outname) = @_;
  63.     
  64.     my %ta;
  65.     my @onlyA;
  66.     my @onlyB;
  67.     my @same;
  68.     
  69.     foreach (@$A_ref)
  70.     {
  71.         chomp;
  72.         $ta{$_} += 1;
  73.     }

  74.     my %count = %ta;

  75.     foreach(@$B_ref){
  76.         chomp;
  77.         if (exists($ta{$_}) && $ta{$_} > 0){
  78.             $ta{$_} -= 1;
  79.         }else
  80.         {
  81.             push @onlyB,$_;
  82.         }
  83.     }    
  84.     
  85.     my $countA;
  86.     
  87.     foreach (keys %ta) {
  88.         my $tmp = $_;
  89.         if ($ta{$_} >= 0)
  90.         {
  91.             #Only A
  92.             for(1..$ta{$_})
  93.             {
  94.                 push @onlyA, $tmp;
  95.             }
  96.             #Same
  97.             for(1..($count{$_} - $ta{$_}))
  98.             {
  99.                 push @same, $tmp;
  100.             }
  101.         }
  102.     }
  103.     
  104.     if ($#onlyB > 0 or $#onlyA > 0)
  105.     {
  106.         print FAIL "$outname\n" if ($#onlyB > 0 or $#onlyA > 0);
  107.         
  108.         open (OUT, ">$outname.csv") or die 'Unable to create diff file for $outname.csv $!';
  109.         print OUT "only in Oracle:\n";
  110.         print OUT $_."\n" foreach @onlyA;
  111.         
  112.         print OUT "only in mysql:\n";
  113.         print OUT $_."\n" foreach @onlyB;        
  114.         
  115.         print OUT "SAME:\n";
  116.         print OUT $_."\n" foreach @same;
  117.         close OUT;
  118.     }
  119. }
阅读(1911) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~