summaryrefslogtreecommitdiff
path: root/src/test/examples/testlibpq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/examples/testlibpq.c')
-rw-r--r--src/test/examples/testlibpq.c84
1 files changed, 35 insertions, 49 deletions
diff --git a/src/test/examples/testlibpq.c b/src/test/examples/testlibpq.c
index ca5ebbafba6..20dfb7220d9 100644
--- a/src/test/examples/testlibpq.c
+++ b/src/test/examples/testlibpq.c
@@ -1,10 +1,10 @@
/*
* testlibpq.c
- * Test the C version of LIBPQ, the POSTGRES frontend library.
- *
*
+ * Test the C version of LIBPQ, the POSTGRES frontend library.
*/
#include <stdio.h>
+#include <stdlib.h>
#include "libpq-fe.h"
static void
@@ -15,76 +15,66 @@ exit_nicely(PGconn *conn)
}
int
-main()
+main(int argc, char **argv)
{
- char *pghost,
- *pgport,
- *pgoptions,
- *pgtty;
- char *dbName;
+ const char *conninfo;
+ PGconn *conn;
+ PGresult *res;
int nFields;
int i,
j;
-#ifdef DEBUG
- FILE *debug;
-#endif /* DEBUG */
-
- PGconn *conn;
- PGresult *res;
-
/*
- * begin, by setting the parameters for a backend connection if the
- * parameters are null, then the system will try to use reasonable
- * defaults by looking up environment variables or, failing that,
- * using hardwired constants
+ * If the user supplies a parameter on the command line, use it as
+ * the conninfo string; otherwise default to setting dbname=template1
+ * and using environment variables or defaults for all other connection
+ * parameters.
*/
- pghost = NULL; /* host name of the backend server */
- pgport = NULL; /* port of the backend server */
- pgoptions = NULL; /* special options to start up the backend
- * server */
- pgtty = NULL; /* debugging tty for the backend server */
- dbName = "template1";
-
- /* make a connection to the database */
- conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
-
- /* check to see that the backend connection was successfully made */
- if (PQstatus(conn) == CONNECTION_BAD)
+ if (argc > 1)
+ conninfo = argv[1];
+ else
+ conninfo = "dbname = template1";
+
+ /* Make a connection to the database */
+ conn = PQconnectdb(conninfo);
+
+ /* Check to see that the backend connection was successfully made */
+ if (PQstatus(conn) != CONNECTION_OK)
{
- fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
+ fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
-#ifdef DEBUG
- debug = fopen("/tmp/trace.out", "w");
- PQtrace(conn, debug);
-#endif /* DEBUG */
+ /*
+ * Our test case here involves using a cursor, for which we must be
+ * inside a transaction block. We could do the whole thing with a
+ * single PQexec() of "select * from pg_database", but that's too
+ * trivial to make a good example.
+ */
- /* start a transaction block */
+ /* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
- fprintf(stderr, "BEGIN command failed\n");
+ fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
- * should PQclear PGresult whenever it is no longer needed to avoid
+ * Should PQclear PGresult whenever it is no longer needed to avoid
* memory leaks
*/
PQclear(res);
/*
- * fetch instances from the pg_database, the system catalog of
- * databases
+ * Fetch rows from pg_database, the system catalog of databases
*/
res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
- fprintf(stderr, "DECLARE CURSOR command failed\n");
+ fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
@@ -93,7 +83,7 @@ main()
res = PQexec(conn, "FETCH ALL in myportal");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
- fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
+ fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
@@ -104,7 +94,7 @@ main()
printf("%-15s", PQfname(res, i));
printf("\n\n");
- /* next, print out the instances */
+ /* next, print out the rows */
for (i = 0; i < PQntuples(res); i++)
{
for (j = 0; j < nFields; j++)
@@ -114,7 +104,7 @@ main()
PQclear(res);
- /* close the portal */
+ /* close the portal ... we don't bother to check for errors ... */
res = PQexec(conn, "CLOSE myportal");
PQclear(res);
@@ -125,9 +115,5 @@ main()
/* close the connection to the database and cleanup */
PQfinish(conn);
-#ifdef DEBUG
- fclose(debug);
-#endif /* DEBUG */
-
return 0;
}