--- db/prgsrc/updatedb.pl 2000/10/18 02:01:19 1.6 +++ db/prgsrc/updatedb.pl 2000/10/19 03:05:39 1.7 @@ -22,7 +22,7 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.6 2000/10/18 02:01:19 boris Exp $ +=head1 $Id: updatedb.pl,v 1.7 2000/10/19 03:05:39 boris Exp $ =cut @@ -95,18 +95,33 @@ sub getField { } sub SelectGroup { - my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_; + my ($dbh, $source, $TourName) = @_; + my ($sth, $ParentId, $i, @arr); $sth = $dbh->prepare("SELECT Id, Title FROM Tournaments WHERE Type = 'Г'"); $sth->execute; - print "Выберите группу для турнира:\n$TourName\n\n"; + print "Выберите группу для турнира:\n$TourName, файл $source\n\n"; while (@arr=$sth->fetchrow) { print "[$arr[0]] $arr[1]\n"; } $ParentId = ; - print "Вы выбрали турнир: $ParentId\n"; - return $ParentId; + if (!$ParentId) { + print "Пропускаем файл $source\n"; + print STDERR "Файл $source отвергнут оператором\n"; + return (0,0); + } else { + print "Вы выбрали турнир: $ParentId\n"; + $sth = $dbh->prepare("INSERT INTO Tournaments + (Title, Type, ParentId, FileName) + VALUES ($TourName, 'Ч', $ParentId, + $source"); + $sth->execute; + my $TournamentId = $sth->{mysql_insertid}; + return ($TournamentId,$ParentId); + } + + } sub UpdateTournament { @@ -122,6 +137,44 @@ sub UpdateQuestion { or die $dbh->errstr; } +sub CheckFile { + my ($dbh, $source,$title) = @_; + my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments + WHERE FileName=$source AND Type='Ч'"); + $sth->execute or die $dbh->errstr; + my @arr = $sth->fetchrow; + if (! scalar @arr) { + return SelectGroup($dbh,$source,$title); + } + my($Id,$ParentId,$QuestionsNum)=@arr; + if($QuestionsNum) { + print "Файл $source с данными $title уже существует. ", + "Заменить?[y/N]\n"; + my $answer = ; + if ($answer !~ /^[yY]/) { + return (0,0); + } else { + DeleteTournament($dbh,$Id,0); + } + } + return($Id,$ParentId); +} + + +sub DeleteTournament { + my ($dbh,$Id,$DeleteMyself) = @_; + my (@Tours) = &GetTours($dbh, $Id); + foreach my $Tour (@Tours) { + DeleteTournament($dbh,$Tour,1); + } + my $sth = $dbh->prepare("DELETE FROM Questions + WHERE ParentId=$Id"); + $sth->execute or die $dbh->errstr; + if($DeleteMyself) { + $sth = $dbh->prepare("DELETE FROM Tournaments + WHERE Id=$Id"); + } +} MAIN: { my($key, $value, $addition); @@ -151,24 +204,21 @@ MAIN: last if (!$key); if ($key =~ /Мета/) { - $value =~ s/[^\d]*//g; - $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE - MetaId=$value"); - $sth->execute - or die "Invalid Meta field: $value"; - $ParentId = ($sth->fetchrow)[0]; - next; + next; # This is obsolete } if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { - $ParentId = &SelectGroup($dbh, $value) - unless ($ParentId); - $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, Type, ParentId, FileName, - CreatedAt) - VALUES ($value, 'Ч', $ParentId, - $source, $CreatedAt)"); + ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value); + if (!$TournamentId) { + last; + } + $sth = $dbh->prepare("UPDATE Tournaments SET + Title=$value, Type='Ч', + ParentId=$ParentId, + FileName=$source, + CreatedAt=$CreatedAt + WHERE + Id=$TournamentId"); $sth->execute; - $TournamentId = $sth->{mysql_insertid}; next; } if ($key =~ /Тур/) { @@ -186,7 +236,7 @@ MAIN: $TourId = $sth->{mysql_insertid}; next; } - if ($key =~ /Вид/) { + if ($key =~ /Вид/ || $key =~ /Тип/) { $qtype = $value; $qtype =~ s/\'//g; next; @@ -208,6 +258,11 @@ MAIN: &UpdateQuestion($dbh, $QuestionId, "Answer", $value); next; } + + if ($key =~ /Рейтинг/) { + &UpdateQuestion($dbh, $QuestionId, "Rating", $value); + next; + } if ($key =~ /Автор/) { @@ -228,7 +283,7 @@ MAIN: } - if ($key =~ /URL/ || key =~ /Ссылка/) { + if ($key =~ /URL/ || $key =~ /Ссылка/) { &UpdateTournament($dbh, $TournamentId, "URL", $value); next; }