~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * src/test/examples/testlibpq4.c
 
3
 *
 
4
 *
 
5
 * testlibpq4.c
 
6
 *              this test program shows to use LIBPQ to make multiple backend
 
7
 * connections
 
8
 *
 
9
 */
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include "libpq-fe.h"
 
13
 
 
14
static void
 
15
exit_nicely(PGconn *conn1, PGconn *conn2)
 
16
{
 
17
        if (conn1)
 
18
                PQfinish(conn1);
 
19
        if (conn2)
 
20
                PQfinish(conn2);
 
21
        exit(1);
 
22
}
 
23
 
 
24
static void
 
25
check_conn(PGconn *conn, const char *dbName)
 
26
{
 
27
        /* check to see that the backend connection was successfully made */
 
28
        if (PQstatus(conn) != CONNECTION_OK)
 
29
        {
 
30
                fprintf(stderr, "Connection to database \"%s\" failed: %s",
 
31
                                dbName, PQerrorMessage(conn));
 
32
                exit(1);
 
33
        }
 
34
}
 
35
 
 
36
int
 
37
main(int argc, char **argv)
 
38
{
 
39
        char       *pghost,
 
40
                           *pgport,
 
41
                           *pgoptions,
 
42
                           *pgtty;
 
43
        char       *dbName1,
 
44
                           *dbName2;
 
45
        char       *tblName;
 
46
        int                     nFields;
 
47
        int                     i,
 
48
                                j;
 
49
 
 
50
        PGconn     *conn1,
 
51
                           *conn2;
 
52
 
 
53
        /*
 
54
         * PGresult   *res1, *res2;
 
55
         */
 
56
        PGresult   *res1;
 
57
 
 
58
        if (argc != 4)
 
59
        {
 
60
                fprintf(stderr, "usage: %s tableName dbName1 dbName2\n", argv[0]);
 
61
                fprintf(stderr, "      compares two tables in two databases\n");
 
62
                exit(1);
 
63
        }
 
64
        tblName = argv[1];
 
65
        dbName1 = argv[2];
 
66
        dbName2 = argv[3];
 
67
 
 
68
 
 
69
        /*
 
70
         * begin, by setting the parameters for a backend connection if the
 
71
         * parameters are null, then the system will try to use reasonable
 
72
         * defaults by looking up environment variables or, failing that, using
 
73
         * hardwired constants
 
74
         */
 
75
        pghost = NULL;                          /* host name of the backend */
 
76
        pgport = NULL;                          /* port of the backend */
 
77
        pgoptions = NULL;                       /* special options to start up the backend
 
78
                                                                 * server */
 
79
        pgtty = NULL;                           /* debugging tty for the backend */
 
80
 
 
81
        /* make a connection to the database */
 
82
        conn1 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName1);
 
83
        check_conn(conn1, dbName1);
 
84
 
 
85
        conn2 = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName2);
 
86
        check_conn(conn2, dbName2);
 
87
 
 
88
        /* start a transaction block */
 
89
        res1 = PQexec(conn1, "BEGIN");
 
90
        if (PQresultStatus(res1) != PGRES_COMMAND_OK)
 
91
        {
 
92
                fprintf(stderr, "BEGIN command failed\n");
 
93
                PQclear(res1);
 
94
                exit_nicely(conn1, conn2);
 
95
        }
 
96
 
 
97
        /*
 
98
         * make sure to PQclear() a PGresult whenever it is no longer needed to
 
99
         * avoid memory leaks
 
100
         */
 
101
        PQclear(res1);
 
102
 
 
103
        /*
 
104
         * fetch instances from the pg_database, the system catalog of databases
 
105
         */
 
106
        res1 = PQexec(conn1, "DECLARE myportal CURSOR FOR select * from pg_database");
 
107
        if (PQresultStatus(res1) != PGRES_COMMAND_OK)
 
108
        {
 
109
                fprintf(stderr, "DECLARE CURSOR command failed\n");
 
110
                PQclear(res1);
 
111
                exit_nicely(conn1, conn2);
 
112
        }
 
113
        PQclear(res1);
 
114
 
 
115
        res1 = PQexec(conn1, "FETCH ALL in myportal");
 
116
        if (PQresultStatus(res1) != PGRES_TUPLES_OK)
 
117
        {
 
118
                fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
 
119
                PQclear(res1);
 
120
                exit_nicely(conn1, conn2);
 
121
        }
 
122
 
 
123
        /* first, print out the attribute names */
 
124
        nFields = PQnfields(res1);
 
125
        for (i = 0; i < nFields; i++)
 
126
                printf("%-15s", PQfname(res1, i));
 
127
        printf("\n\n");
 
128
 
 
129
        /* next, print out the instances */
 
130
        for (i = 0; i < PQntuples(res1); i++)
 
131
        {
 
132
                for (j = 0; j < nFields; j++)
 
133
                        printf("%-15s", PQgetvalue(res1, i, j));
 
134
                printf("\n");
 
135
        }
 
136
 
 
137
        PQclear(res1);
 
138
 
 
139
        /* close the portal */
 
140
        res1 = PQexec(conn1, "CLOSE myportal");
 
141
        PQclear(res1);
 
142
 
 
143
        /* end the transaction */
 
144
        res1 = PQexec(conn1, "END");
 
145
        PQclear(res1);
 
146
 
 
147
        /* close the connections to the database and cleanup */
 
148
        PQfinish(conn1);
 
149
        PQfinish(conn2);
 
150
 
 
151
/*       fclose(debug); */
 
152
        return 0;
 
153
}