655
653
bool fail = true; /* default to backward compatible */
656
654
bool freeconn = false;
658
/* check to see if caller supports us returning a tuplestore */
659
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
661
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
662
errmsg("set-valued function called in context that cannot accept a set")));
663
if (!(rsinfo->allowedModes & SFRM_Materialize))
665
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
666
errmsg("materialize mode required, but it is not " \
667
"allowed in this context")));
656
prepTuplestoreResult(fcinfo);
760
* Materialize the PGresult to return them as the function result.
761
* The res will be released in this function.
744
* Verify function caller can handle a tuplestore result, and set up for that.
746
* Note: if the caller returns without actually creating a tuplestore, the
747
* executor will treat the function result as an empty set.
750
prepTuplestoreResult(FunctionCallInfo fcinfo)
752
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
754
/* check to see if query supports us returning a tuplestore */
755
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
757
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
758
errmsg("set-valued function called in context that cannot accept a set")));
759
if (!(rsinfo->allowedModes & SFRM_Materialize))
761
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
762
errmsg("materialize mode required, but it is not allowed in this context")));
764
/* let the executor know we're sending back a tuplestore */
765
rsinfo->returnMode = SFRM_Materialize;
767
/* caller must fill these to return a non-empty result */
768
rsinfo->setResult = NULL;
769
rsinfo->setDesc = NULL;
773
* Copy the contents of the PGresult into a tuplestore to be returned
774
* as the result of the current function.
775
* The PGresult will be released in this function.
764
778
materializeResult(FunctionCallInfo fcinfo, PGresult *res)
766
780
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
782
/* prepTuplestoreResult must have been called previously */
768
783
Assert(rsinfo->returnMode == SFRM_Materialize);
1014
1032
dblink_exec(PG_FUNCTION_ARGS)
1017
PGresult *res = NULL;
1018
text *sql_cmd_status = NULL;
1019
PGconn *conn = NULL;
1020
char *connstr = NULL;
1022
char *conname = NULL;
1023
remoteConn *rconn = NULL;
1024
bool freeconn = false;
1025
bool fail = true; /* default to backward compatible behavior */
1034
text *volatile sql_cmd_status = NULL;
1035
PGconn *volatile conn = NULL;
1036
volatile bool freeconn = false;
1029
if (PG_NARGS() == 3)
1031
/* must be text,text,bool */
1033
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
1034
fail = PG_GETARG_BOOL(2);
1036
else if (PG_NARGS() == 2)
1038
/* might be text,text or text,bool */
1039
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
1043
PGresult *res = NULL;
1044
char *connstr = NULL;
1046
char *conname = NULL;
1047
remoteConn *rconn = NULL;
1048
bool fail = true; /* default to backward compatible behavior */
1050
if (PG_NARGS() == 3)
1052
/* must be text,text,bool */
1054
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
1055
fail = PG_GETARG_BOOL(2);
1057
else if (PG_NARGS() == 2)
1059
/* might be text,text or text,bool */
1060
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
1063
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
1064
fail = PG_GETARG_BOOL(1);
1069
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
1072
else if (PG_NARGS() == 1)
1074
/* must be single text argument */
1041
1075
conn = pconn->conn;
1042
1076
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
1043
fail = PG_GETARG_BOOL(1);
1048
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
1051
else if (PG_NARGS() == 1)
1053
/* must be single text argument */
1055
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
1058
/* shouldn't happen */
1059
elog(ERROR, "wrong number of arguments");
1062
DBLINK_CONN_NOT_AVAIL;
1064
res = PQexec(conn, sql);
1066
(PQresultStatus(res) != PGRES_COMMAND_OK &&
1067
PQresultStatus(res) != PGRES_TUPLES_OK))
1069
dblink_res_error(conname, res, "could not execute command", fail);
1072
* and save a copy of the command status string to return as our
1075
sql_cmd_status = cstring_to_text("ERROR");
1077
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
1080
* and save a copy of the command status string to return as our
1083
sql_cmd_status = cstring_to_text(PQcmdStatus(res));
1090
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
1091
errmsg("statement returning results not allowed")));
1094
/* if needed, close the connection to the database and cleanup */
1079
/* shouldn't happen */
1080
elog(ERROR, "wrong number of arguments");
1083
DBLINK_CONN_NOT_AVAIL;
1085
res = PQexec(conn, sql);
1087
(PQresultStatus(res) != PGRES_COMMAND_OK &&
1088
PQresultStatus(res) != PGRES_TUPLES_OK))
1090
dblink_res_error(conname, res, "could not execute command", fail);
1093
* and save a copy of the command status string to return as our
1096
sql_cmd_status = cstring_to_text("ERROR");
1098
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
1101
* and save a copy of the command status string to return as our
1104
sql_cmd_status = cstring_to_text(PQcmdStatus(res));
1111
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
1112
errmsg("statement returning results not allowed")));
1117
/* if needed, close the connection to the database */
1124
/* if needed, close the connection to the database */
1096
1126
PQfinish(conn);