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