Annotation of db/prgsrc/deletefiles.pl, revision 1.4

1.1       boris       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: 
1.2       boris       9: deletefiles.pl  file file file...
1.1       boris      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: 
1.2       boris      25: createindex.pl(1), updatedb.pl(1), updateindex.pl(1)
1.1       boris      26: 
                     27: =head1 AUTHOR
                     28: 
                     29: Boris Veytsman
                     30: 
1.4     ! boris      31: =head1 $Id: deletefiles.pl,v 1.3 2001/07/28 21:19:06 boris Exp $
1.1       boris      32: 
                     33: =cut
                     34: 
                     35:     use strict;
                     36: 
                     37: use DBI;
                     38: 
                     39: MAIN: 
                     40: {
1.4     ! boris      41:     my $USAGE="Usage: deletefiles.pl file file file...\n";
1.1       boris      42:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
                     43:        or die "Can't connect to DB chgk\n";
1.2       boris      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;
1.3       boris      49:        if (!$sth->rows) {
                     50:            print STDERR "File $file is not found in the database\n";
                     51:            next;
                     52:        }
1.2       boris      53:        while (my ($Id,$ParentId,$QuestionsNum) = $sth->fetchrow) {
1.3       boris      54:            print STDERR "Deleting $file, Id=$Id, $QuestionsNum questions\n";
1.2       boris      55:            UpdateParents($dbh,$ParentId,$QuestionsNum);
                     56:            UpdateChildrenAndDie($dbh,$Id);
                     57:        }
                     58:     }
1.1       boris      59: 
1.2       boris      60: 
                     61:     $dbh->disconnect;
                     62:     exit 0;
                     63: }
                     64: 
                     65: sub UpdateParents {
                     66:     my ($dbh,$Id,$Num) = @_;
                     67:     if ($Id==0) {
                     68:        return 0;
                     69:     }
                     70:     my $sth=$dbh->prepare("
                     71:          Update Tournaments set QuestionsNum=QuestionsNum-$Num
                     72:          where Id='$Id'");
                     73:     $sth->execute;
                     74:     $sth=$dbh->prepare("
                     75:          select ParentId from Tournaments where Id=$Id");
                     76:     $sth->execute;
                     77:     while (my ($ParentId)=$sth->fetchrow) {
                     78:        UpdateParents($dbh,$ParentId,$Num);
                     79:     }
                     80:     return 0;
                     81:             
1.1       boris      82: }
                     83: 
1.2       boris      84: sub UpdateChildrenAndDie {
                     85:     my($dbh,$Id)=@_;
                     86:     my $sth=$dbh->prepare("
                     87:           select QuestionId from Questions where ParentId=$Id");
                     88:     $sth->execute;
                     89:     while (my($QuestionId)=$sth->fetchrow) {
                     90:        $dbh->do("delete from Questions where QuestionId=$QuestionId");
                     91:     }
                     92:     $sth=$dbh->prepare("
                     93:           select Id from Tournaments where ParentId=$Id");
                     94:     $sth->execute;
                     95:     while(my ($ChildId)=$sth->fetchrow) {
                     96:        UpdateChildrenAndDie($dbh,$ChildId);
                     97:     }
                     98:     $dbh->do("delete from Tournaments where Id=$Id");
                     99:     return 0;
                    100: }

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