agora inbox for [email protected]  
help / color / mirror / Atom feed
From: Robert Patrick <[email protected]>
To: [email protected]
Cc: [email protected] <[email protected]>
Date: Wed, 28 Jun 1995 11:54:01 -0700
Message-ID: <[email protected]> (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 "[email protected]".
   If this fails, send mail to "[email protected]" 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: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: 
  In-Reply-To: <[email protected]>

* 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