Update pginterface for 6.4. add manual page.

This commit is contained in:
Bruce Momjian 1998-09-11 05:14:08 +00:00
parent 4da5714ced
commit a5d4642578
3 changed files with 119 additions and 23 deletions

View File

@ -16,14 +16,14 @@ have a global variable that allows you to disable the error checking I
have added to the doquery() routine. have added to the doquery() routine.
I have added a function called fetch(), which allows you to pass I have added a function called fetch(), which allows you to pass
pointers as parameters, and on return the variables are filled with the pointers as parameters, and on return the variables are filled with data
data from the binary cursor you opened. These binary cursors are not from the binary cursor you opened. These binary cursors are not useful
useful if you are running the query engine on a system with a different if you are running the query engine on a system with a different
architecture than the database server. If you pass a NULL pointer, the architecture than the database server. If you pass a NULL pointer, the
column is skipped, and you can use libpq to handle it as you wish. column is skipped, and you can use libpq to handle it as you wish.
There are two functions, get_result() and set_result, that allow you to There are two functions, get_result() and set_result(), that allow you
handle multiple result sets at the same time. to handle multiple result sets at the same time.
There is a reset_fetch() that starts the fetch back at the beginning. There is a reset_fetch() that starts the fetch back at the beginning.

View File

@ -0,0 +1,71 @@
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
.\" $Header: /cvsroot/pgsql/contrib/pginterface/Attic/pginterface.3,v 1.1 1998/09/11 05:14:08 momjian Exp $
.TH PGINTERFACE INTRO 08/08/98 PostgreSQL PostgreSQL
.SH DESCRIPTION
Pginterface allows you to cleanly interface to the libpq library,
more like a 4gl SQL interface.
.PP
It consists of set of simplified C functions that encapsulate the
functionality of libpq.
The functions are:
.nf
PGresult *doquery(char *query);
PGconn *connectdb();
void disconnectdb();
int fetch(void *param,...);
int fetchwithnulls(void *param,...);
void reset_fetch();
void on_error_continue();
void on_error_stop();
PGresult *get_result();
void set_result(PGresult *newres);
void unset_result(PGresult *oldres);
.fi
.PP
Many functions return a structure or value, so you can do more work
with the result if required.
.PP
You basically connect to the database with
.BR connectdb ,
issue your query with
.BR doquery ,
fetch the results with
.BR fetch ,
and finish with
.BR disconnectdb .
.PP
For
.IR select
queries,
.BR fetch
allows you to pass pointers as parameters, and on return the variables
are filled with data from the binary cursor you opened. These binary
cursors can not be used if you are running the
.BR pginterface
client on a system with a different architecture than the database
server. If you pass a NULL pointer parameter, the column is skipped.
.BR fetchwithnulls
allows you to retieve the
.IR null
status of the field by passing an
.IR int*
after each result pointer, which returns true or false if the field is null.
You can always use libpq functions on the PGresult pointer returned by
.BR doquery .
.BR reset_fetch
starts the fetch back at the beginning.
.PP
.BR get_result ,
.BR set_result ,
and
.BR unset_result
allow you to handle multiple result sets at the same time.
.PP
There are a variety of demonstration programs in the
.BR pginterface
source directory.

View File

@ -13,6 +13,14 @@
#define NUL '\0' #define NUL '\0'
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* GLOBAL VARIABLES */ /* GLOBAL VARIABLES */
static PGconn *conn; static PGconn *conn;
static PGresult *res = NULL; static PGresult *res = NULL;
@ -22,8 +30,8 @@ static PGresult *res = NULL;
static int on_error_state = ON_ERROR_STOP; static int on_error_state = ON_ERROR_STOP;
static in_result_block = false; static in_result_block = FALSE;
static was_get_unset_result = false; static was_get_unset_result = FALSE;
/* LOCAL VARIABLES */ /* LOCAL VARIABLES */
static int tuple; static int tuple;
@ -67,10 +75,10 @@ disconnectdb()
PGresult * PGresult *
doquery(char *query) doquery(char *query)
{ {
if (res != NULL && in_result_block == false && was_get_unset_result == false) if (res != NULL && in_result_block == FALSE && was_get_unset_result == FALSE)
PQclear(res); PQclear(res);
was_get_unset_result = false; was_get_unset_result = FALSE;
res = PQexec(conn, query); res = PQexec(conn, query);
if (on_error_state == ON_ERROR_STOP && if (on_error_state == ON_ERROR_STOP &&
@ -131,7 +139,7 @@ fetch(void *param,...)
** **
** fetchwithnulls - returns tuple number (starts at 0), ** fetchwithnulls - returns tuple number (starts at 0),
** or the value END_OF_TUPLES ** or the value END_OF_TUPLES
** Returns true or false into null indicator variables ** Returns TRUE or FALSE into null indicator variables
** NULL pointers are skipped ** NULL pointers are skipped
*/ */
int int
@ -200,9 +208,14 @@ on_error_continue()
*/ */
PGresult *get_result() PGresult *get_result()
{ {
was_get_unset_result = true; char *cmdstatus = PQcmdStatus(res);
was_get_unset_result = TRUE;
/* we have to store the fetch location somewhere */ /* we have to store the fetch location somewhere */
memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)],&tuple, sizeof(tuple)); cmdstatus[0] = NUL;
memcpy(&cmdstatus[1],&tuple, sizeof(tuple));
return res; return res;
} }
@ -213,18 +226,27 @@ PGresult *get_result()
*/ */
void set_result(PGresult *newres) void set_result(PGresult *newres)
{ {
char *cmdstatus = PQcmdStatus(res);
if (newres == NULL) if (newres == NULL)
halt("set_result called with null result pointer\n"); halt("set_result called with null result pointer\n");
if (res != NULL && was_get_unset_result == false) if (res != NULL && was_get_unset_result == FALSE)
if (in_result_block == false) if (in_result_block == FALSE)
PQclear(res); PQclear(res);
else else
memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple)); {
cmdstatus[0] = NUL;
memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
}
in_result_block = TRUE;
was_get_unset_result = FALSE;
cmdstatus = PQcmdStatus(newres);
memcpy(&tuple, &cmdstatus[1], sizeof(tuple));
in_result_block = true;
was_get_unset_result = false;
memcpy(&tuple, &newres->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], sizeof(tuple));
res = newres; res = newres;
} }
@ -236,15 +258,18 @@ void set_result(PGresult *newres)
*/ */
void unset_result(PGresult *oldres) void unset_result(PGresult *oldres)
{ {
char *cmdstatus = PQcmdStatus(oldres);
if (oldres == NULL) if (oldres == NULL)
halt("unset_result called with null result pointer\n"); halt("unset_result called with null result pointer\n");
if (in_result_block == false) if (in_result_block == FALSE)
halt("Unset of result without being set.\n"); halt("Unset of result without being set.\n");
was_get_unset_result = true; was_get_unset_result = TRUE;
memcpy(&oldres->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple)); cmdstatus[0] = NUL;
in_result_block = false; memcpy(&cmdstatus[1], &tuple, sizeof(tuple));
in_result_block = FALSE;
} }
/* /*