Postgresql 中文操作指南
4.3. Calling Functions #
PostgreSQL允许使用_positional_或_named_表示法调用具有命名参数的函数。对于具有大量参数的函数,命名表示法特别有用,因为它使参数和实际参数之间的关联更加明确和可靠。在位置表示法中,一个函数调用是用其参数值编写的,其顺序与在函数声明中定义的顺序相同。在命名表示法中,通过名称将参数与函数参数匹配,并且可以按任何顺序编写。对于每种表示法,还要考虑 Section 10.3中记录的函数参数类型的影响。
PostgreSQL allows functions that have named parameters to be called using either positional or named notation. Named notation is especially useful for functions that have a large number of parameters, since it makes the associations between parameters and actual arguments more explicit and reliable. In positional notation, a function call is written with its argument values in the same order as they are defined in the function declaration. In named notation, the arguments are matched to the function parameters by name and can be written in any order. For each notation, also consider the effect of function argument types, documented in Section 10.3.
在这两种符号中,无须在函数声明中写出具有默认值的任何参数。但在命名符号中,这尤其有用,因为可以省略任何参数组合;而在位置符号中,只能从右到左省略参数。
In either notation, parameters that have default values given in the function declaration need not be written in the call at all. But this is particularly useful in named notation, since any combination of parameters can be omitted; while in positional notation parameters can only be omitted from right to left.
PostgreSQL 还支持 mixed 符号,它结合了位置符号和命名符号。在这种情况下,先写出位置参数,然后写出命名参数。
PostgreSQL also supports mixed notation, which combines positional and named notation. In this case, positional parameters are written first and named parameters appear after them.
以下示例将说明所有三种符号的用法,并采用以下函数定义:
The following examples will illustrate the usage of all three notations, using the following function definition:
CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase boolean DEFAULT false)
RETURNS text
AS
$$
SELECT CASE
WHEN $3 THEN UPPER($1 || ' ' || $2)
ELSE LOWER($1 || ' ' || $2)
END;
$$
LANGUAGE SQL IMMUTABLE STRICT;
函数_concat_lower_or_upper_有两个强制参数,a_和_b。此外,还有一个可选参数_uppercase_,其默认值是_false_。将_a_和_b_输入连接,并根据_uppercase_参数强制转换为大写或小写。此函数定义的其余详细信息在此处并不重要(有关更多信息,请参阅 Chapter 38)。
Function concat_lower_or_upper has two mandatory parameters, a and b. Additionally there is one optional parameter uppercase which defaults to false. The a and b inputs will be concatenated, and forced to either upper or lower case depending on the uppercase parameter. The remaining details of this function definition are not important here (see Chapter 38 for more information).
4.3.1. Using Positional Notation #
位置符号是将参数传递给 PostgreSQL 中函数的传统机制。示例如下:
Positional notation is the traditional mechanism for passing arguments to functions in PostgreSQL. An example is:
SELECT concat_lower_or_upper('Hello', 'World', true);
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
所有参数都按顺序指定。结果为大写字符,因为 uppercase 被指定为 true。另一个示例如下:
All arguments are specified in order. The result is upper case since uppercase is specified as true. Another example is:
SELECT concat_lower_or_upper('Hello', 'World');
concat_lower_or_upper
-----------------------
hello world
(1 row)
在此,省略了 uppercase 参数,所以它接收其 false,导致结果为小写字符。在位置符号中,只要有默认值,就可以从右到左省略参数。
Here, the uppercase parameter is omitted, so it receives its default value of false, resulting in lower case output. In positional notation, arguments can be omitted from right to left so long as they have defaults.
4.3.2. Using Named Notation #
在命名符号中,使用 ⇒ 指定每个参数的名称,以将其与参数表达式分开。例如:
In named notation, each argument’s name is specified using ⇒ to separate it from the argument expression. For example:
SELECT concat_lower_or_upper(a => 'Hello', b => 'World');
concat_lower_or_upper
-----------------------
hello world
(1 row)
同样,参数 uppercase 已被省略,因此它被隐式设为 false。使用命名符号的一个优点是参数可以按任意顺序指定,例如:
Again, the argument uppercase was omitted so it is set to false implicitly. One advantage of using named notation is that the arguments may be specified in any order, for example:
SELECT concat_lower_or_upper(a => 'Hello', b => 'World', uppercase => true);
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
SELECT concat_lower_or_upper(a => 'Hello', uppercase => true, b => 'World');
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
基于 ":=" 的较旧语法受支持,出于向后兼容性的目的:
An older syntax based on ":=" is supported for backward compatibility:
SELECT concat_lower_or_upper(a := 'Hello', uppercase := true, b := 'World');
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
4.3.3. Using Mixed Notation #
混合符号结合了位置符号和命名符号。但是,如前所述,命名参数不能位于位置参数之前。例如:
The mixed notation combines positional and named notation. However, as already mentioned, named arguments cannot precede positional arguments. For example:
SELECT concat_lower_or_upper('Hello', 'World', uppercase => true);
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
在上文中,参数 a 和 b 是按位置指定的,而 uppercase 是按名称指定的。在此示例中,只有文档除外。如果函数较复杂,具有大量的参数且具有默认值,那么,命名符号或混合符号可以节省大量的书写,并降低出错的几率。
In the above query, the arguments a and b are specified positionally, while uppercase is specified by name. In this example, that adds little except documentation. With a more complex function having numerous parameters that have default values, named or mixed notation can save a great deal of writing and reduce chances for error.