Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include "libpq-fe.h"
00013
00014 static void
00015 exit_nicely(PGconn *conn1, PGconn *conn2)
00016 {
00017 if (conn1)
00018 PQfinish(conn1);
00019 if (conn2)
00020 PQfinish(conn2);
00021 exit(1);
00022 }
00023
00024 static void
00025 check_conn(PGconn *conn, const char *dbName)
00026 {
00027
00028 if (PQstatus(conn) != CONNECTION_OK)
00029 {
00030 fprintf(stderr, "Connection to database \"%s\" failed: %s",
00031 dbName, PQerrorMessage(conn));
00032 exit(1);
00033 }
00034 }
00035
00036 int
00037 main(int argc, char **argv)
00038 {
00039 char *pghost,
00040 *pgport,
00041 *pgoptions,
00042 *pgtty;
00043 char *dbName1,
00044 *dbName2;
00045 char *tblName;
00046 int nFields;
00047 int i,
00048 j;
00049
00050 PGconn *conn1,
00051 *conn2;
00052
00053
00054
00055
00056 PGresult *res1;
00057
00058 if (argc != 4)
00059 {
00060 fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
00061 fprintf(stderr, " compares two tables in two databases\n");
00062 exit(1);
00063 }
00064 tblName = argv[1];
00065 dbName1 = argv[2];
00066 dbName2 = argv[3];
00067
00068
00069
00070
00071
00072
00073
00074
00075 pghost = NULL;
00076 pgport = NULL;
00077 pgoptions = NULL;
00078
00079 pgtty = NULL;
00080
00081
00082 conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
00083 check_conn(conn1, dbName1);
00084
00085 conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
00086 check_conn(conn2, dbName2);
00087
00088
00089 res1 = PQexec(conn1, "BEGIN");
00090 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
00091 {
00092 fprintf(stderr, "BEGIN command failed\n");
00093 PQclear(res1);
00094 exit_nicely(conn1, conn2);
00095 }
00096
00097
00098
00099
00100
00101 PQclear(res1);
00102
00103
00104
00105
00106 res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database");
00107 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
00108 {
00109 fprintf(stderr, "DECLARE CURSOR command failed\n");
00110 PQclear(res1);
00111 exit_nicely(conn1, conn2);
00112 }
00113 PQclear(res1);
00114
00115 res1 = PQexec(conn1, "FETCH ALL in myportal");
00116 if (PQresultStatus(res1) != PGRES_TUPLES_OK)
00117 {
00118 fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
00119 PQclear(res1);
00120 exit_nicely(conn1, conn2);
00121 }
00122
00123
00124 nFields = PQnfields(res1);
00125 for (i = 0; i < nFields; i++)
00126 printf("%-15s", PQfname(res1, i));
00127 printf("\n\n");
00128
00129
00130 for (i = 0; i < PQntuples(res1); i++)
00131 {
00132 for (j = 0; j < nFields; j++)
00133 printf("%-15s", PQgetvalue(res1, i, j));
00134 printf("\n");
00135 }
00136
00137 PQclear(res1);
00138
00139
00140 res1 = PQexec(conn1, "CLOSE myportal");
00141 PQclear(res1);
00142
00143
00144 res1 = PQexec(conn1, "END");
00145 PQclear(res1);
00146
00147
00148 PQfinish(conn1);
00149 PQfinish(conn2);
00150
00151
00152 return 0;
00153 }