From a5d4642578e1ea4f2c414e7c94d8115890c0a814 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 11 Sep 1998 05:14:08 +0000 Subject: [PATCH] Update pginterface for 6.4. add manual page. --- contrib/pginterface/README | 10 ++--- contrib/pginterface/pginterface.3 | 71 +++++++++++++++++++++++++++++++ contrib/pginterface/pginterface.c | 61 ++++++++++++++++++-------- 3 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 contrib/pginterface/pginterface.3 diff --git a/contrib/pginterface/README b/contrib/pginterface/README index 32b0957242..1d4acdd99b 100644 --- a/contrib/pginterface/README +++ b/contrib/pginterface/README @@ -16,14 +16,14 @@ have a global variable that allows you to disable the error checking I have added to the doquery() routine. I have added a function called fetch(), which allows you to pass -pointers as parameters, and on return the variables are filled with the -data from the binary cursor you opened. These binary cursors are not -useful if you are running the query engine on a system with a different +pointers as parameters, and on return the variables are filled with data +from the binary cursor you opened. These binary cursors are not useful +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 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 -handle multiple result sets at the same time. +There are two functions, get_result() and set_result(), that allow you +to handle multiple result sets at the same time. There is a reset_fetch() that starts the fetch back at the beginning. diff --git a/contrib/pginterface/pginterface.3 b/contrib/pginterface/pginterface.3 new file mode 100644 index 0000000000..22dfb5d559 --- /dev/null +++ b/contrib/pginterface/pginterface.3 @@ -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. diff --git a/contrib/pginterface/pginterface.c b/contrib/pginterface/pginterface.c index 52b1011e76..42e4a3c47c 100644 --- a/contrib/pginterface/pginterface.c +++ b/contrib/pginterface/pginterface.c @@ -13,6 +13,14 @@ #define NUL '\0' +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + /* GLOBAL VARIABLES */ static PGconn *conn; static PGresult *res = NULL; @@ -22,8 +30,8 @@ static PGresult *res = NULL; static int on_error_state = ON_ERROR_STOP; -static in_result_block = false; -static was_get_unset_result = false; +static in_result_block = FALSE; +static was_get_unset_result = FALSE; /* LOCAL VARIABLES */ static int tuple; @@ -67,10 +75,10 @@ disconnectdb() PGresult * 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); - was_get_unset_result = false; + was_get_unset_result = FALSE; res = PQexec(conn, query); if (on_error_state == ON_ERROR_STOP && @@ -131,7 +139,7 @@ fetch(void *param,...) ** ** fetchwithnulls - returns tuple number (starts at 0), ** 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 */ int @@ -200,9 +208,14 @@ on_error_continue() */ 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 */ - memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)],&tuple, sizeof(tuple)); + cmdstatus[0] = NUL; + memcpy(&cmdstatus[1],&tuple, sizeof(tuple)); + return res; } @@ -213,18 +226,27 @@ PGresult *get_result() */ void set_result(PGresult *newres) { + + char *cmdstatus = PQcmdStatus(res); + if (newres == NULL) halt("set_result called with null result pointer\n"); - if (res != NULL && was_get_unset_result == false) - if (in_result_block == false) + if (res != NULL && was_get_unset_result == FALSE) + if (in_result_block == FALSE) PQclear(res); else - memcpy(&res->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple)); - - in_result_block = true; - was_get_unset_result = false; - memcpy(&tuple, &newres->cmdStatus[CMDSTATUS_LEN-sizeof(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)); + res = newres; } @@ -236,15 +258,18 @@ void set_result(PGresult *newres) */ void unset_result(PGresult *oldres) { + char *cmdstatus = PQcmdStatus(oldres); + if (oldres == NULL) 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"); - was_get_unset_result = true; - memcpy(&oldres->cmdStatus[CMDSTATUS_LEN-sizeof(tuple)], &tuple, sizeof(tuple)); - in_result_block = false; + was_get_unset_result = TRUE; + cmdstatus[0] = NUL; + memcpy(&cmdstatus[1], &tuple, sizeof(tuple)); + in_result_block = FALSE; } /* -- 2.40.0