1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
>PL/Tcl Functions and Arguments</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.1beta1 Documentation"
13
HREF="index.html"><LINK
15
TITLE="PL/Tcl - Tcl Procedural Language"
16
HREF="pltcl.html"><LINK
19
HREF="pltcl-overview.html"><LINK
21
TITLE="Data Values in PL/Tcl"
22
HREF="pltcl-data.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2011-04-27T21:20:33"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.1beta1 Documentation</A
57
HREF="pltcl-overview.html"
66
TITLE="PL/Tcl - Tcl Procedural Language"
74
>Chapter 40. PL/Tcl - Tcl Procedural Language</TD
80
TITLE="PL/Tcl - Tcl Procedural Language"
89
TITLE="Data Values in PL/Tcl"
90
HREF="pltcl-data.html"
104
NAME="PLTCL-FUNCTIONS"
105
>40.2. PL/Tcl Functions and Arguments</A
108
> To create a function in the <SPAN
113
HREF="sql-createfunction.html"
118
CLASS="PROGRAMLISTING"
135
# PL/Tcl function body
136
$$ LANGUAGE pltcl;</PRE
142
> is the same, except that the language has to be specified as
149
> The body of the function is simply a piece of Tcl script.
150
When the function is called, the argument values are passed as
163
Tcl script. The result is returned
164
from the Tcl code in the usual way, with a <TT
171
> For example, a function
172
returning the greater of two integer values could be defined as:
175
CLASS="PROGRAMLISTING"
176
>CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
177
if {$1 > $2} {return $1}
179
$$ LANGUAGE pltcl STRICT;</PRE
185
>, which saves us from
186
having to think about null input values: if a null value is passed, the
187
function will not be called at all, but will just return a null
188
result automatically.
191
> In a nonstrict function,
192
if the actual value of an argument is null, the corresponding
201
> variable will be set to an empty string.
202
To detect whether a particular argument is null, use the function
206
>. For example, suppose that we wanted <CODE
210
with one null and one nonnull argument to return the nonnull
211
argument, rather than null:
214
CLASS="PROGRAMLISTING"
215
>CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
217
if {[argisnull 2]} { return_null }
220
if {[argisnull 2]} { return $1 }
221
if {$1 > $2} {return $1}
223
$$ LANGUAGE pltcl;</PRE
228
to return a null value from a PL/Tcl function, execute
232
>. This can be done whether the
233
function is strict or not.
236
> Composite-type arguments are passed to the function as Tcl
237
arrays. The element names of the array are the attribute names
238
of the composite type. If an attribute in the passed row has the
239
null value, it will not appear in the array. Here is an example:
242
CLASS="PROGRAMLISTING"
243
>CREATE TABLE employee (
249
CREATE FUNCTION overpaid(employee) RETURNS boolean AS $$
250
if {200000.0 < $1(salary)} {
253
if {$1(age) < 30 && 100000.0 < $1(salary)} {
257
$$ LANGUAGE pltcl;</PRE
261
> There is currently no support for returning a composite-type
262
result value, nor for returning sets.
268
> does not currently have full support for
269
domain types: it treats a domain the same as the underlying scalar
270
type. This means that constraints associated with the domain will
271
not be enforced. This is not an issue for function arguments, but
272
it is a hazard if you declare a <SPAN
276
as returning a domain type.
284
SUMMARY="Footer navigation table"
295
HREF="pltcl-overview.html"
313
HREF="pltcl-data.html"
337
>Data Values in PL/Tcl</TD
b'\\ No newline at end of file'