Return-Path: owner-postman
Received: from localhost.Berkeley.EDU (localhost.Berkeley.EDU [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with SMTP id LAA00554 for postgres-redist; Wed, 28 Jun 1995 11:54:02 -0700
Resent-From: POSTGRES mailing list <postman@postgres.Berkeley.EDU>
Resent-Message-Id: <199506281854.LAA00554@nobozo.CS.Berkeley.EDU>
X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host localhost.Berkeley.EDU didn't use HELO protocol
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: owner-postman
Received: from utopia.CS.Berkeley.EDU (utopia.CS.Berkeley.EDU [128.32.37.54]) by nobozo.CS.Berkeley.EDU (8.6.10/8.6.3) with ESMTP id LAA00544 for <postgres@nobozo.cs.berkeley.edu>; Wed, 28 Jun 1995 11:54:02 -0700
Received: from utopia.CS.Berkeley.EDU (localhost.Berkeley.EDU [127.0.0.1]) by utopia.CS.Berkeley.EDU (8.6.10/8.6.3) with ESMTP id LAA01242; Wed, 28 Jun 1995 11:54:01 -0700
From: Robert Patrick <rp2y@postgres.Berkeley.EDU>
Message-Id: <199506281854.LAA01242@utopia.CS.Berkeley.EDU>
X-Mailer: exmh version 1.6 4/21/95
To: postgres@postgres.Berkeley.EDU
cc: "matthew@risetime.com" <matthew@interaccess.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Date: Wed, 28 Jun 1995 11:54:01 -0700
Resent-To: postgres-redist@postgres.Berkeley.EDU
Resent-Date: Wed, 28 Jun 95 11:54:02 -0700
Resent-XMts: smtp

> Aside from staring that the source code for monitor and psql, is there any
> documentation on the C API for postgres95?

The C API documentation for Postgres 4.2 should be a good starting point.

> Something simple, like connecting to the DB, passing a query, checking to
> see how many results where matched, and cycling through everything printing
> them out.

This is exactly what monitor.c does.

> If anyone has some smaller source code that  illustrates these ideas,
> please let me know.

Here's a crude example of how to do it in Postgres 4.2 (i.e., no error 
checking, DB name and query hard-coded, output columns are not lined up, may 
contain typos, etc.).  Converting this example to Postgres95 should be as easy 
as replacing the Postquel with SQL.

#include <tmp/libpq.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    PortalBuffer *pbuf;
    int ngroups, groupnum, tuple_index = 0;
    char *portalname, *result;

    PQsetdb(foo);
    result = PQexec("retrieve (bar.all)");
    if (result[0] != 'P') {
        fprintf(stderr, "Unexpected result from PQexec (%s)\n", result);
        exit(1);
    }
    portalname = &(results[1]);
    pbuf = PQparray(portalname);
    ngroups = PQngroups(pbuf);

    for (groupnum = 0; groupnum < ngroups; groupnum++) {
        int ntuples, nfields, fieldnum, tuplenum;

        ntuples = PQntuplesGroup(pbuf, groupnum);
        nfields = PQnfieldsGroup(pbuf, groupnum);

        /*
         * Print out the field names.
         */
        for (fieldnum = 0; fieldnum < nfields; fieldnum++) {
            char *fieldname;

            fieldname = PQfnameGroup(pbuf, groupnum, fieldnum);
            if (fieldnum == 0)
                fprintf(stdout, "|  %s  |", fieldname);
            else
                fprintf(stdout, "  %s  |", fieldname);
        }
        fprintf(stdout, "\n");

        /*
         * Print out the tuples.
         */
        for (tuplenum = 0; tuplenum < ntuples; tuplenum++) {
            for (fieldnum = 0; fieldnum < nfields; fieldnum++) {
                char *fieldvalue;

                fieldvalue = PQgetvalue(pbuf, (tuple_index + tuple_num), 
fieldnum);
                if (fieldvalue == NULL)  /* field value is null */
                    fieldvalue = "        ";
                if (fieldnum == 0)
                    fprintf(stdout, "|  %s  |", fieldvalue);
                else
                    fprintf(stdout, "  %s  |", fieldvalue);
            }
        }
        tuple_index += ntuples;
    }
    PQclear(portalname);
    PQfinish();
    exit(0);
}

Robert

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/
