8
8
#include <stdio.h>
9
9
#include "libpq-fe.h"
10
10
11
- void
11
+ static void
12
12
exit_nicely (PGconn * conn1 , PGconn * conn2 )
13
13
{
14
14
if (conn1 )
@@ -18,8 +18,8 @@ exit_nicely(PGconn *conn1, PGconn *conn2)
18
18
exit (1 );
19
19
}
20
20
21
- void
22
- check_conn (PGconn * conn )
21
+ static void
22
+ check_conn (PGconn * conn , const char * dbName )
23
23
{
24
24
/* check to see that the backend connection was successfully made */
25
25
if (PQstatus (conn ) == CONNECTION_BAD )
@@ -30,23 +30,26 @@ check_conn(PGconn *conn)
30
30
}
31
31
}
32
32
33
- main ()
33
+ int
34
+ main (int argc , char * * argv )
34
35
{
35
36
char * pghost ,
36
37
* pgport ,
37
38
* pgoptions ,
38
39
* pgtty ;
39
40
char * dbName1 ,
40
- dbName2 ;
41
+ * dbName2 ;
41
42
char * tblName ;
42
43
int nFields ;
43
44
int i ,
44
45
j ;
45
46
46
47
PGconn * conn1 ,
47
- conn2 ;
48
- PGresult * res1 ,
49
- res2 ;
48
+ * conn2 ;
49
+ /* PGresult *res1,
50
+ * *res2;
51
+ */
52
+ PGresult * res1 ;
50
53
51
54
if (argc != 4 )
52
55
{
@@ -73,10 +76,10 @@ main()
73
76
74
77
/* make a connection to the database */
75
78
conn1 = PQsetdb (pghost , pgport , pgoptions , pgtty , dbName1 );
76
- check_conn (conn1 );
79
+ check_conn (conn1 , dbName1 );
77
80
78
81
conn2 = PQsetdb (pghost , pgport , pgoptions , pgtty , dbName2 );
79
- check_conn (conn2 );
82
+ check_conn (conn2 , dbName2 );
80
83
81
84
/* start a transaction block */
82
85
res1 = PQexec (conn1 , "BEGIN" );
@@ -97,53 +100,54 @@ main()
97
100
* fetch instances from the pg_database, the system catalog of
98
101
* databases
99
102
*/
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 )
102
105
{
103
106
fprintf (stderr , "DECLARE CURSOR command failed\n" );
104
- PQclear (res );
105
- exit_nicely (conn );
107
+ PQclear (res1 );
108
+ exit_nicely (conn1 ,( PGconn * ) NULL );
106
109
}
107
- PQclear (res );
110
+ PQclear (res1 );
108
111
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 )
111
114
{
112
115
fprintf (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 );
115
118
}
116
119
117
120
/* first, print out the attribute names */
118
- nFields = PQnfields (res );
121
+ nFields = PQnfields (res1 );
119
122
for (i = 0 ; i < nFields ; i ++ )
120
123
{
121
- printf ("%-15s" , PQfname (res , i ));
124
+ printf ("%-15s" , PQfname (res1 , i ));
122
125
}
123
126
printf ("\n\n" );
124
127
125
128
/* next, print out the instances */
126
- for (i = 0 ; i < PQntuples (res ); i ++ )
129
+ for (i = 0 ; i < PQntuples (res1 ); i ++ )
127
130
{
128
131
for (j = 0 ; j < nFields ; j ++ )
129
132
{
130
- printf ("%-15s" , PQgetvalue (res , i , j ));
133
+ printf ("%-15s" , PQgetvalue (res1 , i , j ));
131
134
}
132
135
printf ("\n" );
133
136
}
134
137
135
- PQclear (res );
138
+ PQclear (res1 );
136
139
137
140
/* close the portal */
138
- res = PQexec (conn , "CLOSE myportal" );
139
- PQclear (res );
141
+ res1 = PQexec (conn1 , "CLOSE myportal" );
142
+ PQclear (res1 );
140
143
141
144
/* end the transaction */
142
- res = PQexec (conn , "END" );
143
- PQclear (res );
145
+ res1 = PQexec (conn1 , "END" );
146
+ PQclear (res1 );
144
147
145
148
/* close the connection to the database and cleanup */
146
- PQfinish (conn );
149
+ PQfinish (conn1 );
147
150
148
151
/* fclose(debug); */
152
+ return 0 ; /* Though PQfinish(conn1) has called exit(1) */
149
153
}
0 commit comments