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

F.41. spi — サーバプログラミングインタフェース機能/例 #

<title>spi &mdash; Server Programming Interface features/examples</title>

The <application>spi</application> module provides several workable examples of using the <link linkend="spi">Server Programming Interface</link> (<acronym>SPI</acronym>) and triggers. While these functions are of some value in their own right, they are even more useful as examples to modify for your own purposes. The functions are general enough to be used with any table, but you have to specify table and field names (as described below) while creating a trigger. spiモジュールは、サーバプログラミングインタフェース(SPI)およびトリガを使用した、動作可能な例を複数提供します。 これらの関数は独自の何らかの価値を持つものですが、目的に応じて変更するための例としてより有用です。 関数は任意のテーブルと使用できるほど一般的なものですが、トリガを作成する場合は(後述のように)テーブル名とフィールド名を指定する必要があります。

Each of the groups of functions described below is provided as a separately-installable extension. 以下で説明する関数グループのそれぞれは、別々にインストールすることができる拡張として提供されます。

F.41.1. refint — 参照整合性を実装する関数 #

<title>refint &mdash; Functions for Implementing Referential Integrity</title>

<function>check_primary_key()</function> and <function>check_foreign_key()</function> are used to check foreign key constraints. (This functionality is long since superseded by the built-in foreign key mechanism, of course, but the module is still useful as an example.) check_primary_key()およびcheck_foreign_key()は、外部キー制約を検査するために使用されます。 (当然ながら、この機能はかなり前に組み込みの外部キー機能に取って代わりました。しかし例としてはまだ有用です。)

<function>check_primary_key()</function> checks the referencing table. To use, create a <literal>BEFORE INSERT OR UPDATE</literal> trigger using this function on a table referencing another table. Specify as the trigger arguments: the referencing table's column name(s) which form the foreign key, the referenced table name, and the column names in the referenced table which form the primary/unique key. To handle multiple foreign keys, create a trigger for each reference. check_primary_key()は参照テーブルを検査します。 使用方法は、この関数を使用するBEFORE INSERT OR UPDATEトリガを他のテーブルを参照するテーブルに作成することです。 トリガ引数は、外部キーを形成する参照テーブルの列名、被参照テーブル名、プライマリ/一意キーを形成する被参照テーブルの列名です。 複数の外部キーを扱うためには、各参照に対してトリガを作成してください。

<function>check_foreign_key()</function> checks the referenced table. To use, create a <literal>BEFORE DELETE OR UPDATE</literal> trigger using this function on a table referenced by other table(s). Specify as the trigger arguments: the number of referencing tables for which the function has to perform checking, the action if a referencing key is found (<literal>cascade</literal> &mdash; to delete the referencing row, <literal>restrict</literal> &mdash; to abort transaction if referencing keys exist, <literal>setnull</literal> &mdash; to set referencing key fields to null), the triggered table's column names which form the primary/unique key, then the referencing table name and column names (repeated for as many referencing tables as were specified by first argument). Note that the primary/unique key columns should be marked NOT NULL and should have a unique index. check_foreign_key()は被参照テーブルを検査します。 使用方法は、この関数を使用するBEFORE DELETE OR UPDATEトリガを他のテーブルで参照されるテーブルに作成することです。 トリガ引数は、この関数が検査を実行しなければならない参照テーブル数、参照キーが見つかった場合の動作(cascade — 参照行を削除、restrict — 参照キーが存在する場合トランザクションをアボート、setnull —参照キーフィールドをNULLに設定)、プライマリ/一意キーを形成するトリガを発行したテーブルの列名、参照テーブルの名前と列名(最初の引数で指定された数のテーブル分繰り返す)です。 プライマリ/一意キー列はNOT NULLと指定されていなければならず、また、一意性インデックスを持つべきであることに注意してください。

There are examples in <filename>refint.example</filename>. refint.exampleに例があります。

F.41.2. autoinc — フィールド自動増分用の関数 #

<title>autoinc &mdash; Functions for Autoincrementing Fields</title>

<function>autoinc()</function> is a trigger that stores the next value of a sequence into an integer field. This has some overlap with the built-in <quote>serial column</quote> feature, but it is not the same: <function>autoinc()</function> will override attempts to substitute a different field value during inserts, and optionally it can be used to increment the field during updates, too. autoinc()は、整数型フィールドにシーケンスの次の値を格納するトリガです。 これは、組み込みの連番列機能と一部重複しますが、同一ではありません。 autoinc()は挿入時に別のフィールド値に置き換える試みを上書きし、さらに省略可能ですが、更新時にフィールドを増加させるために使用することもできます。

To use, create a <literal>BEFORE INSERT</literal> (or optionally <literal>BEFORE INSERT OR UPDATE</literal>) trigger using this function. Specify two trigger arguments: the name of the integer column to be modified, and the name of the sequence object that will supply values. (Actually, you can specify any number of pairs of such names, if you'd like to update more than one autoincrementing column.) 使用方法は、この関数を使用するBEFORE INSERT(または BEFORE INSERT OR UPDATE)トリガを作成することです。 2つのトリガ引数、変更する整数型列の名前と値を生み出すシーケンスオブジェクトの名前を指定します。 (実際、自動増分列を複数更新したい場合、これらの名前の組み合わせを任意の数指定することができます。)

There is an example in <filename>autoinc.example</filename>. autoinc.exampleに例があります。

F.41.3. insert_username — 誰がテーブルを変更したかを追跡する関数 #

<title>insert_username &mdash; Functions for Tracking Who Changed a Table</title>

<function>insert_username()</function> is a trigger that stores the current user's name into a text field. This can be useful for tracking who last modified a particular row within a table. insert_username()は現在のユーザ名をテキスト型のフィールドに格納するトリガです。 これはテーブル内のある行を最後に変更したユーザを追跡する際に有用です。

To use, create a <literal>BEFORE INSERT</literal> and/or <literal>UPDATE</literal> trigger using this function. Specify a single trigger argument: the name of the text column to be modified. 使用方法は、この関数を使用するBEFORE INSERTUPDATEまたはその両方のトリガを作成することです。 1つのトリガ引数、変更するテキスト型の列の名前を指定してください。

There is an example in <filename>insert_username.example</filename>. insert_username.exampleに例があります。

F.41.4. moddatetime — 最終更新時刻を追跡する関数 #

<title>moddatetime &mdash; Functions for Tracking Last Modification Time</title>

<function>moddatetime()</function> is a trigger that stores the current time into a <type>timestamp</type> field. This can be useful for tracking the last modification time of a particular row within a table. moddatetime()は現在時刻をtimestamp型のフィールドに格納するトリガです。 これは、テーブル内のある行の最終更新時刻を追跡する際に有用です。

To use, create a <literal>BEFORE UPDATE</literal> trigger using this function. Specify a single trigger argument: the name of the column to be modified. The column must be of type <type>timestamp</type> or <type>timestamp with time zone</type>. 使用方法は、この関数を使用するBEFORE UPDATEトリガを作成することです。 1つのトリガ引数、変更する列名を指定してください。 この列はtimestamp型またはtimestamp with time zone型でなければなりません。

There is an example in <filename>moddatetime.example</filename>. moddatetime.exampleに例があります。