Updated version.
authorBruce Momjian <[email protected]>
Thu, 26 Sep 1996 00:48:48 +0000 (00:48 +0000)
committerBruce Momjian <[email protected]>
Thu, 26 Sep 1996 00:48:48 +0000 (00:48 +0000)
contrib/pginterface/pginterface.c
contrib/pginterface/pginterface.h
contrib/pginterface/pgnulltest.c

index 58f7cfa7eb614c233e729ba5166c80c87d32e118..7593cd9f97f9096daf75de3f92fa56a60c55ace1 100644 (file)
@@ -99,15 +99,15 @@ PGresult *doquery(char *query)
 int fetch(void *param, ...)
 {
    va_list ap;
-   int arg, num_args;
+   int arg, num_fields;
 
-   num_args = PQnfields(res);
+   num_fields = PQnfields(res);
 
    if (tuple >= PQntuples(res))
        return END_OF_TUPLES;
 
    va_start(ap, param);
-   for (arg = 0; arg < num_args; arg++)
+   for (arg = 0; arg < num_fields; arg++)
    {
        if (param != NULL)
        {
@@ -127,36 +127,43 @@ int fetch(void *param, ...)
 
 /*
 **
-** fetchisnull - returns tuple number (starts at 0), or the value END_OF_TUPLES
-**             NULL pointers are skipped
+** fetchwithnulls - returns tuple number (starts at 0),
+**                                         or the value END_OF_TUPLES
 **             Returns true or false into null indicator variables
+**             NULL pointers are skipped
 */
-int fetchisnull(void *param, ...)
+int fetchwithnulls(void *param, ...)
 {
    va_list ap;
-   int arg, num_args;
+   int arg, num_fields;
 
-   if (tuple == 0)
-       halt("pginterface:fetchisnull():  You must call fetch() first.\n");
+   num_fields = PQnfields(res);
 
-   num_args = PQnfields(res);
-
-   if (tuple-1 >= PQntuples(res))
+   if (tuple >= PQntuples(res))
        return END_OF_TUPLES;
+
    va_start(ap, param);
-   for (arg = 0; arg < num_args; arg++)
+   for (arg = 0; arg < num_fields; arg++)
    {
        if (param != NULL)
        {
-           if (PQgetisnull(res,tuple-1,arg) != 0)
-               *(int *)param = 1;
+           if (PQfsize(res, arg) == -1)
+           {
+               memcpy(param,PQgetvalue(res,tuple,arg),PQgetlength(res,tuple,arg));
+               ((char *)param)[PQgetlength(res,tuple,arg)] = NUL;
+           }
            else
-               *(int *)param = 0;
+               memcpy(param,PQgetvalue(res,tuple,arg),PQfsize(res,arg));
        }
        param = va_arg(ap, char *);
+       if (PQgetisnull(res,tuple,arg) != 0)
+           *(int *)param = 1;
+       else
+           *(int *)param = 0;
+       param = va_arg(ap, char *);
    }
    va_end(ap);
-   return tuple-1;
+   return tuple++;
 }
 
 /*
index d73739f4859374fffa370ae8a4c4c11900beeea2..7e9cbdb5eb8880520eb92b9d036511bbbfe81463 100644 (file)
@@ -7,7 +7,7 @@ PGresult *doquery(char *query);
 PGconn     *connectdb();
 void   disconnectdb();
 int    fetch(void *param, ...);
-int    fetchisnull(void *param, ...);
+int    fetchwithnulls(void *param, ...);
 void   on_error_continue();
 void   on_error_stop();
 
index 85e8a0a6d6d5b9c893a8c217f9063e8d86e25804..b99292113d8c999af732cf8b5f561ef69b4b1c36 100644 (file)
@@ -3,14 +3,14 @@
  *
 */
 
-/*#define TEST_NON_NULLS*/
+#define TEST_NON_NULLS
 
 #include <stdio.h>
 #include <signal.h>
 #include <time.h>
+#include <halt.h>
 #include <libpq-fe.h>
-#include "halt.h"
-#include "pginterface.h"
+#include <pginterface.h>
 
 int main(int argc, char **argv)
 {
@@ -84,16 +84,25 @@ int main(int argc, char **argv)
 
    doquery("FETCH ALL IN c_testfetch");
 
-   if (fetch(
+   if (fetchwithnulls(
        &aint,
+       &aint_null,
        &afloat,
+       &afloat_null,
        &adouble,
+       &adouble_null,
        achar,
+       &achar_null,
        achar16,
+       &achar16_null,
        abpchar,
+       &abpchar_null,
        avarchar,
+       &avarchar_null,
        atext,
-       &aabstime) != END_OF_TUPLES)
+       &atext_null,
+       &aabstime,
+       &aabstime_null) != END_OF_TUPLES)
            printf("int %d\nfloat %f\ndouble %f\nchar %s\nchar16 %s\n\
 bpchar %s\nvarchar %s\ntext %s\nabstime %s\n",
            aint,
@@ -105,16 +114,6 @@ bpchar %s\nvarchar %s\ntext %s\nabstime %s\n",
            avarchar,
            atext,
            ctime(&aabstime));
-   if (fetchisnull(
-       &aint_null,
-       &afloat_null,
-       &adouble_null,
-       &achar_null,
-       &achar16_null,
-       &abpchar_null,
-       &avarchar_null,
-       &atext_null,
-       &aabstime_null) != END_OF_TUPLES)
            printf("NULL:\nint %d\nfloat %d\ndouble %d\nchar %d\nchar16 %d\n\
 bpchar %d\nvarchar %d\ntext %d\nabstime %d\n",
            aint_null,
@@ -130,7 +129,7 @@ bpchar %d\nvarchar %d\ntext %d\nabstime %d\n",
 
    doquery("CLOSE c_testfetch");
    doquery("COMMIT WORK");
-   printf("--- 1 row inserted\n");
+   printf("--- %-d rows inserted so far\n",row);
 
    row++;