# This is archiver.pm module of ptxArchiver.pl, a program to archive 
# records from one MySQL table to an another table, a file or simply purged.
#
# This program is copyright 2009 frederic descamps <lefred@inuits.be>.
# Feedback and improvements are welcome.
#
# This program requires mk-archiver from maatkit <http://www.maatkit.org/>
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA.

package archiver;
use ptxArchiverVars;
use warnings;
use strict;

sub new {
      my ( $class, %args ) = @_;
      print " archiver plugin started...\n" if $debug;
      return bless(\%args, $class);
}

sub before_begin {
      my ( $self, %args ) = @_;
      $self->{cols} = $args{cols};
}

sub is_archivable {
     my ( $self, %args ) = @_;
     return 1;
}

sub before_delete {
     my ( $self, %args ) = @_;
     my $dbh = $self->{dbh};
     $self->{cols} = $args{cols};
     my $id=$args{row}[0];
     my $tbl=$self->{tbl};
     my $destination;
     my $ignore;
     #find the children tables
     my $result=find($tbl);
     foreach my $key ( keys %$result) {
        if (($key ne 'field') && ($key ne 'p_field'))
        {
          $_ = $result->{$key}->{p_field};
          my @fields = split(/,/);
          foreach my $element (@fields)
          {
            print " field = $element\n" if $debug;
            print "delete from ". $key ." where $element = '$id';\n" if $debug;
            print "SELECT ". $result->{$key}->{field} ." from $key where $element = '$id';\n" if $debug;
            my $sel=$dbh->prepare("SELECT ". $result->{$key}->{field} ." from $key where $element = '$id';");
            $sel->execute;
            while (my @el = $sel->fetchrow_array) {
              if ($purge) {
                $destination = "--purge -P 100";
              }
              elsif ($fileout) {
                $destination = "--file ". $filename ."_". $key . "_" . timestampStr() . " --port 100";
              }
              else {
                $destination = "--dest h=$dest_dbhost,P=3306,D=$dest_dbname,t=".$key."_archived,u=$dest_dbuser,p=$dest_dbpass";
              }
              if ($ignorereplica) { $ignore = ",b=1"; }
              else { $ignore =""; }
              print "perl -I $modulesPath $archiver --source h=$src_dbhost,P=3306,D=$src_dbname,t=$key,u=$src_dbuser,p=$src_dbpass,m=archiver$ignore $destination --where \"". $result->{$key}->{field} ."='$el[0]'\"\n" if $debug;
              system("perl -I $modulesPath $archiver --source h=$src_dbhost,P=3306,D=$src_dbname,t=$key,u=$src_dbuser,p=$src_dbpass,m=archiver$ignore $destination --where \"". $result->{$key}->{field} ."='$el[0]'\"");
            }
          }
        }
     }
     print "it has been done\n" if $debug;
     print $tab[$i] if !$debug;;
     print "\b" if !$debug;
     if ($i < 7) { $i++; }
     else { $i = 0; }
}

sub before_insert {} # Take no action
sub custom_sth    {} # Take no action
sub after_finish  {} # Take no action

1;
