27.16. ���ӳ���

Example 27-1. libpq ���ӳ��� 1

/*
 * testlibpq.c
 *
 * ���� PostgreSQL ǰ�˿� libpq �� C �汾
 */
#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 failed: %s",
                        PQerrorMessage(conn));
                exit_nicely(conn);

	}


        /*
         * ��������IJ��԰����漰ʹ���α꣬������������DZ�����һ����������档
         * ���ǿ�����һ���򵥵� PQexec()��ִ��
         * "select * from pg_database"�����ȫ�����������������Ļ���һ�����Ӿ�̫���ˡ�
         */
	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);


	/* �ر���ڣ����Dz��ù��Ĵ�����... */
        res = PQexec(conn, "CLOSE myportal");
        PQclear(res);

	/* �ύ���� */
        res = PQexec(conn, "END");
        PQclear(res);

	/* �ر������ݿ�����Ӳ������� */
        PQfinish(conn);

	return 0;

}

Example 27-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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include "libpq-fe.h"

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 failed: %s",
                        PQerrorMessage(conn));
                exit_nicely(conn);
	}
        /*
         * ���� LISTEN ��������Թ��� NOTIFY ��֪ͨ
         */
        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);

        /* �յ��Ĵ�֪֮ͨ���˳��� */
        nnotifies = 0;
        while (nnotifies < 4)
        {
                /*
                 * ˯�ߣ�ֱ��ijЩ�¼�����������ʹ�� select(2) �ȴ����룬Sleep until something happens on the connection.  We use 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;

}

Example 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,           /* һ������ */
                                           NULL,        /* �ú���Ƶ��������� */
                                           paramValues,
                                           NULL,        /* ��Ϊ���ı������Բ���Ҫ�������� */
                                           NULL,        /* ȱʡ�Dz��������ı� */
                                           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;

                /* ��ȡ�ֶ�ֵ�����Ǻ��������ǿ����ǿյ����������*/
                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 �Ķ�������ʽ��һ���ֽڣ������ܰ���Ƕ��Ŀ��㣬�������Ӧ��
		 * ע������ֶεij��ȡ�
                 */
                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;
}