#!/usr/local/bin/perl 

use DBI;
use strict;

MAIN: 
{
	my($key, $value, $addition);

	my($source, $TourId, $QId);

	my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
		or die "Can't connect to DB chgk\n";

	while ($source = shift) {
		print "$source\n";
		
		my ($sth) = $dbh->query("SELECT Id FROM Tournaments 
			WHERE ParentId = $source");
		while ($TourId = ($sth->fetchrow)[0]) {
			my ($sth) = $dbh->query("SELECT QuestionId FROM Questions
				WHERE ParentId = $TourId");
			while ($QId = ($sth->fetchrow)[0]) {
				$dbh->query("DELETE FROM Lines WHERE Id = $QId");
				$dbh->query("DELETE FROM Questions WHERE QuestionId = $QId");
			}
			$dbh->query("DELETE FROM Tournaments WHERE Id = $TourId");
		}
		$dbh->query("DELETE FROM Tournaments WHERE Id = $source");
	}
}
