Ordinarily, <application>libpq</application> collects an SQL command's
entire result and returns it to the application as a single
<structname>PGresult</structname>. This can be unworkable for commands
that return a large number of rows. For such cases, applications can use
<xref linkend="libpq-PQsendQuery"/> and <xref linkend="libpq-PQgetResult"/> in
<firstterm>single-row mode</firstterm>. In this mode, the result row(s) are
returned to the application one at a time, as they are received from the
server.
通常、libpqはSQLコマンドの結果全体を収集し、それを1つのPGresult
としてアプリケーションに返します。
これは、多くの行数を返すコマンドでは動作しなくなるかもしれません。
こうした場合、アプリケーションはPQsendQuery
とPQgetResult
を単一行モードで使用することができます。
このモードでは、結果行は、サーバから受け取ったかのように、アプリケーションに1度に1行返されます。
To enter single-row mode, call <xref linkend="libpq-PQsetSingleRowMode"/>
immediately after a successful call of <xref linkend="libpq-PQsendQuery"/>
(or a sibling function). This mode selection is effective only for the
currently executing query. Then call <xref linkend="libpq-PQgetResult"/>
repeatedly, until it returns null, as documented in <xref
linkend="libpq-async"/>. If the query returns any rows, they are returned
as individual <structname>PGresult</structname> objects, which look like
normal query results except for having status code
<literal>PGRES_SINGLE_TUPLE</literal> instead of
<literal>PGRES_TUPLES_OK</literal>. After the last row, or immediately if
the query returns zero rows, a zero-row object with status
<literal>PGRES_TUPLES_OK</literal> is returned; this is the signal that no
more rows will arrive. (But note that it is still necessary to continue
calling <xref linkend="libpq-PQgetResult"/> until it returns null.) All of
these <structname>PGresult</structname> objects will contain the same row
description data (column names, types, etc.) that an ordinary
<structname>PGresult</structname> object for the query would have.
Each object should be freed with <xref linkend="libpq-PQclear"/> as usual.
単一行モードに入るためには、PQsendQuery
(または同系列の関数)の呼び出しに成功した直後にPQsetSingleRowMode
を呼び出してください。
このモード選択は、現在実行中の問い合わせに対してのみ有効です。
その後、34.4の説明通りに、NULLを返すようになるまでPQgetResult
を繰り返し呼び出してください。
問い合わせが何らかの行を返す場合、PGRES_TUPLES_OK
ではなくPGRES_SINGLE_TUPLE
である点を除けば、通常の問い合わせ結果と同じように見える、個々のPGresult
オブジェクトを返します。
最後の行の後、または問い合わせがゼロ行を返す場合は即座に、PGRES_TUPLES_OK
状態のゼロ行のオブジェクトが返されます。
これはもう行が届かないことを通知するものです。
(しかしNULLが返るまでPQgetResult
を呼び出さなければならないことに注意してください。)
PGresult
オブジェクトのすべては、その問い合わせに対する通常のPGresult
と同一の行説明データ(列名、型など)を持ちます。
各オブジェクトは通常通りPQclear
で解放しなければなりません。
When using pipeline mode, single-row mode needs to be activated for each
query in the pipeline before retrieving results for that query
with <function>PQgetResult</function>.
See <xref linkend="libpq-pipeline-mode"/> for more information.
パイプラインモードを使用する場合、単一行モードは、PQgetResult
でその問い合わせの結果を取得する前に、パイプラインの各問い合わせに対してアクティブにする必要があります。
詳細は34.5を参照してください。
PQsetSingleRowMode
#Select single-row mode for the currently-executing query. 現在実行中の問い合わせについて単一行モードを選択します。
int PQsetSingleRowMode(PGconn *conn);
This function can only be called immediately after
<xref linkend="libpq-PQsendQuery"/> or one of its sibling functions,
before any other operation on the connection such as
<xref linkend="libpq-PQconsumeInput"/> or
<xref linkend="libpq-PQgetResult"/>. If called at the correct time,
the function activates single-row mode for the current query and
returns 1. Otherwise the mode stays unchanged and the function
returns 0. In any case, the mode reverts to normal after
completion of the current query.
この関数はPQsendQuery
またはその系列の関数のいずれかの後即座に、PQconsumeInput
やPQgetResult
など接続に対する何らかの他の操作を行う前のみに呼び出すことができます。
正しい時点で呼び出された場合、この関数は現在の問い合わせに対して単一行モードを有効にし、1を返します。
この他の場合、モードは変更されず、関数はゼロを返します。
いずれの場合でも、現在の問い合わせが完了した後に通常モードに戻ります。
While processing a query, the server may return some rows and then
encounter an error, causing the query to be aborted. Ordinarily,
<application>libpq</application> discards any such rows and reports only the
error. But in single-row mode, those rows will have already been
returned to the application. Hence, the application will see some
<literal>PGRES_SINGLE_TUPLE</literal> <structname>PGresult</structname>
objects followed by a <literal>PGRES_FATAL_ERROR</literal> object. For
proper transactional behavior, the application must be designed to
discard or undo whatever has been done with the previously-processed
rows, if the query ultimately fails.
問い合わせを処理している間、サーバはいくつか行を返した後にエラーになり、問い合わせがアボートする可能性があります。
通常のlibpqでは、こうした行を破棄しエラーのみを報告します。
しかし単一行モードでは、これらの行はすでにアプリケーションに返されています。
このためアプリケーションはPGRES_SINGLE_TUPLE
状態のPGresult
オブジェクトをいくつか見た後にPGRES_FATAL_ERROR
オブジェクトを見るかもしれません。
適切な振る舞いのトランザクションのために、最終的に問い合わせが失敗した場合、アプリケーションはこれまで処理した行を破棄するまたは取り消すように設計しなければなりません。