]> granicus.if.org Git - postgresql/blob - src/test/examples/testlibpq2.c
Remove cvs keywords from all files.
[postgresql] / src / test / examples / testlibpq2.c
1 /*
2  * src/test/examples/testlibpq2.c
3  *
4  *
5  * testlibpq2.c
6  *              Test of the asynchronous notification interface
7  *
8  * Start this program, then from psql in another window do
9  *       NOTIFY TBL2;
10  * Repeat four times to get this program to exit.
11  *
12  * Or, if you want to get fancy, try this:
13  * populate a database with the following commands
14  * (provided in src/test/examples/testlibpq2.sql):
15  *
16  *       CREATE TABLE TBL1 (i int4);
17  *
18  *       CREATE TABLE TBL2 (i int4);
19  *
20  *       CREATE RULE r1 AS ON INSERT TO TBL1 DO
21  *         (INSERT INTO TBL2 VALUES (new.i); NOTIFY TBL2);
22  *
23  * and do this four times:
24  *
25  *       INSERT INTO TBL1 VALUES (10);
26  */
27
28 #ifdef WIN32
29 #include <windows.h>
30 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include "libpq-fe.h"
38
39 static void
40 exit_nicely(PGconn *conn)
41 {
42         PQfinish(conn);
43         exit(1);
44 }
45
46 int
47 main(int argc, char **argv)
48 {
49         const char *conninfo;
50         PGconn     *conn;
51         PGresult   *res;
52         PGnotify   *notify;
53         int                     nnotifies;
54
55         /*
56          * If the user supplies a parameter on the command line, use it as the
57          * conninfo string; otherwise default to setting dbname=postgres and using
58          * environment variables or defaults for all other connection parameters.
59          */
60         if (argc > 1)
61                 conninfo = argv[1];
62         else
63                 conninfo = "dbname = postgres";
64
65         /* Make a connection to the database */
66         conn = PQconnectdb(conninfo);
67
68         /* Check to see that the backend connection was successfully made */
69         if (PQstatus(conn) != CONNECTION_OK)
70         {
71                 fprintf(stderr, "Connection to database failed: %s",
72                                 PQerrorMessage(conn));
73                 exit_nicely(conn);
74         }
75
76         /*
77          * Issue LISTEN command to enable notifications from the rule's NOTIFY.
78          */
79         res = PQexec(conn, "LISTEN TBL2");
80         if (PQresultStatus(res) != PGRES_COMMAND_OK)
81         {
82                 fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
83                 PQclear(res);
84                 exit_nicely(conn);
85         }
86
87         /*
88          * should PQclear PGresult whenever it is no longer needed to avoid memory
89          * leaks
90          */
91         PQclear(res);
92
93         /* Quit after four notifies are received. */
94         nnotifies = 0;
95         while (nnotifies < 4)
96         {
97                 /*
98                  * Sleep until something happens on the connection.  We use select(2)
99                  * to wait for input, but you could also use poll() or similar
100                  * facilities.
101                  */
102                 int                     sock;
103                 fd_set          input_mask;
104
105                 sock = PQsocket(conn);
106
107                 if (sock < 0)
108                         break;                          /* shouldn't happen */
109
110                 FD_ZERO(&input_mask);
111                 FD_SET(sock, &input_mask);
112
113                 if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
114                 {
115                         fprintf(stderr, "select() failed: %s\n", strerror(errno));
116                         exit_nicely(conn);
117                 }
118
119                 /* Now check for input */
120                 PQconsumeInput(conn);
121                 while ((notify = PQnotifies(conn)) != NULL)
122                 {
123                         fprintf(stderr,
124                                         "ASYNC NOTIFY of '%s' received from backend pid %d\n",
125                                         notify->relname, notify->be_pid);
126                         PQfreemem(notify);
127                         nnotifies++;
128                 }
129         }
130
131         fprintf(stderr, "Done.\n");
132
133         /* close the connection to the database and cleanup */
134         PQfinish(conn);
135
136         return 0;
137 }