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.14 and 1.15

version 1.14, 2005/05/10 10:11:17 version 1.15, 2005/05/11 10:17:48
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.13 2005/05/10 10:06:14 rajo Exp $  # $Platon: scripts/perl/mysql/mysql-table-copy.pl,v 1.14 2005/05/10 10:11:17 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 22  use Time::HiRes qw(gettimeofday tv_inter
Line 22  use Time::HiRes qw(gettimeofday tv_inter
 use POSIX qw(strftime);  use POSIX qw(strftime);
   
 use vars qw (  use vars qw (
         $opt_pk $opt_exit $opt_verbose          $opt_exit $opt_pk $opt_nd $opt_verbose
         $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
   
         $sql_read_data $sql_write_data $sql_delete_data          $sql_max_id $sql_read_data $sql_write_data $sql_delete_data
 );  );
   
 sub cur_date();  sub cur_date();
Line 41  while ($ARGV[0] =~ /^-/) {
Line 41  while ($ARGV[0] =~ /^-/) {
         my $option = shift @ARGV;          my $option = shift @ARGV;
         if ($option eq '-pk') {          if ($option eq '-pk') {
                 $opt_pk = 1;                  $opt_pk = 1;
           } elsif ($option eq '-nd') {
                   $opt_nd = 1;
         } elsif ($option eq '-exit') {          } elsif ($option eq '-exit') {
                 $opt_exit = 1;                  $opt_exit = 1;
         } elsif ($option eq '-verbose') {          } elsif ($option eq '-verbose') {
Line 55  if (! defined $db_name1 or ! defined $db
Line 57  if (! defined $db_name1 or ! defined $db
         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] [<time_interval>]\n";          print STDERR "    [user1] [pass1] [user2] [pass2] [<time_interval>]\n";
         print STDERR "Options:\n";          print STDERR "Options:\n";
         print STDERR "  -exit     exit immediately after execution\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 "  -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";
         exit 1;          exit 1;
 }  }
Line 123  print "# Preparing SQL commands...\n" if
Line 126  print "# Preparing SQL commands...\n" if
 #print "SELECT $primary_key, " . join(', ', @columns) . " FROM $table\n";  #print "SELECT $primary_key, " . join(', ', @columns) . " FROM $table\n";
 #print "INSERT INTO $table  (" . join(', ', @columns) . ") VALUES (?" . ", ?" x $#columns . ")\n";  #print "INSERT INTO $table  (" . join(', ', @columns) . ") VALUES (?" . ", ?" x $#columns . ")\n";
 #print "DELETE FROM $table WHERE $primary_key = ? LIMIT 1\n";  #print "DELETE FROM $table WHERE $primary_key = ? LIMIT 1\n";
 $sql_read_data  = $dbh1->prepare(  
         "SELECT $primary_key, " . join(', ', @columns) . " FROM $table") or die $dbh1->errstr;  if ($opt_nd) {
 $sql_write_data = $dbh2->prepare(      $sql_max_id = $dbh2->prepare(
         "INSERT INTO $table  (" . join(', ', @columns) . ") VALUES (?" . ", ?" x $#columns . ")") or die $dbh2->errstr;          "SELECT MAX($primary_key) FROM $table" )
 $sql_delete_data = $dbh1->prepare(        or die $dbh1->errstr;
         "DELETE FROM $table WHERE $primary_key = ? LIMIT 1") or die $dbh1->errstr;      $sql_read_data = $dbh1->prepare(
                   "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table WHERE $primary_key > ?" )
         or die $dbh1->errstr;
   } else {
       $sql_delete_data = $dbh1->prepare(
                   "DELETE FROM $table WHERE $primary_key = ? LIMIT 1" ) or die $dbh1->errstr;
       $sql_read_data = $dbh1->prepare(
           "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table" )
         or die $dbh1->errstr;
   }
   $sql_write_data = $dbh2->prepare(
           "INSERT INTO $table  (" . join( ', ', @columns ) . ") VALUES (?" . ", ?" x $#columns . ")" )
     or die $dbh2->errstr;
   
 while (1) {  while (1) {
   
Line 151  while (1) {
Line 166  while (1) {
                 }                  }
         } # }}}          } # }}}
   
         $sql_read_data->execute() or warn $dbh1->errstr;      if ($opt_nd) {
           $sql_max_id->execute() or warn $dbh2->errstr;
           my $max_id = ($sql_max_id->fetchrow_array())[0] || 0;
           $sql_read_data->execute($max_id) or warn $dbh1->errstr;
       } else {
           $sql_read_data->execute() or warn $dbh1->errstr;
       }
         while (my @row = $sql_read_data->fetchrow_array()) {          while (my @row = $sql_read_data->fetchrow_array()) {
                 my $prim_key = shift @row; # cut PRIMARY KEY column, which is not inserted                  my $prim_key = shift @row; # cut PRIMARY KEY column, which is not inserted
                 if ($sql_write_data->execute(@row)) {                  if ($sql_write_data->execute(@row)) {
                         print cur_date(), "\tData with key '$prim_key' succesfully inserted with key ", $dbh2->{"mysql_insertid"}, "\n";                          print cur_date(), "\tData with key '$prim_key' succesfully inserted with key ",
                         $sql_delete_data->execute($prim_key) or warn cur_date(), "\tError removing data with key '$prim_key': ",                          $dbh2->{"mysql_insertid"}, "\n";
                           if (! $opt_nd) {
                                   $sql_delete_data->execute($prim_key)
                                           or warn cur_date(), "\tError removing data with key '$prim_key': ",
                                 $dbh1->errstr, "\n";                                  $dbh1->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";
                 }                  }
         }          }
   

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

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