@@ -57,15 +57,15 @@ dblink(PG_FUNCTION_ARGS)
57
57
conn = PQconnectdb (optstr );
58
58
if (PQstatus (conn ) == CONNECTION_BAD )
59
59
{
60
- msg = PQerrorMessage (conn );
60
+ msg = pstrdup ( PQerrorMessage (conn ) );
61
61
PQfinish (conn );
62
62
elog (ERROR , "dblink: connection error: %s" , msg );
63
63
}
64
64
65
65
res = PQexec (conn , "BEGIN" );
66
66
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
67
67
{
68
- msg = PQerrorMessage (conn );
68
+ msg = pstrdup ( PQerrorMessage (conn ) );
69
69
PQclear (res );
70
70
PQfinish (conn );
71
71
elog (ERROR , "dblink: begin error: %s" , msg );
@@ -84,7 +84,7 @@ dblink(PG_FUNCTION_ARGS)
84
84
res = PQexec (conn , execstatement );
85
85
if (!res || (PQresultStatus (res ) != PGRES_COMMAND_OK && PQresultStatus (res ) != PGRES_TUPLES_OK ))
86
86
{
87
- msg = PQerrorMessage (conn );
87
+ msg = pstrdup ( PQerrorMessage (conn ) );
88
88
PQclear (res );
89
89
PQfinish (conn );
90
90
elog (ERROR , "dblink: sql error: %s" , msg );
@@ -96,7 +96,7 @@ dblink(PG_FUNCTION_ARGS)
96
96
97
97
res = PQexec (conn , "FETCH ALL in mycursor" );
98
98
if (!res || PQresultStatus (res ) != PGRES_TUPLES_OK ) {
99
- msg = PQerrorMessage (conn );
99
+ msg = pstrdup ( PQerrorMessage (conn ) );
100
100
PQclear (res );
101
101
PQfinish (conn );
102
102
elog (ERROR , "dblink: sql error: %s" , msg );
@@ -230,20 +230,28 @@ dblink_tok(PG_FUNCTION_ARGS)
230
230
elog (ERROR , "dblink: field number %d does not exist" , fldnum );
231
231
}
232
232
233
- text_len = PQgetlength ( results -> res , results -> tup_num , fldnum );
233
+ if ( PQgetisnull ( results -> res , results -> tup_num , fldnum ) == 1 ) {
234
234
235
- result = ( char * ) palloc ( text_len + 1 );
235
+ PG_RETURN_NULL ( );
236
236
237
- if (result != NULL ) {
238
- strcpy (result , PQgetvalue (results -> res , results -> tup_num , fldnum ));
239
- strcat (result , "\0" );
240
237
} else {
241
- elog (ERROR , "dblink: insufficient memory" );
242
- }
243
238
244
- result_text = DatumGetTextP (DirectFunctionCall1 (textin , CStringGetDatum (result )));
239
+ text_len = PQgetlength (results -> res , results -> tup_num , fldnum );
240
+
241
+ result = (char * ) palloc (text_len + 1 );
245
242
246
- PG_RETURN_TEXT_P (result_text );
243
+ if (result != NULL ) {
244
+ strcpy (result , PQgetvalue (results -> res , results -> tup_num , fldnum ));
245
+ strcat (result , "\0" );
246
+ } else {
247
+ elog (ERROR , "dblink: insufficient memory" );
248
+ }
249
+
250
+ result_text = DatumGetTextP (DirectFunctionCall1 (textin , CStringGetDatum (result )));
251
+
252
+ PG_RETURN_TEXT_P (result_text );
253
+
254
+ }
247
255
}
248
256
249
257
0 commit comments