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.15 and 1.16

version 1.15, 2005/05/11 10:17:48 version 1.16, 2008/02/03 23:41:35
Line 6 
Line 6 
 #                       table at once.  #                       table at once.
 #  #
 # Developed by Lubomir Host <rajo AT platon.sk>  # Developed by Lubomir Host <rajo AT platon.sk>
 # Copyright (c) 2003-2005 Platon SDG, http://platon.sk/  # Copyright (c) 2003-2008 Platon SDG, http://platon.sk/
 # 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.14 2005/05/10 10:11:17 rajo Exp $  # $Platon: scripts/perl/mysql/mysql-table-copy.pl,v 1.15 2005/05/11 10:17:48 nepto 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 20  use strict;
Line 20  use strict;
 use DBI;  use DBI;
 use Time::HiRes qw(gettimeofday tv_interval);  use Time::HiRes qw(gettimeofday tv_interval);
 use POSIX qw(strftime);  use POSIX qw(strftime);
   use Getopt::Long;
   
 use vars qw (  use vars qw (
         $opt_exit $opt_pk $opt_nd $opt_verbose          $opt_exit $opt_pk $opt_nd $opt_where $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
Line 34  use vars qw (
Line 35  use vars qw (
   
 sub cur_date();  sub cur_date();
 sub read_input();  sub read_input();
   sub usage();
   
 $default_timeout = 10; # 10 seconds  $default_timeout = 10; # 10 seconds
   
 while ($ARGV[0] =~ /^-/) {  my $res = GetOptions(
         my $option = shift @ARGV;          'pk|p'                  => \$opt_pk,
         if ($option eq '-pk') {          'nd|n'                  => \$opt_nd,
                 $opt_pk = 1;          'where|w=s'             => \$opt_where,
         } elsif ($option eq '-nd') {          'exit|e'                => \$opt_exit,
                 $opt_nd = 1;          'verbose|v'             => \$opt_verbose,
         } elsif ($option eq '-exit') {          'timeout|t=i'   => \$timeout,
                 $opt_exit = 1;  );
         } elsif ($option eq '-verbose') {  
                 $opt_verbose = 1;  
         }  
 }  
   
 ($db_name1, $db_name2, $table, $user1, $pass1, $user2, $pass2, $timeout) = @ARGV;  use Data::Dumper;
   #die Dumper({
   #       'pk'                    => $opt_pk,
   #       'nd'                    => $opt_nd,
   #       'where|w=s'             => $opt_where,
   #       'exit'                  => $opt_exit,
   #       'verbose|v'             => $opt_verbose,
   #       'timeout|t=i'   => $timeout,
   #});
   #die Dumper(\@ARGV);
   
 if (! defined $db_name1 or ! defined $db_name2) {  usage() unless (scalar(@ARGV));
         print STDERR "Usage:\n";  
         print STDERR "  $0 [options] database=DB1,host=localhost database=DB2,host=localhost Table\n";  ($db_name1, $db_name2, $table, $user1, $pass1, $user2, $pass2) = @ARGV;
         print STDERR "    [user1] [pass1] [user2] [pass2] [<time_interval>]\n";  
         print STDERR "Options:\n";  usage() if (! defined $db_name1 or ! defined $db_name2);
         print STDERR "  -pk       copy/replicate primary key as well\n";  
         print STDERR "  -nd       skip removal of source items, insert new only\n";  $opt_where = " WHERE $opt_where" if (defined $opt_where and !defined $opt_nd);
         print STDERR "  -exit     exit immediately after execution\n";  
         print STDERR "  -verbose  add some notes on the output\n";  
         exit 1;  
 }  
   
 # we are using ',' as separators, because ';' is separator in shell  # we are using ',' as separators, because ';' is separator in shell
 $db_name1 =~ s/,/;/g;  $db_name1 =~ s/,/;/g;
Line 129  print "# Preparing SQL commands...\n" if
Line 132  print "# Preparing SQL commands...\n" if
   
 if ($opt_nd) {  if ($opt_nd) {
     $sql_max_id = $dbh2->prepare(      $sql_max_id = $dbh2->prepare(
         "SELECT MAX($primary_key) FROM $table" )          "SELECT MAX($primary_key) FROM $table $opt_where" )
       or die $dbh1->errstr;        or die $dbh1->errstr;
     $sql_read_data = $dbh1->prepare(      $sql_read_data = $dbh1->prepare(
                 "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table WHERE $primary_key > ?" )                  "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table WHERE $primary_key > ? AND $opt_where" )
       or die $dbh1->errstr;        or die $dbh1->errstr;
 } else {  }
   else {
     $sql_delete_data = $dbh1->prepare(      $sql_delete_data = $dbh1->prepare(
                 "DELETE FROM $table WHERE $primary_key = ? LIMIT 1" ) or die $dbh1->errstr;                  "DELETE FROM $table WHERE $primary_key = ? LIMIT 1" ) or die $dbh1->errstr;
     $sql_read_data = $dbh1->prepare(      $sql_read_data = $dbh1->prepare(
         "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table" )          "SELECT $primary_key, " . join( ', ', @columns ) . " FROM $table $opt_where" )
       or die $dbh1->errstr;        or die $dbh1->errstr;
 }  }
 $sql_write_data = $dbh2->prepare(  $sql_write_data = $dbh2->prepare(
Line 211  sub read_input()
Line 215  sub read_input()
         return $str;          return $str;
 } # }}}  } # }}}
   
   sub usage()
   { # {{{
           print STDERR "Usage:\n";
           print STDERR "  $0 [options] database=DB1,host=localhost database=DB2,host=localhost Table\n";
           print STDERR "    [user1] [pass1] [user2] [pass2]\n";
           print STDERR "Options:\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 "  -where    copy/replicate only records where condition is true\n";
           print STDERR "  -timeout  how often pool database (default 10 sec)\n";
           print STDERR "  -exit     exit immediately after execution\n";
           print STDERR "  -verbose  add some notes on the output\n";
           exit 1;
   } # }}}
   
 # vim: ts=4  # vim: ts=4
 # vim600: fdm=marker fdl=0 fdc=3  # vim600: fdm=marker fdl=0 fdc=3

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

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