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

version 1.4, 2000/10/17 16:56:09 version 1.5, 2000/10/18 01:52:16
Line 27  Dmitry Rubinstein Line 27  Dmitry Rubinstein
 =cut  =cut
   
 my (%RevMonths) =   my (%RevMonths) = 
         ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',      ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
         'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',       'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
         'Dec', '12',        'Dec', '12', 
          'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',       'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
         'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',       'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
         'DEC', '12',        'DEC', '12', 
          'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',       'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',
          'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8',        'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8', 
          'Окт', '9', 'Ноя', '19', 'Дек', '11');       'Окт', '9', 'Ноя', '19', 'Дек', '11');
 my ($sth);  my ($sth);
   
 use vars qw($/);  use vars qw($/);
Line 46  use DBI; Line 46  use DBI;
 use strict;  use strict;
   
 sub UpdateParents {  sub UpdateParents {
         my ($dbh, $ParentId, $all_qnum) = @_;      my ($dbh, $ParentId, $all_qnum) = @_;
         if ($ParentId) {      if ($ParentId) {
                 my ($sth1) = $dbh->prepare("          my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId 
                         SELECT QuestionsNum, ParentId FROM Tournaments WHERE Id = $ParentId  FROM Tournaments WHERE Id = $ParentId");
                 ");          $sth1->execute;
                 $sth1->execute;          my ($q, $p) = ($sth1->fetchrow)[0, 1];
                 my ($q, $p) = ($sth1->fetchrow)[0, 1];          $dbh->do("UPDATE Tournaments SET 
                 $dbh->do("                    QuestionsNum=$q + $all_qnum 
                         UPDATE Tournaments SET QuestionsNum=$q + $all_qnum WHERE Id =                    WHERE Id = $ParentId");
                         $ParentId");          &UpdateParents($dbh, $p, $all_qnum);
                 &UpdateParents($dbh, $p, $all_qnum);      }
         }  
 }  }
   
 sub getField {  sub getField {
         my($desc, $dbh) = @_;      my($desc, $dbh) = @_;
         my($key);      my($key);
         my($value) = ('');      my($value) = ('');
         while (<$desc>) {      while (<$desc>) {
                 s/ //;          s/ //;
                 if ($key && /^\s*$/) {          if ($key && /^\s*$/) {
                         chomp $value;              chomp $value;
                         chomp $key;              chomp $key;
                         if ($key eq 'Дата') {              if ($key eq 'Дата') {
                                 $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;                  $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
                                 my($month) = $RevMonths{$2};                  my($month) = $RevMonths{$2};
                                 $value =~ s/$2/$month/;                  $value =~ s/$2/$month/;
                         }              }
                         $value = $dbh->quote($value);              $value = $dbh->quote($value);
                         return ($key, $value);              return ($key, $value);
                 }          }
                 next if (/^\s*$/);          next if (/^\s*$/);
           
                 if (/^(.*):\s*$/ && !$key) {          if (/^(.*):\s*$/ && !$key) {
                         $key = $1;              $key = $1;
                         next;              next;
                 }  
                 if ($key) {  
                         $value .= $_;  
                         next;  
                 }  
         }          }
         if ($key && $value) {          if ($key) {
                 $value = $dbh->quote($value);              $value .= $_;
                 return ($key, $value);              next;
         }          }
         return (0, 0);      }
       if ($key && $value) {
           $value = $dbh->quote($value);
           return ($key, $value);
       }
       return (0, 0);
 }  }
   
 sub SelectGroup {  sub SelectGroup {
         my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;      my ($dbh, $TourName, $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\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";      print "Вы выбрали турнир: $ParentId\n";
         return $ParentId;      return $ParentId;
 }  }
   
 sub UpdateTournament {  sub UpdateTournament {
         my ($dbh, $TournamentId, $field, $value) = @_;      my ($dbh, $TournamentId, $field, $value) = @_;
         $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")      $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
                 or die $dbh->errstr;          or die $dbh->errstr;
 }  }
   
 sub UpdateQuestion {  sub UpdateQuestion {
         my ($dbh, $QuestionId, $field, $value) = @_;      my ($dbh, $QuestionId, $field, $value) = @_;
         $dbh->do("UPDATE Questions SET $field=$value       $dbh->do("UPDATE Questions SET $field=$value 
                 WHERE QuestionId=$QuestionId")                  WHERE QuestionId=$QuestionId")
                         or die $dbh->errstr;          or die $dbh->errstr;
 }  }
   
 MAIN:   MAIN: 
 {  {
         my($key, $value, $addition);      my($key, $value, $addition);
       
         my($source);      my($source);
       
         my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")      my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                 or die "Can't connect to DB chgk\n";          or die "Can't connect to DB chgk\n";
       
         while ($source = shift) {      while ($source = shift) {
                 my($PlayedAt) = '';          my($PlayedAt) = '';
                 my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);          my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
                 my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');          my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
                 my (@d) = (localtime((stat($source))[9]))[5,4,3];          my (@d) = (localtime((stat($source))[9]))[5,4,3];
                 $d[1]++;          $d[1]++;
                 $d[0]+=1900;          $d[0]+=1900;
                 my ($CreatedAt) = $dbh->quote( join('-', @d));          my ($CreatedAt) = $dbh->quote( join('-', @d));
                 print "File created on: $CreatedAt\n";          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 "Processing file: $source \n";          print STDERR "Processing file: $source \n";
           
                 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;                  $value =~ s/[^\d]*//g;
                                 $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE                  $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE
                                         MetaId=$value");                                          MetaId=$value");
                                 $sth->execute                   $sth->execute 
                                         or die "Invalid Meta field: $value";                      or die "Invalid Meta field: $value";
                                 $ParentId = ($sth->fetchrow)[0];                  $ParentId = ($sth->fetchrow)[0];
                                 next;                  next;
                         }              }
                         if ($key =~ /Чемпионат/) {              if ($key =~ /Чемпионат/) {
                                 $ParentId = &SelectGroup($dbh, $value)                  $ParentId = &SelectGroup($dbh, $value)
                                         unless ($ParentId);                      unless ($ParentId);
                                 $sth = $dbh->prepare("INSERT INTO Tournaments                  $sth = $dbh->prepare("INSERT INTO Tournaments
                                         (Title, Type, ParentId, FileName, CreatedAt)                                        (Title, Type, ParentId, FileName, 
                                         VALUES ($value, 'Ч', $ParentId, $source,                                        CreatedAt) 
                                         $CreatedAt)");                                        VALUES ($value, 'Ч', $ParentId, 
                                 $sth->execute;                                         $source, $CreatedAt)");
                                 $TournamentId = $sth->{mysql_insertid};                  $sth->execute;
                                 next;                  $TournamentId = $sth->{mysql_insertid};
                         }                  next;
                         if ($key =~ /Тур/) {              }
                                 if ($TourId) {              if ($key =~ /Тур/) {
                                         $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum                  if ($TourId) {
                                                 WHERE Id=$TourId");                      $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                                 }                                WHERE Id=$TourId");
                                 $qnum = 0;  
                                 $qtype = 'Ч';  
                                 $sth = $dbh->prepare("INSERT INTO Tournaments  
                                         (Title, Type, ParentId, CreatedAt)   
                                         VALUES ($value, 'Т', $TournamentId, $CreatedAt)");  
                                 $sth->execute;  
                                 $TourId = $sth->{mysql_insertid};  
                                 next;  
                         }  
                         if ($key =~ /Вид/) {  
                                 $qtype = $value;  
                                 $qtype =~ s/'//g;  
                                 next;  
                         }  
                         if ($key =~ /Вопрос/) {  
                             my $query = "INSERT INTO Questions   
                                         (ParentId, Number, Type)   
                                         VALUES ($TourId, $qnum+1, \'$qtype\')";  
                                 $sth = $dbh->prepare($query);  
                                 $sth->execute or print $query;;  
                                 $QuestionId = $sth->{mysql_insertid};  
                                 &UpdateQuestion($dbh, $QuestionId, "Question", $value);  
                                 $qnum++;  
                                 $all_qnum++;  
                                 next;  
                         }  
                         &UpdateQuestion($dbh, $QuestionId, "Answer", $value)   
                                 if ($key =~ /Ответ/);  
           
                         &UpdateQuestion($dbh, $QuestionId, "Authors", $value)   
                                 if ($key =~ /Автор/);  
           
                         &UpdateQuestion($dbh, $QuestionId, "Sources", $value)   
                                 if ($key =~ /Источник/);  
           
                         &UpdateQuestion($dbh, $QuestionId, "Comments", $value)   
                                 if ($key =~ /Комментарий/);  
           
                         &UpdateTournament($dbh, $TournamentId, "URL", $value)  
                                 if ($key =~ /URL/);  
   
                         &UpdateTournament($dbh, $TournamentId, "Copyright", $value)  
                                 if ($key =~ /Копирайт/);  
   
                         &UpdateTournament($dbh, $TournamentId, "Info", $value)  
                                 if ($key =~ /Инфо/);  
   
                         if ($key =~ /Дата/) {  
                                 if ($TourId) {  
                                         &UpdateTournament($dbh, $TourId, "PlayedAt", $value);  
                                 } else {  
                                         &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);  
                                 }  
                         }  
                 }                  }
                 $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum                  $qnum = 0;
                   $qtype = 'Ч';
                   $sth = $dbh->prepare("INSERT INTO Tournaments
                                         (Title, Type, ParentId, CreatedAt) 
                                         VALUES ($value, 'Т', $TournamentId, 
                                         $CreatedAt)");
                   $sth->execute;
                   $TourId = $sth->{mysql_insertid};
                   next;
               }
               if ($key =~ /Вид/) {
                   $qtype = $value;
                   $qtype =~ s/\'//g;
                   next;
               }
               if ($key =~ /Вопрос/) {
                   my $query = "INSERT INTO Questions 
                                (ParentId, Number, Type) 
                                VALUES ($TourId, $qnum+1, \'$qtype\')";
                   $sth = $dbh->prepare($query);
                   $sth->execute or print $query;;
                   $QuestionId = $sth->{mysql_insertid};
                   &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                   $qnum++;
                   $all_qnum++;
                   next;
               }
               &UpdateQuestion($dbh, $QuestionId, "Answer", $value) 
                   if ($key =~ /Ответ/);
               
               &UpdateQuestion($dbh, $QuestionId, "Authors", $value) 
                   if ($key =~ /Автор/);
               
               &UpdateQuestion($dbh, $QuestionId, "Sources", $value) 
                   if ($key =~ /Источник/);
               
               &UpdateQuestion($dbh, $QuestionId, "Comments", $value) 
                   if ($key =~ /Комментарий/);
               
               &UpdateTournament($dbh, $TournamentId, "URL", $value)
                   if ($key =~ /URL/);
               
               &UpdateTournament($dbh, $TournamentId, "Copyright", $value)
                   if ($key =~ /Копирайт/);
               
               &UpdateTournament($dbh, $TournamentId, "Info", $value)
                   if ($key =~ /Инфо/);
               
               &UpdateTournament($dbh, $TournamentId, "Editors", $value)
                   if ($key =~ /Редактор/);
               
               &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value)
                   if ($key =~ /Обработан/);
               
               if ($key =~ /Дата/) {
                   if ($TourId) {
                       &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
                   } else {
                       &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
                   }
               }
           }
           $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";
         $dbh->disconnect;      }
       $dbh->disconnect;
 }  }

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


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