|
|
The following example script provides a request procedure which describes the columns of a database table using the AOLserver "ns_tableinfo" command .
This example can be found in the examples/tcl/desctabl.tcl file.
# Example 5: Describing a database table
#
# /example/describetable prints out a column-by-column
# description of a database table. The database
# pool name and table name are specified at the end
# of the URL -- e.g.,
#
# /example/describetable/nsdbpool/ns_users
#
# Note: You must have the ns_db module loaded into your virtual
# server for this example to work.
#
# Things to notice:
#
# * ns_returnbadrequest returns a nicely formatted message
# telling the client they submitted an invalid request.
#
# * "ns_conn urlv" returns a Tcl array whose elements are the
# slash-delimited parts of the URL.
#
# * The describetable function loops through all the columns
# and uses "ns_column valuebyindex" to get the type of each
# one.
#
# * ns_returnnotice nicely formats the return value.
ns_register_proc GET /example/describetable describetable
proc describetable {conn ignore} {
if {[ns_conn urlc $conn] != 4} {
return [ns_returnbadrequest $conn \
"Missing table name and/or poolname"]
}
set pool [lindex [ns_conn urlv $conn] 2]
if {[lsearch $pool [ns_db pools]] == -1} {
return [ns_returnbadrequest $conn \
"Pool $pool does not exist"]
}
set db [ns_db gethandle $pool]
set table [lindex [ns_conn urlv $conn] 3]
set tinfo [ns_table info $db $table]
if {$tinfo == ""} {
return [ns_returnbadrequest $conn \
"Table $table does not exist"]
}
set output "<dl>"
set size [ns_column count $tinfo]
for {set i 0} {$i < $size} {incr i} {
append output "<dt>[ns_column name $tinfo $i] \
<dd>[ns_column typebyindex $tinfo $i]</dd>"
}
append output "</dl><hr>"
ns_returnnotice $conn 200 "Table $table in pool $pool" $output
}