Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
41.2. PL/Tcl Functions and Arguments
Prev UpChapter 41. PL/Tcl - Tcl Procedural LanguageHome Next

41.2. PL/Tcl Functions and Arguments

To create a function in thePL/Tcl language, use the standardCREATE FUNCTION syntax:

CREATE FUNCTIONfuncname (argument-types) RETURNSreturn-type AS $$    # PL/Tcl function body$$ LANGUAGE pltcl;

PL/TclU is the same, except that the language has to be specified aspltclu.

The body of the function is simply a piece of Tcl script. When the function is called, the argument values are passed as variables$1 ...$n to the Tcl script. The result is returned from the Tcl code in the usual way, with areturn statement.

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 clauseSTRICT, which saves us from having to think about null input values: if a null value is passed, the function will not be called at all, but will just return a null result automatically.

In a nonstrict function, if the actual value of an argument is null, the corresponding$n variable will be set to an empty string. To detect whether a particular argument is null, use the functionargisnull. For example, suppose that we wantedtcl_max with one null and one nonnull argument to return the nonnull argument, rather than null:

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, executereturn_null. This can be done whether the function is strict or not.

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 aPL/Tcl function as returning a domain type.


Prev Up Next
41.1. Overview Home 41.3. Data Values in PL/Tcl
epubpdf
Go to PostgreSQL 9.5
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp