# /www/seia/examples-basics/quotations-pseudo-code.txt
# philg@mit.edu
# any script should start out at the top with a comment or some other
# specification at the top of what query data can be expected to
# come in with the request. In this case we expect that this
# script can be invoked via /quotations (no extra info) or
# /quotations?order_by=date
# Before you can run this program, you must create the quotations table
# in the RDBMS. To do this, run your RDBMS shell client (e.g., SQL*Plus
# for Oracle) and enter the following table definition:
# create table quotations (
# quotation_id integer primary key,
# insertion_date date,
# author_name varchar(100) not null,
# category varchar(100),
# quote varchar(4000)
# );
set page_content "
Quotations
Quotations
"
# Now check "order_by" and create a link (in the variable "option")
# that the user can click on to choose the order in which the
# quotes will be listed. The ordering will be determined by the SQL
# query (in the variable "sql_query"), which is used below to extract rows
# from the table.
# here we use the AOLserver API calls ns_queryexists and ns_queryget
# that tell us whether "order_by" was included in the URI
if { [ns_queryexists "order_by"] && [ns_queryget "order_by"] == "date" } {
set sql_query "select * from quotations order by insertion_date"
# sadly we don't have abstract URLs working on a raw AOLserver so we
# need to link to an explicit .tcl extension
set option "sort by category"
} else {
set sql_query "select * from quotations order by category"
set option "sort by insertion date"
}
# This next command is part of the Arsdigita Database Access API.
# db_foreach performs the query specified by $sql_query. The name
# "get_quotes" is a logical name for the sql_query. For each row that
# is returned by the query, db_foreach will perform the commands
# within the first code block. If no rows are returned by the query,
# then the commands within the code block following "if_no_rows". See
# http://philip.greenspun.com/teaching/manuals/db-api/ for a brief
# overview of the database API, and
# http://www.arsdigita.com/doc/core-arch-guide/database-access-api for
# even more of the gory details.
db_foreach get_quotes $sql_query {
append page_content "- $author_name: \"$quote\"\n"
} if_no_rows {
append page_content "there are no quotations in the database right now"
}
# db_release_unused_handles releases any unused database handles. The
# database API automatically allocates database handles when they are
# needed. This procedure will be called automatically when the page is
# finished executing, but on a high-volume site you always want to
# release explicitly after the last time you access the database on a
# page. Why? You don't want to be holding onto a database connection,
# a scarce resource, while writing bytes out to the browser. Suppose
# that the user is in Croatia on a 9600 baud modem. It might take 15
# minutes to deliver a big page to him or her. If you have configured 4
# database connections and you have four users like this you'll have
# launched a denial of service attack on your own server! In other
# words nobody else will be able to get a page if that page requires the
# RDBMS.
db_release_unused_handles
append page_content "
$option
Add a Quotation
superstudent@greatschool.edu
"
# this is the AOLserver API call to return a page to the user
# note that we have to explicitly specify the HTTP status code
# (200 or "OK") and the MIME type of "text/html"
ns_return 200 text/html $page_content