PostgreSQL 9.3.2ʸ�� | ||||
---|---|---|---|---|
前のページ | 上に戻る | 第 31章libpq - C �饤�֥�� | 次のページ |
�ʲ���ޤॵ��ץ�ץ�����ब������������������ʪ���src/test/examples�ǥ��쥯�ȥ�ˤ���ޤ���
例 31-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=postgres����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 * */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = postgres"; /* �ǡ����١����Ȥ���³���Ω���� */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %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; }
例 31-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=postgres����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = postgres"; /* �ǡ����١����Ȥ���³���Ω���롣 */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %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; }
例 31-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 * * tuple 0: got * i = (4 bytes) 2 * t = (8 bytes) 'ho there' * b = (5 bytes) \004\003\002\001\000 */ #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); } /* * ���δؿ��Ͼ�Υ����Ȥ���������ơ��֥뤫��Х��ʥ�ե����ޥåȤǥե��å����� * �������̤�ɽ�����ޤ��� * main() �ؿ���2�ٻȤ��Τǡ���̤�ʬ�䤷�ޤ��� */ static void show_binary_results(PGresult *res) { int i, j; int i_fnum, t_fnum, b_fnum; /* �������������β��������Τ� PQfnumber �����Ѥ��� */ /* 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 �ΥХ���ɽ���ϥХ��Ȥν��ޤ�Ǥ��롣 * null �����ߤ�ޤ�Τǥե������Ĺ�����դ�ʧ��ʤ���Ф����ʤ��� */ 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"); } } int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; const char *paramValues[1]; int paramLengths[1]; int paramFormats[1]; uint32_t binaryIntVal; /* * �桼�������ޥ�ɥ饤��ǥѥ�����������硢conninfoʸ����Ȥ��ƻ��Ѥ��롣 * ����ʤ����ϥǥե���Ȥ�dbname=postgres����Ѥ��롣 * ����¾����³�ѥ����ˤĤ��ƤϴĶ��ѿ���ǥե���Ȥ���Ѥ��롣 */ if (argc > 1) conninfo = argv[1]; else conninfo = "dbname = postgres"; /* �ǡ����١����Ȥ���³���Ω���� */ conn = PQconnectdb(conninfo); /* �Хå�����ɤȤ���³��Ω���������������ǧ���� */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %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); } show_binary_results(res); PQclear(res); /* * 2���ܤ���ϡ��Х��ʥ�ե��������������ͥѥ������Ϥ��� * �����ƺƤӥХ��ʥ�ե�����Ƿ�̤������롣 * * �Хå�����ɤ˥ѥ��������פ��¬�����Ƥ���� PQexecParams �������뤬�� * ������ƥ����Ȥ���˥ѥ�������ܥ������뤳�Ȥˤ�ä� ����Ū�˷��ꤹ�롣 * ����ϥХ��ʥ�ѥ���������Ȥ��˰������ɤ��礭���Ǥ��롣 */ /* ������ "2" ��ͥåȥ���Х��ȥ����������Ѵ� */ binaryIntVal = htonl((uint32_t) 2); /* PQexecParams �Ѥ˥ѥ�������åȤ��� */ paramValues[0] = (char *) &binaryIntVal; paramLengths[0] = sizeof(binaryIntVal); paramFormats[0] = 1; /* �Х��ʥ� */ res = PQexecParams(conn, "SELECT * FROM test1 WHERE i = $1::int4", 1, /* �ѥ�����1�� */ NULL, /* �Хå�����ɤ˥ѥ����η����¬�����롣 */ paramValues, paramLengths, paramFormats, 1); /* �Х��ʥ��̤��ᡣ */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } show_binary_results(res); PQclear(res); /* �ǡ����١����Ȥ���³���Ĥ����������Ԥ��� */ PQfinish(conn); return 0; }