PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_conn (PGconn *conn, const char *dbName)
 
int main (int argc, char **argv)
 

Function Documentation

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

Definition at line 25 of file testlibpq4.c.

References CONNECTION_OK, PQerrorMessage(), and PQstatus().

Referenced by main().

26 {
27  /* check to see that the backend connection was successfully made */
28  if (PQstatus(conn) != CONNECTION_OK)
29  {
30  fprintf(stderr, "Connection to database \"%s\" failed: %s",
31  dbName, PQerrorMessage(conn));
32  exit(1);
33  }
34 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5988
char * dbName
Definition: pgbench.c:183
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5935
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:3521
int main ( int  argc,
char **  argv 
)

Definition at line 37 of file testlibpq4.c.

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

38 {
39  char *pghost,
40  *pgport,
41  *pgoptions,
42  *pgtty;
43  char *dbName1,
44  *dbName2;
45  char *tblName;
46  int nFields;
47  int i,
48  j;
49 
50  PGconn *conn1,
51  *conn2;
52 
53  /*
54  * PGresult *res1, *res2;
55  */
56  PGresult *res1;
57 
58  if (argc != 4)
59  {
60  fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
61  fprintf(stderr, " compares two tables in two databases\n");
62  exit(1);
63  }
64  tblName = argv[1];
65  dbName1 = argv[2];
66  dbName2 = argv[3];
67 
68 
69  /*
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
74  */
75  pghost = NULL; /* host name of the backend */
76  pgport = NULL; /* port of the backend */
77  pgoptions = NULL; /* special options to start up the backend
78  * server */
79  pgtty = NULL; /* debugging tty for the backend */
80 
81  /* make a connection to the database */
82  conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
83  check_conn(conn1, dbName1);
84 
85  conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
86  check_conn(conn2, dbName2);
87 
88  /* start a transaction block */
89  res1 = PQexec(conn1, "BEGIN");
90  if (PQresultStatus(res1) != PGRES_COMMAND_OK)
91  {
92  fprintf(stderr, "BEGIN command failed\n");
93  PQclear(res1);
94  exit_nicely(conn1, conn2);
95  }
96 
97  /*
98  * make sure to PQclear() a PGresult whenever it is no longer needed to
99  * avoid memory leaks
100  */
101  PQclear(res1);
102 
103  /*
104  * fetch instances from the pg_database, the system catalog of databases
105  */
106  res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database");
107  if (PQresultStatus(res1) != PGRES_COMMAND_OK)
108  {
109  fprintf(stderr, "DECLARE CURSOR command failed\n");
110  PQclear(res1);
111  exit_nicely(conn1, conn2);
112  }
113  PQclear(res1);
114 
115  res1 = PQexec(conn1, "FETCH ALL in myportal");
116  if (PQresultStatus(res1) != PGRES_TUPLES_OK)
117  {
118  fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
119  PQclear(res1);
120  exit_nicely(conn1, conn2);
121  }
122 
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");
128 
129  /* next, print out the instances */
130  for (i = 0; i < PQntuples(res1); i++)
131  {
132  for (j = 0; j < nFields; j++)
133  printf("%-15s", PQgetvalue(res1, i, j));
134  printf("\n");
135  }
136 
137  PQclear(res1);
138 
139  /* close the portal */
140  res1 = PQexec(conn1, "CLOSE myportal");
141  PQclear(res1);
142 
143  /* end the transaction */
144  res1 = PQexec(conn1, "END");
145  PQclear(res1);
146 
147  /* close the connections to the database and cleanup */
148  PQfinish(conn1);
149  PQfinish(conn2);
150 
151 /* fclose(debug); */
152  return 0;
153 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2759
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3521
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static void check_conn(PGconn *conn, const char *dbName)
Definition: testlibpq4.c:25
static void exit_nicely(PGconn *conn1, PGconn *conn2)
Definition: testlibpq4.c:15
char * pghost
Definition: pgbench.c:180
#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:650
#define NULL
Definition: c.h:229
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
char * pgport
Definition: pgbench.c:181