File:  [Local Repository] / db / prgsrc / deletefiles.pl
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sat Jul 28 17:23:02 2001 UTC (22 years, 9 months ago) by boris
Branches: MAIN
CVS tags: HEAD
wrote a working version

    1: #!/usr/local/bin/perl -w
    2: 
    3: =head1 NAME
    4: 
    5: deletefiles.pl - a script for deleting files form the database
    6: 
    7: =head1 SYNOPSIS
    8: 
    9: deletefiles.pl  file file file...
   10: 
   11: 
   12: =head1 DESCRIPTION
   13: 
   14: The script will delete all questions with the given file name(s) from the
   15: database
   16: 
   17: 
   18: 
   19: =head1 BUGS
   20: 
   21: The database, user and password are hardcoded. 
   22: 
   23: =head1 SEE ALSO
   24: 
   25: createindex.pl(1), updatedb.pl(1), updateindex.pl(1)
   26: 
   27: =head1 AUTHOR
   28: 
   29: Boris Veytsman
   30: 
   31: =head1 $Id: deletefiles.pl,v 1.2 2001/07/28 17:23:02 boris Exp $
   32: 
   33: =cut
   34: 
   35:     use strict;
   36: 
   37: use DBI;
   38: 
   39: MAIN: 
   40: {
   41:     my $USAGE="Usage: deletefiles.pl [-y|-n] file file file...\n";
   42:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
   43: 	or die "Can't connect to DB chgk\n";
   44:     foreach my $file (@ARGV) {
   45: 	my $sth=$dbh->prepare ("
   46:            select Id,ParentId,QuestionsNum from Tournaments 
   47:            where Type='þ' and FileName='$file'");
   48: 	$sth->execute;
   49: 	while (my ($Id,$ParentId,$QuestionsNum) = $sth->fetchrow) {
   50: 	    print "Deleting $file Id=$Id, $QuestionsNum questions\n";
   51: 	    UpdateParents($dbh,$ParentId,$QuestionsNum);
   52: 	    UpdateChildrenAndDie($dbh,$Id);
   53: 	}
   54:     }
   55: 
   56: 
   57:     $dbh->disconnect;
   58:     exit 0;
   59: }
   60: 
   61: sub UpdateParents {
   62:     my ($dbh,$Id,$Num) = @_;
   63:     if ($Id==0) {
   64: 	return 0;
   65:     }
   66:     my $sth=$dbh->prepare("
   67:          Update Tournaments set QuestionsNum=QuestionsNum-$Num
   68:          where Id='$Id'");
   69:     $sth->execute;
   70:     $sth=$dbh->prepare("
   71:          select ParentId from Tournaments where Id=$Id");
   72:     $sth->execute;
   73:     while (my ($ParentId)=$sth->fetchrow) {
   74: 	UpdateParents($dbh,$ParentId,$Num);
   75:     }
   76:     return 0;
   77: 	     
   78: }
   79: 
   80: sub UpdateChildrenAndDie {
   81:     my($dbh,$Id)=@_;
   82:     my $sth=$dbh->prepare("
   83:           select QuestionId from Questions where ParentId=$Id");
   84:     $sth->execute;
   85:     while (my($QuestionId)=$sth->fetchrow) {
   86: 	$dbh->do("delete from Questions where QuestionId=$QuestionId");
   87:     }
   88:     $sth=$dbh->prepare("
   89:           select Id from Tournaments where ParentId=$Id");
   90:     $sth->execute;
   91:     while(my ($ChildId)=$sth->fetchrow) {
   92: 	UpdateChildrenAndDie($dbh,$ChildId);
   93:     }
   94:     $dbh->do("delete from Tournaments where Id=$Id");
   95:     return 0;
   96: }

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