--- db/prgsrc/updatedb.pl 2000/10/19 03:05:39 1.7 +++ db/prgsrc/updatedb.pl 2000/10/22 02:25:02 1.12 @@ -22,7 +22,7 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.7 2000/10/19 03:05:39 boris Exp $ +=head1 $Id: updatedb.pl,v 1.12 2000/10/22 02:25:02 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,8 +78,9 @@ sub getField { } next if (/^\s*$/); - if (/^(.*)[:\.]\s*$/ && !$key) { + if (s/^(.*?)[:\.]\s*// && !$key) { $key = $1; + $value=$_; next; } if ($key) { @@ -106,6 +107,7 @@ sub SelectGroup { print "[$arr[0]] $arr[1]\n"; } $ParentId = ; + chomp $ParentId; if (!$ParentId) { print "Пропускаем файл $source\n"; print STDERR "Файл $source отвергнут оператором\n"; @@ -115,7 +117,7 @@ sub SelectGroup { $sth = $dbh->prepare("INSERT INTO Tournaments (Title, Type, ParentId, FileName) VALUES ($TourName, 'Ч', $ParentId, - $source"); + $source)"); $sth->execute; my $TournamentId = $sth->{mysql_insertid}; return ($TournamentId,$ParentId); @@ -138,7 +140,7 @@ sub UpdateQuestion { } sub CheckFile { - my ($dbh, $source,$title) = @_; + 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; @@ -147,14 +149,14 @@ sub CheckFile { return SelectGroup($dbh,$source,$title); } my($Id,$ParentId,$QuestionsNum)=@arr; - if($QuestionsNum) { + if($QuestionsNum) { print "Файл $source с данными $title уже существует. ", "Заменить?[y/N]\n"; my $answer = ; if ($answer !~ /^[yY]/) { return (0,0); } else { - DeleteTournament($dbh,$Id,0); + DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0); } } return($Id,$ParentId); @@ -162,10 +164,13 @@ sub CheckFile { sub DeleteTournament { - my ($dbh,$Id,$DeleteMyself) = @_; + 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); + DeleteTournament($dbh,$Tour,$Id,0,1); } my $sth = $dbh->prepare("DELETE FROM Questions WHERE ParentId=$Id"); @@ -173,8 +178,27 @@ sub DeleteTournament { 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); @@ -199,20 +223,19 @@ MAIN: $source =~ s/^.*\/([^\/]*)$/$1/; $source = $dbh->quote($source); print STDERR "Файл: $source, дата: $CreatedAt "; - while (($key, $value) = getField(\*INFD, $dbh)) { last if (!$key); if ($key =~ /Мета/) { next; # This is obsolete } - if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { + if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value); if (!$TournamentId) { last; - } + } $sth = $dbh->prepare("UPDATE Tournaments SET - Title=$value, Type='Ч', + Title=$value, Type='Ч', ParentId=$ParentId, FileName=$source, CreatedAt=$CreatedAt @@ -227,7 +250,6 @@ MAIN: WHERE Id=$TourId"); } $qnum = 0; - $qtype = 'Ч'; $sth = $dbh->prepare("INSERT INTO Tournaments (Title, Type, ParentId, CreatedAt) VALUES ($value, 'Т', $TournamentId, @@ -242,6 +264,15 @@ MAIN: 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\')";