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