heldcursor.c
text/x-csrc
Filename: heldcursor.c
Type: text/x-csrc
Part: 0
#include <libpq-fe.h>
#ifdef WIN32
#include <WinSock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
#define MY_CUR "mycur"
int main(int argc, const char **argv)
{
const char *connstr;
PGconn *conn;
PGresult *result;
int sock;
int len, count;
if (argc > 1)
connstr = argv[1];
else
connstr = "host=localhost port=5432 dbname=xxxxx user=xxxxx password=xxxxx";
conn = PQconnectdb(connstr);
result = PQexec(conn, "declare " MY_CUR " cursor with hold for select * from generate_series(1, 2) as i");
if (PQgetssl(conn) != NULL)
{
printf("Use non-ssl connection\n");
return 1;
}
sock = PQsocket(conn);
if (sock < 0)
{
printf("socket error\n");
return 1;
}
// send execute message
send(sock, "E", 1, 0);
len = sizeof(len) + strlen(MY_CUR) + 1 + sizeof(count);
len = htonl(len);
send(sock, (const char *) &len, sizeof(len), 0);
send(sock, MY_CUR, strlen(MY_CUR) + 1, 0);
count = htonl(1);
send(sock, (const char *) &count, sizeof(count), 0);
result = PQexec(conn, "close " MY_CUR);
if (!result)
printf("close error\n");
else
printf("result error=%s\n", PQresultErrorMessage(result));
PQfinish(conn);
return 0;
}