Annotation of db/prgsrc/updatedb.pl, revision 1.21
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.21 ! boris 25: =head1 $Id: updatedb.pl,v 1.20 2001/07/21 23:59: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.21 ! boris 232: 'Источник' => 'Sources',
! 233: 'Тема' => 'Topic');
1.13 boris 234:
235:
1.5 boris 236: my($source);
237:
238: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
239: or die "Can't connect to DB chgk\n";
240:
241: while ($source = shift) {
1.17 boris 242: my $TourNum=0;
1.5 boris 243: my($PlayedAt) = '';
244: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
1.14 boris 245: my($tournum, $qnum, $all_qnum) = (0, 0, 0);
1.5 boris 246: my (@d) = (localtime((stat($source))[9]))[5,4,3];
247: $d[1]++;
248: $d[0]+=1900;
249: my ($CreatedAt) = $dbh->quote( join('-', @d));
1.6 boris 250:
1.5 boris 251: open INFD, $source
252: or die "Can't open input file: $!\n";
253:
254: $source =~ s/^.*\/([^\/]*)$/$1/;
255: $source = $dbh->quote($source);
1.6 boris 256: print STDERR "Файл: $source, дата: $CreatedAt ";
1.13 boris 257: my %TourDefaults=('CreatedAt'=>$CreatedAt);
258: my %QuestionDefaults=();
1.14 boris 259: my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));
1.5 boris 260: while (($key, $value) = getField(\*INFD, $dbh)) {
261: last if (!$key);
262:
263: if ($key =~ /Мета/) {
1.7 boris 264: next; # This is obsolete
1.5 boris 265: }
1.10 boris 266: if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
1.7 boris 267: ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
268: if (!$TournamentId) {
269: last;
1.10 boris 270: }
1.7 boris 271: $sth = $dbh->prepare("UPDATE Tournaments SET
1.10 boris 272: Title=$value, Type='Ч',
1.7 boris 273: ParentId=$ParentId,
274: FileName=$source,
275: CreatedAt=$CreatedAt
276: WHERE
277: Id=$TournamentId");
1.5 boris 278: $sth->execute;
279: next;
280: }
281: if ($key =~ /Тур/) {
282: if ($TourId) {
283: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
284: WHERE Id=$TourId");
285: }
286: $qnum = 0;
1.17 boris 287: $TourNum++;
288: $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
289: \%TourDefaults);
1.13 boris 290: %QuestionDefaults=%QuestionGlobalDefaults;
291: $QuestionId=0;
292: next;
1.5 boris 293: }
294: if ($key =~ /Вопрос/) {
1.11 boris 295: if (!$TourId) {
296: $qnum = 0;
1.19 boris 297: $TourNum++;
298: $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
1.13 boris 299: \%TourDefaults);
300: %QuestionDefaults=%QuestionGlobalDefaults;
1.11 boris 301: }
1.5 boris 302: my $query = "INSERT INTO Questions
1.14 boris 303: (ParentId, Number)
304: VALUES ($TourId, $qnum+1)";
1.5 boris 305: $sth = $dbh->prepare($query);
306: $sth->execute or print $query;;
307: $QuestionId = $sth->{mysql_insertid};
308: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
1.13 boris 309: while (my ($key,$value)=each %QuestionDefaults) {
310: &UpdateQuestion($dbh, $QuestionId, $key, $value);
311: }
1.5 boris 312: $qnum++;
313: $all_qnum++;
314: next;
315: }
1.6 boris 316:
317: if ($key =~ /Ответ/) {
318: &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
319: next;
320: }
1.7 boris 321:
1.6 boris 322: if ($key =~ /Комментари/) {
323: &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
324: next;
325: }
1.5 boris 326:
1.15 boris 327: my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
1.6 boris 328:
1.13 boris 329: if (scalar @Fields) {
330: my $word = shift @Fields;
331: my $field = $QuestionFields{$word};
332: if ($QuestionId) {
333: &UpdateQuestion($dbh, $QuestionId, $field, $value);
334: } elsif ($TourId) {
335: $QuestionDefaults{$field}=$value;
336: } else {
337: $QuestionGlobalDefaults{$field}=$value;
338: }
1.6 boris 339: next;
340: }
341:
1.15 boris 342: @Fields = grep { $key =~ /$_/ } keys %TourFields;
1.6 boris 343:
1.13 boris 344: if (scalar @Fields) {
345: my $word = shift @Fields;
346: my $field = $TourFields{$word};
347: if ($QuestionId) {
348: print STDERR "ОШИБКА: $key $value недопустимы после",
349: " начала вопросов\n";
350: } elsif ($TourId) {
351: &UpdateTournament($dbh, $TourId, $field, $value);
1.5 boris 352: } else {
1.13 boris 353: &UpdateTournament($dbh, $TournamentId, $field, $value);
354: $TourDefaults{$field}=$value;
1.1 boris 355: }
1.6 boris 356: next;
1.5 boris 357: }
1.13 boris 358:
1.6 boris 359:
360: #
361: # If we are here, something got wrong!
362: #
363: print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
364:
1.5 boris 365: }
366: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
1.1 boris 367: WHERE Id=$TourId");
1.5 boris 368: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
1.1 boris 369: WHERE Id=$TournamentId");
1.5 boris 370: &UpdateParents($dbh, $ParentId, $all_qnum);
1.6 boris 371: print STDERR "Всего вопросов: $all_qnum \n";
1.5 boris 372: }
373: $dbh->disconnect;
1.1 boris 374: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>