/*
174)
* testlibpq.c Test the C version of Libpq, the Postgres frontend
* library.
* testlibpq.c : Postgres フロントエンドライブラリ Libpq
* C バージョンのテスト
*
*
*/
#include <stdio.h>
#include "libpq-fe.h"
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
/* FILE *debug; */
PGconn *conn;
PGresult *res;
/*
175)
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
* バックエンドとの接続に必要なパラメータをセットしてプログラムを
* 開始します.
* パラメータが NULL であれば,環境変数を探して妥当と思われる値を
* 使おうとしますが,見つからない場合は,システムデフォルトの定数
* を使います.
*/
176)
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
pghost = NULL; /* バックエンドが動作しているサーバのホスト名 */
pgport = NULL; /* バックエンドのポート番号 */
pgoptions = NULL; /* バックエンドのスタートアップオプション */
pgtty = NULL; /* バックエンドのデバッグ用 tty */
dbName = "template1";
177)
/* make a connection to the database */
/* データベースへ接続 */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
178)
* check to see that the backend connection was successfully made
* バックエンドとの接続が成功したことを確認
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
179)
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, " データベース '%s' への接続に失敗しました \n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
/* debug = fopen("/tmp/trace.out","w"); */
/* PQtrace(conn, debug); */
180)
/* start a transaction block */
/* トランザクションブロックの開始 */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
181)
fprintf(stderr, "BEGIN command failed\n");
fprintf(stderr, "BEGIN コマンドの実行に失敗しました \n");
PQclear(res);
exit_nicely(conn);
}
/*
182)
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
* メモリリークを防ぐため,必要がなくなったら,すぐに
* PQclear(PGresult) しましょう
*/
PQclear(res);
/*
183)
* fetch instances from the pg_database, the system catalog of
* databases
* データベースのシステムカタログ,pg_database からインスタンスを
* 取り出す
*/
res = PQexec(conn, "DECLARE mycursor CURSOR FOR select * from pg_database");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
184)
fprintf(stderr, "DECLARE CURSOR command failed\n");
fprintf(stderr, "DECLARE CURSOR コマンドの実行に失敗しました \n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
185)
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
fprintf(stderr, "FETCH ALL コマンドがタプルを正しく返しませんでした \n");
PQclear(res);
exit_nicely(conn);
}
186)
/* first, print out the attribute names */
/* まず属性名を表示 */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
printf("%-15s", PQfname(res, i));
printf("\n\n");
187)
/* next, print out the instances */
/* 次にインスタンスを表示 */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
188)
/* close the cursor */
/* カーソルを閉じる */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
189)
/* commit the transaction */
/* トランザクションをコミット */
res = PQexec(conn, "COMMIT");
PQclear(res);
190)
/* close the connection to the database and cleanup */
/* データベースとの接続を閉じ,後始末 */
PQfinish(conn);
/* fclose(debug); */
}
/*
192)
* testlibpq2.c
* Test of the asynchronous notification interface
*
* Start this program, then from psql in another window do
* NOTIFY TBL2;
*
* Or, if you want to get fancy, try this:
* Populate a database with the following:
*
* CREATE TABLE TBL1 (i int4);
*
* CREATE TABLE TBL2 (i int4);
*
* CREATE RULE r1 AS ON INSERT TO TBL1 DO
* (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
*
* and do
*
* INSERT INTO TBL1 values (10);
*
* testlibpq2.c : 非同期通知インターフェースのテスト
*
* まずこのプログラムを実行し,それから他のウィンドウで psql から
*
* NOTIFY TBL2;
*
* としてみてください.
*
* もうちょっと凝ってみたい場合は,次のようにします.
* まず以下のようなデータベースをつくります.
*
* CREATE TABLE TBL1 (i int4);
*
* CREATE TABLE TBL2 (i int4);
*
* CREATE RULE r1 AS ON INSERT TO TBL1 DO
* (INSERT INTO TBL2 values (new.i); NOTIFY TBL2);
*
* そして
*
* INSERT INTO TBL1 values (10);
*
* としてください.
*
*/
#include <stdio.h>
#include "libpq-fe.h"
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
PGconn *conn;
PGresult *res;
PGnotify *notify;
/*
193)
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
* バックエンドとの接続に必要なパラメータをセットしてプログラムを
* 開始します.
* パラメータが NULL であれば,環境変数を探して妥当と思われる値を
* 使おうとしますが,見つからない場合は,システムデフォルトの定数
* を使います.
*
*/
194)
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
dbName = getenv("USER"); /* change this to the name of your test
* database */
pghost = NULL; /* バックエンドが動作しているサーバのホスト名 */
pgport = NULL; /* バックエンドのポート番号 */
pgoptions = NULL; /* バックエンドのスタートアップオプション */
pgtty = NULL; /* バックエンドのデバッグ用 tty */
dbName = getenv("USER"); /* これは読者のテスト用データベースの名前に
適宜置き換えてください */
195)
/* make a connection to the database */
/* データベースへ接続 */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
196)
* check to see that the backend connection was successfully made
* バックエンドとの接続が成功したことを確認
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
197)
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, " データベース '%s' への接続に失敗しました \n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "LISTEN TBL2");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
198)
fprintf(stderr, "LISTEN command failed\n");
fprintf(stderr, "LISTEN コマンドの実行に失敗しました \n");
PQclear(res);
exit_nicely(conn);
}
/*
199)
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
* メモリリークを防ぐため,必要がなくなったら,すぐに
* PQclear(PGresult) しましょう
*/
PQclear(res);
while (1)
{
/*
200)
* wait a little bit between checks; waiting with select()
* would be more efficient.
* チェックの繰り返しの間,ちょっとだけウエイトを入れます.
* select() ではウエイトを入れると効率的です.
*/
sleep(1);
201)
/* collect any asynchronous backend messages */
/* バックエンドの非同期メッセージを取り込む */
PQconsumeInput(conn);
202)
/* check for asynchronous notify messages */
/* 非同期通知メッセージのチェック */
while ((notify = PQnotifies(conn)) != NULL)
{
203)
fprintf(stderr,
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
notify->relname, notify->be_pid);
fprintf(stderr,
" 非同期通知 '%s' を PID '%d' のバックエンドから受けました \n",
notify->relname, notify->be_pid);
free(notify);
}
}
204)
/* close the connection to the database and cleanup */
/* データベースとの接続を閉じ,後始末 */
PQfinish(conn);
}
/*
206)
* testlibpq3.c Test the C version of Libpq, the Postgres frontend
* library. tests the binary cursor interface
*
*
*
* populate a database by doing the following:
* testlibpq3.c : Postgres フロントエンドライブラリ Libpq
* C バージョンのテスト
* バイナリカーソルのテスト
*
*
* まず以下のようなデータベースをつくります.
*
* CREATE TABLE test1 (i int4, d float4, p polygon);
*
* INSERT INTO test1 values (1, 3.567, '(3.0, 4.0, 1.0,
* 2.0)'::polygon);
*
* INSERT INTO test1 values (2, 89.05, '(4.0, 3.0, 2.0,
* 1.0)'::polygon);
*
207)
* the expected output is:
* 出力は以下のようになるはずです:
*
208)
* tuple 0: got i = (4 bytes) 1, d = (4 bytes) 3.567000, p = (4
* bytes) 2 points boundbox = (hi=3.000000/4.000000, lo =
* 1.000000,2.000000) tuple 1: got i = (4 bytes) 2, d = (4 bytes)
* 89.050003, p = (4 bytes) 2 points boundbox =
* (hi=4.000000/3.000000, lo = 2.000000,1.000000)
*
* タプル 0:
* i = ( 4 バイト ) 1,
* d = ( 4 バイト ) 3.567000,
* p = ( 68 バイト ) 座標数 2 境界ボックス = (hi=3.000000,4.000000,
* lo = 1.000000,2.000000)
* タプル 1:
* i = ( 4 バイト ) 2,
* d = ( 4 バイト ) 89.050003,
* p = ( 68 バイト ) 座標数 2 境界ボックス = (hi=4.000000,3.000000,
* lo = 2.000000,1.000000)
*
*/
#include <stdio.h>
追加
#include "postgres.h"
追加おわり
#include "libpq-fe.h"
211)
#include "utils/geo-decls.h" /* for the POLYGON type */
変更
#include "utils/geo_decls.h" /* POLYGON 型を使うのに必要 */
変更おわり
void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
main()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j;
int i_fnum,
d_fnum,
p_fnum;
PGconn *conn;
PGresult *res;
/*
214)
* begin, by setting the parameters for a backend connection if the
* parameters are null, then the system will try to use reasonable
* defaults by looking up environment variables or, failing that,
* using hardwired constants
* バックエンドとの接続に必要なパラメータをセットしてプログラムを
* 開始します.
* パラメータが NULL であれば,環境変数を探して妥当と思われる値を
* 使おうとしますが,見つからない場合は,システムデフォルトの定数
* 使います.
*
*/
215)
pghost = NULL; /* host name of the backend server */
pgport = NULL; /* port of the backend server */
pgoptions = NULL; /* special options to start up the backend
* server */
pgtty = NULL; /* debugging tty for the backend server */
dbName = getenv("USER"); /* change this to the name of your test
* database */
pghost = NULL; /* バックエンドが動作しているサーバのホスト名 */
pgport = NULL; /* バックエンドのポート番号 */
pgoptions = NULL; /* バックエンドのスタートアップオプション */
pgtty = NULL; /* バックエンドのデバッグ用 tty */
dbName = getenv("USER"); /* これは読者のテスト用データベースの名前に
適宜置き換えてください */
216)
/* make a connection to the database */
/* データベースへ接続 */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
217)
* check to see that the backend connection was successfully made
* バックエンドとの接続が成功したことを確認
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
218)
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, " データベース '%s' への接続に失敗しました \n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
219)
/* start a transaction block */
/* トランザクションブロックの開始 */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
220)
fprintf(stderr, "BEGIN command failed\n");
fprintf(stderr, "BEGIN コマンドの実行に失敗しました \n");
PQclear(res);
exit_nicely(conn);
}
/*
221)
* should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
* メモリリークを防ぐため,必要がなくなったら,すぐに
* PQclear(PGresult) しましょう
*/
PQclear(res);
/*
222)
* fetch instances from the pg_database, the system catalog of
* databases
* データベースのシステムカタログ,pg_database からインスタンスを
* 取り出す
*/
res = PQexec(conn, "DECLARE mycursor BINARY CURSOR FOR select * from test1");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
223)
fprintf(stderr, "DECLARE CURSOR command failed\n");
fprintf(stderr, "DECLARE CURSOR コマンドの実行に失敗しました \n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
224)
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
fprintf(stderr, "FETCH ALL コマンドがタプルを正しく返しませんでした \n");
PQclear(res);
exit_nicely(conn);
}
i_fnum = PQfnumber(res, "i");
d_fnum = PQfnumber(res, "d");
p_fnum = PQfnumber(res, "p");
for (i = 0; i < 3; i++)
{
printf("type[%d] = %d, size[%d] = %d\n",
i, PQftype(res, i),
i, PQfsize(res, i));
}
for (i = 0; i < PQntuples(res); i++)
{
int *ival;
float *dval;
int plen;
POLYGON *pval;
225)
/* we hard-wire this to the 3 fields we know about */
/*
* テーブル上の三つのフィールドの型はわかっているので
* ここでは変数に直接結び付けます
*/
ival = (int *) PQgetvalue(res, i, i_fnum);
dval = (float *) PQgetvalue(res, i, d_fnum);
plen = PQgetlength(res, i, p_fnum);
/*
226)
* plen doesn't include the length field so need to
* increment by VARHDSZ
* plen はフィールド長の分を含んでいないので,VARHDSZ を
* 加えます
*/
pval = (POLYGON *) malloc(plen + VARHDRSZ);
pval->size = plen;
memmove((char *) &pval->npts, PQgetvalue(res, i, p_fnum), plen);
227)
printf("tuple %d: got\n", i);
printf(" i = (%d bytes) %d,\n",
PQgetlength(res, i, i_fnum), *ival);
printf(" d = (%d bytes) %f,\n",
PQgetlength(res, i, d_fnum), *dval);
printf(" p = (%d bytes) %d points \tboundbox = (hi=%f/%f, lo = %f,%f)\n",
PQgetlength(res, i, d_fnum),
pval->npts,
pval->boundbox.xh,
pval->boundbox.yh,
pval->boundbox.xl,
pval->boundbox.yl);
printf(" タプル %d:\n", i);
printf(" i = ( %d バイト ) %d,\n",
PQgetlength(res, i, i_fnum), *ival);
printf(" d = ( %d バイト ) %f,\n",
PQgetlength(res, i, d_fnum), *dval);
printf(" p = ( %d バイト ) 座標数 %d \t 境界ボックス = (hi=%f,%f, lo = %f,%f)\n",
変更
plen,
pval->npts,
pval->boundbox.high.x,
pval->boundbox.high.y,
pval->boundbox.low.x,
pval->boundbox.low.y);
変更おわり
}
PQclear(res);
230)
/* close the cursor */
/* カーソルを閉じる */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
231)
/* commit the transaction */
/* トランザクションをコミット */
res = PQexec(conn, "COMMIT");
PQclear(res);
232)
/* close the connection to the database and cleanup */
/* データベースとの接続を閉じ,後始末 */
PQfinish(conn);
}