--- db/prgsrc/updatedb.pl 2000/10/18 02:01:19 1.6 +++ db/prgsrc/updatedb.pl 2000/10/20 03:18:38 1.11 @@ -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.11 2000/10/20 03:18:38 boris Exp $ =cut @@ -64,13 +64,13 @@ sub getField { my($key); my($value) = (''); while (<$desc>) { - s/ //; + s/[ ]//g; if ($key && /^\s*$/) { chomp $value; chomp $key; if ($key eq 'Дата') { $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; - my($month) = $RevMonths{$2}; + my($month) = $RevMonths{$2} || '000'; $value =~ s/$2/$month/; } $value = $dbh->quote($value); @@ -78,7 +78,7 @@ sub getField { } next if (/^\s*$/); - if (/^(.*)[:\.]\s*$/ && !$key) { + if (/^(.*)[:\.][\s]*$/ && !$key) { $key = $1; next; } @@ -95,18 +95,34 @@ 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; + chomp $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 +138,66 @@ 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,$ParentId,$QuestionsNum,0); + } + } + return($Id,$ParentId); +} + + +sub DeleteTournament { + my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_; + if ($QuestionsNum) { + UpdateParents($dbh,$ParentId,-$QuestionsNum); + } + 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"); + $sth->execute or die $dbh->errstr; + } +} + +sub GetTours { + my ($dbh, $ParentId) = @_; + my (@arr, @Tours); + + my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments + WHERE ParentId=$ParentId ORDER BY Id"); + + $sth->execute; + + while (@arr = $sth->fetchrow) { + push @Tours, $arr[0]; + } + + return @Tours; +} + + MAIN: { my($key, $value, $addition); @@ -146,29 +222,25 @@ MAIN: $source =~ s/^.*\/([^\/]*)$/$1/; $source = $dbh->quote($source); print STDERR "Файл: $source, дата: $CreatedAt "; - while (($key, $value) = getField(\*INFD, $dbh)) { 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)"); + if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { + ($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 =~ /Тур/) { @@ -177,7 +249,6 @@ MAIN: WHERE Id=$TourId"); } $qnum = 0; - $qtype = 'Ч'; $sth = $dbh->prepare("INSERT INTO Tournaments (Title, Type, ParentId, CreatedAt) VALUES ($value, 'Т', $TournamentId, @@ -186,12 +257,21 @@ MAIN: $TourId = $sth->{mysql_insertid}; next; } - if ($key =~ /Вид/) { + if ($key =~ /Вид/ || $key =~ /Тип/) { $qtype = $value; $qtype =~ s/\'//g; next; } if ($key =~ /Вопрос/) { + if (!$TourId) { + $qnum = 0; + $sth = $dbh->prepare("INSERT INTO Tournaments + (Title, Type, ParentId, CreatedAt) + VALUES ('1', 'Т', $TournamentId, + $CreatedAt)"); + $sth->execute; + $TourId = $sth->{mysql_insertid}; + } my $query = "INSERT INTO Questions (ParentId, Number, Type) VALUES ($TourId, $qnum+1, \'$qtype\')"; @@ -208,6 +288,11 @@ MAIN: &UpdateQuestion($dbh, $QuestionId, "Answer", $value); next; } + + if ($key =~ /Рейтинг/) { + &UpdateQuestion($dbh, $QuestionId, "Rating", $value); + next; + } if ($key =~ /Автор/) { @@ -228,7 +313,7 @@ MAIN: } - if ($key =~ /URL/ || key =~ /Ссылка/) { + if ($key =~ /URL/ || $key =~ /Ссылка/) { &UpdateTournament($dbh, $TournamentId, "URL", $value); next; }