Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Wednesday, October 14, 2026

Diff for scripts/perl/mysql/mysql-table-copy.pl between version 1.17 and 1.18

version 1.17, 2008/02/03 23:57:44 version 1.18, 2008/02/06 00:07:29
Line 10 
Line 10 
 # Licensed under terms of GNU General Public License.  # Licensed under terms of GNU General Public License.
 # All rights reserved.  # All rights reserved.
 #  #
 # $Platon: scripts/perl/mysql/mysql-table-copy.pl,v 1.16 2008-02-03 23:41:35 rajo Exp $  # $Platon: scripts/perl/mysql/mysql-table-copy.pl,v 1.17 2008-02-03 23:57:44 rajo Exp $
   
 # TODO: - reconnect on closed connection  # TODO: - reconnect on closed connection
 #       - managment for tables with multiple KEY columns  #       - managment for tables with multiple KEY columns
Line 23  use POSIX qw(strftime);
Line 23  use POSIX qw(strftime);
 use Getopt::Long;  use Getopt::Long;
   
 use vars qw (  use vars qw (
         $opt_exit $opt_pk $opt_nd $opt_where $opt_verbose          $opt_exit $opt_pk $opt_nd $opt_where $opt_verbose $opt_update_sum
         $dbh1 $db_name1 $user1 $pass1          $dbh1 $db_name1 $user1 $pass1
         $dbh2 $db_name2 $user2 $pass2          $dbh2 $db_name2 $user2 $pass2
         $table $timeout          $table $timeout
         $default_timeout          $default_timeout
         $primary_key @columns          $primary_key @columns
           @unique_keys
           @update_columns
   
         $sql_max_id $sql_read_data $sql_write_data $sql_delete_data          $sql_max_id $sql_read_data $sql_write_data $sql_delete_data $sql_update_data
 );  );
   
 sub cur_date();  sub cur_date();
Line 46  my $res = GetOptions(
Line 48  my $res = GetOptions(
         'exit|e'                => \$opt_exit,          'exit|e'                => \$opt_exit,
         'verbose|v'             => \$opt_verbose,          'verbose|v'             => \$opt_verbose,
         'timeout|t=i'   => \$timeout,          'timeout|t=i'   => \$timeout,
           'update-sum'    => \$opt_update_sum,
 );  );
   
 use Data::Dumper;  #use Data::Dumper;
 #die Dumper({  #die Dumper({
 #       'pk'                    => $opt_pk,  #       'pk'                    => $opt_pk,
 #       'nd'                    => $opt_nd,  #       'nd'                    => $opt_nd,
Line 56  use Data::Dumper;
Line 59  use Data::Dumper;
 #       'exit'                  => $opt_exit,  #       'exit'                  => $opt_exit,
 #       'verbose|v'             => $opt_verbose,  #       'verbose|v'             => $opt_verbose,
 #       'timeout|t=i'   => $timeout,  #       'timeout|t=i'   => $timeout,
   #       'update-sum'    => $opt_update_sum,
 #});  #});
 #die Dumper(\@ARGV);  #die Dumper(\@ARGV);
   
Line 116  foreach my $key (sort keys %$info) {
Line 120  foreach my $key (sort keys %$info) {
                 $primary_key = $key;                  $primary_key = $key;
                 push @columns, $key if ($opt_pk);                  push @columns, $key if ($opt_pk);
         }          }
           elsif ($$info{$key}->{Key} eq 'MUL') {
                   print "# Unique key is '$key'\n" if ($opt_verbose);
                   push @unique_keys, $key;
                   push @columns, $key;
           }
         else {          else {
                 print "# Found column '$key'\n" if ($opt_verbose);                  print "# Found column '$key'\n" if ($opt_verbose);
                 push @columns, $key;                  push @columns, $key;
                   push @update_columns, $key if ($opt_update_sum);
         }          }
 }  }
 #$sql_info->dump_results();  #$sql_info->dump_results();
Line 145  else {
Line 155  else {
 }  }
 $sql_write_data = $dbh2->prepare(  $sql_write_data = $dbh2->prepare(
         "INSERT INTO $table  (" . join( ', ', @columns ) . ") VALUES (?" . ", ?" x $#columns . ")" )          "INSERT INTO $table  (" . join( ', ', @columns ) . ") VALUES (?" . ", ?" x $#columns . ")" )
   or die $dbh2->errstr;          or die $dbh2->errstr;
   
   $sql_update_data = $dbh2->prepare(
           "UPDATE $table SET " . join( ' + ?, ', map { "$_ = $_"; } @update_columns ) . " + ?
           WHERE " . join( ' = ? AND ', @unique_keys ) . " = ?")
           or die  $dbh2->errstr;
   
 while (1) {  while (1) {
   
Line 172  while (1) {
Line 187  while (1) {
         $sql_max_id->execute() or warn $dbh2->errstr;          $sql_max_id->execute() or warn $dbh2->errstr;
         my $max_id = ($sql_max_id->fetchrow_array())[0] || 0;          my $max_id = ($sql_max_id->fetchrow_array())[0] || 0;
         $sql_read_data->execute($max_id) or warn $dbh1->errstr;          $sql_read_data->execute($max_id) or warn $dbh1->errstr;
     } else {      }
           else {
         $sql_read_data->execute() or warn $dbh1->errstr;          $sql_read_data->execute() or warn $dbh1->errstr;
     }      }
         while (my @row = $sql_read_data->fetchrow_array()) {          while (my $row = $sql_read_data->fetchrow_hashref()) {
                 my $prim_key = shift @row; # cut PRIMARY KEY column, which is not inserted                  my $prim_key = $row->{$primary_key}; # PRIMARY KEY column, which is not inserted
                 if ($sql_write_data->execute(@row)) {                  if ($sql_write_data->execute(map { $row->{$_} } @columns)) {
                         print cur_date(), "\tData with key '$prim_key' succesfully inserted with key ",                          print cur_date(), "\tData with key '$prim_key' succesfully inserted with key ",
                         $dbh2->{"mysql_insertid"}, "\n";                          $dbh2->{"mysql_insertid"}, "\n";
                         if (! $opt_nd) {                          if (! $opt_nd) {
Line 186  while (1) {
Line 202  while (1) {
                                 $dbh1->errstr, "\n";                                  $dbh1->errstr, "\n";
                         }                          }
                 }                  }
                   elsif ($opt_update_sum) {
                           my $iv = $sql_update_data->execute(map { $row->{$_} } @update_columns, @unique_keys);
                           if ($iv) {
                                   print cur_date(), "\tData succesfully updated, primary key '$prim_key', unique key ", join('-', map { $row->{$_} } @unique_keys), "\n";
                                   $sql_delete_data->execute($prim_key)
                                           or warn cur_date(), "\tError removing data with key '$prim_key': ",
                                   $dbh1->errstr, "\n";
                           }
                           else {
                                   warn cur_date(), "\tError updating data with key '$prim_key': ",
                                   $dbh2->errstr, "\n";
                           }
                   }
                 else {                  else {
                         warn cur_date(), "\tError writing data with key '$prim_key': ",                          warn cur_date(), "\tError writing data with key '$prim_key': ",
                         $dbh2->errstr, "\n";                          $dbh2->errstr, "\n";
Line 219  sub usage()
Line 248  sub usage()
         print STDERR "  $0 [options] database=DB1,host=localhost database=DB2,host=localhost Table\n";          print STDERR "  $0 [options] database=DB1,host=localhost database=DB2,host=localhost Table\n";
         print STDERR "    [user1] [pass1] [user2] [pass2]\n";          print STDERR "    [user1] [pass1] [user2] [pass2]\n";
         print STDERR "Options:\n";          print STDERR "Options:\n";
         print STDERR "  -pk       copy/replicate primary key as well\n";          print STDERR "  -pk         copy/replicate primary key as well\n";
         print STDERR "  -nd       skip removal of source items, insert new only\n";          print STDERR "  -nd         skip removal of source items, insert new only\n";
         print STDERR "  -where    copy/replicate only records where condition is true\n";          print STDERR "  -where      copy/replicate only records where condition is true\n";
         print STDERR "  -timeout  how often pool database (default 10 sec)\n";          print STDERR "  -timeout    how often pool database (default 10 sec)\n";
         print STDERR "  -exit     exit immediately after execution\n";          print STDERR "  -exit       exit immediately after execution\n";
         print STDERR "  -verbose  add some notes on the output\n";          print STDERR "  -verbose    add some notes on the output\n";
           print STDERR "  -update-sum data in destination table can be updated according UNIQUE KEY\n";
         exit 1;          exit 1;
 } # }}}  } # }}}
   

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top