|
|
| · · · · · · · | |
pgAdmin 1.4 online documentation37.2. PL/Tcl Functions and ArgumentsTo create a function in the PL/Tcl language, use the standard CREATE FUNCTION syntax: CREATE FUNCTION
PL/TclU is the same, except that the language has to be specified as
The body of the function is simply a piece of Tcl script.
When the function is called, the argument values are passed as
variables For example, a function returning the greater of two integer values could be defined as: CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
if {$1 > $2} {return $1}
return $2
$$ LANGUAGE pltcl STRICT;
Note the clause In a nonstrict function,
if the actual value of an argument is null, the corresponding
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
if {[argisnull 1]} {
if {[argisnull 2]} { return_null }
return $2
}
if {[argisnull 2]} { return $1 }
if {$1 > $2} {return $1}
return $2
$$ LANGUAGE pltcl;
As shown above,
to return a null value from a PL/Tcl function, execute
Composite-type arguments are passed to the function as Tcl arrays. The element names of the array are the attribute names of the composite type. If an attribute in the passed row has the null value, it will not appear in the array. Here is an example: CREATE TABLE employee (
name text,
salary integer,
age integer
);
CREATE FUNCTION overpaid(employee) RETURNS boolean AS $$
if {200000.0 < $1(salary)} {
return "t"
}
if {$1(age) < 30 && 100000.0 < $1(salary)} {
return "t"
}
return "f"
$$ LANGUAGE pltcl;
There is currently no support for returning a composite-type result value, nor for returning sets. PL/Tcl does not currently have full support for domain types: it treats a domain the same as the underlying scalar type. This means that constraints associated with the domain will not be enforced. This is not an issue for function arguments, but it is a hazard if you declare a PL/Tcl function as returning a domain type. |