diff options
Diffstat (limited to 'src/interfaces/libpq++/examples/testlibpq4.cc')
-rw-r--r-- | src/interfaces/libpq++/examples/testlibpq4.cc | 92 |
1 files changed, 42 insertions, 50 deletions
diff --git a/src/interfaces/libpq++/examples/testlibpq4.cc b/src/interfaces/libpq++/examples/testlibpq4.cc index dc78031843f..a1b21d33a9d 100644 --- a/src/interfaces/libpq++/examples/testlibpq4.cc +++ b/src/interfaces/libpq++/examples/testlibpq4.cc @@ -1,66 +1,58 @@ /* * testlibpq4.cc - * Test the C++ version of LIBPQ, the POSTGRES frontend library. - * tests the copy in features + * Test of the asynchronous notification interface * - */ -#include <stdio.h> -#include "libpq++.H" + populate a test database with the following (use testlibpq4.sql): -main() -{ - char* dbName; +CREATE TABLE TBL1 (i int4); - /* begin, by creating the parameter environment for a backend - connection. When no parameters are given then the system will - try to use reasonable defaults by looking up environment variables - or, failing that, using hardwired constants */ - PGenv env; - PGdatabase* data; +CREATE TABLE TBL2 (i int4); - dbName = getenv("USER"); /* change this to the name of your test database */ +CREATE RULE r1 AS ON INSERT TO TBL1 DO [INSERT INTO TBL2 values (new.i); NOTIFY TBL2]; - /* make a connection to the database */ - data = new PGdatabase(&env, dbName); + * Then start up this program + * After the program has begun, do - /* check to see that the backend connection was successfully made */ - if (data->status() == CONNECTION_BAD) { - fprintf(stderr,"Connection to database '%s' failed.\n", dbName); - fprintf(stderr,"%s",data->errormessage()); - delete data; - exit(1); - } +INSERT INTO TBL1 values (10); + + * + * + */ +#include <iostream.h> +#include <libpq++.h> +#include <stdlib.h> - /* start a transaction block */ - if(data->exec("BEGIN") != PGRES_COMMAND_OK) { - fprintf(stderr,"BEGIN command failed\n"); - delete data; +main() +{ + // Begin, by connecting to the backend using hardwired constants + // and a test database created by the user prior to the invokation + // of this test program. + char* dbName = getenv("USER"); // change this to the name of your test database + PgDatabase data(dbName); + + // Check to see that the backend connection was successfully made + if ( data.ConnectionBad() ) { + cerr << "Connection to database '" << dbName << "' failed." << endl + << data.ErrorMessage() << endl; exit(1); } - if (data->exec("CREATE TABLE foo (a int4, b char16, d float8)") != - PGRES_COMMAND_OK) { - fprintf(stderr,"CREATE TABLE foo command failed\n"); - delete data; - exit(1); + // Listen to a table + if ( !data.ExecCommandOk("LISTEN TBL2") ) { + cerr << "LISTEN command failed" << endl; + exit(1); } - if (data->exec("COPY foo FROM STDIN") != PGRES_COMMAND_OK) { - fprintf(stderr,"COPY foo FROM STDIN\n"); - delete data; - exit(1); + // Test asynchronous notification + while (1) { + // check for asynchronous returns + PGnotify* notify = data.Notifies(); + if (notify) { + cerr << "ASYNC NOTIFY of '" << notify->relname + << "' from backend pid '" << notify->be_pid + << "' received" << endl; + free(notify); + break; + } } - - data->putline("3\thello world\t4.5\n"); - data->putline("4\tgoodbye word\t7.11\n"); - data->putline(".\n"); - data->endcopy(); - data->exec("SELECT * FROM foo"); - data->printtuples(stdout,1,"|",1,0); - data->exec("DROP TABLE foo"); - // end the transaction - data->exec("END"); - - // close the connection to the database and cleanup - delete data; } |