Hi!
I wrote a simple application to test how pqxx::trigger class works and to test
PostgreSQL NOTIFY to use such construction in my further application. But
this application do not work: I do not see the desired message on NOTIFY
event. What I am doing wrong? The testlibpq2.c expamle from PostgreSQL
documentation works fine. The pqxx documentation do not contain any example
on trigger class.
----------------------- test_libpqxx.h: ------------------------------------
#ifndef TEST_LIBPQXX_H
#define TEST_LIBPQXX_H
#include <iostream>
#include <pqxx/pqxx> // PostgreSQL C++ interface library
using namespace PGSTD;
using namespace pqxx;
//---------------------------------------------------------------------
class DTrendTrigger : trigger
{
public:
DTrendTrigger(connection_base &C, const string &N)
: trigger(C, N) {};
virtual void operator()(int pid) {
cout << "Received message '" << name() <<
"' from process pid: " << pid << endl;
};
};
//---------------------------------------------------------------------
#endif // TEST_LIBPQXX_H
----------------------- test_libpqxx.cpp: ------------------------------------
//#include <QtCore>
#include "test_libpqxx.h"
//---------------------------------------------------------------------
int main(int argc, char **argv)
{
// QCoreApplication app(argc, argv);
connection Conn(argv[1]);
string nname = "trend_event";
DTrendTrigger tr(Conn, nname);
while(true)
pause();
// app.exec();
return(0);
}
//---------------------------------------------------------------------
----------------------- testlibpq2.c: ------------------------------------
/*
* testlibpq2.c
* Test of the asynchronous notification interface
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include "libpq-fe.h"
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
PGnotify *notify;
int nnotifies;
/*
* If the user supplies a parameter on the command line, use it as the
* conninfo string; otherwise default to setting dbname=postgres and using
* environment variables or defaults for all other connection parameters.
*/
if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname = postgres";
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/*
* Issue LISTEN command to enable notifications from the rule's NOTIFY.
*/
res = PQexec(conn, "LISTEN TREND_EVENT");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* should PQclear PGresult whenever it is no longer needed to avoid memory
* leaks
*/
PQclear(res);
/* Quit after four notifies are received. */
nnotifies = 0;
while (nnotifies < 4)
{
/*
* Sleep until something happens on the connection. We use select(2)
* to wait for input, but you could also use poll() or similar
* facilities.
*/
int sock;
fd_set input_mask;
sock = PQsocket(conn);
if (sock < 0)
break; /* shouldn't happen */
FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);
if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
{
fprintf(stderr, "select() failed: %s\n", strerror(errno));
exit_nicely(conn);
}
/* Now check for input */
PQconsumeInput(conn);
while ((notify = PQnotifies(conn)) != NULL)
{
fprintf(stderr,
"ASYNC NOTIFY of '%s' received from backend pid %d\n",
notify->relname, notify->be_pid);
PQfreemem(notify);
nnotifies++;
}
}
fprintf(stderr, "Done.\n");
/* close the connection to the database and cleanup */
PQfinish(conn);
return 0;
}
--
Best regards,
Aleksey.
_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general