Diff for /db/prgsrc/updatedb.pl between versions 1.5 and 1.8

version 1.5, 2000/10/18 01:52:16 version 1.8, 2000/10/19 03:28:25
Line 78  sub getField { Line 78  sub getField {
         }          }
         next if (/^\s*$/);          next if (/^\s*$/);
                   
         if (/^(.*):\s*$/ && !$key) {          if (/^(.*)[:\.]\s*$/ && !$key) {
             $key = $1;              $key = $1;
             next;              next;
         }          }
Line 95  sub getField { Line 95  sub getField {
 }  }
   
 sub SelectGroup {  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      $sth = $dbh->prepare("SELECT Id, Title FROM
                 Tournaments WHERE Type = 'Г'");                  Tournaments WHERE Type = 'Г'");
     $sth->execute;      $sth->execute;
     print "Выберите группу для турнира:\n$TourName\n\n";      print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
     while (@arr=$sth->fetchrow) {      while (@arr=$sth->fetchrow) {
         print "[$arr[0]] $arr[1]\n";          print "[$arr[0]] $arr[1]\n";
     }      }
     $ParentId = <STDIN>;      $ParentId = <STDIN>;
     print "Вы выбрали турнир: $ParentId\n";      chomp $ParentId;
     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 {  sub UpdateTournament {
Line 122  sub UpdateQuestion { Line 138  sub UpdateQuestion {
         or die $dbh->errstr;          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 = <STDIN>;
           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");
       }
   }
   
   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:   MAIN: 
 {  {
     my($key, $value, $addition);      my($key, $value, $addition);
Line 139  MAIN: Line 211  MAIN:
         $d[1]++;          $d[1]++;
         $d[0]+=1900;          $d[0]+=1900;
         my ($CreatedAt) = $dbh->quote( join('-', @d));          my ($CreatedAt) = $dbh->quote( join('-', @d));
         print STDERR "File created on: $CreatedAt\n";  
           
         open INFD, $source           open INFD, $source 
             or die "Can't open input file: $!\n";              or die "Can't open input file: $!\n";
                   
         $source =~ s/^.*\/([^\/]*)$/$1/;          $source =~ s/^.*\/([^\/]*)$/$1/;
         $source = $dbh->quote($source);          $source = $dbh->quote($source);
         print STDERR "Processing file: $source \n";          print STDERR "Файл: $source, дата: $CreatedAt ";
                   
         while (($key, $value) = getField(\*INFD, $dbh)) {          while (($key, $value) = getField(\*INFD, $dbh)) {
             last if (!$key);              last if (!$key);
                           
             if ($key =~ /Мета/) {              if ($key =~ /Мета/) {
                 $value =~ s/[^\d]*//g;                  next;   # This is obsolete
                 $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE  
                                         MetaId=$value");  
                 $sth->execute   
                     or die "Invalid Meta field: $value";  
                 $ParentId = ($sth->fetchrow)[0];  
                 next;  
             }              }
             if ($key =~ /Чемпионат/) {              if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
                 $ParentId = &SelectGroup($dbh, $value)                  ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
                     unless ($ParentId);                  if (!$TournamentId)  {
                 $sth = $dbh->prepare("INSERT INTO Tournaments                      last;
                                      (Title, Type, ParentId, FileName,                   }
                                       CreatedAt)                   $sth = $dbh->prepare("UPDATE Tournaments SET
                                       VALUES ($value, 'Ч', $ParentId,                                        Title=$value, Type='Ч', 
                                        $source, $CreatedAt)");                                       ParentId=$ParentId, 
                                        FileName=$source, 
                                        CreatedAt=$CreatedAt
                                        WHERE
                                        Id=$TournamentId");
                 $sth->execute;                  $sth->execute;
                 $TournamentId = $sth->{mysql_insertid};  
                 next;                  next;
             }              }
             if ($key =~ /Тур/) {              if ($key =~ /Тур/) {
Line 187  MAIN: Line 255  MAIN:
                 $TourId = $sth->{mysql_insertid};                  $TourId = $sth->{mysql_insertid};
                 next;                  next;
             }              }
             if ($key =~ /Вид/) {              if ($key =~ /Вид/ || $key =~ /Тип/) {
                 $qtype = $value;                  $qtype = $value;
                 $qtype =~ s/\'//g;                  $qtype =~ s/\'//g;
                 next;                  next;
Line 204  MAIN: Line 272  MAIN:
                 $all_qnum++;                  $all_qnum++;
                 next;                  next;
             }              }
             &UpdateQuestion($dbh, $QuestionId, "Answer", $value)   
                 if ($key =~ /Ответ/);              if ($key =~ /Ответ/) {
                   &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                   next;
               }
   
               if ($key =~ /Рейтинг/) {
                   &UpdateQuestion($dbh, $QuestionId, "Rating", $value);
                   next;
               }
                           
             &UpdateQuestion($dbh, $QuestionId, "Authors", $value)   
                 if ($key =~ /Автор/);              if ($key =~ /Автор/) {
                   &UpdateQuestion($dbh, $QuestionId, "Authors", $value);
                   next;
               }
                           
             &UpdateQuestion($dbh, $QuestionId, "Sources", $value)   
                 if ($key =~ /Источник/);              if ($key =~ /Источник/) {
                   &UpdateQuestion($dbh, $QuestionId, "Sources", $value);
                   next;
               }
                           
             &UpdateQuestion($dbh, $QuestionId, "Comments", $value)   
                 if ($key =~ /Комментарий/);              if ($key =~ /Комментари/) {
                   &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
                   next;
               }
                           
             &UpdateTournament($dbh, $TournamentId, "URL", $value)  
                 if ($key =~ /URL/);              if ($key =~ /URL/ || $key =~ /Ссылка/) {
                   &UpdateTournament($dbh, $TournamentId, "URL", $value);
                   next;
               }
                           
             &UpdateTournament($dbh, $TournamentId, "Copyright", $value)  
                 if ($key =~ /Копирайт/);              if ($key =~ /Копирайт/) {
                   &UpdateTournament($dbh, $TournamentId, "Copyright", $value);
                   next;
               }
                           
             &UpdateTournament($dbh, $TournamentId, "Info", $value)  
                 if ($key =~ /Инфо/);              if ($key =~ /Инфо/) {
                   &UpdateTournament($dbh, $TournamentId, "Info", $value);
                   next;
               }
                           
             &UpdateTournament($dbh, $TournamentId, "Editors", $value)  
                 if ($key =~ /Редактор/);              if ($key =~ /Редактор/) {
                   &UpdateTournament($dbh, $TournamentId, "Editors", $value);
                   next;
               }
                           
             &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value)  
                 if ($key =~ /Обработан/);              if ($key =~ /Обработан/) {
                   &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
                   next;
                                 }
                           
             if ($key =~ /Дата/) {              if ($key =~ /Дата/) {
                 if ($TourId) {                  if ($TourId) {
Line 237  MAIN: Line 337  MAIN:
                 } else {                  } else {
                     &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);                      &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
                 }                  }
                   next;
             }              }
               
               #
               # If we are here, something got wrong!
               #
               print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
               
         }          }
         $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum          $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                         WHERE Id=$TourId");                          WHERE Id=$TourId");
         $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum          $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
                         WHERE Id=$TournamentId");                          WHERE Id=$TournamentId");
         &UpdateParents($dbh, $ParentId, $all_qnum);                       &UpdateParents($dbh, $ParentId, $all_qnum);             
         print STDERR "Total number of questions: $all_qnum \n";          print STDERR "Всего вопросов: $all_qnum \n";
     }      }
     $dbh->disconnect;      $dbh->disconnect;
 }  }

Removed from v.1.5  
changed lines
  Added in v.1.8


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>