Skip to content

Commit db02073

Browse files
committed
Fix dblink_get_result() as reported by Oleksiy Shchukin. Refactor a bit
while we're at it per request by Tom Lane. Specifically, don't try to perform dblink_send_query() via dblink_record_internal() -- it was inappropriate and ugly.
1 parent bac2ad3 commit db02073

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

contrib/dblink/dblink.c

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Darko Prenosil <Darko.Prenosil@finteh.hr>
99
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
1010
*
11-
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.77 2009/01/01 17:23:31 momjian Exp $
11+
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.78 2009/06/02 03:21:56 joe Exp $
1212
* Copyright (c) 2001-2009, PostgreSQL Global Development Group
1313
* ALL RIGHTS RESERVED;
1414
*
@@ -77,7 +77,7 @@ typedef struct remoteConn
7777
/*
7878
* Internal declarations
7979
*/
80-
static Datum dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get);
80+
static Datum dblink_record_internal(FunctionCallInfo fcinfo, bool is_async);
8181
static remoteConn *getConnectionByName(const char *name);
8282
static HTAB *createConnHash(void);
8383
static void createNewConnection(const char *name, remoteConn * rconn);
@@ -689,25 +689,47 @@ PG_FUNCTION_INFO_V1(dblink_record);
689689
Datum
690690
dblink_record(PG_FUNCTION_ARGS)
691691
{
692-
return dblink_record_internal(fcinfo, false, false);
692+
return dblink_record_internal(fcinfo, false);
693693
}
694694

695695
PG_FUNCTION_INFO_V1(dblink_send_query);
696696
Datum
697697
dblink_send_query(PG_FUNCTION_ARGS)
698698
{
699-
return dblink_record_internal(fcinfo, true, false);
699+
PGconn *conn = NULL;
700+
char *connstr = NULL;
701+
char *sql = NULL;
702+
remoteConn *rconn = NULL;
703+
char *msg;
704+
bool freeconn = false;
705+
int retval;
706+
707+
if (PG_NARGS() == 2)
708+
{
709+
DBLINK_GET_CONN;
710+
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
711+
}
712+
else
713+
/* shouldn't happen */
714+
elog(ERROR, "wrong number of arguments");
715+
716+
/* async query send */
717+
retval = PQsendQuery(conn, sql);
718+
if (retval != 1)
719+
elog(NOTICE, "%s", PQerrorMessage(conn));
720+
721+
PG_RETURN_INT32(retval);
700722
}
701723

702724
PG_FUNCTION_INFO_V1(dblink_get_result);
703725
Datum
704726
dblink_get_result(PG_FUNCTION_ARGS)
705727
{
706-
return dblink_record_internal(fcinfo, true, true);
728+
return dblink_record_internal(fcinfo, true);
707729
}
708730

709731
static Datum
710-
dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
732+
dblink_record_internal(FunctionCallInfo fcinfo, bool is_async)
711733
{
712734
FuncCallContext *funcctx;
713735
TupleDesc tupdesc = NULL;
@@ -775,14 +797,14 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
775797
/* shouldn't happen */
776798
elog(ERROR, "wrong number of arguments");
777799
}
778-
else if (is_async && do_get)
800+
else /* is_async */
779801
{
780802
/* get async result */
781803
if (PG_NARGS() == 2)
782804
{
783805
/* text,bool */
784806
DBLINK_GET_CONN;
785-
fail = PG_GETARG_BOOL(2);
807+
fail = PG_GETARG_BOOL(1);
786808
}
787809
else if (PG_NARGS() == 1)
788810
{
@@ -793,24 +815,10 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
793815
/* shouldn't happen */
794816
elog(ERROR, "wrong number of arguments");
795817
}
796-
else
797-
{
798-
/* send async query */
799-
if (PG_NARGS() == 2)
800-
{
801-
DBLINK_GET_CONN;
802-
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
803-
}
804-
else
805-
/* shouldn't happen */
806-
elog(ERROR, "wrong number of arguments");
807-
}
808818

809819
if (!conn)
810820
DBLINK_CONN_NOT_AVAIL;
811821

812-
if (!is_async || (is_async && do_get))
813-
{
814822
/* synchronous query, or async result retrieval */
815823
if (!is_async)
816824
res = PQexec(conn, sql);
@@ -911,19 +919,6 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
911919
funcctx->attinmeta = attinmeta;
912920

913921
MemoryContextSwitchTo(oldcontext);
914-
}
915-
else
916-
{
917-
/* async query send */
918-
MemoryContextSwitchTo(oldcontext);
919-
PG_RETURN_INT32(PQsendQuery(conn, sql));
920-
}
921-
}
922-
923-
if (is_async && !do_get)
924-
{
925-
/* async query send -- should not happen */
926-
elog(ERROR, "async query send called more than once");
927922

928923
}
929924

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy