Annotation of db/prgsrc/deletefiles.pl, revision 1.2
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.2 ! boris 31: =head1 $Id: deletefiles.pl,v 1.1 2001/07/27 23:54:07 boris Exp $
1.1 boris 32:
33: =cut
34:
35: use strict;
36:
37: use DBI;
38:
39: MAIN:
40: {
1.2 ! boris 41: my $USAGE="Usage: deletefiles.pl [-y|-n] 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;
! 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: }
1.1 boris 55:
1.2 ! boris 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:
1.1 boris 78: }
79:
1.2 ! boris 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>