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

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.3     ! boris      25: =head1 $Id: updatedb.pl,v 1.2 2000/10/17 01:54:16 boris Exp boris $
1.1       boris      26: 
                     27: =cut
                     28: 
                     29: my (%RevMonths) = 
                     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:         'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',
                     34:         'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8', 
                     35:         'Окт', '9', 'Ноя', '19', 'Дек', '11');
                     36: my ($sth);
                     37: 
1.3     ! boris      38: use vars qw($/);
        !            39: 
        !            40: $/=/\n
*/;
        !            41: 
1.1       boris      42: use DBI;
                     43: use strict;
                     44: 
                     45: sub UpdateParents {
                     46:        my ($dbh, $ParentId, $all_qnum) = @_;
                     47:        if ($ParentId) {
                     48:                my ($sth1) = $dbh->prepare("
                     49:                        SELECT QuestionsNum, ParentId FROM Tournaments WHERE Id = $ParentId
                     50:                ");
                     51:                $sth1->execute;
                     52:                my ($q, $p) = ($sth1->fetchrow)[0, 1];
                     53:                $dbh->do("
                     54:                        UPDATE Tournaments SET QuestionsNum=$q + $all_qnum WHERE Id =
                     55:                        $ParentId");
                     56:                &UpdateParents($dbh, $p, $all_qnum);
                     57:        }
                     58: }
                     59: 
                     60: sub getField {
                     61:        my($desc, $dbh) = @_;
                     62:        my($key);
                     63:        my($value) = ('');
                     64:        while (<$desc>) {
                     65:                s/
//;
                     66:                if ($key && /^\s*$/) {
                     67:                        chomp $value;
                     68:                        chomp $key;
                     69:                        if ($key eq 'Дата') {
                     70:                                $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
                     71:                                my($month) = $RevMonths{$2};
                     72:                                $value =~ s/$2/$month/;
                     73:                        }
                     74:                        $value = $dbh->quote($value);
                     75:                        return ($key, $value);
                     76:                }
                     77:                next if (/^\s*$/);
                     78: 
                     79:                if (/^(.*):\s*$/ && !$key) {
                     80:                        $key = $1;
                     81:                        next;
                     82:                }
                     83:                if ($key) {
                     84:                        $value .= $_;
                     85:                        next;
                     86:                }
                     87:        }
                     88:        if ($key && $value) {
                     89:                $value = $dbh->quote($value);
                     90:                return ($key, $value);
                     91:        }
                     92:        return (0, 0);
                     93: }
                     94: 
                     95: sub SelectGroup {
                     96:        my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;
                     97: 
                     98:        $sth = $dbh->prepare("SELECT Id, Title FROM
                     99:                Tournaments WHERE Type = 'Г'");
                    100:        $sth->execute;
                    101:        print "Выберите группу для турнира:\n$TourName\n\n";
                    102:        for ($i = 0; $i < $sth->numrows; $i++) {
                    103:                @arr = $sth->fetchrow;
                    104:                print "[$arr[0]] $arr[1]\n";
                    105:        }
                    106:        $ParentId = <STDIN>;
                    107:        print "Вы выбрали турнир: $ParentId\n";
                    108:        return $ParentId;
                    109: }
                    110: 
                    111: sub UpdateTournament {
                    112:        my ($dbh, $TournamentId, $field, $value) = @_;
                    113:        $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
                    114:                or die $dbh->errstr;
                    115: }
                    116: 
                    117: sub UpdateQuestion {
                    118:        my ($dbh, $QuestionId, $field, $value) = @_;
                    119:        $dbh->do("UPDATE Questions SET $field=$value 
                    120:                WHERE QuestionId=$QuestionId")
                    121:                        or die $dbh->errstr;
                    122: }
                    123: 
                    124: MAIN: 
                    125: {
                    126:        my($key, $value, $addition);
                    127: 
                    128:        my($source);
                    129: 
                    130:        my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                    131:                or die "Can't connect to DB chgk\n";
                    132: 
                    133:        while ($source = shift) {
                    134:                my($PlayedAt) = '';
                    135:                my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
                    136:                my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
                    137:                my (@d) = (localtime((stat($source))[9]))[5,4,3];
                    138:                $d[1]++;
                    139:                my ($CreatedAt) = $dbh->quote( join('-', @d));
                    140:                print "File created on: $CreatedAt\n";
                    141:        
                    142:                open INFD, $source 
                    143:                        or die "Can't open input file: $!\n";
                    144:        
                    145:                $source =~ s/^.*\/([^\/]*)$/$1/;
                    146:                $source = $dbh->quote($source);
                    147:                print "Processing file: $source \n";
                    148: 
                    149:                while (($key, $value) = getField(\*INFD, $dbh)) {
                    150:                        last if (!$key);
                    151:        
                    152:                        if ($key =~ /Мета/) {
                    153:                                $value =~ s/[^\d]*//g;
                    154:                                $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE
                    155:                                        MetaId=$value");
                    156:                                $sth->execute 
                    157:                                        or die "Invalid Meta field: $value";
                    158:                                $ParentId = ($sth->fetchrow)[0];
                    159:                                next;
                    160:                        }
                    161:                        if ($key =~ /Чемпионат/) {
                    162:                                $ParentId = &SelectGroup($dbh, $value)
                    163:                                        unless ($ParentId);
                    164:                                $sth = $dbh->prepare("INSERT INTO Tournaments
                    165:                                        (Title, Type, ParentId, FileName, CreatedAt) 
                    166:                                        VALUES ($value, 'Ч', $ParentId, $source,
                    167:                                        $CreatedAt)");
                    168:                                $sth->execute;
1.2       boris     169:                                $TournamentId = $sth->{mysql_insertid};
1.1       boris     170:                                next;
                    171:                        }
                    172:                        if ($key =~ /Тур/) {
                    173:                                if ($TourId) {
                    174:                                        $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                    175:                                                WHERE Id=$TourId");
                    176:                                }
                    177:                                $qnum = 0;
1.3     ! boris     178:                                $qtype = "Ч";
1.1       boris     179:                                $sth = $dbh->prepare("INSERT INTO Tournaments
                    180:                                        (Title, Type, ParentId, CreatedAt) 
                    181:                                        VALUES ($value, 'Т', $TournamentId, $CreatedAt)");
                    182:                                $sth->execute;
1.2       boris     183:                                $TourId = $sth->{mysql_insertid};
1.1       boris     184:                                next;
                    185:                        }
                    186:                        if ($key =~ /Вид/) {
                    187:                                $qtype = $value;
                    188:                                next;
                    189:                        }
                    190:                        if ($key =~ /Вопрос/) {
                    191:                                $sth = $dbh->prepare("INSERT INTO Questions 
                    192:                                        (ParentId, Number, Type) 
                    193:                                        VALUES ($TourId, $qnum+1, $qtype)");
1.3     ! boris     194:                                $sth->execute or print "Problem at $qnum";
1.2       boris     195:                                $QuestionId = $sth->{mysql_insertid};
1.1       boris     196:                                &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                    197:                                $qnum++;
                    198:                                $all_qnum++;
                    199:                                next;
                    200:                        }
                    201:                        &UpdateQuestion($dbh, $QuestionId, "Answer", $value) 
                    202:                                if ($key =~ /Ответ/);
                    203:        
                    204:                        &UpdateQuestion($dbh, $QuestionId, "Authors", $value) 
                    205:                                if ($key =~ /Автор/);
                    206:        
                    207:                        &UpdateQuestion($dbh, $QuestionId, "Sources", $value) 
                    208:                                if ($key =~ /Источник/);
                    209:        
                    210:                        &UpdateQuestion($dbh, $QuestionId, "Comments", $value) 
                    211:                                if ($key =~ /Комментарий/);
                    212:        
                    213:                        &UpdateTournament($dbh, $TournamentId, "URL", $value)
                    214:                                if ($key =~ /URL/);
                    215: 
                    216:                        &UpdateTournament($dbh, $TournamentId, "Copyright", $value)
                    217:                                if ($key =~ /Копирайт/);
                    218: 
                    219:                        &UpdateTournament($dbh, $TournamentId, "Info", $value)
                    220:                                if ($key =~ /Инфо/);
                    221: 
                    222:                        if ($key =~ /Дата/) {
                    223:                                if ($TourId) {
                    224:                                        &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
                    225:                                } else {
                    226:                                        &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
                    227:                                }
                    228:                        }
                    229:                }
                    230:                $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                    231:                        WHERE Id=$TourId");
                    232:                $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
                    233:                        WHERE Id=$TournamentId");
                    234:                &UpdateParents($dbh, $ParentId, $all_qnum);             
                    235:        }
                    236:        $dbh->disconnect;
                    237: }

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