diff options
Diffstat (limited to 'contrib/dblink/dblink.c')
-rw-r--r-- | contrib/dblink/dblink.c | 169 |
1 files changed, 89 insertions, 80 deletions
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index e858cfd0ba..4ab1315fa7 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -4,18 +4,18 @@ * Functions returning results from a remote database * * Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001; - * + * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without a written agreement * is hereby granted, provided that the above copyright notice and this * paragraph and the following two paragraphs appear in all copies. - * + * * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. - * + * * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS @@ -30,29 +30,28 @@ PG_FUNCTION_INFO_V1(dblink); Datum dblink(PG_FUNCTION_ARGS) { - PGconn *conn = NULL; - PGresult *res = NULL; - dblink_results *results; - char *optstr; - char *sqlstatement; - char *curstr = "DECLARE mycursor CURSOR FOR "; - char *execstatement; - char *msg; - int ntuples = 0; - ReturnSetInfo *rsi; - - if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) { + PGconn *conn = NULL; + PGresult *res = NULL; + dblink_results *results; + char *optstr; + char *sqlstatement; + char *curstr = "DECLARE mycursor CURSOR FOR "; + char *execstatement; + char *msg; + int ntuples = 0; + ReturnSetInfo *rsi; + + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) elog(ERROR, "dblink: NULL arguments are not permitted"); - } - if (fcinfo->resultinfo == NULL || ! IsA(fcinfo->resultinfo, ReturnSetInfo)) { + if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo)) elog(ERROR, "dblink: function called in context that does not accept a set result"); - } optstr = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0)))); sqlstatement = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1)))); - if (fcinfo->flinfo->fn_extra == NULL) { + if (fcinfo->flinfo->fn_extra == NULL) + { conn = PQconnectdb(optstr); if (PQstatus(conn) == CONNECTION_BAD) @@ -73,13 +72,14 @@ dblink(PG_FUNCTION_ARGS) PQclear(res); execstatement = (char *) palloc(strlen(curstr) + strlen(sqlstatement) + 1); - if (execstatement != NULL) { + if (execstatement != NULL) + { strcpy(execstatement, curstr); strcat(execstatement, sqlstatement); strcat(execstatement, "\0"); - } else { - elog(ERROR, "dblink: insufficient memory" ); } + else + elog(ERROR, "dblink: insufficient memory"); res = PQexec(conn, execstatement); if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK)) @@ -88,23 +88,27 @@ dblink(PG_FUNCTION_ARGS) PQclear(res); PQfinish(conn); elog(ERROR, "dblink: sql error: %s", msg); - } else { + } + else + { /* * got results, start fetching them */ - PQclear(res); + PQclear(res); - res = PQexec(conn, "FETCH ALL in mycursor"); - if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { + res = PQexec(conn, "FETCH ALL in mycursor"); + if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) + { msg = pstrdup(PQerrorMessage(conn)); PQclear(res); PQfinish(conn); elog(ERROR, "dblink: sql error: %s", msg); - } + } ntuples = PQntuples(res); - if (ntuples > 0) { + if (ntuples > 0) + { results = init_dblink_results(fcinfo->flinfo->fn_mcxt); results->tup_num = 0; @@ -116,44 +120,48 @@ dblink(PG_FUNCTION_ARGS) results = NULL; results = fcinfo->flinfo->fn_extra; - /* close the cursor */ - res = PQexec(conn, "CLOSE mycursor"); - PQclear(res); + /* close the cursor */ + res = PQexec(conn, "CLOSE mycursor"); + PQclear(res); - /* commit the transaction */ - res = PQexec(conn, "COMMIT"); - PQclear(res); + /* commit the transaction */ + res = PQexec(conn, "COMMIT"); + PQclear(res); - /* close the connection to the database and cleanup */ - PQfinish(conn); + /* close the connection to the database and cleanup */ + PQfinish(conn); - rsi = (ReturnSetInfo *)fcinfo->resultinfo; + rsi = (ReturnSetInfo *) fcinfo->resultinfo; rsi->isDone = ExprMultipleResult; PG_RETURN_POINTER(results); - } else { + } + else + { - PQclear(res); + PQclear(res); - /* close the cursor */ - res = PQexec(conn, "CLOSE mycursor"); - PQclear(res); + /* close the cursor */ + res = PQexec(conn, "CLOSE mycursor"); + PQclear(res); - /* commit the transaction */ - res = PQexec(conn, "COMMIT"); - PQclear(res); + /* commit the transaction */ + res = PQexec(conn, "COMMIT"); + PQclear(res); - /* close the connection to the database and cleanup */ - PQfinish(conn); + /* close the connection to the database and cleanup */ + PQfinish(conn); - rsi = (ReturnSetInfo *)fcinfo->resultinfo; - rsi->isDone = ExprEndResult ; + rsi = (ReturnSetInfo *) fcinfo->resultinfo; + rsi->isDone = ExprEndResult; PG_RETURN_NULL(); } } - } else { + } + else + { /* * check for more results */ @@ -162,29 +170,30 @@ dblink(PG_FUNCTION_ARGS) results->tup_num++; ntuples = PQntuples(results->res); - if (results->tup_num < ntuples) { - + if (results->tup_num < ntuples) + { /* * fetch them if available */ - rsi = (ReturnSetInfo *)fcinfo->resultinfo; + rsi = (ReturnSetInfo *) fcinfo->resultinfo; rsi->isDone = ExprMultipleResult; PG_RETURN_POINTER(results); - } else { - + } + else + { /* * or if no more, clean things up */ results = fcinfo->flinfo->fn_extra; - PQclear(results->res); + PQclear(results->res); - rsi = (ReturnSetInfo *)fcinfo->resultinfo; - rsi->isDone = ExprEndResult ; + rsi = (ReturnSetInfo *) fcinfo->resultinfo; + rsi->isDone = ExprEndResult; PG_RETURN_NULL(); } @@ -204,48 +213,48 @@ PG_FUNCTION_INFO_V1(dblink_tok); Datum dblink_tok(PG_FUNCTION_ARGS) { - dblink_results *results; - int fldnum; - text *result_text; - char *result; - int nfields = 0; - int text_len = 0; - - if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) { + dblink_results *results; + int fldnum; + text *result_text; + char *result; + int nfields = 0; + int text_len = 0; + + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) elog(ERROR, "dblink: NULL arguments are not permitted"); - } results = (dblink_results *) PG_GETARG_POINTER(0); - if (results == NULL) { + if (results == NULL) elog(ERROR, "dblink: function called with invalid result pointer"); - } fldnum = PG_GETARG_INT32(1); - if (fldnum < 0) { + if (fldnum < 0) elog(ERROR, "dblink: field number < 0 not permitted"); - } nfields = PQnfields(results->res); - if (fldnum > (nfields - 1)) { + if (fldnum > (nfields - 1)) elog(ERROR, "dblink: field number %d does not exist", fldnum); - } - if (PQgetisnull(results->res, results->tup_num, fldnum) == 1) { + if (PQgetisnull(results->res, results->tup_num, fldnum) == 1) + { PG_RETURN_NULL(); - } else { + } + else + { text_len = PQgetlength(results->res, results->tup_num, fldnum); result = (char *) palloc(text_len + 1); - if (result != NULL) { + if (result != NULL) + { strcpy(result, PQgetvalue(results->res, results->tup_num, fldnum)); strcat(result, "\0"); - } else { - elog(ERROR, "dblink: insufficient memory" ); } + else + elog(ERROR, "dblink: insufficient memory"); result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(result))); @@ -267,8 +276,8 @@ dblink_tok(PG_FUNCTION_ARGS) dblink_results * init_dblink_results(MemoryContext fn_mcxt) { - MemoryContext oldcontext; - dblink_results *retval; + MemoryContext oldcontext; + dblink_results *retval; oldcontext = MemoryContextSwitchTo(fn_mcxt); |