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 );
8181static remoteConn * getConnectionByName (const char * name );
8282static HTAB * createConnHash (void );
8383static void createNewConnection (const char * name ,remoteConn * rconn );
@@ -689,25 +689,47 @@ PG_FUNCTION_INFO_V1(dblink_record);
689689Datum
690690dblink_record (PG_FUNCTION_ARGS )
691691{
692- return dblink_record_internal (fcinfo , false, false );
692+ return dblink_record_internal (fcinfo , false);
693693}
694694
695695PG_FUNCTION_INFO_V1 (dblink_send_query );
696696Datum
697697dblink_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
702724PG_FUNCTION_INFO_V1 (dblink_get_result );
703725Datum
704726dblink_get_result (PG_FUNCTION_ARGS )
705727{
706- return dblink_record_internal (fcinfo , true, true );
728+ return dblink_record_internal (fcinfo , true);
707729}
708730
709731static Datum
710- dblink_record_internal (FunctionCallInfo fcinfo ,bool is_async , bool do_get )
732+ dblink_record_internal (FunctionCallInfo fcinfo ,bool is_async )
711733{
712734FuncCallContext * funcctx ;
713735TupleDesc tupdesc = NULL ;
@@ -775,14 +797,14 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
775797/* shouldn't happen */
776798elog (ERROR ,"wrong number of arguments" );
777799}
778- else if ( is_async && do_get )
800+ else /* is_async*/
779801{
780802/* get async result */
781803if (PG_NARGS ()== 2 )
782804{
783805/* text,bool */
784806DBLINK_GET_CONN ;
785- fail = PG_GETARG_BOOL (2 );
807+ fail = PG_GETARG_BOOL (1 );
786808}
787809else if (PG_NARGS ()== 1 )
788810{
@@ -793,24 +815,10 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
793815/* shouldn't happen */
794816elog (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
809819if (!conn )
810820DBLINK_CONN_NOT_AVAIL ;
811821
812- if (!is_async || (is_async && do_get ))
813- {
814822/* synchronous query, or async result retrieval */
815823if (!is_async )
816824res = PQexec (conn ,sql );
@@ -911,19 +919,6 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
911919funcctx -> attinmeta = attinmeta ;
912920
913921MemoryContextSwitchTo (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