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