Annotation of db/prgsrc/updatedb.pl, revision 1.9

1.1       boris       1: #!/usr/local/bin/perl 
                      2: 
                      3: =head1 NAME
                      4: 
                      5: updatedb.pl - a script for creation of new database. 
                      6: 
                      7: =head1 SYNOPSIS
                      8: 
                      9: updatedb.pl I<file1> I<file2>....
                     10: 
                     11: 
                     12: =head1 DESCRIPTION
                     13: 
                     14: Updates information in the B<chgk> databse. Uses file
                     15: 
                     16: 
                     17: =head1 BUGS
                     18: 
                     19: The database, user and password are hardcoded. 
                     20: 
                     21: =head1 AUTHOR
                     22: 
                     23: Dmitry Rubinstein
                     24: 
1.9     ! boris      25: =head1 $Id: updatedb.pl,v 1.8 2000/10/19 03:28:25 boris Exp boris $
1.1       boris      26: 
                     27: =cut
                     28: 
                     29: my (%RevMonths) = 
1.5       boris      30:     ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
                     31:      'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
                     32:      'Dec', '12', 
                     33:      'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
                     34:      'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
                     35:      'DEC', '12', 
                     36:      'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',
                     37:      'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8', 
                     38:      'Окт', '9', 'Ноя', '19', 'Дек', '11');
1.1       boris      39: my ($sth);
                     40: 
1.3       boris      41: use vars qw($/);
                     42: 
1.4       boris      43: 
1.3       boris      44: 
1.1       boris      45: use DBI;
                     46: use strict;
                     47: 
                     48: sub UpdateParents {
1.5       boris      49:     my ($dbh, $ParentId, $all_qnum) = @_;
                     50:     if ($ParentId) {
                     51:        my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId 
                     52: FROM Tournaments WHERE Id = $ParentId");
                     53:        $sth1->execute;
                     54:        my ($q, $p) = ($sth1->fetchrow)[0, 1];
                     55:        $dbh->do("UPDATE Tournaments SET 
                     56:                   QuestionsNum=$q + $all_qnum 
                     57:                   WHERE Id = $ParentId");
                     58:        &UpdateParents($dbh, $p, $all_qnum);
                     59:     }
1.1       boris      60: }
                     61: 
                     62: sub getField {
1.5       boris      63:     my($desc, $dbh) = @_;
                     64:     my($key);
                     65:     my($value) = ('');
                     66:     while (<$desc>) {
                     67:        s/
//;
                     68:        if ($key && /^\s*$/) {
                     69:            chomp $value;
                     70:            chomp $key;
                     71:            if ($key eq 'Дата') {
                     72:                $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
1.9     ! boris      73:                my($month) = $RevMonths{$2} || '000';
1.5       boris      74:                $value =~ s/$2/$month/;
                     75:            }
                     76:            $value = $dbh->quote($value);
                     77:            return ($key, $value);
                     78:        }
                     79:        next if (/^\s*$/);
                     80:        
1.6       boris      81:        if (/^(.*)[:\.]\s*$/ && !$key) {
1.5       boris      82:            $key = $1;
                     83:            next;
1.1       boris      84:        }
1.5       boris      85:        if ($key) {
                     86:            $value .= $_;
                     87:            next;
1.1       boris      88:        }
1.5       boris      89:     }
                     90:     if ($key && $value) {
                     91:        $value = $dbh->quote($value);
                     92:        return ($key, $value);
                     93:     }
                     94:     return (0, 0);
1.1       boris      95: }
                     96: 
                     97: sub SelectGroup {
1.7       boris      98:     my ($dbh, $source, $TourName) = @_;
                     99:     my ($sth, $ParentId, $i, @arr);
1.5       boris     100:     
                    101:     $sth = $dbh->prepare("SELECT Id, Title FROM
1.1       boris     102:                Tournaments WHERE Type = 'Г'");
1.5       boris     103:     $sth->execute;
1.7       boris     104:     print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
1.5       boris     105:     while (@arr=$sth->fetchrow) {
                    106:        print "[$arr[0]] $arr[1]\n";
                    107:     }
                    108:     $ParentId = <STDIN>;
1.8       boris     109:     chomp $ParentId;
1.7       boris     110:     if (!$ParentId) {
                    111:        print "Пропускаем файл $source\n";
                    112:        print STDERR "Файл $source отвергнут оператором\n";
                    113:        return (0,0);
                    114:     } else {
                    115:        print "Вы выбрали турнир: $ParentId\n";
                    116:        $sth = $dbh->prepare("INSERT INTO Tournaments
                    117:                              (Title, Type, ParentId, FileName) 
                    118:                               VALUES ($TourName, 'Ч', $ParentId, 
1.8       boris     119:                                        $source)");
1.7       boris     120:        $sth->execute;
                    121:        my $TournamentId = $sth->{mysql_insertid};
                    122:        return ($TournamentId,$ParentId);
                    123:     }
                    124:                
                    125:     
1.1       boris     126: }
                    127: 
                    128: sub UpdateTournament {
1.5       boris     129:     my ($dbh, $TournamentId, $field, $value) = @_;
                    130:     $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
                    131:        or die $dbh->errstr;
1.1       boris     132: }
                    133: 
                    134: sub UpdateQuestion {
1.5       boris     135:     my ($dbh, $QuestionId, $field, $value) = @_;
                    136:     $dbh->do("UPDATE Questions SET $field=$value 
1.1       boris     137:                WHERE QuestionId=$QuestionId")
1.5       boris     138:        or die $dbh->errstr;
1.1       boris     139: }
                    140: 
1.7       boris     141: sub CheckFile {
                    142:     my ($dbh, $source,$title) = @_;
                    143:     my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
                    144:                              WHERE FileName=$source AND Type='Ч'");
                    145:     $sth->execute or die $dbh->errstr;
                    146:     my @arr = $sth->fetchrow;
                    147:     if (! scalar @arr) {
                    148:        return SelectGroup($dbh,$source,$title);
                    149:     }
                    150:     my($Id,$ParentId,$QuestionsNum)=@arr;
                    151:     if($QuestionsNum) {
                    152:        print "Файл $source с данными $title уже существует. ",
                    153:        "Заменить?[y/N]\n";
                    154:        my $answer = <STDIN>;
                    155:        if ($answer !~ /^[yY]/) {
                    156:            return (0,0);
                    157:        } else {
                    158:            DeleteTournament($dbh,$Id,0);
                    159:        }
                    160:     } 
                    161:     return($Id,$ParentId);     
                    162: }
                    163: 
                    164: 
                    165: sub DeleteTournament {
                    166:     my ($dbh,$Id,$DeleteMyself) = @_;
                    167:     my (@Tours) = &GetTours($dbh, $Id);
                    168:     foreach my $Tour (@Tours) {
                    169:        DeleteTournament($dbh,$Tour,1);
                    170:     }
                    171:     my $sth = $dbh->prepare("DELETE FROM Questions
                    172:                              WHERE ParentId=$Id");
                    173:     $sth->execute or die $dbh->errstr;
                    174:     if($DeleteMyself) {
                    175:        $sth = $dbh->prepare("DELETE FROM Tournaments
                    176:                              WHERE Id=$Id");
1.9     ! boris     177:        $sth->execute or die $dbh->errstr;
1.7       boris     178:     }
                    179: }
1.8       boris     180: 
                    181: sub GetTours {
                    182:        my ($dbh, $ParentId) = @_;
                    183:        my (@arr, @Tours);
                    184: 
                    185:        my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
                    186:                WHERE ParentId=$ParentId ORDER BY Id");
                    187: 
                    188:        $sth->execute;
                    189: 
                    190:        while (@arr = $sth->fetchrow) {
                    191:                push @Tours, $arr[0];
                    192:        }
                    193: 
                    194:        return @Tours;
                    195: }
                    196: 
                    197: 
1.1       boris     198: MAIN: 
                    199: {
1.5       boris     200:     my($key, $value, $addition);
                    201:     
                    202:     my($source);
                    203:     
                    204:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                    205:        or die "Can't connect to DB chgk\n";
                    206:     
                    207:     while ($source = shift) {
                    208:        my($PlayedAt) = '';
                    209:        my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
                    210:        my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
                    211:        my (@d) = (localtime((stat($source))[9]))[5,4,3];
                    212:        $d[1]++;
                    213:        $d[0]+=1900;
                    214:        my ($CreatedAt) = $dbh->quote( join('-', @d));
1.6       boris     215: 
1.5       boris     216:        open INFD, $source 
                    217:            or die "Can't open input file: $!\n";
                    218:        
                    219:        $source =~ s/^.*\/([^\/]*)$/$1/;
                    220:        $source = $dbh->quote($source);
1.6       boris     221:        print STDERR "Файл: $source, дата: $CreatedAt ";
1.5       boris     222:        
                    223:        while (($key, $value) = getField(\*INFD, $dbh)) {
                    224:            last if (!$key);
                    225:            
                    226:            if ($key =~ /Мета/) {
1.7       boris     227:                next;   # This is obsolete
1.5       boris     228:            }
1.6       boris     229:            if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
1.7       boris     230:                ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
                    231:                if (!$TournamentId)  {
                    232:                    last;
                    233:                }
                    234:                $sth = $dbh->prepare("UPDATE Tournaments SET
                    235:                                     Title=$value, Type='Ч', 
                    236:                                      ParentId=$ParentId, 
                    237:                                      FileName=$source, 
                    238:                                      CreatedAt=$CreatedAt
                    239:                                      WHERE
                    240:                                      Id=$TournamentId");
1.5       boris     241:                $sth->execute;
                    242:                next;
                    243:            }
                    244:            if ($key =~ /Тур/) {
                    245:                if ($TourId) {
                    246:                    $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                    247:                              WHERE Id=$TourId");
                    248:                }
                    249:                $qnum = 0;
                    250:                $qtype = 'Ч';
                    251:                $sth = $dbh->prepare("INSERT INTO Tournaments
                    252:                                      (Title, Type, ParentId, CreatedAt) 
                    253:                                      VALUES ($value, 'Т', $TournamentId, 
                    254:                                       $CreatedAt)");
                    255:                $sth->execute;
                    256:                $TourId = $sth->{mysql_insertid};
                    257:                next;
                    258:            }
1.7       boris     259:            if ($key =~ /Вид/ || $key =~ /Тип/) {
1.5       boris     260:                $qtype = $value;
                    261:                $qtype =~ s/\'//g;
                    262:                next;
                    263:            }
                    264:            if ($key =~ /Вопрос/) {
                    265:                my $query = "INSERT INTO Questions 
                    266:                             (ParentId, Number, Type) 
                    267:                             VALUES ($TourId, $qnum+1, \'$qtype\')";
                    268:                $sth = $dbh->prepare($query);
                    269:                $sth->execute or print $query;;
                    270:                $QuestionId = $sth->{mysql_insertid};
                    271:                &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                    272:                $qnum++;
                    273:                $all_qnum++;
                    274:                next;
                    275:            }
1.6       boris     276: 
                    277:            if ($key =~ /Ответ/) {
                    278:                &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                    279:                next;
                    280:            }
1.7       boris     281: 
                    282:            if ($key =~ /Рейтинг/) {
                    283:                &UpdateQuestion($dbh, $QuestionId, "Rating", $value);
                    284:                next;
                    285:            }
1.5       boris     286:            
1.6       boris     287: 
                    288:            if ($key =~ /Автор/) {
                    289:                &UpdateQuestion($dbh, $QuestionId, "Authors", $value);
                    290:                next;
                    291:            }
1.5       boris     292:            
1.6       boris     293: 
                    294:            if ($key =~ /Источник/) {
                    295:                &UpdateQuestion($dbh, $QuestionId, "Sources", $value);
                    296:                next;
                    297:            }
1.5       boris     298:            
1.6       boris     299: 
                    300:            if ($key =~ /Комментари/) {
                    301:                &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
                    302:                next;
                    303:            }
1.5       boris     304:            
1.6       boris     305: 
1.7       boris     306:            if ($key =~ /URL/ || $key =~ /Ссылка/) {
1.6       boris     307:                &UpdateTournament($dbh, $TournamentId, "URL", $value);
                    308:                next;
                    309:            }
1.5       boris     310:            
1.6       boris     311: 
                    312:            if ($key =~ /Копирайт/) {
                    313:                &UpdateTournament($dbh, $TournamentId, "Copyright", $value);
                    314:                next;
                    315:            }
1.5       boris     316:            
1.6       boris     317: 
                    318:            if ($key =~ /Инфо/) {
                    319:                &UpdateTournament($dbh, $TournamentId, "Info", $value);
                    320:                next;
                    321:            }
1.5       boris     322:            
1.6       boris     323: 
                    324:            if ($key =~ /Редактор/) {
                    325:                &UpdateTournament($dbh, $TournamentId, "Editors", $value);
                    326:                next;
                    327:            }
1.5       boris     328:            
1.6       boris     329: 
                    330:            if ($key =~ /Обработан/) {
                    331:                &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
                    332:                next;
                    333:                              }
1.5       boris     334:            
                    335:            if ($key =~ /Дата/) {
                    336:                if ($TourId) {
                    337:                    &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
                    338:                } else {
                    339:                    &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
1.1       boris     340:                }
1.6       boris     341:                next;
1.5       boris     342:            }
1.6       boris     343:            
                    344:            #
                    345:            # If we are here, something got wrong!
                    346:            #
                    347:            print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
                    348:            
1.5       boris     349:        }
                    350:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
1.1       boris     351:                        WHERE Id=$TourId");
1.5       boris     352:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
1.1       boris     353:                        WHERE Id=$TournamentId");
1.5       boris     354:        &UpdateParents($dbh, $ParentId, $all_qnum);             
1.6       boris     355:        print STDERR "Всего вопросов: $all_qnum \n";
1.5       boris     356:     }
                    357:     $dbh->disconnect;
1.1       boris     358: }

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