Here's a small patch for dblink:
authorBruce Momjian
Mon, 18 Jun 2001 19:09:50 +0000 (19:09 +0000)
committerBruce Momjian
Mon, 18 Jun 2001 19:09:50 +0000 (19:09 +0000)
   - fixed dblink invalid pointer causing corrupt elog message
   - fixed dblink_tok improper handling of null results
   - fixed examples in README.dblink

Joe Conway

contrib/dblink/README.dblink
contrib/dblink/dblink.c

index 04219b7be24287c1b7dbfb5dadf69cf753269f17..c56720d2fbeb4852b0b58eb622b7dddfa519f690 100644 (file)
  *
  */
 
-Version 0.2 (29 May, 2001):
+
+Version 0.3 (14 June, 2001):
   Function to test returning data set from remote database
   Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel
 
 Release Notes:
 
+  Version 0.3
+    - fixed dblink invalid pointer causing corrupt elog message
+    - fixed dblink_tok improper handling of null results
+    - fixed examples in README.dblink
+
   Version 0.2
     - initial release    
 
@@ -124,11 +130,11 @@ to 'fake' a UNION, e.g.
                    'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
                   ,'select f1, f2 from mytable'
                  ) as dblink_p
-    union
-    select null,null where false
+    union all
+    select null where false
    ) as t1
   where
-    f1 = 'mycriteria';
+    dblink_tok(t1.dblink_p,0) = 'mycriteria';
 
 in order to work around an issue with the query optimizer. A more convenient way to approach
 this problem is to create a view:
@@ -143,8 +149,8 @@ this problem is to create a view:
                    'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
                   ,'select f1, f2 from mytable'
                  ) as dblink_p
-    union
-    select null,null where false
+    union all
+    select null where false
    ) as t1;
 
 Then you can simply write:
index b3c9c6cbbf236f186722353671ba2047838ec530..d457a76cbf9a8968c7be67823d2af3f7853fea8f 100644 (file)
@@ -57,7 +57,7 @@ dblink(PG_FUNCTION_ARGS)
        conn = PQconnectdb(optstr);
        if (PQstatus(conn) == CONNECTION_BAD)
        {
-           msg = PQerrorMessage(conn);
+           msg = pstrdup(PQerrorMessage(conn));
            PQfinish(conn);
            elog(ERROR, "dblink: connection error: %s", msg);
        }
@@ -65,7 +65,7 @@ dblink(PG_FUNCTION_ARGS)
        res = PQexec(conn, "BEGIN");
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
-           msg = PQerrorMessage(conn);
+           msg = pstrdup(PQerrorMessage(conn));
            PQclear(res);
            PQfinish(conn);
            elog(ERROR, "dblink: begin error: %s", msg);
@@ -84,7 +84,7 @@ dblink(PG_FUNCTION_ARGS)
        res = PQexec(conn, execstatement);
        if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK))
        {
-           msg = PQerrorMessage(conn);
+           msg = pstrdup(PQerrorMessage(conn));
            PQclear(res);
            PQfinish(conn);
            elog(ERROR, "dblink: sql error: %s", msg);
@@ -96,7 +96,7 @@ dblink(PG_FUNCTION_ARGS)
 
            res = PQexec(conn, "FETCH ALL in mycursor");
            if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
-               msg = PQerrorMessage(conn);
+               msg = pstrdup(PQerrorMessage(conn));
                PQclear(res);
                PQfinish(conn);
                elog(ERROR, "dblink: sql error: %s", msg);
@@ -230,20 +230,28 @@ dblink_tok(PG_FUNCTION_ARGS)
        elog(ERROR, "dblink: field number %d does not exist", fldnum);
    }
 
-   text_len = PQgetlength(results->res, results->tup_num, fldnum);
+   if (PQgetisnull(results->res, results->tup_num, fldnum) == 1) {
 
-   result = (char *) palloc(text_len + 1);
+       PG_RETURN_NULL();
 
-   if (result != NULL) {
-       strcpy(result, PQgetvalue(results->res, results->tup_num, fldnum));
-       strcat(result, "\0");
    } else {
-       elog(ERROR, "dblink: insufficient memory" );
-   }
 
-   result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(result)));
+       text_len = PQgetlength(results->res, results->tup_num, fldnum);
+
+       result = (char *) palloc(text_len + 1);
 
-   PG_RETURN_TEXT_P(result_text);
+       if (result != NULL) {
+           strcpy(result, PQgetvalue(results->res, results->tup_num, fldnum));
+           strcat(result, "\0");
+       } else {
+           elog(ERROR, "dblink: insufficient memory" );
+       }
+
+       result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(result)));
+
+       PG_RETURN_TEXT_P(result_text);
+
+   }
 }