85) The copy command in Postgres has options to read from or write to the network connection used by libpq++. Therefore, functions are necessary to access this network connection directly so applications may take full advantage of this capability.
Postgres の COPY コマンドは, libpq++ が使っているネットワーク接続に対して読み込み, あるいは書き込みを選ぶことができるようになっています. そこでこのネットワーク接続に直接アクセスするための関数が必要になります. もちろんアプリケーションもこの機能によって十分な恩恵を受けるでしょう.
86) PgDatabase::GetLine reads a newline-terminated line of characters (transmitted by the backend server) into a buffer string of size length.
PgDatabase::GetLine は改行で終端する文字列(バックエンドから送信されたもの)を長さ length のバッファ string に読み込みます.
int PgDatabase::GetLine(char* string, int length)
87) Like the Unix system routine fgets (3), this routine copies up to length-1 characters into string. It is like gets (3), however, in that it converts the terminating newline into a null character.
Unix システムルーチン fgets(3) と同様, このルーチンは string に最大 (length-1) 個の文字をコピーします.しかし終端の改行が NULL 文字に置き換えられる点では gets(3) に似ています.
88) PgDatabase::GetLine returns EOF at end of file, 0 if the entire line has been read, and 1 if the buffer is full but the terminating newline has not yet been read.
PgDatabase::GetLine はファイル終端で EOF を, 行全体が読み込まれれば 0 を返します. そして,まだ終端の改行が読み込まれていないうちにバッファがいっぱいに なってしまった場合は 1 を返します.
89) Notice that the application must check to see if a new line consists of a single period ("."), which indicates that the backend server has finished sending the results of the copy. Therefore, if the application ever expects to receive lines that are more than length-1 characters long, the application must be sure to check the return value of PgDatabase::GetLine very carefully.
アプリケーションは新しく読み込んだ行が, ピリオド一文字 (".") であるかどうか確認しなければなりません. [1] この文字は COPY コマンドの結果をバックエンドサーバが送信し終えたことを示すものです. したがって (length-1) 文字を超える行の受信が想定されるのであれば, アプリケーションは PgDatabase::GetLine が返す値をよく注意して,確実にチェックしなければなりません.
PgDatabase::PutLine 90) Sends a null-terminated string to the backend server. NULL 終端の文字列 string をバックエンドサーバに送信します.
void PgDatabase::PutLine(char* string)
91) The application must explicitly send a single period character (".") to indicate to the backend that it has finished sending its data.
アプリケーションはデータ送信の完了をバックエンドに示すために, ピリオド一文字 (".") を明示的に送信しなければなりません. [1]
PgDatabase::EndCopy 92) syncs with the backend. バックエンドと同期します.
int PgDatabase::EndCopy()93) This function waits until the backend has finished processing the copy. It should either be issued when the last string has been sent to the backend using PgDatabase::PutLine or when the last string has been received from the backend using PgDatabase::GetLine. It must be issued or the backend may get "out of sync" with the frontend. Upon return from this function, the backend is ready to receive the next query. この関数はバックエンドが COPY 処理を完了するのを待ちます. この関数は PgDatabase::PutLine を使ったバックエンドへの文字列送信が完了した時点, あるいは PgDatabase::GetLine を使ったバックエンドからの文字列受信が完了した時点の, いずれでも呼び出さなければなりません. さもないと,バックエンドがフロントエンドとの「同期ずれ」 を起こしてしまうかもしれません. この関数から戻った時点で,バックエンドの次の問い合わせを受ける準備が整います.
94) The return value is 0 on successful completion, nonzero otherwise.
同期が成功すれば 0 を,そうでなければ 0 以外の値を返します.
95) As an example:
一例です:
96) PgDatabase data; data.Exec("create table foo (a int4, b char16, d float8)"); data.Exec("copy foo from stdin"); data.putline("3\etHello World\et4.5\en"); data.putline("4\etGoodbye World\et7.11\en"); &... data.putline(".\en"); data.endcopy(); ※修正 ...'\et','\en' とは??
PgDatabase.data; data.Exec("create table foo (a int4, b char(16), d float8)"); data.Exec("copy foo from stdin"); data.PutLine("3\tHello World\t4.5\n"); data.PutLine("4\tGoodbye World\t7.11\n"); &... data.PutLine(".\n"); data.EndCopy();
[1] | 訳注: 6.5.3 では libpq と同じ "\." で実際には完了するようです. example ディレクトリの testlibpq6.cc 参照. |