File:  [Local Repository] / db / prgsrc / updatedb.pl
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Wed Oct 18 02:01:19 2000 UTC (23 years, 7 months ago) by boris
Branches: MAIN
CVS tags: HEAD
Added . as a field separator.
Added extra logging facilites.

    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: 
   25: =head1 $Id: updatedb.pl,v 1.6 2000/10/18 02:01:19 boris Exp $
   26: 
   27: =cut
   28: 
   29: my (%RevMonths) = 
   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');
   39: my ($sth);
   40: 
   41: use vars qw($/);
   42: 
   43: 
   44: 
   45: use DBI;
   46: use strict;
   47: 
   48: sub UpdateParents {
   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:     }
   60: }
   61: 
   62: sub getField {
   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: 	
   81: 	if (/^(.*)[:\.]\s*$/ && !$key) {
   82: 	    $key = $1;
   83: 	    next;
   84: 	}
   85: 	if ($key) {
   86: 	    $value .= $_;
   87: 	    next;
   88: 	}
   89:     }
   90:     if ($key && $value) {
   91: 	$value = $dbh->quote($value);
   92: 	return ($key, $value);
   93:     }
   94:     return (0, 0);
   95: }
   96: 
   97: sub SelectGroup {
   98:     my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;
   99:     
  100:     $sth = $dbh->prepare("SELECT Id, Title FROM
  101: 		Tournaments WHERE Type = 'Г'");
  102:     $sth->execute;
  103:     print "Выберите группу для турнира:\n$TourName\n\n";
  104:     while (@arr=$sth->fetchrow) {
  105: 	print "[$arr[0]] $arr[1]\n";
  106:     }
  107:     $ParentId = <STDIN>;
  108:     print "Вы выбрали турнир: $ParentId\n";
  109:     return $ParentId;
  110: }
  111: 
  112: sub UpdateTournament {
  113:     my ($dbh, $TournamentId, $field, $value) = @_;
  114:     $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
  115: 	or die $dbh->errstr;
  116: }
  117: 
  118: sub UpdateQuestion {
  119:     my ($dbh, $QuestionId, $field, $value) = @_;
  120:     $dbh->do("UPDATE Questions SET $field=$value 
  121: 		WHERE QuestionId=$QuestionId")
  122: 	or die $dbh->errstr;
  123: }
  124: 
  125: MAIN: 
  126: {
  127:     my($key, $value, $addition);
  128:     
  129:     my($source);
  130:     
  131:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
  132: 	or die "Can't connect to DB chgk\n";
  133:     
  134:     while ($source = shift) {
  135: 	my($PlayedAt) = '';
  136: 	my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
  137: 	my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
  138: 	my (@d) = (localtime((stat($source))[9]))[5,4,3];
  139: 	$d[1]++;
  140: 	$d[0]+=1900;
  141: 	my ($CreatedAt) = $dbh->quote( join('-', @d));
  142: 
  143: 	open INFD, $source 
  144: 	    or die "Can't open input file: $!\n";
  145: 	
  146: 	$source =~ s/^.*\/([^\/]*)$/$1/;
  147: 	$source = $dbh->quote($source);
  148: 	print STDERR "Файл: $source, дата: $CreatedAt ";
  149: 	
  150: 	while (($key, $value) = getField(\*INFD, $dbh)) {
  151: 	    last if (!$key);
  152: 	    
  153: 	    if ($key =~ /Мета/) {
  154: 		$value =~ s/[^\d]*//g;
  155: 		$sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE
  156: 					MetaId=$value");
  157: 		$sth->execute 
  158: 		    or die "Invalid Meta field: $value";
  159: 		$ParentId = ($sth->fetchrow)[0];
  160: 		next;
  161: 	    }
  162: 	    if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
  163: 		$ParentId = &SelectGroup($dbh, $value)
  164: 		    unless ($ParentId);
  165: 		$sth = $dbh->prepare("INSERT INTO Tournaments
  166: 				     (Title, Type, ParentId, FileName, 
  167:                                       CreatedAt) 
  168: 				      VALUES ($value, 'Ч', $ParentId, 
  169:                                        $source, $CreatedAt)");
  170: 		$sth->execute;
  171: 		$TournamentId = $sth->{mysql_insertid};
  172: 		next;
  173: 	    }
  174: 	    if ($key =~ /Тур/) {
  175: 		if ($TourId) {
  176: 		    $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
  177: 			      WHERE Id=$TourId");
  178: 		}
  179: 		$qnum = 0;
  180: 		$qtype = 'Ч';
  181: 		$sth = $dbh->prepare("INSERT INTO Tournaments
  182: 				      (Title, Type, ParentId, CreatedAt) 
  183: 				      VALUES ($value, 'Т', $TournamentId, 
  184:                                       $CreatedAt)");
  185: 		$sth->execute;
  186: 		$TourId = $sth->{mysql_insertid};
  187: 		next;
  188: 	    }
  189: 	    if ($key =~ /Вид/) {
  190: 		$qtype = $value;
  191: 		$qtype =~ s/\'//g;
  192: 		next;
  193: 	    }
  194: 	    if ($key =~ /Вопрос/) {
  195: 		my $query = "INSERT INTO Questions 
  196: 			     (ParentId, Number, Type) 
  197: 			     VALUES ($TourId, $qnum+1, \'$qtype\')";
  198: 		$sth = $dbh->prepare($query);
  199: 		$sth->execute or print $query;;
  200: 		$QuestionId = $sth->{mysql_insertid};
  201: 		&UpdateQuestion($dbh, $QuestionId, "Question", $value);
  202: 		$qnum++;
  203: 		$all_qnum++;
  204: 		next;
  205: 	    }
  206: 
  207: 	    if ($key =~ /Ответ/) {
  208: 		&UpdateQuestion($dbh, $QuestionId, "Answer", $value);
  209: 		next;
  210: 	    }
  211: 	    
  212: 
  213: 	    if ($key =~ /Автор/) {
  214: 		&UpdateQuestion($dbh, $QuestionId, "Authors", $value);
  215: 		next;
  216: 	    }
  217: 	    
  218: 
  219: 	    if ($key =~ /Источник/) {
  220: 		&UpdateQuestion($dbh, $QuestionId, "Sources", $value);
  221: 		next;
  222: 	    }
  223: 	    
  224: 
  225: 	    if ($key =~ /Комментари/) {
  226: 		&UpdateQuestion($dbh, $QuestionId, "Comments", $value);
  227: 		next;
  228: 	    }
  229: 	    
  230: 
  231: 	    if ($key =~ /URL/ || key =~ /Ссылка/) {
  232: 		&UpdateTournament($dbh, $TournamentId, "URL", $value);
  233: 		next;
  234: 	    }
  235: 	    
  236: 
  237: 	    if ($key =~ /Копирайт/) {
  238: 		&UpdateTournament($dbh, $TournamentId, "Copyright", $value);
  239: 		next;
  240: 	    }
  241: 	    
  242: 
  243: 	    if ($key =~ /Инфо/) {
  244: 		&UpdateTournament($dbh, $TournamentId, "Info", $value);
  245: 		next;
  246: 	    }
  247: 	    
  248: 
  249: 	    if ($key =~ /Редактор/) {
  250: 		&UpdateTournament($dbh, $TournamentId, "Editors", $value);
  251: 		next;
  252: 	    }
  253: 	    
  254: 
  255: 	    if ($key =~ /Обработан/) {
  256: 		&UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
  257: 		next;
  258: 			      }
  259: 	    
  260: 	    if ($key =~ /Дата/) {
  261: 		if ($TourId) {
  262: 		    &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
  263: 		} else {
  264: 		    &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
  265: 		}
  266: 		next;
  267: 	    }
  268: 	    
  269: 	    #
  270: 	    # If we are here, something got wrong!
  271: 	    #
  272: 	    print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
  273: 	    
  274: 	}
  275: 	$dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
  276: 			WHERE Id=$TourId");
  277: 	$dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
  278: 			WHERE Id=$TournamentId");
  279: 	&UpdateParents($dbh, $ParentId, $all_qnum);		
  280: 	print STDERR "Всего вопросов: $all_qnum \n";
  281:     }
  282:     $dbh->disconnect;
  283: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>