Header And Logo

PostgreSQL
| The world's most advanced open source database.

testlibpq4.c

Go to the documentation of this file.
00001 /*
00002  * src/test/examples/testlibpq4.c
00003  *
00004  *
00005  * testlibpq4.c
00006  *      this test program shows to use LIBPQ to make multiple backend
00007  * connections
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     /* check to see that the backend connection was successfully made */
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      * PGresult   *res1, *res2;
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      * begin, by setting the parameters for a backend connection if the
00071      * parameters are null, then the system will try to use reasonable
00072      * defaults by looking up environment variables or, failing that, using
00073      * hardwired constants
00074      */
00075     pghost = NULL;              /* host name of the backend */
00076     pgport = NULL;              /* port of the backend */
00077     pgoptions = NULL;           /* special options to start up the backend
00078                                  * server */
00079     pgtty = NULL;               /* debugging tty for the backend */
00080 
00081     /* make a connection to the database */
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     /* start a transaction block */
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      * make sure to PQclear() a PGresult whenever it is no longer needed to
00099      * avoid memory leaks
00100      */
00101     PQclear(res1);
00102 
00103     /*
00104      * fetch instances from the pg_database, the system catalog of databases
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     /* first, print out the attribute names */
00124     nFields = PQnfields(res1);
00125     for (i = 0; i < nFields; i++)
00126         printf("%-15s", PQfname(res1, i));
00127     printf("\n\n");
00128 
00129     /* next, print out the instances */
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     /* close the portal */
00140     res1 = PQexec(conn1, "CLOSE myportal");
00141     PQclear(res1);
00142 
00143     /* end the transaction */
00144     res1 = PQexec(conn1, "END");
00145     PQclear(res1);
00146 
00147     /* close the connections to the database and cleanup */
00148     PQfinish(conn1);
00149     PQfinish(conn2);
00150 
00151 /*   fclose(debug); */
00152     return 0;
00153 }