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

34.13. 警告処理 #

<title>Notice Processing</title>

Notice and warning messages generated by the server are not returned by the query execution functions, since they do not imply failure of the query. Instead they are passed to a notice handling function, and execution continues normally after the handler returns. The default notice handling function prints the message on <filename>stderr</filename>, but the application can override this behavior by supplying its own handling function. 問い合わせ実行関数では、サーバにより生成された通知と警告メッセージは、問い合わせの失敗を意味していないので返されません。 その代わり、それらは通知処理関数に渡され、ハンドラから返った後も実行は通常通り継続します。 デフォルトの通知処理関数はstderrにメッセージを出力しますが、アプリケーションは自身の処理関数を提供することでこの動作を書き換えることができます。

For historical reasons, there are two levels of notice handling, called the notice receiver and notice processor. The default behavior is for the notice receiver to format the notice and pass a string to the notice processor for printing. However, an application that chooses to provide its own notice receiver will typically ignore the notice processor layer and just do all the work in the notice receiver. 歴史的理由で、通知レシーバと通知プロセッサと呼ばれる2階層の通知処理が存在します。 デフォルトの動作は、通知レシーバが通知を書式化し、出力のため通知プロセッサに文字列を渡します。 しかし、独自の通知レシーバを提供することを選んだアプリケーションでは、通常、通知プロセッサ層を無視し、すべての作業を単に通知レシーバで行います。

The function <function id="libpq-PQsetNoticeReceiver">PQsetNoticeReceiver</function> <indexterm><primary>notice receiver</primary></indexterm> <indexterm><primary>PQsetNoticeReceiver</primary></indexterm> sets or examines the current notice receiver for a connection object. Similarly, <function id="libpq-PQsetNoticeProcessor">PQsetNoticeProcessor</function> <indexterm><primary>notice processor</primary></indexterm> <indexterm><primary>PQsetNoticeProcessor</primary></indexterm> sets or examines the current notice processor. 関数PQsetNoticeReceiverは接続オブジェクトに対し現在の通知レシーバを設定もしくは確認します。 同様に、PQsetNoticeProcessorは現在の通知プロセッサの設定もしくは確認を行います。

typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);

PQnoticeReceiver
PQsetNoticeReceiver(PGconn *conn,
                    PQnoticeReceiver proc,
                    void *arg);

typedef void (*PQnoticeProcessor) (void *arg, const char *message);

PQnoticeProcessor
PQsetNoticeProcessor(PGconn *conn,
                     PQnoticeProcessor proc,
                     void *arg);

Each of these functions returns the previous notice receiver or processor function pointer, and sets the new value. If you supply a null function pointer, no action is taken, but the current pointer is returned. 各関数は、以前の通知レシーバもしくは通知プロセッサ用の関数へのポインタを返し、新しい値を設定します。 関数ポインタにNULLを渡した場合、何も変更されず、現在のポインタが返されるだけです。

When a notice or warning message is received from the server, or generated internally by <application>libpq</application>, the notice receiver function is called. It is passed the message in the form of a <symbol>PGRES_NONFATAL_ERROR</symbol> <structname>PGresult</structname>. (This allows the receiver to extract individual fields using <xref linkend="libpq-PQresultErrorField"/>, or obtain a complete preformatted message using <xref linkend="libpq-PQresultErrorMessage"/> or <xref linkend="libpq-PQresultVerboseErrorMessage"/>.) The same void pointer passed to <function>PQsetNoticeReceiver</function> is also passed. (This pointer can be used to access application-specific state if needed.) サーバから注意/警告メッセージを受け取ると、あるいは、libpq内部で注意/警告メッセージが生成されると、通知レシーバ関数が呼び出されます。 PGRES_NONFATAL_ERROR PGresultという形でメッセージが渡されます。 (これにより、レシーバはPQresultErrorFieldを使用して個々のフィールドを取り出すことや、PQresultErrorMessageあるいはPQresultVerboseErrorMessageを使用して事前に整形された完全なメッセージを取得することができます。) PQsetNoticeReceiverに渡されたvoidポインタと同じものも渡されます。 (このポインタを使用して、必要に応じてアプリケーション特有の状態にアクセスすることができます。)

The default notice receiver simply extracts the message (using <xref linkend="libpq-PQresultErrorMessage"/>) and passes it to the notice processor. デフォルトの通知レシーバは単に(PQresultErrorMessageを使用して)メッセージを取り出し、それを通知プロセッサに渡すだけです。

The notice processor is responsible for handling a notice or warning message given in text form. It is passed the string text of the message (including a trailing newline), plus a void pointer that is the same one passed to <function>PQsetNoticeProcessor</function>. (This pointer can be used to access application-specific state if needed.) 通知プロセッサは、テキスト形式で与えられた注意/警告メッセージの取扱いに責任を持ちます。 メッセージは(最後の改行を含む)文字列テキストで渡され、更に、PQsetNoticeProcessorに渡したものと同じvoidポインタも渡されます。 (このポインタを使用して、必要に応じてアプリケーション特有の状態にアクセスすることができます。)

The default notice processor is simply: デフォルトの通知プロセッサは以下のような単純なものです。

static void
defaultNoticeProcessor(void *arg, const char *message)
{
    fprintf(stderr, "%s", message);
}

Once you have set a notice receiver or processor, you should expect that that function could be called as long as either the <structname>PGconn</structname> object or <structname>PGresult</structname> objects made from it exist. At creation of a <structname>PGresult</structname>, the <structname>PGconn</structname>'s current notice handling pointers are copied into the <structname>PGresult</structname> for possible use by functions like <xref linkend="libpq-PQgetvalue"/>. 一旦通知レシーバや通知プロセッサを設定したら、PGconnオブジェクトか、それから生成されたPGresultオブジェクトが存在している間は、その関数が呼び出される可能性があると考えておくべきです。 PGresultの生成時には、PGconnの現在の警告処理用のポインタが、PQgetvalueのような関数で使用可能であるように、PGresultへコピーされます。