Example 1-1. libpq ����ץ�ץ������ 1
/*
* testlibpq.c
*
* PostgreSQL �Υե���ȥ���ɥ饤�֥��Ǥ��� 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;
/*
* �Хå�����ɤȤ���³��ɬ�פʥѥ����åȤ��ƥץ������Ϥ��ޤ���
* �ѥ����� NULL �Ǥ���С��Ķ��ѿ���õ���������Ȼפ����ͤ�
* �Ȥ����Ȥ��ޤ��������Ĥ���ʤ����ϡ������ƥ�ǥե���Ȥ������Ȥ��ޤ���
*/
pghost = NULL; /* �Хå�����ɥ����ФΥۥ���̾ */
pgport = NULL; /* �Хå�����ɥ����ФΥݡ����ֹ� */
pgoptions = NULL; /* �Хå�����ɥ����Фε�ư���ץ���� */
pgtty = NULL; /* �Хå�����ɤΥǥХå��� tty */
dbName = "template1";
/* �ǡ����١�������³���ޤ� */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
* �Хå�����ɤȤ���³�������������Ȥ��ǧ
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
/* debug = fopen("/tmp/trace.out","w"); */
/* PQtrace(conn, debug); */
/* �ȥ�������֥��å��γ��� */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed\n");
PQclear(res);
exit_nicely(conn);
}
/*
* ���������ɤ����ᡢɬ�פ��ʤ��ʤä��顢������
* PQclear(PGresult) ���ޤ��礦
*/
PQclear(res);
/*
* �ǡ����١����Υ����ƥ५��������pg_database ���饤����
* ����
*/
res = PQexec(conn, "DECLARE mycursor CURSOR FOR SELECT * FROM pg_database");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
PQclear(res);
exit_nicely(conn);
}
/* �ޤ�°��̾��ɽ�� */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
printf("%-15s", PQfname(res, i));
printf("\n\n");
/* ���˥�����ɽ�� */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
printf("%-15s", PQgetvalue(res, i, j));
printf("\n");
}
PQclear(res);
/* ����������Ĥ��� */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
/* �ȥ�������ߥå� */
res = PQexec(conn, "COMMIT");
PQclear(res);
/* �ǡ����١����Ȥ���³���Ĥ�������� */
PQfinish(conn);
/* fclose(debug); */
return 0;
}
Example 1-2. libpq ����ץ�ץ������ 2
/* * 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; /* * �Хå�����ɤȤ���³��ɬ�פʥѥ����åȤ��ƥץ������Ϥ��ޤ��� * �ѥ����� NULL �Ǥ���С��Ķ��ѿ���õ���������Ȼפ����ͤ� * �Ȥ����Ȥ��ޤ��������Ĥ���ʤ����ϡ������ƥ�ǥե���Ȥ������Ȥ��ޤ��� */ pghost = NULL; /* �Хå�����ɥ����ФΥۥ���̾ */ pgport = NULL; /* �Хå�����ɥ����ФΥݡ����ֹ� */ pgoptions = NULL; /* �Хå�����ɥ����Фε�ư���ץ���� */ pgtty = NULL; /* �Хå�����ɤΥǥХå��� tty */ dbName = getenv("USER"); /* ������ɼԤΥƥ����ѥǡ����١�����̾����Ŭ���֤������Ƥ���������*/ /* �ǡ����١�������³���ޤ� */ conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); /* * �Хå�����ɤȤ���³�������������Ȥ��ǧ */ if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } res = PQexec(conn, "LISTEN TBL2"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "LISTEN command failed\n"); PQclear(res); exit_nicely(conn); } /* * ���������ɤ����ᡢɬ�פ��ʤ��ʤä��顢������ * PQclear(PGresult) ���ޤ��礦 */ PQclear(res); while (1) { /* * �����å��η����֤��δ֡�����äȤ����Ԥ����֤�����ޤ��� * select() ���Ԥ����֤������Ф�����Ū�ˤʤ�ޤ��� */ sleep(1); /* �Хå�����ɤ���Ʊ����å��������ꤳ�� */ PQconsumeInput(conn); /* ��Ʊ�����Υ�å������Υ����å� */ while ((notify = PQnotifies(conn)) != NULL) { fprintf(stderr, "ASYNC NOTIFY of '%s' from backend pid '%d' received\n", notify->relname, notify->be_pid); free(notify); } } /* �ǡ����١����Ȥ���³���Ĥ�������� */ PQfinish(conn); return 0; }
Example 1-3. libpq ����ץ�ץ������ 3
/*
* PostgreSQL �Υե���ȥ���ɥ饤�֥��Ǥ��� libpq ��
* C �ǤΥƥ��ȡ��Х��ʥꥫ������Υƥ���
*
*
*
* �ޤ��ʲ��Τ褦�ʥǡ����١�������ޤ���
*
* CREATE TABLE test1 (i int4, d real, p polygon);
*
* INSERT INTO test1 values (1, 3.567, polygon '(3.0, 4.0, 1.0, 2.0)');
*
* INSERT INTO test1 values (2, 89.05, polygon '(4.0, 3.0, 2.0, 1.0)');
*
* ���Ϥϰʲ��Τ褦�ˤʤ�Ϥ��Ǥ���
*
* 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)
*
*
*/
#include <stdio.h>
#include "libpq-fe.h"
<#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;
/*
* �Хå�����ɤȤ���³��ɬ�פʥѥ����åȤ��ƥץ������Ϥ��ޤ���
* �ѥ����� NULL �Ǥ���С��Ķ��ѿ���õ���������Ȼפ����ͤ�
* �Ȥ����Ȥ��ޤ��������Ĥ���ʤ����ϡ������ƥ�ǥե���Ȥ������Ȥ��ޤ���
*/
pghost = NULL; /* �Хå�����ɥ����ФΥۥ���̾ */
pgport = NULL; /* �Хå�����ɥ����ФΥݡ����ֹ� */
pgoptions = NULL; /* �Хå�����ɥ����Фε�ư���ץ���� */
pgtty = NULL; /* �Хå�����ɤΥǥХå��� tty */
dbName = getenv("USER"); /* ������ɼԤΥƥ����ѥǡ����١�����̾����Ŭ���֤������Ƥ���������*/
/* �ǡ����١�������³���ޤ� */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
* �Хå�����ɤȤ���³�������������Ȥ��ǧ
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
/* �ȥ�������֥��å��γ��� */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed\n");
PQclear(res);
exit_nicely(conn);
}
/*
* ���������ɤ����ᡢɬ�פ��ʤ��ʤä��顢������
* PQclear(PGresult) ���ޤ��礦
*/
PQclear(res);
/*
* �ǡ����١����Υ����ƥ५��������pg_database ���饤����
* ����
*/
res = PQexec(conn, "DECLARE mycursor BINARY CURSOR FOR SELECT * FROM test1");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples properly\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;
/* �ơ��֥���3�ĤΥե�����ɤη��Ϥ狼�äƤ���Τ� �����Ǥ��ѿ���ľ�ܷ���դ��ޤ� */
ival = (int *) PQgetvalue(res, i, i_fnum);
dval = (float *) PQgetvalue(res, i, d_fnum);
plen = PQgetlength(res, i, p_fnum);
/*
* plan �ϥե������Ĺ��ʬ��ޤ�Ǥ��ʤ��Τǡ�VARHDSZ ��
* �ä��ޤ�
*/
pval = (POLYGON *) malloc(plen + VARHDRSZ);
pval->size = plen;
memmove((char *) &pval->npts, PQgetvalue(res, i, p_fnum), plen);
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);
}
PQclear(res);
/* ����������Ĥ��� */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
/* �ȥ�������ߥå� */
res = PQexec(conn, "COMMIT");
PQclear(res);
/* �ǡ����١����Ȥ���³���Ĥ�������� */
PQfinish(conn);
return 0;
}