PostgreSQL Source Code  git master
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.

References CONNECTION_OK, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQresultStatus(), and PQstatus().

Referenced by main().

26 {
27  PGresult *res;
28 
29  /* check to see that the backend connection was successfully made */
30  if (PQstatus(conn) != CONNECTION_OK)
31  {
32  fprintf(stderr, "Connection to database \"%s\" failed: %s",
33  dbName, PQerrorMessage(conn));
34  exit(1);
35  }
36 
37  /* Set always-secure search path, so malicous users can't take control. */
38  res = PQexec(conn,
39  "SELECT pg_catalog.set_config('search_path', '', false)");
40  if (PQresultStatus(res) != PGRES_COMMAND_OK)
41  {
42  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
43  PQclear(res);
44  exit(1);
45  }
46  PQclear(res);
47 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * dbName
Definition: pgbench.c:196
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6063

◆ exit_nicely()

static void exit_nicely ( PGconn conn1,
PGconn conn2 
)
static

Definition at line 15 of file testlibpq4.c.

References PQfinish().

Referenced by main().

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:3638

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 50 of file testlibpq4.c.

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

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