Please send questions to st10@humboldt.edu .
#!/usr/local/bin/perl -w

# ^NOTE --- I had to change the perl path on sorrel above
#    so this would work!!!

#######################################################
# lab12_table_setup.pl
#
# quick'n'sleazy table setup for the Week 12 Lab
#    Exercise and HW #10 -
# creates the book reviewer tables.
#
# by Sharon Tuttle
#
# last modified: 11-09-04
#######################################################

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser carpout);

use DBI;

warningsToBrowser(1);  # activate warnings

# PRINT a form for user to react to

print header, "\n";

print start_html(-title=>'book review setup'), "\n";

print h1('book review setup'), "\n";

print start_form, "\n",
      br, br, "\n",

      "Oracle username: \n", 
      textfield(-name=>'oracle_username',
                -size=>15,
                -maxlength=>15), "\n",
      
      br, br, "\n",

      "Oracle password: \n",
      password_field(-name=>'oracle_password',
                     -size=>10,
                     -maxlength=>10), "\n",

      br, br, "\n",

      submit(-value=>'Create/Recreate tables'), "\n",
      endform, "\n";

print hr, "\n";

# if user has hit submit, try to connect to the database and
#    show the specified table's contents

if (param())
{
    # set environment variables needed for Oracle database
    #    software

    $ENV{'ORACLE_HOME'} = '/apps1/oracle/product/9iAS/';
    $ENV{'LD_LIBRARY_PATH'} = '/apps1/oracle/product/9iAS/lib';

    # try to connect to database named student reachable with the
    #    above environment informaton, for the account username and
    #    account password given, for an Oracle database
    # (and remember --- what you connect, REMEMBER TO DISCONNECT!!!

    my $username = param('oracle_username')
        or die "Must enter an Oracle username!\n";

    my $password = param('oracle_password')
        or die "Must enter an Oracle password!\n";        

    my $db_handle = DBI->connect('dbi:Oracle:student',
                    "$username", "$password")
        || die "Database connection not made: $DBI::errstr";

    # try to drop and create the book review scenario
    #    tables

    # this will fail the first time --- we DON'T want
    #    the script to die. BUT you may see warnings.
    $db_handle->do("drop table book cascade constraints");

    $db_handle->do("create table book
                    (book_id           char(5),
                     book_author_lname varchar2(20),
                     book_title        varchar2(50),
                     book_type         varchar2(15),
                     primary key       (book_id))");

    $db_handle->do("drop table reviewer cascade constraints");

    $db_handle->do("create table reviewer
                    (reviewer_id      char(3),
                     reviewer_lname   varchar2(20),
                     reviewer_email   varchar2(30),
                     primary key     (reviewer_id))");

    $db_handle->do("drop table review cascade constraints");

    $db_handle->do("create table review
                    (review_id         char(4),
                     book_id           char(5),
                     reviewer_id       char(3),
                     review_date       date,
                     review_rating     int check(review_rating
                                                 between 1 and 5),
                     review_text       varchar2(30),
                     primary key       (review_id),
                     foreign key       (book_id) references book,
                     foreign key       (reviewer_id) references reviewer)");

    # let's show the names of the tables now in their database
    my $stmt_handle = $db_handle->prepare( 'select table_name
                                            from user_tables' )
        or die $db_handle->errstr . "\n";

    $stmt_handle->execute();

    my $next_tbl;

    print hr, "\n";
    while ($next_tbl = $stmt_handle->fetchrow_array())
    {
        print "$next_tbl", br, "\n";
    }

    $stmt_handle->finish;
    print hr, "\n";

    # DON'T FORGET THIS! Not only is it POOR STYLE to do so, but
    #   could cause REAL Oracle performance problems if you leave
    #   it off!!!!!!!!!!!!!

    $db_handle->disconnect;

}

print end_html, "\n";

# end of lab12_table_setup.pl