package dbchgk; use DBI; use Exporter; use locale; use vars qw(@ISA @EXPORT); @ISA=qw(Exporter); @EXPORT = qw(&getbase &closebase &getrow $z &in2out &getall &out2in &mydo &getequalto &forbidden &getquestion &checktable); my $z; my $qbase; BEGIN {do "chgk.cnf"; $qbase = DBI -> connect ("DBI:mysql:$base",'piataev',''); }; sub getbase { my $a=join(", ",@_); $z= $qbase -> prepare("select $a FROM Questions WHERE QuestionId<=$qnumber"); $z -> execute; } sub getquestion { my $a=shift; $z= $qbase -> prepare("select Question, Answer, Comments FROM Questions WHERE QuestionId=$a"); $z -> execute; $z -> fetchrow; } sub getequalto { $z= $qbase -> prepare("select first, second FROM equalto"); $z -> execute; my %h; while ( my ($first, $second)=$z -> fetchrow) { $h{$first}=$second; } $z -> finish; %h; } sub closebase { $z -> finish; $qbase -> disconnect; } sub getrow { $z -> fetchrow } sub mydo { $qbase -> do (shift); } sub getall { $z -> fetchall_arrayref; } sub in2out { $qid=shift; $z= $qbase -> prepare ( "select t2.Id, t2.Number, t3.FileName from Questions AS t1, Tournaments AS t2 , Tournaments AS t3 where (t1.QuestionId = $qid) && (t1.ParentId = t2.Id) && (t2.ParentId = t3.Id) "); $z -> execute; ($tourid, $tourname, $filename)= $z -> fetchrow; $z= $qbase -> prepare("select QuestionId from Questions WHERE ParentId = $tourid"); $z -> execute; my $i; for ($i=1; ($q= $z->fetchrow) && $q!=$qid; $i++){}; $_=lc $_; $filename=~s/\.txt$//i; "$filename\.$tourname\.$i"; } sub out2in { @q= split(/\./, lc shift); $q[0].='.txt'; # $z= $qbase -> prepare ( "select q.QuestionId from Questions as q, Tournaments as t1, Tournaments as t2 where (t2.FileName= \"$q[0]\") && (t1.ParentId = t2.Id) && (q.ParentId = t1.Id) && (t1.Number=\"$q[1]\") "); $z -> execute; # ($tourid)=$z -> fetchrow or die "Bad identifier". join (".", @q); # print "--$tourid--"; # $z= $qbase -> prepare("select QuestionId from questions WHERE ParentId = $tourid"); my $i; $z -> execute; for ($i=1; $i <= $q[2]; $i++){@qq= $z->fetchrow}; $z -> finish; $qq[0]; } sub forbidden { keys %getequalto } sub checktable { my ($TabName) = @_; my ($ans); if (scalar(grep(/^$TabName$/, &tablelist))) { print "Table $TabName exists. Do you want to delete it? "; $ans = ; if ($ans =~ /[yY]/) { $qbase->do("DROP TABLE $TabName"); print "deleted table $TabName\n"; return 0; } else { return 1 } } 0 } sub tablelist { $qbase->func( '_ListTables' ); } 1;