mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +03:00
Massive commit to run PGINDENT on all *.c and *.h files.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* testlibpq2.c
|
||||
* Test of the asynchronous notification interface
|
||||
* Test of the asynchronous notification interface
|
||||
*
|
||||
populate a database with the following:
|
||||
|
||||
@@ -21,73 +21,87 @@ INSERT INTO TBL1 values (10);
|
||||
#include <stdio.h>
|
||||
#include "libpq-fe.h"
|
||||
|
||||
void exit_nicely(PGconn* conn)
|
||||
void
|
||||
exit_nicely(PGconn * conn)
|
||||
{
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
main()
|
||||
{
|
||||
char *pghost, *pgport, *pgoptions, *pgtty;
|
||||
char* dbName;
|
||||
int nFields;
|
||||
int i,j;
|
||||
char *pghost,
|
||||
*pgport,
|
||||
*pgoptions,
|
||||
*pgtty;
|
||||
char *dbName;
|
||||
int nFields;
|
||||
int i,
|
||||
j;
|
||||
|
||||
PGconn* conn;
|
||||
PGresult* res;
|
||||
PGnotify* notify;
|
||||
PGconn *conn;
|
||||
PGresult *res;
|
||||
PGnotify *notify;
|
||||
|
||||
/* begin, by setting the parameters for a backend connection
|
||||
if the parameters are null, then the system will try to use
|
||||
reasonable defaults by looking up environment variables
|
||||
or, failing that, using hardwired constants */
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = getenv("USER"); /* change this to the name of your test database*/
|
||||
/*
|
||||
* begin, by setting the parameters for a backend connection if the
|
||||
* parameters are null, then the system will try to use reasonable
|
||||
* defaults by looking up environment variables or, failing that,
|
||||
* using hardwired constants
|
||||
*/
|
||||
pghost = NULL; /* host name of the backend server */
|
||||
pgport = NULL; /* port of the backend server */
|
||||
pgoptions = NULL; /* special options to start up the backend
|
||||
* server */
|
||||
pgtty = NULL; /* debugging tty for the backend server */
|
||||
dbName = getenv("USER"); /* change this to the name of your test
|
||||
* database */
|
||||
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
/* make a connection to the database */
|
||||
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
||||
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||
fprintf(stderr,"Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr,"%s",PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* check to see that the backend connection was successfully made */
|
||||
if (PQstatus(conn) == CONNECTION_BAD)
|
||||
{
|
||||
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
|
||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
res = PQexec(conn, "LISTEN TBL2");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
fprintf(stderr,"LISTEN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
/* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
memory leaks */
|
||||
PQclear(res);
|
||||
res = PQexec(conn, "LISTEN TBL2");
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
fprintf(stderr, "LISTEN command failed\n");
|
||||
PQclear(res);
|
||||
exit_nicely(conn);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
/* async notification only come back as a result of a query*/
|
||||
/* we can send empty queries */
|
||||
res = PQexec(conn, " ");
|
||||
/* printf("res->status = %s\n", pgresStatus[PQresultStatus(res)]); */
|
||||
/* check for asynchronous returns */
|
||||
notify = PQnotifies(conn);
|
||||
if (notify) {
|
||||
fprintf(stderr,
|
||||
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
|
||||
notify->relname, notify->be_pid);
|
||||
free(notify);
|
||||
break;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
/*
|
||||
* should PQclear PGresult whenever it is no longer needed to avoid
|
||||
* memory leaks
|
||||
*/
|
||||
PQclear(res);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* async notification only come back as a result of a query */
|
||||
/* we can send empty queries */
|
||||
res = PQexec(conn, " ");
|
||||
/* printf("res->status = %s\n", pgresStatus[PQresultStatus(res)]); */
|
||||
/* check for asynchronous returns */
|
||||
notify = PQnotifies(conn);
|
||||
if (notify)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
|
||||
notify->relname, notify->be_pid);
|
||||
free(notify);
|
||||
break;
|
||||
}
|
||||
PQclear(res);
|
||||
}
|
||||
|
||||
/* close the connection to the database and cleanup */
|
||||
PQfinish(conn);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user