バージョンごとのドキュメント一覧

4.3. 関数呼び出し #

<title>Calling Functions</title>

<productname>PostgreSQL</productname> allows functions that have named parameters to be called using either <firstterm>positional</firstterm> or <firstterm>named</firstterm> 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 <xref linkend="typeconv-func"/>. PostgreSQLでは名前付きパラメータを持つ関数について、位置表記と名前付け表記のいずれでも呼び出すことが可能です。 名前付け表記は、パラメータと引数の関連をより明確・確実にするので、多数のパラメータを持つ関数において特に有用です。 位置表記の関数呼び出しでは、関数宣言で定義されたのと同じ並び順で、引数を記述します。 名前付け表記では、引数と関数パラメータは名前で対応付けられ、引数はどのような並び順で書いても構いません。 それぞれの表記で、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. どちらの表記でも、関数定義時にデフォルト値を与えられているパラメータについては、呼び出し時に記述される必要はありません。 しかしこれは、名前付け表記で特に有用です。 なぜなら、パラメータ群の任意の組み合わせを省略できるからです。 一方、位置表記のパラメータは右から左へ省略していくことしかできません。

<productname>PostgreSQL</productname> also supports <firstterm>mixed</firstterm> notation, which combines positional and named notation. In this case, positional parameters are written first and named parameters appear after them. PostgreSQLでは、名前付け表記と位置表記の混在表記もサポートしています。この場合、位置表記のパラメータが最初に記述され、その後に名前付け表記のパラメータが記述されることになります。

The following examples will illustrate the usage of all three notations, using the following function definition: 本節の例では、次の関数定義を使って、3通りすべての表記方法について説明します。

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;

Function <function>concat_lower_or_upper</function> has two mandatory parameters, <literal>a</literal> and <literal>b</literal>. Additionally there is one optional parameter <literal>uppercase</literal> which defaults to <literal>false</literal>. The <literal>a</literal> and <literal>b</literal> inputs will be concatenated, and forced to either upper or lower case depending on the <literal>uppercase</literal> parameter. The remaining details of this function definition are not important here (see <xref linkend="extend"/> for more information). concat_lower_or_upper関数は、abの指定必須となる2つのパラメータを持ちます。 加えて、uppercaseというデフォルトがfalseとなっている省略可能なパラメータを一つ持ちます。 abで入力された文字列が結合され、uppercaseパラメータにより大文字か小文字に変換されます。 他のこの関数定義についての詳細は、ここでは重要ではありません。(詳細は第38章を参照してください。)

4.3.1. 位置表記の使用 #

<title>Using Positional Notation</title>

Positional notation is the traditional mechanism for passing arguments to functions in <productname>PostgreSQL</productname>. An example is: 位置表記は、PostgreSQLの引数を関数に渡す伝統的な仕組みです。 例を挙げます。

SELECT concat_lower_or_upper('Hello', 'World', true);
 concat_lower_or_upper
-----------------------
 HELLO WORLD
(1 row)

All arguments are specified in order. The result is upper case since <literal>uppercase</literal> is specified as <literal>true</literal>. Another example is: すべての引数を順番通りに指定します。uppercasetrueと指定されていますので、結果は大文字です。 別の例を示します。

SELECT concat_lower_or_upper('Hello', 'World');
 concat_lower_or_upper
-----------------------
 hello world
(1 row)

Here, the <literal>uppercase</literal> parameter is omitted, so it receives its default value of <literal>false</literal>, resulting in lower case output. In positional notation, arguments can be omitted from right to left so long as they have defaults. ここではuppercaseパラメータが省略されていますので、そのデフォルト値であるfalseを受け取ることとなり、結果は小文字になります。 位置表記では引数がデフォルト値を持つ限り右側から左の方向で、引数を省略できます。

4.3.2. 名前付け表記の使用 #

<title>Using Named Notation</title>

In named notation, each argument's name is specified using <literal>=&gt;</literal> 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)

Again, the argument <literal>uppercase</literal> was omitted so it is set to <literal>false</literal> implicitly. One advantage of using named notation is that the arguments may be specified in any order, for example: この場合も、uppercase引数が省略されていますので、暗黙的にfalseに設定されます。 名前付け表記使用の利点の1つとして、引数を任意の順序で指定できる点があります。 以下に例を示します。

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. 混在表記の利用 #

<title>Using Mixed Notation</title>

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)

In the above query, the arguments <literal>a</literal> and <literal>b</literal> are specified positionally, while <literal>uppercase</literal> 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. 上記の問い合わせでは、abが位置で指定され、uppercaseは名前で指定されています。 この例では文書化の目的以外ほとんど意味がありません。 デフォルト値が割り当てられた多くのパラメータを持つ、もっと複雑な関数では、名前付けもしくは混在表記により記述量を大きく減らすことができ、かつ、エラーが紛れ込む可能性を抑えることができます。

注記

Named and mixed call notations currently cannot be used when calling an aggregate function (but they do work when an aggregate function is used as a window function). 名前付けと混在呼び出し表記は集約関数の呼び出しでは現在使用できません(が、集約関数がウィンドウ関数として使われる場合には動作します)。