#!/usr/local/bin/perl -w

=head1 NAME

updatem.pl - a script for creation of new database. 

=head1 SYNOPSIS

updatem.pl [B<-m> I<metafile>]


=head1 DESCRIPTION

Upadets metainformation in the B<chgk> databse. Uses file
    L<./meta> unless [B<-m>] option is used

=head1 BUGS

The database, user and password are hardcoded. 

=head1 AUTHOR

Dmitry Rubinstein

=head1 $Id: updatem.pl,v 1.1 2000/10/17 01:34:16 boris Exp $

=cut

use DBI;

MAIN: 
{
    
	my($source) = 'meta';
	my($depth, @depthId);
	my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
		or die "Can't connect to DB chgk\n";

	open INFD, $source or die "Can't open input file: $!\n";
	while (<INFD>) {
		s/^\[(\d*)\]\t//;
		$MetaId = $1;
		s/^(\t*)//;
		chomp;
		$depth = length($1);
		$title = $dbh->quote($_);
		$ParentId = ($depth) ? $depthId[$depth - 1] : 0;
		my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments WHERE
			MetaId=$MetaId");
		$sth->execute;
		if ($sth->fetchrow) {
			print "$title is already in the DB!\n";
			next;
		}
		$sth = $dbh->prepare("INSERT INTO Tournaments
			(Title, ParentId, MetaId, Type) 
			VALUES ($title, $ParentId, $MetaId, 'į')");
		$sth->execute;
		$Id = $sth->{insertid};
		$depthId[$depth] = $Id;
	}
	$dbh->disconnect;
}
