OSDN Git Service

Update pginterface for 6.4. add manual page.
authorBruce Momjian <bruce@momjian.us>
Fri, 11 Sep 1998 05:14:08 +0000 (05:14 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 11 Sep 1998 05:14:08 +0000 (05:14 +0000)
contrib/pginterface/README
contrib/pginterface/pginterface.3 [new file with mode: 0644]
contrib/pginterface/pginterface.c

index 32b0957..1d4acdd 100644 (file)
@@ -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 (file)
index 0000000..22dfb5d
--- /dev/null
@@ -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.
index 52b1011..42e4a3c 100644 (file)
 
 #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;
 }
 
 /*