--- db/prgsrc/deletefiles.pl 2001/07/27 23:54:07 1.1 +++ db/prgsrc/deletefiles.pl 2001/07/28 17:23:02 1.2 @@ -6,7 +6,7 @@ deletefiles.pl - a script for deleting f =head1 SYNOPSIS -deletefiles.pl [B<-y>|B<-n>] file file file... +deletefiles.pl file file file... =head1 DESCRIPTION @@ -14,16 +14,6 @@ deletefiles.pl [B<-y>|B<-n>] file file The script will delete all questions with the given file name(s) from the database -=head1 OPTIONS - - -=item B<-y> - -Answer 'yes' to all questions - -=item B<-n> - -Answer 'no' to all questions =head1 BUGS @@ -32,39 +22,75 @@ The database, user and password are hard =head1 SEE ALSO -createindex.pl(1) +createindex.pl(1), updatedb.pl(1), updateindex.pl(1) =head1 AUTHOR Boris Veytsman -=head1 $Id: deletefiles.pl,v 1.1 2001/07/27 23:54:07 boris Exp $ +=head1 $Id: deletefiles.pl,v 1.2 2001/07/28 17:23:02 boris Exp $ =cut use strict; -use vars qw($opt_h $opt_y $opt_n); -use Getopt::Std; use DBI; MAIN: { - my $USAGE="Usage: deletefiles.pl [-y|-n][-r] file file file...\n"; - getopts('ynh') or die $USAGE; - if ($opt_h) { - print $USAGE; - exit 0; - } - my $decision='askuser'; - if ($opt_y) { - $decision = 'yes'; - } - if ($opt_n ) { - $decision = 'no'; - } + my $USAGE="Usage: deletefiles.pl [-y|-n] 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; + while (my ($Id,$ParentId,$QuestionsNum) = $sth->fetchrow) { + print "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; +}