File: tcl-app6.htm

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (106 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<HTML><HEAD>
<TITLE>Examples -- Example 5: desctable</TITLE>
<LINK rel=Previous href="tcl-app5.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="tcl-app7.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
  <TR>
    <TD ALIGN=LEFT>
      <A NAME="topofpage"></A> <IMG  SRC="as-c-sm.gif">
    </TD>
    <TD ALIGN=RIGHT>
      <A href="tcl-app5.htm"><IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
      <A href=toc.htm> <IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
      <A href=master.htm> <IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
      <A href="tcl-app7.htm"> <IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
      <A name="7983"> </A>
    </TD>
  </TR>
</TABLE>

<a name="18380">
</a><h3>Example 5: desctable</h3>
<p><a name="18839">
</a>The following example script provides a request procedure which describes the columns of a database table using the AOLserver "ns_tableinfo" command .</p>
<p><a name="18382">
</a>This example can be found in the <code>examples/tcl/desctabl.tcl</code> file.</p>
<pre>    <a name="18383"></a>
    <a name="18853"></a># Example 5: Describing a database table
    <a name="18858"></a>#
    <a name="18859"></a># /example/describetable prints out a column-by-column
    <a name="18860"></a>#   description of a database table.  The database 
    <a name="18861"></a>#   pool name and table name are specified at the end 
    <a name="18862"></a>#   of the URL -- e.g.,
    <a name="18863"></a>#
    <a name="18864"></a>#       /example/describetable/nsdbpool/ns_users
    <a name="18865"></a>#
    <a name="18866"></a># Note: You must have the ns_db module loaded into your virtual
    <a name="18867"></a>#       server for this example to work.
    <a name="18868"></a>#
    <a name="18869"></a># Things to notice:
    <a name="18870"></a>#
    <a name="18871"></a># * ns_returnbadrequest returns a nicely formatted message
    <a name="18872"></a>#   telling the client they submitted an invalid request.
    <a name="18873"></a>#
    <a name="18874"></a># * "ns_conn urlv" returns a Tcl array whose elements are the
    <a name="18875"></a>#   slash-delimited parts of the URL.
    <a name="18876"></a>#
    <a name="18877"></a># * The describetable function loops through all the columns
    <a name="18878"></a>#   and uses "ns_column valuebyindex" to get the type of each
    <a name="18879"></a>#   one.
    <a name="18880"></a>#
    <a name="18881"></a># * ns_returnnotice nicely formats the return value.
    <a name="18882"></a>
    <a name="18883"></a>ns_register_proc GET /example/describetable describetable
    <a name="18884"></a>
    <a name="18885"></a>proc describetable {conn ignore} {
    <a name="18886"></a>    if {[ns_conn urlc $conn] != 4} {
    <a name="18887"></a>	return [ns_returnbadrequest $conn \
    <a name="18888"></a>		"Missing table name and/or poolname"]
    <a name="18889"></a>    }
    <a name="18890"></a>    set pool [lindex [ns_conn urlv $conn] 2]
    <a name="18891"></a>    if {[lsearch $pool [ns_db pools]] == -1} {
    <a name="18892"></a>	return [ns_returnbadrequest $conn \
    <a name="18893"></a>		"Pool $pool does not exist"]
    <a name="18894"></a>    }
    <a name="18895"></a>    set db [ns_db gethandle $pool]
    <a name="18896"></a>    set table [lindex [ns_conn urlv $conn] 3]
    <a name="18897"></a>    set tinfo [ns_table info $db $table]
    <a name="18898"></a>    if {$tinfo == ""} {
    <a name="18899"></a>	return [ns_returnbadrequest $conn \
    <a name="18900"></a>		"Table $table does not exist"]
    <a name="18901"></a>    }
    <a name="18902"></a>    set output "&lt;dl&gt;"
    <a name="18903"></a>    set size [ns_column count $tinfo]
    <a name="18904"></a>    for {set i 0} {$i &lt; $size} {incr i} {
    <a name="18905"></a>	append output "&lt;dt&gt;[ns_column name $tinfo $i] \
    <a name="18906"></a>                &lt;dd&gt;[ns_column typebyindex $tinfo $i]&lt;/dd&gt;"
    <a name="18907"></a>    }
    <a name="18908"></a>    append output "&lt;/dl&gt;&lt;hr&gt;"
    <a name="18909"></a>    ns_returnnotice $conn 200 "Table $table in pool $pool" $output
    <a name="18910"></a>}
    <a name="18911"></a>
</pre><p>

<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="tcl-app5.htm">
<IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tcl-app7.htm">
<IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright &copy; 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>