PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
testlibpq4.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
Include dependency graph for testlibpq4.c:

Go to the source code of this file.

Functions

static void exit_nicely (PGconn *conn1, PGconn *conn2)
 
static void check_prepare_conn (PGconn *conn, const char *dbName)
 
int main (int argc, char **argv)
 

Function Documentation

◆ check_prepare_conn()

static void check_prepare_conn ( PGconn conn,
const char *  dbName 
)
static

Definition at line 25 of file testlibpq4.c.

26{
27 PGresult *res;
28
29 /* check to see that the backend connection was successfully made */
31 {
32 fprintf(stderr, "%s", PQerrorMessage(conn));
33 exit(1);
34 }
35
36 /* Set always-secure search path, so malicious users can't take control. */
37 res = PQexec(conn,
38 "SELECT pg_catalog.set_config('search_path', '', false)");
40 {
41 fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
42 PQclear(res);
43 exit(1);
44 }
45 PQclear(res);
46}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7490
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7553
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
@ CONNECTION_OK
Definition: libpq-fe.h:83
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:127
PGconn * conn
Definition: streamutil.c:52

References conn, CONNECTION_OK, fprintf, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQresultStatus(), and PQstatus().

Referenced by main().

◆ exit_nicely()

static void exit_nicely ( PGconn conn1,
PGconn conn2 
)
static

Definition at line 15 of file testlibpq4.c.

16{
17 if (conn1)
18 PQfinish(conn1);
19 if (conn2)
20 PQfinish(conn2);
21 exit(1);
22}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224

References PQfinish().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 49 of file testlibpq4.c.

50{
51 char *pghost,
52 *pgport,
53 *pgoptions;
54 char *dbName1,
55 *dbName2;
56 char *tblName;
57 int nFields;
58 int i,
59 j;
60
61 PGconn *conn1,
62 *conn2;
63
64 /*
65 * PGresult *res1, *res2;
66 */
67 PGresult *res1;
68
69 if (argc != 4)
70 {
71 fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
72 fprintf(stderr, " compares two tables in two databases\n");
73 exit(1);
74 }
75 tblName = argv[1];
76 dbName1 = argv[2];
77 dbName2 = argv[3];
78
79
80 /*
81 * begin, by setting the parameters for a backend connection if the
82 * parameters are null, then the system will try to use reasonable
83 * defaults by looking up environment variables or, failing that, using
84 * hardwired constants
85 */
86 pghost = NULL; /* host name of the backend */
87 pgport = NULL; /* port of the backend */
88 pgoptions = NULL; /* special options to start up the backend
89 * server */
90
91 /* make a connection to the database */
92 conn1 = PQsetdb(pghost, pgport, pgoptions, NULL, dbName1);
93 check_prepare_conn(conn1, dbName1);
94
95 conn2 = PQsetdb(pghost, pgport, pgoptions, NULL, dbName2);
96 check_prepare_conn(conn2, dbName2);
97
98 /* start a transaction block */
99 res1 = PQexec(conn1, "BEGIN");
100 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
101 {
102 fprintf(stderr, "BEGIN command failed\n");
103 PQclear(res1);
104 exit_nicely(conn1, conn2);
105 }
106
107 /*
108 * make sure to PQclear() a PGresult whenever it is no longer needed to
109 * avoid memory leaks
110 */
111 PQclear(res1);
112
113 /*
114 * fetch instances from the pg_database, the system catalog of databases
115 */
116 res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database");
117 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
118 {
119 fprintf(stderr, "DECLARE CURSOR command failed\n");
120 PQclear(res1);
121 exit_nicely(conn1, conn2);
122 }
123 PQclear(res1);
124
125 res1 = PQexec(conn1, "FETCH ALL in myportal");
126 if (PQresultStatus(res1) != PGRES_TUPLES_OK)
127 {
128 fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
129 PQclear(res1);
130 exit_nicely(conn1, conn2);
131 }
132
133 /* first, print out the attribute names */
134 nFields = PQnfields(res1);
135 for (i = 0; i < nFields; i++)
136 printf("%-15s", PQfname(res1, i));
137 printf("\n\n");
138
139 /* next, print out the instances */
140 for (i = 0; i < PQntuples(res1); i++)
141 {
142 for (j = 0; j < nFields; j++)
143 printf("%-15s", PQgetvalue(res1, i, j));
144 printf("\n");
145 }
146
147 PQclear(res1);
148
149 /* close the portal */
150 res1 = PQexec(conn1, "CLOSE myportal");
151 PQclear(res1);
152
153 /* end the transaction */
154 res1 = PQexec(conn1, "END");
155 PQclear(res1);
156
157 /* close the connections to the database and cleanup */
158 PQfinish(conn1);
159 PQfinish(conn2);
160
161/* fclose(debug); */
162 return 0;
163}
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3567
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
int j
Definition: isn.c:73
int i
Definition: isn.c:72
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:124
#define PQsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME)
Definition: libpq-fe.h:338
static const char * pghost
Definition: pgbench.c:295
static const char * pgport
Definition: pgbench.c:296
#define printf(...)
Definition: port.h:245
static void exit_nicely(PGconn *conn1, PGconn *conn2)
Definition: testlibpq4.c:15
static void check_prepare_conn(PGconn *conn, const char *dbName)
Definition: testlibpq4.c:25

References check_prepare_conn(), exit_nicely(), fprintf, i, j, pghost, pgport, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQexec(), PQfinish(), PQfname(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQsetdb, and printf.