Header And Logo

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

testlibpq.c

Go to the documentation of this file.
00001 /*
00002  * src/test/examples/testlibpq.c
00003  *
00004  *
00005  * testlibpq.c
00006  *
00007  *      Test the C version of libpq, the PostgreSQL frontend library.
00008  */
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "libpq-fe.h"
00012 
00013 static void
00014 exit_nicely(PGconn *conn)
00015 {
00016     PQfinish(conn);
00017     exit(1);
00018 }
00019 
00020 int
00021 main(int argc, char **argv)
00022 {
00023     const char *conninfo;
00024     PGconn     *conn;
00025     PGresult   *res;
00026     int         nFields;
00027     int         i,
00028                 j;
00029 
00030     /*
00031      * If the user supplies a parameter on the command line, use it as the
00032      * conninfo string; otherwise default to setting dbname=postgres and using
00033      * environment variables or defaults for all other connection parameters.
00034      */
00035     if (argc > 1)
00036         conninfo = argv[1];
00037     else
00038         conninfo = "dbname = postgres";
00039 
00040     /* Make a connection to the database */
00041     conn = PQconnectdb(conninfo);
00042 
00043     /* Check to see that the backend connection was successfully made */
00044     if (PQstatus(conn) != CONNECTION_OK)
00045     {
00046         fprintf(stderr, "Connection to database failed: %s",
00047                 PQerrorMessage(conn));
00048         exit_nicely(conn);
00049     }
00050 
00051     /*
00052      * Our test case here involves using a cursor, for which we must be inside
00053      * a transaction block.  We could do the whole thing with a single
00054      * PQexec() of "select * from pg_database", but that's too trivial to make
00055      * a good example.
00056      */
00057 
00058     /* Start a transaction block */
00059     res = PQexec(conn, "BEGIN");
00060     if (PQresultStatus(res) != PGRES_COMMAND_OK)
00061     {
00062         fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
00063         PQclear(res);
00064         exit_nicely(conn);
00065     }
00066 
00067     /*
00068      * Should PQclear PGresult whenever it is no longer needed to avoid memory
00069      * leaks
00070      */
00071     PQclear(res);
00072 
00073     /*
00074      * Fetch rows from pg_database, the system catalog of databases
00075      */
00076     res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
00077     if (PQresultStatus(res) != PGRES_COMMAND_OK)
00078     {
00079         fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
00080         PQclear(res);
00081         exit_nicely(conn);
00082     }
00083     PQclear(res);
00084 
00085     res = PQexec(conn, "FETCH ALL in myportal");
00086     if (PQresultStatus(res) != PGRES_TUPLES_OK)
00087     {
00088         fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
00089         PQclear(res);
00090         exit_nicely(conn);
00091     }
00092 
00093     /* first, print out the attribute names */
00094     nFields = PQnfields(res);
00095     for (i = 0; i < nFields; i++)
00096         printf("%-15s", PQfname(res, i));
00097     printf("\n\n");
00098 
00099     /* next, print out the rows */
00100     for (i = 0; i < PQntuples(res); i++)
00101     {
00102         for (j = 0; j < nFields; j++)
00103             printf("%-15s", PQgetvalue(res, i, j));
00104         printf("\n");
00105     }
00106 
00107     PQclear(res);
00108 
00109     /* close the portal ... we don't bother to check for errors ... */
00110     res = PQexec(conn, "CLOSE myportal");
00111     PQclear(res);
00112 
00113     /* end the transaction */
00114     res = PQexec(conn, "END");
00115     PQclear(res);
00116 
00117     /* close the connection to the database and cleanup */
00118     PQfinish(conn);
00119 
00120     return 0;
00121 }