Thread: Calling stored function that returns a cursor from a libpq program
Mazen Abdel-Rahman <saba.mazen@gmail.com> writes: > I am trying to use a stored functions that returns a CURSOR in a C > program that uses that libpq library. It looks like you've just hardwired an assumption about what the name of the cursor will be. It'd be better to pay attention to the name returned by the function. It also looks like you're not bothering to check that the BEGIN command succeeded. If it didn't for some reason, that could explain the failure. regards, tom lane
Re: Calling stored function that returns a cursor from a libpq program
char * cursorCall = "DECLARE myPortal CURSOR for select * from cars";
//BEGIN the transaction
queryResult4 = PQexec(connection, "BEGIN");
execStatus = PQresultStatus(queryResult4); //Get the result status
char * beginTransactionErrorMessage = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:beginTransactionErrorMessage]);
char* cursorCall = "select reffunc('portalName')"; //call stored function to get the cursor
//Call the stored function that returns a CURSOR
queryResult4 = PQexec(connection, cursorCall);
execStatus = PQresultStatus(queryResult4); //Get the result status
char * errorMessage = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:errorMessage]);
if ((execStatus == PGRES_COMMAND_OK) || (execStatus == PGRES_TUPLES_OK)) { //cursor retrieved succesfully
int numRows; //variable to store number of rows returned in query
int numColumns; //number of columns per row
numRows = PQntuples(queryResult4);
NSLog(@"The stored function call returned %i rows.", numRows);
numColumns = PQnfields(queryResult4); //get number of columns
//get the name of the returned cursor
char * cursorReturned;
cursorReturned = PQgetvalue(queryResult4, 0, 0);
NSString * fetchNextQuery = [NSString stringWithFormat:@"FETCH ALL in %s", cursorReturned];
queryResult4 = PQexec(connection,[fetchNextQuery UTF8String] );
execStatus = PQresultStatus(queryResult4); //Get the result status
char * errorMessage2 = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:errorMessage2]); ....
Mazen Abdel-Rahman <saba.mazen@gmail.com> writes:It looks like you've just hardwired an assumption about what the name of
> I am trying to use a stored functions that returns a CURSOR in a C
> program that uses that libpq library.
the cursor will be. It'd be better to pay attention to the name
returned by the function.
It also looks like you're not bothering to check that the BEGIN command
succeeded. If it didn't for some reason, that could explain the
failure.
regards, tom lane
Mazen Abdel-Rahman <saba.mazen@gmail.com> writes: > //get the name of the returned cursor > char * cursorReturned; > cursorReturned = PQgetvalue(queryResult4, 0, 0); > NSString * fetchNextQuery = [NSString stringWithFormat:@"FETCH ALL in %s", > cursorReturned]; I'd try double quoting that, ie stringWithFormat:@"FETCH ALL in \"%s\"", As-is you're risking trouble with mixed-case names, which is what you seem to be using. regards, tom lane