Annotation of db/prgsrc/mkdb.pl, revision 1.9

1.1       boris       1: #!/usr/local/bin/perl -w
                      2: 
                      3: =head1 NAME
                      4: 
                      5: mkdb.pl - a script for creation of new database. 
                      6: 
1.2       boris       7: =head1 SYNOPSIS
                      8: 
                      9: mkdb.pl
                     10: 
                     11: 
1.1       boris      12: =head1 DESCRIPTION
                     13: 
                     14: This script will create tables Questions and Tournaments
                     15: in the B<chgk> databse. If the tables exist, it will ask user whether
                     16: new tables should be created.
                     17: 
                     18: =head1 BUGS
                     19: 
                     20: The database, user and password are hardcoded. 
                     21: 
                     22: =head1 AUTHOR
                     23: 
                     24: Dmitry Rubinstein
                     25: 
1.9     ! boris      26: =head1 $Id: mkdb.pl,v 1.8 2000/11/11 00:23:20 boris Exp $
1.1       boris      27: 
                     28: =cut
                     29: 
                     30: 
                     31: use DBI;
                     32: use strict;
                     33: my (@tbl_list, $dbh);
                     34: 
                     35: sub CheckTable
                     36: {
                     37:        my ($TabName) = @_;
                     38:        my ($ans);
                     39: 
                     40:        if (scalar(grep(/^$TabName$/, @tbl_list))) {
                     41:                print "Table $TabName exists. Do you want to delete it? ";
                     42:                $ans = <STDIN>;
                     43:                if ($ans =~ /[yY]/) {
                     44:                        $dbh->do("DROP TABLE $TabName");
                     45:                        print "deleted table $TabName\n";
                     46:                } else {
                     47:                        exit;
                     48:                }
                     49:        }
                     50: }
                     51: 
                     52: MAIN:
                     53: {
                     54: print "Before connecting to the DB\n";
                     55: 
                     56:        $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
                     57:                or die "Can't connect to DB chgk\n" . $dbh->errstr;
                     58: print "Connected successfully\n";
                     59: 
                     60:        @tbl_list = $dbh->func( '_ListTables' );
                     61: 
                     62:        &CheckTable("Questions");
                     63:        $dbh->do("CREATE TABLE Questions (
                     64:                QuestionId      INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                     65:                        KEY QuestionIdKey (QuestionId),
                     66:                ParentId        SMALLINT UNSIGNED NOT NULL,
                     67:                        KEY ParentIdKey (ParentId),
1.8       boris      68:                Number         SMALLINT UNSIGNED NOT NULL,
1.1       boris      69:                        KEY NumberKey (Number),
1.3       boris      70:                Type            TEXT NOT NULL,
1.1       boris      71:                Question        TEXT,
1.7       boris      72:                Answer          TEXT,
                     73:                Authors         TEXT,
                     74:                Sources         TEXT,
1.4       boris      75:                Comments        TEXT,
                     76:                 Rating          TEXT
1.1       boris      77:        )")
                     78:        or die "Can't create Questions table: $!\n";
                     79:        
                     80:        &CheckTable("Tournaments");
                     81: 
                     82:        $dbh->do("CREATE TABLE Tournaments (
                     83:                Id                      INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                     84:                        KEY IdKey (Id),
                     85:                ParentId        INT UNSIGNED NOT NULL,
                     86:                        KEY ParentIdKey (ParentId),
                     87:                Title           TINYTEXT NOT NULL,
1.9     ! boris      88:                 Number    SMALLINT UNSIGNED NOT NULL,
1.1       boris      89:                QuestionsNum INT UNSIGNED DEFAULT 0,
1.6       boris      90:                Type            ENUM('ç','ô','þ'),
1.1       boris      91:                Copyright       TEXT,
                     92:                Info                    TEXT,
                     93:                URL                     TINYTEXT,
                     94:                FileName        CHAR(25),
1.7       boris      95:                 Editors         TEXT,
                     96:                 EnteredBy       TEXT,
1.1       boris      97:                PlayedAt        DATE,
                     98:                CreatedAt       DATE NOT NULL
                     99:        )") 
                    100:        or die "Can't create Tournaments table: $!\n";
                    101: 
                    102: #      CREATE INDEX ParentInd ON Tournaments (ParentId)
                    103: #      CREATE UNIQUE INDEX IdInd ON Tournaments (Id)
                    104:        
                    105:        $dbh->disconnect;
                    106: }      

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