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