--- db/prgsrc/updateindex.pl 2000/10/19 01:06:18 1.3 +++ db/prgsrc/updateindex.pl 2000/10/19 01:56:44 1.4 @@ -39,7 +39,7 @@ createindex.pl(1) Boris Veytsman -=head1 $Id: updateindex.pl,v 1.3 2000/10/19 01:06:18 boris Exp $ +=head1 $Id: updateindex.pl,v 1.4 2000/10/19 01:56:44 boris Exp $ =cut @@ -82,42 +82,70 @@ MAIN: } s/^\s*//; s/\s$//; - my $title = $dbh->quote($_); + my $title = $_; my $ParentId = ($depth) ? $depthId[$depth - 1] : 0; - my $sth; - my $type; - if ($filename) { - $type=$dbh->quote('Ч'); - $filename = $dbh->quote($filename); - $sth = $dbh->prepare("SELECT Id FROM Tournaments - WHERE FileName=$filename"); - $sth->execute; - if ($sth->fetchrow) { - print "$filename is already in the DB!\n"; - next; - } - $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, ParentId, FileName, Type) - VALUES ($title, $ParentId, $filename, $type)"); - $sth->execute; - } else { - $type=$dbh->quote('Г'); - $sth = $dbh->prepare("SELECT Id FROM Tournaments - WHERE Title=$title"); - - $sth->execute; - if ($sth->fetchrow) { - print "$title is already in the DB!\n"; - next; - } - $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, ParentId, Type) - VALUES ($title, $ParentId, $type)"); - $sth->execute; - my $Id = $sth->{'mysql_insertid'}; - $depthId[$depth] = $Id; + my $Id = CheckId($dbh,$title,$ParentId,$filename); + if (!$Id || $filename) { + next; } + $depthId[$depth] = $Id; } $dbh->disconnect; } + + +sub CheckId { + my ($dbh,$title,$ParentId,$filename) = @_; + my $type; + my $key; + my $value; + my $Id = 0; + if ($filename) { + $type=$dbh->quote('Ч'); + $key = "FileName"; + $value = $dbh->quote($filename); + } else { + $type=$dbh->quote('Г'); + $key = "Title"; + $value = $dbh->quote($title); + } + $title=$dbh->quote($title); + my $sth = $dbh->prepare("SELECT Id FROM Tournaments + WHERE $key=$value"); + $sth->execute or die $dbh->errstr; + my @arr = $sth->fetchrow; + if (scalar @arr) { + print "$value is already in the DB!\n"; + print "Заменить новым значением? [y/N]\n"; + my $answer = ; + if ($answer !~ /^[yY]/) { + return 0; + } else { + $Id = $arr[0]; + } + } + if ($Id) { + $sth = $dbh->prepare("UPDATE Tournaments + SET Title=$title, ParentId=$ParentId, + Type=$type + WHERE Id=$Id"); + + } else { + $sth = $dbh->prepare("INSERT INTO Tournaments + (Title, ParentId, Type) + VALUES + ($title, $ParentId,$type)"); + } + $sth->execute or die $dbh->errstr; + if (!$Id) { + $Id = $sth->{'mysql_insertid'}; + } + if ($filename) { + $filename=$dbh->quote($filename); + $sth = $dbh->prepare("UPDATE Tournaments + SET FileName=$filename + WHERE Id=$Id"); + } + return $Id; +}