1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >PL/Tcl Functions and Arguments</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
15 TITLE="PL/Tcl - Tcl Procedural Language"
16 HREF="pltcl.html"><LINK
18 TITLE="PL/Tcl - Tcl Procedural Language"
19 HREF="pltcl.html"><LINK
21 TITLE="Data Values in PL/Tcl"
22 HREF="pltcl-data.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
67 >Chapter 38. PL/Tcl - Tcl Procedural Language</TD
81 HREF="pltcl-data.html"
95 NAME="PLTCL-FUNCTIONS"
96 >38.2. PL/Tcl Functions and Arguments</A
99 > To create a function in the <SPAN
102 > language, use the standard syntax:
105 CLASS="PROGRAMLISTING"
106 >CREATE FUNCTION <VAR
116 # PL/Tcl function body
117 ' LANGUAGE pltcl;</PRE
123 > is the same, except that the language has to be specified as
130 > The body of the function is simply a piece of Tcl script.
131 When the function is called, the argument values are passed as
142 Tcl script. The result is returned
143 from the Tcl code in the usual way, with a <TT
150 > For example, a function
151 returning the greater of two integer values could be defined as:
154 CLASS="PROGRAMLISTING"
155 >CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS '
156 if {$1 > $2} {return $1}
158 ' LANGUAGE pltcl STRICT;</PRE
164 >, which saves us from
165 having to think about null input values: if a null value is passed, the
166 function will not be called at all, but will just return a null
167 result automatically.
170 > In a nonstrict function,
171 if the actual value of an argument is null, the corresponding
178 > variable will be set to an empty string.
179 To detect whether a particular argument is null, use the function
183 >. For example, suppose that we wanted <CODE
187 with one null and one nonnull argument to return the nonnull
188 argument, rather than null:
191 CLASS="PROGRAMLISTING"
192 >CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS '
194 if {[argisnull 2]} { return_null }
197 if {[argisnull 2]} { return $1 }
198 if {$1 > $2} {return $1}
200 ' LANGUAGE pltcl;</PRE
205 to return a null value from a PL/Tcl function, execute
209 >. This can be done whether the
210 function is strict or not.
213 > Composite-type arguments are passed to the function as Tcl
214 arrays. The element names of the array are the attribute names
215 of the composite type. If an attribute in the passed row has the
216 null value, it will not appear in the array. Here is an example:
219 CLASS="PROGRAMLISTING"
220 >CREATE TABLE employee (
226 CREATE FUNCTION overpaid(employee) RETURNS boolean AS '
227 if {200000.0 < $1(salary)} {
230 if {$1(age) < 30 && 100000.0 < $1(salary)} {
234 ' LANGUAGE pltcl;</PRE
238 > There is currently no support for returning a composite-type
247 SUMMARY="Footer navigation table"
276 HREF="pltcl-data.html"
286 >PL/Tcl - Tcl Procedural Language</TD
300 >Data Values in PL/Tcl</TD