File:  [Local Repository] / db / prgsrc / deletefiles.pl
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Sat Jul 28 21:21:29 2001 UTC (22 years, 8 months ago) by boris
Branches: MAIN
CVS tags: HEAD
corrected USAGE

#!/usr/local/bin/perl -w

=head1 NAME

deletefiles.pl - a script for deleting files form the database

=head1 SYNOPSIS

deletefiles.pl  file file file...


=head1 DESCRIPTION

The script will delete all questions with the given file name(s) from the
database



=head1 BUGS

The database, user and password are hardcoded. 

=head1 SEE ALSO

createindex.pl(1), updatedb.pl(1), updateindex.pl(1)

=head1 AUTHOR

Boris Veytsman

=head1 $Id: deletefiles.pl,v 1.4 2001/07/28 21:21:29 boris Exp $

=cut

    use strict;

use DBI;

MAIN: 
{
    my $USAGE="Usage: deletefiles.pl file file file...\n";
    my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
	or die "Can't connect to DB chgk\n";
    foreach my $file (@ARGV) {
	my $sth=$dbh->prepare ("
           select Id,ParentId,QuestionsNum from Tournaments 
           where Type='þ' and FileName='$file'");
	$sth->execute;
	if (!$sth->rows) {
	    print STDERR "File $file is not found in the database\n";
	    next;
	}
	while (my ($Id,$ParentId,$QuestionsNum) = $sth->fetchrow) {
	    print STDERR "Deleting $file, Id=$Id, $QuestionsNum questions\n";
	    UpdateParents($dbh,$ParentId,$QuestionsNum);
	    UpdateChildrenAndDie($dbh,$Id);
	}
    }


    $dbh->disconnect;
    exit 0;
}

sub UpdateParents {
    my ($dbh,$Id,$Num) = @_;
    if ($Id==0) {
	return 0;
    }
    my $sth=$dbh->prepare("
         Update Tournaments set QuestionsNum=QuestionsNum-$Num
         where Id='$Id'");
    $sth->execute;
    $sth=$dbh->prepare("
         select ParentId from Tournaments where Id=$Id");
    $sth->execute;
    while (my ($ParentId)=$sth->fetchrow) {
	UpdateParents($dbh,$ParentId,$Num);
    }
    return 0;
	     
}

sub UpdateChildrenAndDie {
    my($dbh,$Id)=@_;
    my $sth=$dbh->prepare("
          select QuestionId from Questions where ParentId=$Id");
    $sth->execute;
    while (my($QuestionId)=$sth->fetchrow) {
	$dbh->do("delete from Questions where QuestionId=$QuestionId");
    }
    $sth=$dbh->prepare("
          select Id from Tournaments where ParentId=$Id");
    $sth->execute;
    while(my ($ChildId)=$sth->fetchrow) {
	UpdateChildrenAndDie($dbh,$ChildId);
    }
    $dbh->do("delete from Tournaments where Id=$Id");
    return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>