~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/test/examples/testlibpq.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * testlibpq.c
 
3
 *
 
4
 *              Test the C version of libpq, the PostgreSQL frontend library.
 
5
 */
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include "libpq-fe.h"
 
9
 
 
10
static void
 
11
exit_nicely(PGconn *conn)
 
12
{
 
13
        PQfinish(conn);
 
14
        exit(1);
 
15
}
 
16
 
 
17
int
 
18
main(int argc, char **argv)
 
19
{
 
20
        const char *conninfo;
 
21
        PGconn     *conn;
 
22
        PGresult   *res;
 
23
        int                     nFields;
 
24
        int                     i,
 
25
                                j;
 
26
 
 
27
        /*
 
28
         * If the user supplies a parameter on the command line, use it as the
 
29
         * conninfo string; otherwise default to setting dbname=template1 and
 
30
         * using environment variables or defaults for all other connection
 
31
         * parameters.
 
32
         */
 
33
        if (argc > 1)
 
34
                conninfo = argv[1];
 
35
        else
 
36
                conninfo = "dbname = template1";
 
37
 
 
38
        /* Make a connection to the database */
 
39
        conn = PQconnectdb(conninfo);
 
40
 
 
41
        /* Check to see that the backend connection was successfully made */
 
42
        if (PQstatus(conn) != CONNECTION_OK)
 
43
        {
 
44
                fprintf(stderr, "Connection to database failed: %s",
 
45
                                PQerrorMessage(conn));
 
46
                exit_nicely(conn);
 
47
        }
 
48
 
 
49
        /*
 
50
         * Our test case here involves using a cursor, for which we must be
 
51
         * inside a transaction block.  We could do the whole thing with a
 
52
         * single PQexec() of "select * from pg_database", but that's too
 
53
         * trivial to make a good example.
 
54
         */
 
55
 
 
56
        /* Start a transaction block */
 
57
        res = PQexec(conn, "BEGIN");
 
58
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
 
59
        {
 
60
                fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
 
61
                PQclear(res);
 
62
                exit_nicely(conn);
 
63
        }
 
64
 
 
65
        /*
 
66
         * Should PQclear PGresult whenever it is no longer needed to avoid
 
67
         * memory leaks
 
68
         */
 
69
        PQclear(res);
 
70
 
 
71
        /*
 
72
         * Fetch rows from pg_database, the system catalog of databases
 
73
         */
 
74
        res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
 
75
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
 
76
        {
 
77
                fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
 
78
                PQclear(res);
 
79
                exit_nicely(conn);
 
80
        }
 
81
        PQclear(res);
 
82
 
 
83
        res = PQexec(conn, "FETCH ALL in myportal");
 
84
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
 
85
        {
 
86
                fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
 
87
                PQclear(res);
 
88
                exit_nicely(conn);
 
89
        }
 
90
 
 
91
        /* first, print out the attribute names */
 
92
        nFields = PQnfields(res);
 
93
        for (i = 0; i < nFields; i++)
 
94
                printf("%-15s", PQfname(res, i));
 
95
        printf("\n\n");
 
96
 
 
97
        /* next, print out the rows */
 
98
        for (i = 0; i < PQntuples(res); i++)
 
99
        {
 
100
                for (j = 0; j < nFields; j++)
 
101
                        printf("%-15s", PQgetvalue(res, i, j));
 
102
                printf("\n");
 
103
        }
 
104
 
 
105
        PQclear(res);
 
106
 
 
107
        /* close the portal ... we don't bother to check for errors ... */
 
108
        res = PQexec(conn, "CLOSE myportal");
 
109
        PQclear(res);
 
110
 
 
111
        /* end the transaction */
 
112
        res = PQexec(conn, "END");
 
113
        PQclear(res);
 
114
 
 
115
        /* close the connection to the database and cleanup */
 
116
        PQfinish(conn);
 
117
 
 
118
        return 0;
 
119
}