�ʲ���ޤॵ��ץ�ץ�����ब������������������ʪ���src/test/examples�ǥ��쥯�ȥ�ˤ���ޤ���
�� 27-1. libpq ����ץ�ץ������ 1
/* * testlibpq.c * * C����Postgresql�ե���ȥ���ɥ饤�֥��libpq�λ */ #include <stdio.h> #include <stdlib.h> #include "libpq-fe.h" static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; int nFields; int i, j; /* * �桼�������ޥ�ɥ饤��ǥѥ�����������硢conninfoʸ����Ȥ��ƻ��Ѥ��롣 * ����ʤ����ϥǥե���Ȥ�dbname=template1����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 * */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = template1"; /* �ǡ����١����Ȥ���³���Ω���� */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } /* * ���λ�������Ǥϥ����������Ѥ��롣 * ���Τ��ᡢ�ȥ�������֥��å���Ǽ¹Ԥ���ɬ�פ����롣 * ���٤Ƥ�ñ���"select * from pg_database"�Ȥ���PQexec()�ǹԤʤ����� * ���ǽ��������Ȥ��Ƥϴ�ñ��롣 */ /* �ȥ�������֥��å��Ϥ��롣 */ res = PQexec(conn, "BEGIN"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } /* * ���פˤʤä��顢���������ɤ������PGresult��PQclear���٤��� * */ PQclear(res); /* * �ǡ����١����Υ����ƥ५������pg_database����Ԥ���Ф��� */ res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } PQclear(res); res = PQexec(conn, "FETCH ALL in myportal"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn)); 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 myportal"); PQclear(res); /* �ȥ��������λ���� */ res = PQexec(conn, "END"); PQclear(res); /* �ǡ����١����Ȥ���³���Ĥ����������Ԥʤ��� */ PQfinish(conn); return 0; }
�� 27-2. libpq ����ץ�ץ������ 2
/* * testlibpq2.c * ��Ʊ�����Υ��ե������λ * * ���Υץ�������ư�����̥�����ɥ�����psql����Ѥ��ưʲ���¹Ԥ��Ƥ��������� * NOTIFY TBL2; * 4���֤��Ȥ��Υץ������Ͻ�λ���ޤ��� * * �⤦�����Ťꤿ����С��ʲ���»ܤ��Ƥ��������� * �ʲ��Υ��ޥ��(src/test/examples/testlibpq2.sql����)�ǥǡ����١�����������ޤ��� * * 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); * * �����ơ��ʲ���4��¹Ԥ��Ƥ��������� * * INSERT INTO TBL1 VALUES (10); */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/time.h> #include "libpq-fe.h" static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; PGnotify *notify; int nnotifies; /* * �桼�������ޥ�ɥ饤��ǥѥ�����������硢conninfoʸ����Ȥ��ƻ��Ѥ��롣 * ����ʤ����ϥǥե���Ȥ�dbname=template1����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = template1"; /* �ǡ����١����Ȥ���³���Ω���롣 */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } /* * LISTEN���ޥ�ɤ�ȯ�Ԥ��ơ�INSERT�롼��ˤ�����Τ�ͭ���ˤ��롣 */ res = PQexec(conn, "LISTEN TBL2"); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } /* * ���פˤʤä��顢���������ɤ������PGresult��PQclear���٤��� */ PQclear(res); /* 4�����Τ�������齪λ���롣 */ nnotifies = 0; while (nnotifies < 4) { /* * ������³�Dz�����������ޤ��Ե����롣�����Ǥ������Ԥ��Τ���� * select(2)����Ѥ��롣Poll()�������ǽ����Ѥ��뤳�Ȥ��ǽ * �Ǥ��롣 */ int sock; fd_set input_mask; sock = PQsocket(conn); if (sock < 0) break; /* ȯ�����ƤϤʤ�ʤ��� */ FD_ZERO(&input_mask); FD_SET(sock, &input_mask); if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0) { fprintf(stderr, "select() failed: %s\n", strerror(errno)); exit_nicely(conn); } /* ���������Ϥ��ǧ���롣 */ PQconsumeInput(conn); while ((notify = PQnotifies(conn)) != NULL) { fprintf(stderr, "ASYNC NOTIFY of '%s' received from backend pid %d\n", notify->relname, notify->be_pid); PQfreemem(notify); nnotifies++; } } fprintf(stderr, "Done.\n"); /* �ǡ����١����Ȥ���³���Ĥ����������Ԥʤ��� */ PQfinish(conn); return 0; }
�� 27-3. libpq ����ץ�ץ������ 3
/* * testlibpq3.c * �ʳ��Υѥ����ȥХ��ʥ�I/O�λ�� * * �¹����ˡ��ʲ��Υ��ޥ��(src/test/examples/testlibpq3.sql����)����Ѥ��� * �ǡ����١�����������Ƥ��������� * * CREATE TABLE test1 (i int4, t text, b bytea); * * INSERT INTO test1 values (1, 'joe''s place', '\\000\\001\\002\\003\\004'); * INSERT INTO test1 values (2, 'ho there', '\\004\\003\\002\\001\\000'); * * �ʲ��ν��Ϥ����ꤵ��ޤ��� * * tuple 0: got * i = (4 bytes) 1 * t = (11 bytes) 'joe's place' * b = (5 bytes) \000\001\002\003\004 * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include "libpq-fe.h" /* ntohl/htonl�� */ #include <netinet/in.h> #include <arpa/inet.h> static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; const char *paramValues[1]; int i, j; int i_fnum, t_fnum, b_fnum; /* * �桼�������ޥ�ɥ饤��ǥѥ�����������硢conninfoʸ����Ȥ��ƻ��Ѥ��롣 * ����ʤ����ϥǥե���Ȥ�dbname=template1����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = template1"; /* �ǡ����١����Ȥ���³���Ω���� */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } /* * ���Υץ������Υݥ���Ȥϡ��Գ��ѥ��������PQexecParams()�λ�����ˡ�� * ����ӡ���̤ΥХ��ʥ�ž�������ȤǤ��롣�Գ��ѥ�������Ѥ��뤳�Ȥǡ� * ������Ť��䥨�����������Ȥ��ä�¿����Ĺ���餷�����ߤ�ʤ������Ȥ��Ǥ��롣 * �ѥ����������ΰ���������Ф����ü�ʽ�����Ԥʤ�ɬ�פ��ʤ����Ȥ����ܤ��� * �ۤ����� * */ /* �ʲ����Գ��ѥ������ͤǤ��롣 */ paramValues[0] = "joe's place"; res = PQexecParams(conn, "SELECT * FROM test1 WHERE t = $1", 1, /* �ѥ�����1�ġ� */ NULL, /* �Хå�����ɤ˥ѥ����η����¬�����롣 */ paramValues, NULL, /* �ƥ����ȤΤ��ᡢ�ѥ���Ĺ�����ס� */ NULL, /* �ǥե���ȤǤ��٤ƤΥѥ����ϥƥ����ȡ� */ 1); /* �Х��ʥ��̤��ᡣ */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } /* PQfnumber����Ѥ��Ʒ����Υե�����ɽ���ꤷ�ʤ��� */ i_fnum = PQfnumber(res, "i"); t_fnum = PQfnumber(res, "t"); b_fnum = PQfnumber(res, "b"); for (i = 0; i < PQntuples(res); i++) { char *iptr; char *tptr; char *bptr; int blen; int ival; /* �ե�������ͤ���Ф���(NULL�Ǥ����ǽ����̵�뤹��!) */ iptr = PQgetvalue(res, i, i_fnum); tptr = PQgetvalue(res, i, t_fnum); bptr = PQgetvalue(res, i, b_fnum); /* * INT4�ΥХ��ʥ�ɽ���ϥͥåȥ���Х��Ƚ�Ǥ��롣 * ���Ѥ���ޥ���ΥХ��Ƚ�˹�碌�������褤�� */ ival = ntohl(*((uint32_t *) iptr)); /* * TEXT�ΥХ��ʥ�ɽ����Ʊ�����ƥ����ȤǤ��롣libpq�ˤ�를���Х��Ȥ��ɲ� * �ǽ�ʬ�Ǥ��뤫�顢�����C��ʸ����Ȥ��Ƥ��ޤ�ư��롣 * * BYTEA�ΥХ��ʥ�ɽ���ϥХ��ȷ��Ǥ��롣����ˤϥ̥뤬�����ޤ�Ƥ��뤿�ᡢ * �ե������Ĺ�����ܤ��ʤ���Фʤ�ʤ��� * */ blen = PQgetlength(res, i, b_fnum); printf("tuple %d: got\n", i); printf(" i = (%d bytes) %d\n", PQgetlength(res, i, i_fnum), ival); printf(" t = (%d bytes) '%s'\n", PQgetlength(res, i, t_fnum), tptr); printf(" b = (%d bytes) ", blen); for (j = 0; j < blen; j++) printf("\\%03o", bptr[j]); printf("\n\n"); } PQclear(res); /* �ǡ����١����Ȥ���³���Ĥ����������Ԥʤ��� */ PQfinish(conn); return 0; }