agora inbox for postgres@postgres.berkeley.edu  
help / color / mirror / Atom feed
From: Robert Patrick <rp2y@postgres.Berkeley.EDU>
To: postgres@postgres.Berkeley.EDU
Cc: matthew@risetime.com <matthew@interaccess.com>
Date: Wed, 28 Jun 1995 11:54:01 -0700
Message-ID: <199506281854.LAA01242@utopia.CS.Berkeley.EDU> (raw)

> 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/



reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: postgres@postgres.berkeley.edu
  Cc: rp2y@postgres.Berkeley.EDU, matthew@interaccess.com
  Subject: Re: 
  In-Reply-To: <199506281854.LAA01242@utopia.CS.Berkeley.EDU>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox