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

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.12    ! boris      25: =head1 $Id: updatedb.pl,v 1.11 2000/10/20 03:18:38 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>) {
1.11      boris      67:        s/[
]//g;
1.5       boris      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.12    ! boris      81:        if (s/^(.*?)[:\.]\s*// && !$key) {
1.5       boris      82:            $key = $1;
1.12    ! boris      83:            $value=$_;
1.5       boris      84:            next;
1.1       boris      85:        }
1.5       boris      86:        if ($key) {
                     87:            $value .= $_;
                     88:            next;
1.1       boris      89:        }
1.5       boris      90:     }
                     91:     if ($key && $value) {
                     92:        $value = $dbh->quote($value);
                     93:        return ($key, $value);
                     94:     }
                     95:     return (0, 0);
1.1       boris      96: }
                     97: 
                     98: sub SelectGroup {
1.7       boris      99:     my ($dbh, $source, $TourName) = @_;
                    100:     my ($sth, $ParentId, $i, @arr);
1.5       boris     101:     
                    102:     $sth = $dbh->prepare("SELECT Id, Title FROM
1.1       boris     103:                Tournaments WHERE Type = 'Г'");
1.5       boris     104:     $sth->execute;
1.7       boris     105:     print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
1.5       boris     106:     while (@arr=$sth->fetchrow) {
                    107:        print "[$arr[0]] $arr[1]\n";
                    108:     }
                    109:     $ParentId = <STDIN>;
1.8       boris     110:     chomp $ParentId;
1.7       boris     111:     if (!$ParentId) {
                    112:        print "Пропускаем файл $source\n";
                    113:        print STDERR "Файл $source отвергнут оператором\n";
                    114:        return (0,0);
                    115:     } else {
                    116:        print "Вы выбрали турнир: $ParentId\n";
                    117:        $sth = $dbh->prepare("INSERT INTO Tournaments
                    118:                              (Title, Type, ParentId, FileName) 
                    119:                               VALUES ($TourName, 'Ч', $ParentId, 
1.8       boris     120:                                        $source)");
1.7       boris     121:        $sth->execute;
                    122:        my $TournamentId = $sth->{mysql_insertid};
                    123:        return ($TournamentId,$ParentId);
                    124:     }
                    125:                
                    126:     
1.1       boris     127: }
                    128: 
                    129: sub UpdateTournament {
1.5       boris     130:     my ($dbh, $TournamentId, $field, $value) = @_;
                    131:     $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
                    132:        or die $dbh->errstr;
1.1       boris     133: }
                    134: 
                    135: sub UpdateQuestion {
1.5       boris     136:     my ($dbh, $QuestionId, $field, $value) = @_;
                    137:     $dbh->do("UPDATE Questions SET $field=$value 
1.1       boris     138:                WHERE QuestionId=$QuestionId")
1.5       boris     139:        or die $dbh->errstr;
1.1       boris     140: }
                    141: 
1.7       boris     142: sub CheckFile {
1.10      boris     143:     my ($dbh, $source, $title) = @_;
1.7       boris     144:     my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
                    145:                              WHERE FileName=$source AND Type='Ч'");
                    146:     $sth->execute or die $dbh->errstr;
                    147:     my @arr = $sth->fetchrow;
                    148:     if (! scalar @arr) {
                    149:        return SelectGroup($dbh,$source,$title);
                    150:     }
                    151:     my($Id,$ParentId,$QuestionsNum)=@arr;
1.10      boris     152:     if($QuestionsNum) {        
1.7       boris     153:        print "Файл $source с данными $title уже существует. ",
                    154:        "Заменить?[y/N]\n";
                    155:        my $answer = <STDIN>;
                    156:        if ($answer !~ /^[yY]/) {
                    157:            return (0,0);
                    158:        } else {
1.10      boris     159:            DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
1.7       boris     160:        }
                    161:     } 
                    162:     return($Id,$ParentId);     
                    163: }
                    164: 
                    165: 
                    166: sub DeleteTournament {
1.10      boris     167:     my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
                    168:     if ($QuestionsNum) {
                    169:        UpdateParents($dbh,$ParentId,-$QuestionsNum);
                    170:     }
1.7       boris     171:     my (@Tours) = &GetTours($dbh, $Id);
                    172:     foreach my $Tour (@Tours) {
1.12    ! boris     173:        DeleteTournament($dbh,$Tour,$Id,0,1);
1.7       boris     174:     }
                    175:     my $sth = $dbh->prepare("DELETE FROM Questions
                    176:                              WHERE ParentId=$Id");
                    177:     $sth->execute or die $dbh->errstr;
                    178:     if($DeleteMyself) {
                    179:        $sth = $dbh->prepare("DELETE FROM Tournaments
                    180:                              WHERE Id=$Id");
1.9       boris     181:        $sth->execute or die $dbh->errstr;
1.7       boris     182:     }
                    183: }
1.8       boris     184: 
                    185: sub GetTours {
                    186:        my ($dbh, $ParentId) = @_;
                    187:        my (@arr, @Tours);
                    188: 
                    189:        my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
                    190:                WHERE ParentId=$ParentId ORDER BY Id");
                    191: 
                    192:        $sth->execute;
                    193: 
                    194:        while (@arr = $sth->fetchrow) {
                    195:                push @Tours, $arr[0];
                    196:        }
                    197: 
                    198:        return @Tours;
                    199: }
                    200: 
                    201: 
1.1       boris     202: MAIN: 
                    203: {
1.5       boris     204:     my($key, $value, $addition);
                    205:     
                    206:     my($source);
                    207:     
                    208:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                    209:        or die "Can't connect to DB chgk\n";
                    210:     
                    211:     while ($source = shift) {
                    212:        my($PlayedAt) = '';
                    213:        my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
                    214:        my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
                    215:        my (@d) = (localtime((stat($source))[9]))[5,4,3];
                    216:        $d[1]++;
                    217:        $d[0]+=1900;
                    218:        my ($CreatedAt) = $dbh->quote( join('-', @d));
1.6       boris     219: 
1.5       boris     220:        open INFD, $source 
                    221:            or die "Can't open input file: $!\n";
                    222:        
                    223:        $source =~ s/^.*\/([^\/]*)$/$1/;
                    224:        $source = $dbh->quote($source);
1.6       boris     225:        print STDERR "Файл: $source, дата: $CreatedAt ";
1.5       boris     226:        while (($key, $value) = getField(\*INFD, $dbh)) {
                    227:            last if (!$key);
                    228:            
                    229:            if ($key =~ /Мета/) {
1.7       boris     230:                next;   # This is obsolete
1.5       boris     231:            }
1.10      boris     232:            if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {               
1.7       boris     233:                ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
                    234:                if (!$TournamentId)  {
                    235:                    last;
1.10      boris     236:                }       
1.7       boris     237:                $sth = $dbh->prepare("UPDATE Tournaments SET
1.10      boris     238:                                     Title=$value, Type='Ч', 
1.7       boris     239:                                      ParentId=$ParentId, 
                    240:                                      FileName=$source, 
                    241:                                      CreatedAt=$CreatedAt
                    242:                                      WHERE
                    243:                                      Id=$TournamentId");
1.5       boris     244:                $sth->execute;
                    245:                next;
                    246:            }
                    247:            if ($key =~ /Тур/) {
                    248:                if ($TourId) {
                    249:                    $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                    250:                              WHERE Id=$TourId");
                    251:                }
                    252:                $qnum = 0;
                    253:                $sth = $dbh->prepare("INSERT INTO Tournaments
                    254:                                      (Title, Type, ParentId, CreatedAt) 
                    255:                                      VALUES ($value, 'Т', $TournamentId, 
                    256:                                       $CreatedAt)");
                    257:                $sth->execute;
                    258:                $TourId = $sth->{mysql_insertid};
                    259:                next;
                    260:            }
1.7       boris     261:            if ($key =~ /Вид/ || $key =~ /Тип/) {
1.5       boris     262:                $qtype = $value;
                    263:                $qtype =~ s/\'//g;
                    264:                next;
                    265:            }
                    266:            if ($key =~ /Вопрос/) {
1.11      boris     267:                if (!$TourId) {
                    268:                    $qnum = 0;
                    269:                    $sth = $dbh->prepare("INSERT INTO Tournaments
                    270:                                         (Title, Type, ParentId, CreatedAt) 
                    271:                                          VALUES ('1', 'Т', $TournamentId, 
                    272:                                           $CreatedAt)");
                    273:                    $sth->execute;
                    274:                    $TourId = $sth->{mysql_insertid};
                    275:                }
1.5       boris     276:                my $query = "INSERT INTO Questions 
                    277:                             (ParentId, Number, Type) 
                    278:                             VALUES ($TourId, $qnum+1, \'$qtype\')";
                    279:                $sth = $dbh->prepare($query);
                    280:                $sth->execute or print $query;;
                    281:                $QuestionId = $sth->{mysql_insertid};
                    282:                &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                    283:                $qnum++;
                    284:                $all_qnum++;
                    285:                next;
                    286:            }
1.6       boris     287: 
                    288:            if ($key =~ /Ответ/) {
                    289:                &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                    290:                next;
                    291:            }
1.7       boris     292: 
                    293:            if ($key =~ /Рейтинг/) {
                    294:                &UpdateQuestion($dbh, $QuestionId, "Rating", $value);
                    295:                next;
                    296:            }
1.5       boris     297:            
1.6       boris     298: 
                    299:            if ($key =~ /Автор/) {
                    300:                &UpdateQuestion($dbh, $QuestionId, "Authors", $value);
                    301:                next;
                    302:            }
1.5       boris     303:            
1.6       boris     304: 
                    305:            if ($key =~ /Источник/) {
                    306:                &UpdateQuestion($dbh, $QuestionId, "Sources", $value);
                    307:                next;
                    308:            }
1.5       boris     309:            
1.6       boris     310: 
                    311:            if ($key =~ /Комментари/) {
                    312:                &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
                    313:                next;
                    314:            }
1.5       boris     315:            
1.6       boris     316: 
1.7       boris     317:            if ($key =~ /URL/ || $key =~ /Ссылка/) {
1.6       boris     318:                &UpdateTournament($dbh, $TournamentId, "URL", $value);
                    319:                next;
                    320:            }
1.5       boris     321:            
1.6       boris     322: 
                    323:            if ($key =~ /Копирайт/) {
                    324:                &UpdateTournament($dbh, $TournamentId, "Copyright", $value);
                    325:                next;
                    326:            }
1.5       boris     327:            
1.6       boris     328: 
                    329:            if ($key =~ /Инфо/) {
                    330:                &UpdateTournament($dbh, $TournamentId, "Info", $value);
                    331:                next;
                    332:            }
1.5       boris     333:            
1.6       boris     334: 
                    335:            if ($key =~ /Редактор/) {
                    336:                &UpdateTournament($dbh, $TournamentId, "Editors", $value);
                    337:                next;
                    338:            }
1.5       boris     339:            
1.6       boris     340: 
                    341:            if ($key =~ /Обработан/) {
                    342:                &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
                    343:                next;
                    344:                              }
1.5       boris     345:            
                    346:            if ($key =~ /Дата/) {
                    347:                if ($TourId) {
                    348:                    &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
                    349:                } else {
                    350:                    &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
1.1       boris     351:                }
1.6       boris     352:                next;
1.5       boris     353:            }
1.6       boris     354:            
                    355:            #
                    356:            # If we are here, something got wrong!
                    357:            #
                    358:            print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
                    359:            
1.5       boris     360:        }
                    361:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
1.1       boris     362:                        WHERE Id=$TourId");
1.5       boris     363:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
1.1       boris     364:                        WHERE Id=$TournamentId");
1.5       boris     365:        &UpdateParents($dbh, $ParentId, $all_qnum);             
1.6       boris     366:        print STDERR "Всего вопросов: $all_qnum \n";
1.5       boris     367:     }
                    368:     $dbh->disconnect;
1.1       boris     369: }

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