version 1.1, 2001/07/27 23:54:07
|
version 1.3, 2001/07/28 21:19:06
|
Line 6 deletefiles.pl - a script for deleting f
|
Line 6 deletefiles.pl - a script for deleting f
|
|
|
=head1 SYNOPSIS |
=head1 SYNOPSIS |
|
|
deletefiles.pl [B<-y>|B<-n>] file file file... |
deletefiles.pl file file file... |
|
|
|
|
=head1 DESCRIPTION |
=head1 DESCRIPTION |
Line 14 deletefiles.pl [B<-y>|B<-n>] file file
|
Line 14 deletefiles.pl [B<-y>|B<-n>] file file
|
The script will delete all questions with the given file name(s) from the |
The script will delete all questions with the given file name(s) from the |
database |
database |
|
|
=head1 OPTIONS |
|
|
|
|
|
=item B<-y> |
|
|
|
Answer 'yes' to all questions |
|
|
|
=item B<-n> |
|
|
|
Answer 'no' to all questions |
|
|
|
|
|
=head1 BUGS |
=head1 BUGS |
Line 32 The database, user and password are hard
|
Line 22 The database, user and password are hard
|
|
|
=head1 SEE ALSO |
=head1 SEE ALSO |
|
|
createindex.pl(1) |
createindex.pl(1), updatedb.pl(1), updateindex.pl(1) |
|
|
=head1 AUTHOR |
=head1 AUTHOR |
|
|
Line 43 Boris Veytsman
|
Line 33 Boris Veytsman
|
=cut |
=cut |
|
|
use strict; |
use strict; |
use vars qw($opt_h $opt_y $opt_n); |
|
|
|
use Getopt::Std; |
|
use DBI; |
use DBI; |
|
|
MAIN: |
MAIN: |
{ |
{ |
my $USAGE="Usage: deletefiles.pl [-y|-n][-r] file file file...\n"; |
my $USAGE="Usage: deletefiles.pl [-y|-n] 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($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") |
my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") |
or die "Can't connect to DB chgk\n"; |
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; |
|
} |