88#include <stdio.h>
99#include "libpq-fe.h"
1010
11- void
11+ static void
1212exit_nicely (PGconn * conn1 ,PGconn * conn2 )
1313{
1414if (conn1 )
@@ -18,8 +18,8 @@ exit_nicely(PGconn *conn1, PGconn *conn2)
1818exit (1 );
1919}
2020
21- void
22- check_conn (PGconn * conn )
21+ static void
22+ check_conn (PGconn * conn , const char * dbName )
2323{
2424/* check to see that the backend connection was successfully made */
2525if (PQstatus (conn )== CONNECTION_BAD )
@@ -30,23 +30,26 @@ check_conn(PGconn *conn)
3030}
3131}
3232
33- main ()
33+ int
34+ main (int argc ,char * * argv )
3435{
3536char * pghost ,
3637* pgport ,
3738* pgoptions ,
3839* pgtty ;
3940char * dbName1 ,
40- dbName2 ;
41+ * dbName2 ;
4142char * tblName ;
4243int nFields ;
4344int i ,
4445j ;
4546
4647PGconn * conn1 ,
47- conn2 ;
48- PGresult * res1 ,
49- res2 ;
48+ * conn2 ;
49+ /* PGresult *res1,
50+ **res2;
51+ */
52+ PGresult * res1 ;
5053
5154if (argc != 4 )
5255{
@@ -73,10 +76,10 @@ main()
7376
7477/* make a connection to the database */
7578conn1 = PQsetdb (pghost ,pgport ,pgoptions ,pgtty ,dbName1 );
76- check_conn (conn1 );
79+ check_conn (conn1 , dbName1 );
7780
7881conn2 = PQsetdb (pghost ,pgport ,pgoptions ,pgtty ,dbName2 );
79- check_conn (conn2 );
82+ check_conn (conn2 , dbName2 );
8083
8184/* start a transaction block */
8285res1 = PQexec (conn1 ,"BEGIN" );
@@ -97,53 +100,54 @@ main()
97100 * fetch instances from the pg_database, the system catalog of
98101 * databases
99102 */
100- res = PQexec (conn ,"DECLARE myportal CURSOR FOR select * from pg_database" );
101- if (PQresultStatus (res )!= PGRES_COMMAND_OK )
103+ res1 = PQexec (conn1 ,"DECLARE myportal CURSOR FOR select * from pg_database" );
104+ if (PQresultStatus (res1 )!= PGRES_COMMAND_OK )
102105{
103106fprintf (stderr ,"DECLARE CURSOR command failed\n" );
104- PQclear (res );
105- exit_nicely (conn );
107+ PQclear (res1 );
108+ exit_nicely (conn1 ,( PGconn * ) NULL );
106109}
107- PQclear (res );
110+ PQclear (res1 );
108111
109- res = PQexec (conn ,"FETCH ALL in myportal" );
110- if (PQresultStatus (res )!= PGRES_TUPLES_OK )
112+ res1 = PQexec (conn1 ,"FETCH ALL in myportal" );
113+ if (PQresultStatus (res1 )!= PGRES_TUPLES_OK )
111114{
112115fprintf (stderr ,"FETCH ALL command didn't return tuples properly\n" );
113- PQclear (res );
114- exit_nicely (conn );
116+ PQclear (res1 );
117+ exit_nicely (conn1 ,( PGconn * ) NULL );
115118}
116119
117120/* first, print out the attribute names */
118- nFields = PQnfields (res );
121+ nFields = PQnfields (res1 );
119122for (i = 0 ;i < nFields ;i ++ )
120123{
121- printf ("%-15s" ,PQfname (res ,i ));
124+ printf ("%-15s" ,PQfname (res1 ,i ));
122125}
123126printf ("\n\n" );
124127
125128/* next, print out the instances */
126- for (i = 0 ;i < PQntuples (res );i ++ )
129+ for (i = 0 ;i < PQntuples (res1 );i ++ )
127130{
128131for (j = 0 ;j < nFields ;j ++ )
129132{
130- printf ("%-15s" ,PQgetvalue (res ,i ,j ));
133+ printf ("%-15s" ,PQgetvalue (res1 ,i ,j ));
131134}
132135printf ("\n" );
133136}
134137
135- PQclear (res );
138+ PQclear (res1 );
136139
137140/* close the portal */
138- res = PQexec (conn ,"CLOSE myportal" );
139- PQclear (res );
141+ res1 = PQexec (conn1 ,"CLOSE myportal" );
142+ PQclear (res1 );
140143
141144/* end the transaction */
142- res = PQexec (conn ,"END" );
143- PQclear (res );
145+ res1 = PQexec (conn1 ,"END" );
146+ PQclear (res1 );
144147
145148/* close the connection to the database and cleanup */
146- PQfinish (conn );
149+ PQfinish (conn1 );
147150
148151/* fclose(debug); */
152+ return 0 ;/* Though PQfinish(conn1) has called exit(1) */
149153}