#!/usr/local/bin/perl # Dilip Barman, http://www.cs.unc.edu/Courses/wwwp-f97/members/barman # Sep 21 1997 # # dbAddRecord.pl.cgi: As part of my perl programming project, described # at www.cs.unc.edu/Courses/wwwp-f97/members/barman/perlProjectProposal.html, # this perl cgi-bin script is called from the HTML generated in # dbProcessChoice.pl.cgi to add a record. All this does is inserts # the record into the database. Input parameters are set in STDIN: # . . . # to process the input. Using POST, these input variables are set in # STDIN: genre and whatToDo (set to either Enquire or AddRecord). # genre indicates the musical genre of interest (e.g., "Jazz - contemporary" # or "Rock"), whatToDo = Enquire means we are to return a list of music # in that genre, and whatToDo = AddRecord means we are to add a record. # We start off by including a nice library file by Steven Brenner which does # lots of things for us require "/afs/cs.unc.edu/project/courses/190-25f97/programs/perl/lib/cgi-lib.pl"; # GLOBAL VARIABLES $FALSE = 0; $TRUE = 1; $DBFILENAME = "../musicDatabase/musicDB"; # name of DBM database file $ROACCESS = "0555"; # Read-Only open: RWX, so U=101, G=101, O=101 $RWACCESS = "0777"; # Read-Write open: 111 111 111 # Remember to set AFS directory permissions so system:anyuser (a web # client, for example) has write privilege to this directory -- # fs setat ../musicDatabase system:anyuser write $RECDELIMSTART = "\*\$\["; # delimit record entries: *$[ ... ]$* $RECDELIMEND = "\]\$\*"; # * * * P R O C E S S U S E R I N P U T * * * # Get user input &ReadParse(*input); # Store input as attrib-value keys in variable input $title = $input{"title"}; $artist = $input{"artist"}; $format = $input{"format"}; $time = $input{"time"}; $comments = $input{"comments"}; # * * * T O P L E V E L H T M L S T U F F * * * # Okay, since this is run from the web, we have to first send back # http header information - and there is a library function to do it print &PrintHeader; print ""; print "Dilip\'s Music Database: Add Record Confirmation\n"; print "

Music Database: Add Record Confirmation

\n"; # Basic validation - if a title is provided, add the record if ($title eq "") { print "\n

ERROR: No Title

"; print "\n

You did not specify a title. The title is"; print "\n(the only field that is) required."; } else { &addToDB; } print &PrintVariables(%input); # * * * W R A P U P H T M L * * * # Give option to return to try again or return home print "\n

Go back to main screen"; print "to add other genre records or retrieve records

"; print "\n
"; print "\n

Read"; print "\n"; print "\nproject proposal or system"; print "\n"; print "\nimplementation notes

"; print "\n

Return to Dilip's course "; print""; print "home page

"; print "\n
Background courtesy of "; print "\nMoyra's Web Jewels
"; # End HTML page print "\n "; # * * * S U B R O U T I N E S * * * sub addToDB { # # addToDB subroutine # Sep 21 1997 # We have at least a title, and we add it to the database. # Because this is transaction-based and not a persistent # session, we must open and close the DB for each request. # So this subroutine opens the database (whose name is # defined above as global variable $DBFILENAME), creates # a string representing the value fields, and writes the # record. # # We use two more global variables, RECDELIMSTART and RECDELIMEND, # to build the values. $title is the key and since DBM databases # are 1-key 1-value structured, we build the value as a concatenation # of the individual values with delimiters. For example, with # the delimiters being *$[ and ]$*, a record might look like: # # key="The Eagle's Greatest Hits" # value="*$[Eagles]$**$[CD]$**$[48]$**$[A great CD from early 70s!]$*" # # There are other ways to have designed this, instead of a rigid # positional way, but again this is one quick way for purposes of # a short project (which isn't so short!). In any case, we build # a positional string with artist, format (again, in real life we # would just store a byte for each of the formats), time, and comments. # if (-e $DBFILENAME) {print "\n$DBFILENAME exists";} # else {print "\n$DBFILENAME does not exist";} $d1 = $RECDELIMSTART; $d2 = $RECDELIMEND; # reduce typing in formula below $x = $d1 . $artist . $d2 . $d1 . $format . $d2 . $d1 . $time . $d2 . $d1 . $comments . $d2; print "\n

Add record with:"; print "\n
KEY: $title\n
VALUE: $x"; # Open the file and check if key already exists. Otherwise, write record! dbmopen(%RECORDS, $DBFILENAME, $RWACCESS); print "open db!\n"; $newkey = TRUE; # assume it's a new entry if ($newkey) {print "\nTRUE: enter loop";} else {print "\nFALSE: enter loop";} # Above debug code works fine - $newkey is getting set to T. But ... # while ( (($key, $value)=each(%RECORDS)) && ($newkey) ) # Above didn't work - ?? So I'm being ineff. and looping all the way # while ( (($key, $value) = each(%RECORDS)) ) # { # if ($key eq $title) {$newkey = FALSE;} # print "\nLooking at title $key"; # } # ... this still isn't working! I don't get any "Looking at title" # print messages but just get "end loop" ... then, the # following if stmt generates no print msg for either path!! ???? foreach $key (keys %RECORDS) { print "

\nLooking at title $key"; } print "\nend loop"; if ($newkey) {$RECORDS{$title} = $x; print "\n

Added new title";} else { print "\n

duplicate key!"; } # end else - i.e., duplicate key dbmclose(%RECORDS); print "close DB! \n"; } # End addToDB subroutine