~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/test/examples/testlibpq3.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
 * testlibpq3.c
 
3
 *              Test out-of-line parameters and binary I/O.
 
4
 *
 
5
 * Before running this, populate a database with the following commands
 
6
 * (provided in src/test/examples/testlibpq3.sql):
 
7
 *
 
8
 * CREATE TABLE test1 (i int4, t text, b bytea);
 
9
 *
 
10
 * INSERT INTO test1 values (1, 'joe''s place', '\\000\\001\\002\\003\\004');
 
11
 * INSERT INTO test1 values (2, 'ho there', '\\004\\003\\002\\001\\000');
 
12
 *
 
13
 * The expected output is:
 
14
 *
 
15
 * tuple 0: got
 
16
 *      i = (4 bytes) 1
 
17
 *      t = (11 bytes) 'joe's place'
 
18
 *      b = (5 bytes) \000\001\002\003\004
 
19
 *
 
20
 */
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include <sys/types.h>
 
25
#include "libpq-fe.h"
 
26
 
 
27
/* for ntohl/htonl */
 
28
#include <netinet/in.h>
 
29
#include <arpa/inet.h>
 
30
 
 
31
 
 
32
static void
 
33
exit_nicely(PGconn *conn)
 
34
{
 
35
        PQfinish(conn);
 
36
        exit(1);
 
37
}
 
38
 
 
39
int
 
40
main(int argc, char **argv)
 
41
{
 
42
        const char *conninfo;
 
43
        PGconn     *conn;
 
44
        PGresult   *res;
 
45
        const char *paramValues[1];
 
46
        int                     i,
 
47
                                j;
 
48
        int                     i_fnum,
 
49
                                t_fnum,
 
50
                                b_fnum;
 
51
 
 
52
        /*
 
53
         * If the user supplies a parameter on the command line, use it as the
 
54
         * conninfo string; otherwise default to setting dbname=template1 and
 
55
         * using environment variables or defaults for all other connection
 
56
         * parameters.
 
57
         */
 
58
        if (argc > 1)
 
59
                conninfo = argv[1];
 
60
        else
 
61
                conninfo = "dbname = template1";
 
62
 
 
63
        /* Make a connection to the database */
 
64
        conn = PQconnectdb(conninfo);
 
65
 
 
66
        /* Check to see that the backend connection was successfully made */
 
67
        if (PQstatus(conn) != CONNECTION_OK)
 
68
        {
 
69
                fprintf(stderr, "Connection to database failed: %s",
 
70
                                PQerrorMessage(conn));
 
71
                exit_nicely(conn);
 
72
        }
 
73
 
 
74
        /*
 
75
         * The point of this program is to illustrate use of PQexecParams()
 
76
         * with out-of-line parameters, as well as binary transmission of
 
77
         * results.  By using out-of-line parameters we can avoid a lot of
 
78
         * tedious mucking about with quoting and escaping.  Notice how we
 
79
         * don't have to do anything special with the quote mark in the
 
80
         * parameter value.
 
81
         */
 
82
 
 
83
        /* Here is our out-of-line parameter value */
 
84
        paramValues[0] = "joe's place";
 
85
 
 
86
        res = PQexecParams(conn,
 
87
                                           "SELECT * FROM test1 WHERE t = $1",
 
88
                                           1,           /* one param */
 
89
                                           NULL,        /* let the backend deduce param type */
 
90
                                           paramValues,
 
91
                                           NULL,        /* don't need param lengths since text */
 
92
                                           NULL,        /* default to all text params */
 
93
                                           1);          /* ask for binary results */
 
94
 
 
95
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
 
96
        {
 
97
                fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
 
98
                PQclear(res);
 
99
                exit_nicely(conn);
 
100
        }
 
101
 
 
102
        /* Use PQfnumber to avoid assumptions about field order in result */
 
103
        i_fnum = PQfnumber(res, "i");
 
104
        t_fnum = PQfnumber(res, "t");
 
105
        b_fnum = PQfnumber(res, "b");
 
106
 
 
107
        for (i = 0; i < PQntuples(res); i++)
 
108
        {
 
109
                char       *iptr;
 
110
                char       *tptr;
 
111
                char       *bptr;
 
112
                int                     blen;
 
113
                int                     ival;
 
114
 
 
115
                /* Get the field values (we ignore possibility they are null!) */
 
116
                iptr = PQgetvalue(res, i, i_fnum);
 
117
                tptr = PQgetvalue(res, i, t_fnum);
 
118
                bptr = PQgetvalue(res, i, b_fnum);
 
119
 
 
120
                /*
 
121
                 * The binary representation of INT4 is in network byte order,
 
122
                 * which we'd better coerce to the local byte order.
 
123
                 */
 
124
                ival = ntohl(*((uint32_t *) iptr));
 
125
 
 
126
                /*
 
127
                 * The binary representation of TEXT is, well, text, and since
 
128
                 * libpq was nice enough to append a zero byte to it, it'll work
 
129
                 * just fine as a C string.
 
130
                 *
 
131
                 * The binary representation of BYTEA is a bunch of bytes, which
 
132
                 * could include embedded nulls so we have to pay attention to
 
133
                 * field length.
 
134
                 */
 
135
                blen = PQgetlength(res, i, b_fnum);
 
136
 
 
137
                printf("tuple %d: got\n", i);
 
138
                printf(" i = (%d bytes) %d\n",
 
139
                           PQgetlength(res, i, i_fnum), ival);
 
140
                printf(" t = (%d bytes) '%s'\n",
 
141
                           PQgetlength(res, i, t_fnum), tptr);
 
142
                printf(" b = (%d bytes) ", blen);
 
143
                for (j = 0; j < blen; j++)
 
144
                        printf("\\%03o", bptr[j]);
 
145
                printf("\n\n");
 
146
        }
 
147
 
 
148
        PQclear(res);
 
149
 
 
150
        /* close the connection to the database and cleanup */
 
151
        PQfinish(conn);
 
152
 
 
153
        return 0;
 
154
}