����ץ�ץ������

����ץ�ץ������ 1

/*

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); */
}

����ץ�ץ������ 2

/*

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);

}

����ץ�ץ������ 3

/*

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);

}