Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / test / examples / testlibpq4.c
blob1144fdbc9a528b3bc592cff96aaf06bd23795cae
1 /*
2 * $PostgreSQL$
5 * testlibpq4.c
6 * this test program shows to use LIBPQ to make multiple backend
7 * connections
9 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "libpq-fe.h"
14 static void
15 exit_nicely(PGconn *conn1, PGconn *conn2)
17 if (conn1)
18 PQfinish(conn1);
19 if (conn2)
20 PQfinish(conn2);
21 exit(1);
24 static void
25 check_conn(PGconn *conn, const char *dbName)
27 /* check to see that the backend connection was successfully made */
28 if (PQstatus(conn) != CONNECTION_OK)
30 fprintf(stderr, "Connection to database \"%s\" failed: %s",
31 dbName, PQerrorMessage(conn));
32 exit(1);
36 int
37 main(int argc, char **argv)
39 char *pghost,
40 *pgport,
41 *pgoptions,
42 *pgtty;
43 char *dbName1,
44 *dbName2;
45 char *tblName;
46 int nFields;
47 int i,
50 PGconn *conn1,
51 *conn2;
54 * PGresult *res1, *res2;
56 PGresult *res1;
58 if (argc != 4)
60 fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
61 fprintf(stderr, " compares two tables in two databases\n");
62 exit(1);
64 tblName = argv[1];
65 dbName1 = argv[2];
66 dbName2 = argv[3];
70 * begin, by setting the parameters for a backend connection if the
71 * parameters are null, then the system will try to use reasonable
72 * defaults by looking up environment variables or, failing that, using
73 * hardwired constants
75 pghost = NULL; /* host name of the backend server */
76 pgport = NULL; /* port of the backend server */
77 pgoptions = NULL; /* special options to start up the backend
78 * server */
79 pgtty = NULL; /* debugging tty for the backend server */
81 /* make a connection to the database */
82 conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
83 check_conn(conn1, dbName1);
85 conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
86 check_conn(conn2, dbName2);
88 /* start a transaction block */
89 res1 = PQexec(conn1, "BEGIN");
90 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
92 fprintf(stderr, "BEGIN command failed\n");
93 PQclear(res1);
94 exit_nicely(conn1, conn2);
98 * make sure to PQclear() a PGresult whenever it is no longer needed to
99 * avoid memory leaks
101 PQclear(res1);
104 * fetch instances from the pg_database, the system catalog of databases
106 res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database");
107 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
109 fprintf(stderr, "DECLARE CURSOR command failed\n");
110 PQclear(res1);
111 exit_nicely(conn1, conn2);
113 PQclear(res1);
115 res1 = PQexec(conn1, "FETCH ALL in myportal");
116 if (PQresultStatus(res1) != PGRES_TUPLES_OK)
118 fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
119 PQclear(res1);
120 exit_nicely(conn1, conn2);
123 /* first, print out the attribute names */
124 nFields = PQnfields(res1);
125 for (i = 0; i < nFields; i++)
126 printf("%-15s", PQfname(res1, i));
127 printf("\n\n");
129 /* next, print out the instances */
130 for (i = 0; i < PQntuples(res1); i++)
132 for (j = 0; j < nFields; j++)
133 printf("%-15s", PQgetvalue(res1, i, j));
134 printf("\n");
137 PQclear(res1);
139 /* close the portal */
140 res1 = PQexec(conn1, "CLOSE myportal");
141 PQclear(res1);
143 /* end the transaction */
144 res1 = PQexec(conn1, "END");
145 PQclear(res1);
147 /* close the connections to the database and cleanup */
148 PQfinish(conn1);
149 PQfinish(conn2);
151 /* fclose(debug); */
152 return 0;