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

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.6     ! boris      25: =head1 $Id: updatedb.pl,v 1.5 2000/10/18 01:52:16 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/;
                     73:                my($month) = $RevMonths{$2};
                     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.5       boris      98:     my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;
                     99:     
                    100:     $sth = $dbh->prepare("SELECT Id, Title FROM
1.1       boris     101:                Tournaments WHERE Type = 'Г'");
1.5       boris     102:     $sth->execute;
                    103:     print "Выберите группу для турнира:\n$TourName\n\n";
                    104:     while (@arr=$sth->fetchrow) {
                    105:        print "[$arr[0]] $arr[1]\n";
                    106:     }
                    107:     $ParentId = <STDIN>;
                    108:     print "Вы выбрали турнир: $ParentId\n";
                    109:     return $ParentId;
1.1       boris     110: }
                    111: 
                    112: sub UpdateTournament {
1.5       boris     113:     my ($dbh, $TournamentId, $field, $value) = @_;
                    114:     $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
                    115:        or die $dbh->errstr;
1.1       boris     116: }
                    117: 
                    118: sub UpdateQuestion {
1.5       boris     119:     my ($dbh, $QuestionId, $field, $value) = @_;
                    120:     $dbh->do("UPDATE Questions SET $field=$value 
1.1       boris     121:                WHERE QuestionId=$QuestionId")
1.5       boris     122:        or die $dbh->errstr;
1.1       boris     123: }
                    124: 
                    125: MAIN: 
                    126: {
1.5       boris     127:     my($key, $value, $addition);
                    128:     
                    129:     my($source);
                    130:     
                    131:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                    132:        or die "Can't connect to DB chgk\n";
                    133:     
                    134:     while ($source = shift) {
                    135:        my($PlayedAt) = '';
                    136:        my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
                    137:        my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
                    138:        my (@d) = (localtime((stat($source))[9]))[5,4,3];
                    139:        $d[1]++;
                    140:        $d[0]+=1900;
                    141:        my ($CreatedAt) = $dbh->quote( join('-', @d));
1.6     ! boris     142: 
1.5       boris     143:        open INFD, $source 
                    144:            or die "Can't open input file: $!\n";
                    145:        
                    146:        $source =~ s/^.*\/([^\/]*)$/$1/;
                    147:        $source = $dbh->quote($source);
1.6     ! boris     148:        print STDERR "Файл: $source, дата: $CreatedAt ";
1.5       boris     149:        
                    150:        while (($key, $value) = getField(\*INFD, $dbh)) {
                    151:            last if (!$key);
                    152:            
                    153:            if ($key =~ /Мета/) {
                    154:                $value =~ s/[^\d]*//g;
                    155:                $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE
1.1       boris     156:                                        MetaId=$value");
1.5       boris     157:                $sth->execute 
                    158:                    or die "Invalid Meta field: $value";
                    159:                $ParentId = ($sth->fetchrow)[0];
                    160:                next;
                    161:            }
1.6     ! boris     162:            if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
1.5       boris     163:                $ParentId = &SelectGroup($dbh, $value)
                    164:                    unless ($ParentId);
                    165:                $sth = $dbh->prepare("INSERT INTO Tournaments
                    166:                                     (Title, Type, ParentId, FileName, 
                    167:                                       CreatedAt) 
                    168:                                      VALUES ($value, 'Ч', $ParentId, 
                    169:                                        $source, $CreatedAt)");
                    170:                $sth->execute;
                    171:                $TournamentId = $sth->{mysql_insertid};
                    172:                next;
                    173:            }
                    174:            if ($key =~ /Тур/) {
                    175:                if ($TourId) {
                    176:                    $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                    177:                              WHERE Id=$TourId");
                    178:                }
                    179:                $qnum = 0;
                    180:                $qtype = 'Ч';
                    181:                $sth = $dbh->prepare("INSERT INTO Tournaments
                    182:                                      (Title, Type, ParentId, CreatedAt) 
                    183:                                      VALUES ($value, 'Т', $TournamentId, 
                    184:                                       $CreatedAt)");
                    185:                $sth->execute;
                    186:                $TourId = $sth->{mysql_insertid};
                    187:                next;
                    188:            }
                    189:            if ($key =~ /Вид/) {
                    190:                $qtype = $value;
                    191:                $qtype =~ s/\'//g;
                    192:                next;
                    193:            }
                    194:            if ($key =~ /Вопрос/) {
                    195:                my $query = "INSERT INTO Questions 
                    196:                             (ParentId, Number, Type) 
                    197:                             VALUES ($TourId, $qnum+1, \'$qtype\')";
                    198:                $sth = $dbh->prepare($query);
                    199:                $sth->execute or print $query;;
                    200:                $QuestionId = $sth->{mysql_insertid};
                    201:                &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                    202:                $qnum++;
                    203:                $all_qnum++;
                    204:                next;
                    205:            }
1.6     ! boris     206: 
        !           207:            if ($key =~ /Ответ/) {
        !           208:                &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
        !           209:                next;
        !           210:            }
1.5       boris     211:            
1.6     ! boris     212: 
        !           213:            if ($key =~ /Автор/) {
        !           214:                &UpdateQuestion($dbh, $QuestionId, "Authors", $value);
        !           215:                next;
        !           216:            }
1.5       boris     217:            
1.6     ! boris     218: 
        !           219:            if ($key =~ /Источник/) {
        !           220:                &UpdateQuestion($dbh, $QuestionId, "Sources", $value);
        !           221:                next;
        !           222:            }
1.5       boris     223:            
1.6     ! boris     224: 
        !           225:            if ($key =~ /Комментари/) {
        !           226:                &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
        !           227:                next;
        !           228:            }
1.5       boris     229:            
1.6     ! boris     230: 
        !           231:            if ($key =~ /URL/ || key =~ /Ссылка/) {
        !           232:                &UpdateTournament($dbh, $TournamentId, "URL", $value);
        !           233:                next;
        !           234:            }
1.5       boris     235:            
1.6     ! boris     236: 
        !           237:            if ($key =~ /Копирайт/) {
        !           238:                &UpdateTournament($dbh, $TournamentId, "Copyright", $value);
        !           239:                next;
        !           240:            }
1.5       boris     241:            
1.6     ! boris     242: 
        !           243:            if ($key =~ /Инфо/) {
        !           244:                &UpdateTournament($dbh, $TournamentId, "Info", $value);
        !           245:                next;
        !           246:            }
1.5       boris     247:            
1.6     ! boris     248: 
        !           249:            if ($key =~ /Редактор/) {
        !           250:                &UpdateTournament($dbh, $TournamentId, "Editors", $value);
        !           251:                next;
        !           252:            }
1.5       boris     253:            
1.6     ! boris     254: 
        !           255:            if ($key =~ /Обработан/) {
        !           256:                &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
        !           257:                next;
        !           258:                              }
1.5       boris     259:            
                    260:            if ($key =~ /Дата/) {
                    261:                if ($TourId) {
                    262:                    &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
                    263:                } else {
                    264:                    &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
1.1       boris     265:                }
1.6     ! boris     266:                next;
1.5       boris     267:            }
1.6     ! boris     268:            
        !           269:            #
        !           270:            # If we are here, something got wrong!
        !           271:            #
        !           272:            print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
        !           273:            
1.5       boris     274:        }
                    275:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
1.1       boris     276:                        WHERE Id=$TourId");
1.5       boris     277:        $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
1.1       boris     278:                        WHERE Id=$TournamentId");
1.5       boris     279:        &UpdateParents($dbh, $ParentId, $all_qnum);             
1.6     ! boris     280:        print STDERR "Всего вопросов: $all_qnum \n";
1.5       boris     281:     }
                    282:     $dbh->disconnect;
1.1       boris     283: }

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