aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq++/examples/testlibpq4.cc
blob: 6be620e07362a5465ade26490f41a9bdc76d3ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* testlibpq4.cc
*	Test of the asynchronous notification interface
*
  populate a test database with the following (use testlibpq4.sql):

CREATE TABLE TBL1 (i int4);

CREATE TABLE TBL2 (i int4);

CREATE RULE r1 AS ON INSERT TO TBL1 DO [INSERT INTO TBL2 values (new.i); NOTIFY TBL2];

* Then start up this program
* After the program has begun, do

INSERT INTO TBL1 values (10);

*
*
*/
#include <iostream.h>
#include "libpq++.h"
#include <stdlib.h>

int 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.
	const char* dbName = "dbname=template1";
	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);
	}

	// Listen to a table
	if ( !data.ExecCommandOk("LISTEN TBL2") )
	{
		cerr << "LISTEN command failed" << endl;
		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;
		}
	}
	return 0;
}